Debian Codegraph @

deps/svg/svgpan-1.2mod.js

raw
1 /*!
2 @licstart The following is the entire license notice for the JavaScript code in this file.
3 The code below is based on SVGPan Library 1.2 and was modified for doxygen
4 to support both zooming and panning via the mouse and via embedded buttons.
5
6 This code is licensed under the following BSD license:
7
8 Copyright 2009-2010 Andrea Leofreddi <[email protected]>. All rights reserved.
9
10 Redistribution and use in source and binary forms, with or without modification, are
11 permitted provided that the following conditions are met:
12
13 1. Redistributions of source code must retain the above copyright notice, this list of
14 conditions and the following disclaimer.
15
16 2. Redistributions in binary form must reproduce the above copyright notice, this list
17 of conditions and the following disclaimer in the documentation and/or other materials
18 provided with the distribution.
19
20 THIS SOFTWARE IS PROVIDED BY Andrea Leofreddi ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Andrea Leofreddi OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 The views and conclusions contained in the software and documentation are those of the
31 authors and should not be interpreted as representing official policies, either expressed
32 or implied, of Andrea Leofreddi.
33
34 @licend The above is the entire license notice for the JavaScript code in this file
35 */
36
37 var root = document.documentElement;
38 var state = 'none';
39 var stateOrigin;
40 var stateTf = root.createSVGMatrix();
41 var cursorGrab = ' url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAMAAAAolt3jAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAlQTFRFAAAA////////c3ilYwAAAAN0Uk5T//8A18oNQQAAAD1JREFUeNp0zlEKACAIA9Bt9z90bZBZkQj29qFBEuBOzQHSnWTTyckEfqUuZgFvslH4ch3qLCO/Kr8cAgwATw4Ax6XRCcoAAAAASUVORK5CYII="), move';
42 var zoomSteps = 10;
43 var zoomInFactor;
44 var zoomOutFactor;
45 var windowWidth;
46 var windowHeight;
47 var svgDoc;
48 var minZoom;
49 var maxZoom;
50 if (!window) window=this;
51
52 /**
53 * Show the graph in the middle of the view, scaled to fit
54 */
55 function show()
56 {
57 if (window.innerHeight) // Firefox
58 {
59 windowWidth = window.innerWidth;
60 windowHeight = window.innerHeight;
61 }
62 else if (document.documentElement.clientWidth) // Chrome/Safari
63 {
64 windowWidth = document.documentElement.clientWidth
65 windowHeight = document.documentElement.clientHeight
66 }
67 if (!windowWidth || !windowHeight) // failsafe
68 {
69 windowWidth = 800;
70 windowHeight = 600;
71 }
72 minZoom = Math.min(Math.min(viewHeight,windowHeight)/viewHeight,Math.min(viewWidth,windowWidth)/viewWidth);
73 maxZoom = minZoom+1.5;
74 zoomInFactor = Math.pow(maxZoom/minZoom,1.0/zoomSteps);
75 zoomOutFactor = 1.0/zoomInFactor;
76
77 var g = svgDoc.getElementById('viewport');
78 try
79 {
80 var bb = g.getBBox(); // this can throw an exception if css { display: none }
81 var tx = (windowWidth-viewWidth*minZoom+8)/(2*minZoom);
82 var ty = viewHeight+(windowHeight-viewHeight*minZoom)/(2*minZoom);
83 var a = 'scale('+minZoom+') rotate(0) translate('+tx+' '+ty+')';
84 g.setAttribute('transform',a);
85 }
86 catch(e) {}
87 }
88
89 /**
90 * Register handlers
91 */
92 function init(evt)
93 {
94 svgDoc = evt.target.ownerDocument;
95 try {
96 if (top.window && top.window.registerShow) { // register show function in html doc for dynamic sections
97 top.window.registerShow(sectionId,show);
98 }
99 } catch(e) {
100 // ugh, we are not allowed to talk to the parent; can happen with Chrome when viewing pages
101 // locally, since they treat every local page as having a different origin
102 }
103 show();
104
105 setAttributes(root, {
106 "onmousedown" : "handleMouseDown(evt)",
107 "onmousemove" : "handleMouseMove(evt)",
108 "onmouseup" : "handleMouseUp(evt)"
109 });
110
111 if (window.addEventListener)
112 {
113 if (navigator.userAgent.toLowerCase().indexOf('webkit') >= 0 ||
114 navigator.userAgent.toLowerCase().indexOf("opera") >= 0 ||
115 navigator.appVersion.indexOf("MSIE") != -1)
116 {
117 window.addEventListener('mousewheel', handleMouseWheel, false); // Chrome/Safari/IE9
118 }
119 else
120 {
121 window.addEventListener('DOMMouseScroll', handleMouseWheel, false); // Others
122 }
123 }
124 }
125
126 window.onresize=function()
127 {
128 if (svgDoc) { show(); }
129 }
130
131 /**
132 * Instance an SVGPoint object with given event coordinates.
133 */
134 function getEventPoint(evt)
135 {
136 var p = root.createSVGPoint();
137 p.x = evt.clientX;
138 p.y = evt.clientY;
139 return p;
140 }
141
142 /**
143 * Sets the current transform matrix of an element.
144 */
145 function setCTM(element, matrix)
146 {
147 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
148 element.setAttribute("transform", s);
149 }
150
151 /**
152 * Sets attributes of an element.
153 */
154 function setAttributes(element, attributes)
155 {
156 for (i in attributes)
157 element.setAttributeNS(null, i, attributes[i]);
158 }
159
160 function doZoom(g,point,zoomFactor)
161 {
162 var p = point.matrixTransform(g.getCTM().inverse());
163 var k = root.createSVGMatrix().translate(p.x, p.y).scale(zoomFactor).translate(-p.x, -p.y);
164 var n = g.getCTM().multiply(k);
165 var s = Math.max(n.a,n.d);
166 if (s>maxZoom) n=n.translate(p.x,p.y).scale(maxZoom/s).translate(-p.x,-p.y);
167 else if (s<minZoom) n=n.translate(p.x,p.y).scale(minZoom/s).translate(-p.x,-p.y);
168 setCTM(g, n);
169 stateTf = stateTf.multiply(n.inverse());
170 }
171
172 /**
173 * Handle mouse move event.
174 */
175 function handleMouseWheel(evt)
176 {
177 if (!evt) evt = window.evt;
178 if (!evt.shiftKey) return; // only zoom when shift is pressed
179 if (evt.preventDefault) evt.preventDefault();
180 evt.returnValue = false;
181
182 if (state!='pan')
183 {
184 var delta;
185 if (evt.wheelDelta)
186 {
187 delta = evt.wheelDelta / 7200; // Opera/Chrome/IE9/Safari
188 }
189 else
190 {
191 delta = evt.detail / -180; // Mozilla
192 }
193 var svgDoc = evt.target.ownerDocument;
194 var g = svgDoc.getElementById("viewport");
195 var p = getEventPoint(evt);
196 doZoom(g,p,1+delta);
197 }
198 }
199
200 /**
201 * Handle mouse move event.
202 */
203 function handleMouseMove(evt)
204 {
205 if(evt.preventDefault)
206 evt.preventDefault();
207
208 evt.returnValue = false;
209
210 var g = svgDoc.getElementById("viewport");
211
212 if (state == 'pan')
213 {
214 // Pan mode
215 var p = getEventPoint(evt).matrixTransform(stateTf);
216 setCTM(g,stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y));
217 }
218 }
219
220 /**
221 * Handle click event.
222 */
223 function handleMouseDown(evt)
224 {
225 if(evt.preventDefault)
226 evt.preventDefault();
227 evt.returnValue = false;
228 var g = svgDoc.getElementById("viewport");
229 state = 'pan';
230 stateTf = g.getCTM().inverse();
231 stateOrigin = getEventPoint(evt).matrixTransform(stateTf);
232 g.style.cursor = cursorGrab;
233 }
234
235 /**
236 * Handle mouse button release event.
237 */
238 function handleMouseUp(evt)
239 {
240 if (evt.preventDefault) evt.preventDefault();
241 evt.returnValue = false;
242 var g = svgDoc.getElementById("viewport");
243 g.style.cursor = "default";
244 // Quit pan mode
245 state = '';
246 }
247
248 /**
249 * Dumps a matrix to a string (useful for debug).
250 */
251 function dumpMatrix(matrix)
252 {
253 var s = "[ " + matrix.a + ", " + matrix.c + ", " + matrix.e + "\n " + matrix.b + ", " + matrix.d + ", " + matrix.f + "\n 0, 0, 1 ]";
254 return s;
255 }
256
257 /**
258 * Handler for pan buttons
259 */
260 function handlePan(x,y)
261 {
262 var g = svgDoc.getElementById("viewport");
263 setCTM(g,g.getCTM().translate(x*20/minZoom,y*20/minZoom));
264 }
265
266 /**
267 * Handle reset button
268 */
269 function handleReset()
270 {
271 show();
272 }
273
274 /**
275 * Handler for zoom buttons
276 */
277 function handleZoom(evt,direction)
278 {
279 var g = svgDoc.getElementById("viewport");
280 var factor = direction=='in' ? zoomInFactor : zoomOutFactor;
281 var m = g.getCTM();
282 var p = root.createSVGPoint();
283 p.x = windowWidth/2;
284 p.y = windowHeight/2;
285 doZoom(g,p,factor);
286 }
287
288 function serializeXmlNode(xmlNode)
289 {
290 if (typeof window.XMLSerializer != "undefined") {
291 return (new window.XMLSerializer()).serializeToString(xmlNode);
292 } else if (typeof xmlNode.xml != "undefined") {
293 return xmlNode.xml;
294 }
295 return "";
296 }
297
298 /**
299 * Handler for print function
300 */
301 function handlePrint(evt)
302 {
303 evt.returnValue = false;
304 var g = svgDoc.getElementById("graph");
305 var xs = serializeXmlNode(g);
306 try {
307 var w = window.open('about:blank','_blank','width='+windowWidth+',height='+windowHeight+
308 ',toolbar=0,status=0,menubar=0,scrollbars=0,resizable=0,location=0,directories=0');
309 var d = w.document;
310 d.write('<html xmlns="http://www.w3.org/1999/xhtml" '+
311 'xmlns:svg="http://www.w3.org/2000/svg" '+
312 'xmlns:xlink="http://www.w3.org/1999/xlink">');
313 d.write('<head><title>Print SVG</title></head>');
314 d.write('<body style="margin: 0px; padding: 0px;" onload="window.print();">');
315 d.write('<div id="svg" style="width:'+windowWidth+'px; height:'+windowHeight+'px;">'+xs+'</div>');
316 d.write('</body>');
317 d.write('</html>');
318 d.close();
319 } catch(e) {
320 alert('Failed to open popup window needed for printing!\n'+e.message);
321 }
322 }
323
324 /** Highlight edges that are hovered */
325 function highlightEdges()
326 {
327 var elems = document.getElementsByTagName('g');
328 if (elems) {
329 for (var i=0;i<elems.length;i++) {
330 if (elems[i].id.substr(0,4)=='edge') {
331 elems[i].setAttribute('class','edge selected');
332 }
333 else if (elems[i].id.substr(0,4)=='Node') {
334 elems[i].setAttribute('class','node selected');
335 }
336 }
337 }
338 }
339
340 function highlightAdjacentNodes()
341 {
342 /**
343 * @param {SVG.G} node
344 * @param {SVG.G} topG
345 * @return {{node: Set<SVG.G>, edges:Set<SVG.G>}}
346 */
347 function getEdgesAndDistance1Nodes(node, topG) {
348 const nodeName = node.attr('id');
349 const selector = '[id^=edge]'
350 const candidates = topG.find(selector)
351 let edges = new Set();
352 let nodes = new Set();
353 for (let edge of candidates) {
354 const res = edge.attr('id').split('_');
355 if (res && res.length==3) {
356 const N1 = res[1];
357 const N2 = res[2];
358 if (N1==nodeName) {
359 const N2selector = `[id^=${N2}]`;
360 nodes.add(topG.findOne(N2selector));
361 edges.add(edge);
362 } else if (N2==nodeName) {
363 const N1selector = `[id^=${N1}]`;
364 nodes.add(topG.findOne(N1selector));
365 edges.add(edge);
366 }
367 }
368 }
369 return {
370 'nodes' : nodes,
371 'edges' : edges
372 };
373 }
374
375 /**
376 * @param {SVG.G} node
377 * @param {function(SVG.Dom)}
378 * @return {{node: Set<SVG.G>, edges:Set<SVG.G>}}
379 */
380 function walk(node, func) {
381 let children = node.children();
382 for (let child of children) {
383 walk(child, func)
384 }
385 func(node);
386 }
387 let s = SVG('svg > g')
388 /**
389 * @param {SVGElement} domEl
390 * @return {{SVGElement}}
391 */
392 function findEnclosingG(domEl) {
393 let curEl = domEl;
394 while (curEl.nodeName != 'g' || curEl.id.substr(0,4)!='Node') {
395 curEl = curEl.parentElement;
396 }
397 return curEl;
398 }
399 function onMouseOverElem(domEl) {
400 let e = SVG(findEnclosingG(domEl.target));
401 walk(s,
402 e => { if (SVG(e)!=s)
403 SVG(e).attr('data-mouse-over-selected','false');
404 });
405 walk(e, e => SVG(e).attr('data-mouse-over-selected','true'));
406 let {nodes, edges} = getEdgesAndDistance1Nodes(SVG(e), s);
407 for (let node of nodes) {
408 walk(node, e => SVG(e).attr('data-mouse-over-selected','true'));
409 }
410 for (let edge of edges) {
411 walk(edge, e => SVG(e).attr('data-mouse-over-selected','true'));
412 }
413 }
414
415 function onMouseOutElem(domEl) {
416 let e = SVG(findEnclosingG(domEl.target));
417 walk(s, e => e.attr('data-mouse-over-selected',null));
418 }
419 let gs = s.find('g[id^=Node]');
420 for (let g of gs) {
421 g.on('mouseover', onMouseOverElem);
422 g.on('mouseout', onMouseOutElem);
423 }
424 }
425
426 /* @license-end */