Blame templates/html/svgpan.js

Packit Service 50c9f2
/*
Packit Service 50c9f2
 @licstart  The following is the entire license notice for the
Packit Service 50c9f2
 JavaScript code in this file.
Packit Service 50c9f2
Packit Service 50c9f2
 Copyright (C) 1997-2017 by Dimitri van Heesch
Packit Service 50c9f2
Packit Service 50c9f2
 This program is free software; you can redistribute it and/or modify
Packit Service 50c9f2
 it under the terms of the GNU General Public License as published by
Packit Service 50c9f2
 the Free Software Foundation; either version 2 of the License, or
Packit Service 50c9f2
 (at your option) any later version.
Packit Service 50c9f2
Packit Service 50c9f2
 This program is distributed in the hope that it will be useful,
Packit Service 50c9f2
 but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 50c9f2
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 50c9f2
 GNU General Public License for more details.
Packit Service 50c9f2
Packit Service 50c9f2
 You should have received a copy of the GNU General Public License along
Packit Service 50c9f2
 with this program; if not, write to the Free Software Foundation, Inc.,
Packit Service 50c9f2
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Packit Service 50c9f2
Packit Service 50c9f2
 @licend  The above is the entire license notice
Packit Service 50c9f2
 for the JavaScript code in this file
Packit Service 50c9f2
 */
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * The code below is based on SVGPan Library 1.2 and was modified for doxygen
Packit Service 50c9f2
 * to support both zooming and panning via the mouse and via embedded bottons.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * This code is licensed under the following BSD license:
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Copyright 2009-2010 Andrea Leofreddi <a.leofreddi@itcharm.com>. All rights reserved.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Redistribution and use in source and binary forms, with or without modification, are
Packit Service 50c9f2
 * permitted provided that the following conditions are met:
Packit Service 50c9f2
 *
Packit Service 50c9f2
 *    1. Redistributions of source code must retain the above copyright notice, this list of
Packit Service 50c9f2
 *       conditions and the following disclaimer.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
Packit Service 50c9f2
 *       of conditions and the following disclaimer in the documentation and/or other materials
Packit Service 50c9f2
 *       provided with the distribution.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * THIS SOFTWARE IS PROVIDED BY Andrea Leofreddi ``AS IS'' AND ANY EXPRESS OR IMPLIED
Packit Service 50c9f2
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
Packit Service 50c9f2
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Andrea Leofreddi OR
Packit Service 50c9f2
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit Service 50c9f2
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit Service 50c9f2
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
Packit Service 50c9f2
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit Service 50c9f2
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Packit Service 50c9f2
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * The views and conclusions contained in the software and documentation are those of the
Packit Service 50c9f2
 * authors and should not be interpreted as representing official policies, either expressed
Packit Service 50c9f2
 * or implied, of Andrea Leofreddi.
Packit Service 50c9f2
 */
Packit Service 50c9f2
Packit Service 50c9f2
var root = document.documentElement;
Packit Service 50c9f2
var state = 'none';
Packit Service 50c9f2
var stateOrigin;
Packit Service 50c9f2
var stateTf = root.createSVGMatrix();
Packit Service 50c9f2
var cursorGrab = ' url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAMAAAAolt3jAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAlQTFRFAAAA////////c3ilYwAAAAN0Uk5T//8A18oNQQAAAD1JREFUeNp0zlEKACAIA9Bt9z90bZBZkQj29qFBEuBOzQHSnWTTyckEfqUuZgFvslH4ch3qLCO/Kr8cAgwATw4Ax6XRCcoAAAAASUVORK5CYII="), move';
Packit Service 50c9f2
var zoomSteps = 10;
Packit Service 50c9f2
var zoomInFactor;
Packit Service 50c9f2
var zoomOutFactor;
Packit Service 50c9f2
var windowWidth;
Packit Service 50c9f2
var windowHeight;
Packit Service 50c9f2
var svgDoc;
Packit Service 50c9f2
var minZoom;
Packit Service 50c9f2
var maxZoom;
Packit Service 50c9f2
if (!window) window=this;
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * Show the graph in the middle of the view, scaled to fit
Packit Service 50c9f2
 */
