libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo es el conjunto de objetos y funciones, que permiten ejecutar las operaciones básicas de la plataforma Moldeo, y que compone su núcleo.
svgpan.js
Ir a la documentación de este archivo.
1 /**
2  * The code below is based on SVGPan Library 1.2 and was modified for doxygen
3  * to support both zooming and panning via the mouse and via embedded bottons.
4  *
5  * This code is licensed under the following BSD license:
6  *
7  * Copyright 2009-2010 Andrea Leofreddi <a.leofreddi@itcharm.com>. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modification, are
10  * permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice, this list of
13  * conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
16  * of conditions and the following disclaimer in the documentation and/or other materials
17  * provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY Andrea Leofreddi ``AS IS'' AND ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Andrea Leofreddi OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * The views and conclusions contained in the software and documentation are those of the
30  * authors and should not be interpreted as representing official policies, either expressed
31  * or implied, of Andrea Leofreddi.
32  */
33 
34 var root = document.documentElement;
35 var state = 'none';
36 var stateOrigin;
37 var stateTf = root.createSVGMatrix();
38 var cursorGrab = ' url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAMAAAAolt3jAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAlQTFRFAAAA////////c3ilYwAAAAN0Uk5T//8A18oNQQAAAD1JREFUeNp0zlEKACAIA9Bt9z90bZBZkQj29qFBEuBOzQHSnWTTyckEfqUuZgFvslH4ch3qLCO/Kr8cAgwATw4Ax6XRCcoAAAAASUVORK5CYII="), move';
39 var zoomSteps = 10;
40 var zoomInFactor;
41 var zoomOutFactor;
42 var windowWidth;
43 var windowHeight;
44 var svgDoc;
45 var minZoom;
46 var maxZoom;
47 if (!window) window=this;
48 
49 /**
50  * Show the graph in the middle of the view, scaled to fit
51  */
52 function show()
53 {
54  if (window.innerHeight) // Firefox
55  {
56  windowWidth = window.innerWidth;
57  windowHeight = window.innerHeight;
58  }
59  else if (document.documentElement.clientWidth) // Chrome/Safari
60  {
61  windowWidth = document.documentElement.clientWidth
62  windowHeight = document.documentElement.clientHeight
63  }
64  if (!windowWidth || !windowHeight) // failsafe
65  {
66  windowWidth = 800;
67  windowHeight = 600;
68  }
69  minZoom = Math.min(Math.min(viewHeight,windowHeight)/viewHeight,Math.min(viewWidth,windowWidth)/viewWidth);
70  maxZoom = minZoom+1.5;
71  zoomInFactor = Math.pow(maxZoom/minZoom,1.0/zoomSteps);
72  zoomOutFactor = 1.0/zoomInFactor;
73 
74  var g = svgDoc.getElementById('viewport');
75  try
76  {
77  var bb = g.getBBox(); // this can throw an exception if css { display: none }
78  var tx = (windowWidth-viewWidth*minZoom+8)/(2*minZoom);
79  var ty = viewHeight+(windowHeight-viewHeight*minZoom)/(2*minZoom);
80  var a = 'scale('+minZoom+') rotate(0) translate('+tx+' '+ty+')';
81  g.setAttribute('transform',a);
82  }
83  catch(e) {}
84 }
85 
86 /**
87  * Register handlers
88  */
89 function init(evt)
90 {
91  svgDoc = evt.target.ownerDocument;
92  try {
93  if (top.window && top.window.registerShow) { // register show function in html doc for dynamic sections
94  top.window.registerShow(sectionId,show);
95  }
96  } catch(e) {
97  // ugh, we are not allowed to talk to the parent; can happen with Chrome when viewing pages
98  // locally, since they treat every local page as having a different origin
99  }
100  show();
101 
102  setAttributes(root, {
103  "onmousedown" : "handleMouseDown(evt)",
104  "onmousemove" : "handleMouseMove(evt)",
105  "onmouseup" : "handleMouseUp(evt)"
106  });
107 
108  if (window.addEventListener)
109  {
110  if (navigator.userAgent.toLowerCase().indexOf('webkit') >= 0 ||
111  navigator.userAgent.toLowerCase().indexOf("opera") >= 0 ||
112  navigator.appVersion.indexOf("MSIE") != -1)
113  {
114  window.addEventListener('mousewheel', handleMouseWheel, false); // Chrome/Safari/IE9
115  }
116  else
117  {
118  window.addEventListener('DOMMouseScroll', handleMouseWheel, false); // Others
119  }
120  }
121 }
122 
123 window.onresize=function()
124 {
125  if (svgDoc) { show(); }
126 }
127 
128 /**
129  * Instance an SVGPoint object with given event coordinates.
130  */
131 function getEventPoint(evt)
132 {
133  var p = root.createSVGPoint();
134  p.x = evt.clientX;
135  p.y = evt.clientY;
136  return p;
137 }
138 
139 /**
140  * Sets the current transform matrix of an element.
141  */
142 function setCTM(element, matrix)
143 {
144  var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
145  element.setAttribute("transform", s);
146 }
147 
148 /**
149  * Sets attributes of an element.
150  */
151 function setAttributes(element, attributes)
152 {
153  for (i in attributes)
154  element.setAttributeNS(null, i, attributes[i]);
155 }
156 
157 function doZoom(g,point,zoomFactor)
158 {
159  var p = point.matrixTransform(g.getCTM().inverse());
160  var k = root.createSVGMatrix().translate(p.x, p.y).scale(zoomFactor).translate(-p.x, -p.y);
161  var n = g.getCTM().multiply(k);
162  var s = Math.max(n.a,n.d);
163  if (s>maxZoom) n=n.translate(p.x,p.y).scale(maxZoom/s).translate(-p.x,-p.y);
164  else if (s<minZoom) n=n.translate(p.x,p.y).scale(minZoom/s).translate(-p.x,-p.y);
165  setCTM(g, n);
166  stateTf = stateTf.multiply(n.inverse());
167 }
168 
169 /**
170  * Handle mouse move event.
171  */
172 function handleMouseWheel(evt)
173 {
174  if (!evt) evt = window.evt;
175  if (!evt.shiftKey) return; // only zoom when shift is pressed
176  if (evt.preventDefault) evt.preventDefault();
177  evt.returnValue = false;
178 
179  if (state!='pan')
180  {
181  var delta;
182  if (evt.wheelDelta)
183  {
184  delta = evt.wheelDelta / 7200; // Opera/Chrome/IE9/Safari
185  }
186  else
187  {
188  delta = evt.detail / -180; // Mozilla
189  }
190  var svgDoc = evt.target.ownerDocument;
191  var g = svgDoc.getElementById("viewport");
192  var p = getEventPoint(evt);
193  doZoom(g,p,1+delta);
194  }
195 }
196 
197 /**
198  * Handle mouse move event.
199  */
200 function handleMouseMove(evt)
201 {
202  if(evt.preventDefault)
203  evt.preventDefault();
204 
205  evt.returnValue = false;
206 
207  var g = svgDoc.getElementById("viewport");
208 
209  if (state == 'pan')
210  {
211  // Pan mode
212  var p = getEventPoint(evt).matrixTransform(stateTf);
213  setCTM(g,stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y));
214  }
215 }
216 
217 /**
218  * Handle click event.
219  */
220 function handleMouseDown(evt)
221 {
222  if(evt.preventDefault)
223  evt.preventDefault();
224  evt.returnValue = false;
225  var g = svgDoc.getElementById("viewport");
226  state = 'pan';
227  stateTf = g.getCTM().inverse();
228  stateOrigin = getEventPoint(evt).matrixTransform(stateTf);
229  g.style.cursor = cursorGrab;
230 }
231 
232 /**
233  * Handle mouse button release event.
234  */
235 function handleMouseUp(evt)
236 {
237  if (evt.preventDefault) evt.preventDefault();
238  evt.returnValue = false;
239  var g = svgDoc.getElementById("viewport");
240  g.style.cursor = "default";
241  // Quit pan mode
242  state = '';
243 }
244 
245 /**
246  * Dumps a matrix to a string (useful for debug).
247  */
248 function dumpMatrix(matrix)
249 {
250  var s = "[ " + matrix.a + ", " + matrix.c + ", " + matrix.e + "\n " + matrix.b + ", " + matrix.d + ", " + matrix.f + "\n 0, 0, 1 ]";
251  return s;
252 }
253 
254 /**
255  * Handler for pan buttons
256  */
257 function handlePan(x,y)
258 {
259  var g = svgDoc.getElementById("viewport");
260  setCTM(g,g.getCTM().translate(x*20/minZoom,y*20/minZoom));
261 }
262 
263 /**
264  * Handle reset button
265  */
266 function handleReset()
267 {
268  show();
269 }
270 
271 /**
272  * Handler for zoom buttons
273  */
274 function handleZoom(evt,direction)
275 {
276  var g = svgDoc.getElementById("viewport");
277  var factor = direction=='in' ? zoomInFactor : zoomOutFactor;
278  var m = g.getCTM();
279  var p = root.createSVGPoint();
280  p.x = windowWidth/2;
281  p.y = windowHeight/2;
282  doZoom(g,p,factor);
283 }
284 
285 function serializeXmlNode(xmlNode)
286 {
287  if (typeof window.XMLSerializer != "undefined") {
288  return (new window.XMLSerializer()).serializeToString(xmlNode);
289  } else if (typeof xmlNode.xml != "undefined") {
290  return xmlNode.xml;
291  }
292  return "";
293 }
294 
295 /**
296  * Handler for print function
297  */
298 function handlePrint(evt)
299 {
300  evt.returnValue = false;
301  var g = svgDoc.getElementById("graph");
302  var xs = serializeXmlNode(g);
303  try {
304  var w = window.open('about:blank','_blank','width='+windowWidth+',height='+windowHeight+
305  ',toolbar=0,status=0,menubar=0,scrollbars=0,resizable=0,location=0,directories=0');
306  var d = w.document;
307  d.write('<html xmlns="http://www.w3.org/1999/xhtml" '+
308  'xmlns:svg="http://www.w3.org/2000/svg" '+
309  'xmlns:xlink="http://www.w3.org/1999/xlink">');
310  d.write('<head><title>Print SVG</title></head>');
311  d.write('<body style="margin: 0px; padding: 0px;" onload="window.print();">');
312  d.write('<div id="svg" style="width:'+windowWidth+'px; height:'+windowHeight+'px;">'+xs+'</div>');
313  d.write('</body>');
314  d.write('</html>');
315  d.close();
316  } catch(e) {
317  alert('Failed to open popup window needed for printing!\n'+e.message);
318  }
319 }
320 
321 
322 
323