Packit Service 50c9f2
function show()
Packit Service 50c9f2
{
Packit Service 50c9f2
  if (window.innerHeight) // Firefox
Packit Service 50c9f2
  {
Packit Service 50c9f2
    windowWidth = window.innerWidth;
Packit Service 50c9f2
    windowHeight = window.innerHeight;
Packit Service 50c9f2
  }
Packit Service 50c9f2
  else if (document.documentElement.clientWidth) // Chrome/Safari
Packit Service 50c9f2
  {
Packit Service 50c9f2
    windowWidth = document.documentElement.clientWidth
Packit Service 50c9f2
    windowHeight = document.documentElement.clientHeight
Packit Service 50c9f2
  }
Packit Service 50c9f2
  if (!windowWidth || !windowHeight) // failsafe
Packit Service 50c9f2
  {
Packit Service 50c9f2
    windowWidth = 800;
Packit Service 50c9f2
    windowHeight = 600;
Packit Service 50c9f2
  }
Packit Service 50c9f2
  minZoom = Math.min(Math.min(viewHeight,windowHeight)/viewHeight,Math.min(viewWidth,windowWidth)/viewWidth);
Packit Service 50c9f2
  maxZoom = minZoom+1.5;
Packit Service 50c9f2
  zoomInFactor = Math.pow(maxZoom/minZoom,1.0/zoomSteps);
Packit Service 50c9f2
  zoomOutFactor = 1.0/zoomInFactor;
Packit Service 50c9f2
Packit Service 50c9f2
  var g = svgDoc.getElementById('viewport');
Packit Service 50c9f2
  try
Packit Service 50c9f2
  {
Packit Service 50c9f2
    var bb = g.getBBox(); // this can throw an exception if css { display: none }
Packit Service 50c9f2
    var tx = (windowWidth-viewWidth*minZoom+8)/(2*minZoom);
Packit Service 50c9f2
    var ty = viewHeight+(windowHeight-viewHeight*minZoom)/(2*minZoom);
Packit Service 50c9f2
    var a = 'scale('+minZoom+') rotate(0) translate('+tx+' '+ty+')';
Packit Service 50c9f2
    g.setAttribute('transform',a);
Packit Service 50c9f2
  }
Packit Service 50c9f2
  catch(e) {}
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * Register handlers
Packit Service 50c9f2
 */
Packit Service 50c9f2
function init(evt)
Packit Service 50c9f2
{
Packit Service 50c9f2
  svgDoc = evt.target.ownerDocument;
Packit Service 50c9f2
  try {
Packit Service 50c9f2
    if (top.window && top.window.registerShow) { // register show function in html doc for dynamic sections
Packit Service 50c9f2
      top.window.registerShow(sectionId,show);
Packit Service 50c9f2
    }
Packit Service 50c9f2
  } catch(e) {
Packit Service 50c9f2
    // ugh, we are not allowed to talk to the parent; can happen with Chrome when viewing pages
Packit Service 50c9f2
    // locally, since they treat every local page as having a different origin
Packit Service 50c9f2
  }
Packit Service 50c9f2
  show();
Packit Service 50c9f2
Packit Service 50c9f2
  setAttributes(root, {
Packit Service 50c9f2
     "onmousedown" : "handleMouseDown(evt)",
Packit Service 50c9f2
     "onmousemove" : "handleMouseMove(evt)",
Packit Service 50c9f2
     "onmouseup"   : "handleMouseUp(evt)"
Packit Service 50c9f2
  });
Packit Service 50c9f2
Packit Service 50c9f2
  if (window.addEventListener)
Packit Service 50c9f2
  {
Packit Service 50c9f2
    if (navigator.userAgent.toLowerCase().indexOf('webkit') >= 0 ||
Packit Service 50c9f2
        navigator.userAgent.toLowerCase().indexOf("opera") >= 0 ||
Packit Service 50c9f2
        navigator.appVersion.indexOf("MSIE") != -1)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      window.addEventListener('mousewheel', handleMouseWheel, false); // Chrome/Safari/IE9
Packit Service 50c9f2
    }
Packit Service 50c9f2
    else
Packit Service 50c9f2
    {
Packit Service 50c9f2
      window.addEventListener('DOMMouseScroll', handleMouseWheel, false); // Others
Packit Service 50c9f2
    }
Packit Service 50c9f2
  }
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
window.onresize=function()
Packit Service 50c9f2
{
Packit Service 50c9f2
  if (svgDoc) { show(); }
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * Instance an SVGPoint object with given event coordinates.
Packit Service 50c9f2
 */
Packit Service 50c9f2
function getEventPoint(evt)
Packit Service 50c9f2
{
Packit Service 50c9f2
  var p = root.createSVGPoint();
Packit Service 50c9f2
  p.x = evt.clientX;
Packit Service 50c9f2
  p.y = evt.clientY;
Packit Service 50c9f2
  return p;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * Sets the current transform matrix of an element.
Packit Service 50c9f2
 */
Packit Service 50c9f2
function setCTM(element, matrix)
Packit Service 50c9f2
{
Packit Service 50c9f2
  var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
Packit Service 50c9f2
  element.setAttribute("transform", s);
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * Sets attributes of an element.
Packit Service 50c9f2
 */
Packit Service 50c9f2
function setAttributes(element, attributes)
Packit Service 50c9f2
{
Packit Service 50c9f2
  for (i in attributes)
Packit Service 50c9f2
    element.setAttributeNS(null, i, attributes[i]);
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
function doZoom(g,point,zoomFactor)
Packit Service 50c9f2
{
Packit Service 50c9f2
  var p = point.matrixTransform(g.getCTM().inverse());
Packit Service 50c9f2
  var k = root.createSVGMatrix().translate(p.x, p.y).scale(zoomFactor).translate(-p.x, -p.y);
Packit Service 50c9f2
  var n = g.getCTM().multiply(k);
Packit Service 50c9f2
  var s = Math.max(n.a,n.d);
Packit Service 50c9f2
  if      (s>maxZoom) n=n.translate(p.x,p.y).scale(maxZoom/s).translate(-p.x,-p.y);
Packit Service 50c9f2
  else if (s
Packit Service 50c9f2
  setCTM(g, n);
Packit Service 50c9f2
  stateTf = stateTf.multiply(n.inverse());
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * Handle mouse move event.
Packit Service 50c9f2
 */
Packit Service 50c9f2
function handleMouseWheel(evt)
Packit Service 50c9f2
{
Packit Service 50c9f2
  if (!evt) evt = window.evt;
Packit Service 50c9f2
  if (!evt.shiftKey) return; // only zoom when shift is pressed
Packit Service 50c9f2
  if (evt.preventDefault) evt.preventDefault();
Packit Service 50c9f2
  evt.returnValue = false;
Packit Service 50c9f2
Packit Service 50c9f2
  if (state!='pan')
Packit Service 50c9f2
  {
Packit Service 50c9f2
    var delta;
Packit Service 50c9f2
    if (evt.wheelDelta)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      delta = evt.wheelDelta / 7200; // Opera/Chrome/IE9/Safari
Packit Service 50c9f2
    }
Packit Service 50c9f2
    else
Packit Service 50c9f2
    {
Packit Service 50c9f2
      delta = evt.detail / -180; // Mozilla
Packit Service 50c9f2
    }
Packit Service 50c9f2
    var svgDoc = evt.target.ownerDocument;
Packit Service 50c9f2
    var g = svgDoc.getElementById("viewport");
Packit Service 50c9f2
    var p = getEventPoint(evt);
Packit Service 50c9f2
    doZoom(g,p,1+delta);
Packit Service 50c9f2
  }
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * Handle mouse move event.
Packit Service 50c9f2
 */
Packit Service 50c9f2
function handleMouseMove(evt)
Packit Service 50c9f2
{
Packit Service 50c9f2
  if(evt.preventDefault)
Packit Service 50c9f2
    evt.preventDefault();
Packit Service 50c9f2
Packit Service 50c9f2
  evt.returnValue = false;
Packit Service 50c9f2
Packit Service 50c9f2
  var g = svgDoc.getElementById("viewport");
Packit Service 50c9f2
Packit Service 50c9f2
  if (state == 'pan')
Packit Service 50c9f2
  {
Packit Service 50c9f2
    // Pan mode
Packit Service 50c9f2
    var p = getEventPoint(evt).matrixTransform(stateTf);
Packit Service 50c9f2
    setCTM(g,stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y));
Packit Service 50c9f2
  }
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * Handle click event.
Packit Service 50c9f2
 */
Packit Service 50c9f2
function handleMouseDown(evt)
Packit Service 50c9f2
{
Packit Service 50c9f2
  if(evt.preventDefault)
Packit Service 50c9f2
    evt.preventDefault();
Packit Service 50c9f2
  evt.returnValue = false;
Packit Service 50c9f2
  var g = svgDoc.getElementById("viewport");
Packit Service 50c9f2
  state = 'pan';
Packit Service 50c9f2
  stateTf = g.getCTM().inverse();
Packit Service 50c9f2
  stateOrigin = getEventPoint(evt).matrixTransform(stateTf);
Packit Service 50c9f2
  g.style.cursor = cursorGrab;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * Handle mouse button release event.
Packit Service 50c9f2
 */
Packit Service 50c9f2
function handleMouseUp(evt)
Packit Service 50c9f2
{
Packit Service 50c9f2
  if (evt.preventDefault) evt.preventDefault();
Packit Service 50c9f2
  evt.returnValue = false;
Packit Service 50c9f2
  var g = svgDoc.getElementById("viewport");
Packit Service 50c9f2
  g.style.cursor = "default";
Packit Service 50c9f2
  // Quit pan mode
Packit Service 50c9f2
  state = '';
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * Dumps a matrix to a string (useful for debug).
Packit Service 50c9f2
 */
Packit Service 50c9f2
function dumpMatrix(matrix)
Packit Service 50c9f2
{
Packit Service 50c9f2
  var s = "[ " + matrix.a + ", " + matrix.c + ", " + matrix.e + "\n  " + matrix.b + ", " + matrix.d + ", " + matrix.f + "\n  0, 0, 1 ]";
Packit Service 50c9f2
  return s;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * Handler for pan buttons
Packit Service 50c9f2
 */
Packit Service 50c9f2
function handlePan(x,y)
Packit Service 50c9f2
{
Packit Service 50c9f2
  var g = svgDoc.getElementById("viewport");
Packit Service 50c9f2
  setCTM(g,g.getCTM().translate(x*20/minZoom,y*20/minZoom));
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * Handle reset button
Packit Service 50c9f2
 */
Packit Service 50c9f2
function handleReset()
Packit Service 50c9f2
{
Packit Service 50c9f2
  show();
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * Handler for zoom buttons
Packit Service 50c9f2
 */
Packit Service 50c9f2
function handleZoom(evt,direction)
Packit Service 50c9f2
{
Packit Service 50c9f2
  var g = svgDoc.getElementById("viewport");
Packit Service 50c9f2
  var factor = direction=='in' ? zoomInFactor : zoomOutFactor;
Packit Service 50c9f2
  var m = g.getCTM();
Packit Service 50c9f2
  var p = root.createSVGPoint();
Packit Service 50c9f2
  p.x = windowWidth/2;
Packit Service 50c9f2
  p.y = windowHeight/2;
Packit Service 50c9f2
  doZoom(g,p,factor);
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
function serializeXmlNode(xmlNode)
Packit Service 50c9f2
{
Packit Service 50c9f2
  if (typeof window.XMLSerializer != "undefined") {
Packit Service 50c9f2
    return (new window.XMLSerializer()).serializeToString(xmlNode);
Packit Service 50c9f2
  } else if (typeof xmlNode.xml != "undefined") {
Packit Service 50c9f2
    return xmlNode.xml;
Packit Service 50c9f2
  }
Packit Service 50c9f2
  return "";
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * Handler for print function
Packit Service 50c9f2
 */
Packit Service 50c9f2
function handlePrint(evt)
Packit Service 50c9f2
{
Packit Service 50c9f2
  evt.returnValue = false;
Packit Service 50c9f2
  var g = svgDoc.getElementById("graph");
Packit Service 50c9f2
  var xs = serializeXmlNode(g);
Packit Service 50c9f2
  try {
Packit Service 50c9f2
    var w = window.open('about:blank','_blank','width='+windowWidth+',height='+windowHeight+
Packit Service 50c9f2
                        ',toolbar=0,status=0,menubar=0,scrollbars=0,resizable=0,location=0,directories=0');
Packit Service 50c9f2
    var d = w.document;
Packit Service 50c9f2
    d.write('
Packit Service 50c9f2
            'xmlns:svg="http://www.w3.org/2000/svg" '+
Packit Service 50c9f2
            'xmlns:xlink="http://www.w3.org/1999/xlink">');
Packit Service 50c9f2
    d.write('<head><title>Print SVG</title></head>');
Packit Service 50c9f2
    d.write('<body style="margin: 0px; padding: 0px;" onload="window.print();">');
Packit Service 50c9f2
    d.write('
'+xs+'
');
Packit Service 50c9f2
    d.write('</body>');
Packit Service 50c9f2
    d.write('</html>');
Packit Service 50c9f2
    d.close();
Packit Service 50c9f2
  } catch(e) {
Packit Service 50c9f2
    alert('Failed to open popup window needed for printing!\n'+e.message);
Packit Service 50c9f2
  }
Packit Service 50c9f2
}
Packit Service 50c9f2
/* @license-end */