svg-pan-zoom.js 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  1. // svg-pan-zoom v3.6.2
  2. // https://github.com/bumbu/svg-pan-zoom
  3. (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  4. var SvgUtils = require("./svg-utilities");
  5. module.exports = {
  6. enable: function(instance) {
  7. // Select (and create if necessary) defs
  8. var defs = instance.svg.querySelector("defs");
  9. if (!defs) {
  10. defs = document.createElementNS(SvgUtils.svgNS, "defs");
  11. instance.svg.appendChild(defs);
  12. }
  13. // Check for style element, and create it if it doesn't exist
  14. var styleEl = defs.querySelector("style#svg-pan-zoom-controls-styles");
  15. if (!styleEl) {
  16. var style = document.createElementNS(SvgUtils.svgNS, "style");
  17. style.setAttribute("id", "svg-pan-zoom-controls-styles");
  18. style.setAttribute("type", "text/css");
  19. style.textContent =
  20. ".svg-pan-zoom-control { cursor: pointer; fill: black; fill-opacity: 0.333; } .svg-pan-zoom-control:hover { fill-opacity: 0.8; } .svg-pan-zoom-control-background { fill: white; fill-opacity: 0.5; } .svg-pan-zoom-control-background { fill-opacity: 0.8; }";
  21. defs.appendChild(style);
  22. }
  23. // Zoom Group
  24. var zoomGroup = document.createElementNS(SvgUtils.svgNS, "g");
  25. zoomGroup.setAttribute("id", "svg-pan-zoom-controls");
  26. zoomGroup.setAttribute(
  27. "transform",
  28. "translate(" +
  29. (instance.width - 70) +
  30. " " +
  31. (instance.height - 76) +
  32. ") scale(0.75)"
  33. );
  34. zoomGroup.setAttribute("class", "svg-pan-zoom-control");
  35. // Control elements
  36. zoomGroup.appendChild(this._createZoomIn(instance));
  37. zoomGroup.appendChild(this._createZoomReset(instance));
  38. zoomGroup.appendChild(this._createZoomOut(instance));
  39. // Finally append created element
  40. instance.svg.appendChild(zoomGroup);
  41. // Cache control instance
  42. instance.controlIcons = zoomGroup;
  43. },
  44. _createZoomIn: function(instance) {
  45. var zoomIn = document.createElementNS(SvgUtils.svgNS, "g");
  46. zoomIn.setAttribute("id", "svg-pan-zoom-zoom-in");
  47. zoomIn.setAttribute("transform", "translate(30.5 5) scale(0.015)");
  48. zoomIn.setAttribute("class", "svg-pan-zoom-control");
  49. zoomIn.addEventListener(
  50. "click",
  51. function() {
  52. instance.getPublicInstance().zoomIn();
  53. },
  54. false
  55. );
  56. zoomIn.addEventListener(
  57. "touchstart",
  58. function() {
  59. instance.getPublicInstance().zoomIn();
  60. },
  61. false
  62. );
  63. var zoomInBackground = document.createElementNS(SvgUtils.svgNS, "rect"); // TODO change these background space fillers to rounded rectangles so they look prettier
  64. zoomInBackground.setAttribute("x", "0");
  65. zoomInBackground.setAttribute("y", "0");
  66. zoomInBackground.setAttribute("width", "1500"); // larger than expected because the whole group is transformed to scale down
  67. zoomInBackground.setAttribute("height", "1400");
  68. zoomInBackground.setAttribute("class", "svg-pan-zoom-control-background");
  69. zoomIn.appendChild(zoomInBackground);
  70. var zoomInShape = document.createElementNS(SvgUtils.svgNS, "path");
  71. zoomInShape.setAttribute(
  72. "d",
  73. "M1280 576v128q0 26 -19 45t-45 19h-320v320q0 26 -19 45t-45 19h-128q-26 0 -45 -19t-19 -45v-320h-320q-26 0 -45 -19t-19 -45v-128q0 -26 19 -45t45 -19h320v-320q0 -26 19 -45t45 -19h128q26 0 45 19t19 45v320h320q26 0 45 19t19 45zM1536 1120v-960 q0 -119 -84.5 -203.5t-203.5 -84.5h-960q-119 0 -203.5 84.5t-84.5 203.5v960q0 119 84.5 203.5t203.5 84.5h960q119 0 203.5 -84.5t84.5 -203.5z"
  74. );
  75. zoomInShape.setAttribute("class", "svg-pan-zoom-control-element");
  76. zoomIn.appendChild(zoomInShape);
  77. return zoomIn;
  78. },
  79. _createZoomReset: function(instance) {
  80. // reset
  81. var resetPanZoomControl = document.createElementNS(SvgUtils.svgNS, "g");
  82. resetPanZoomControl.setAttribute("id", "svg-pan-zoom-reset-pan-zoom");
  83. resetPanZoomControl.setAttribute("transform", "translate(5 35) scale(0.4)");
  84. resetPanZoomControl.setAttribute("class", "svg-pan-zoom-control");
  85. resetPanZoomControl.addEventListener(
  86. "click",
  87. function() {
  88. instance.getPublicInstance().reset();
  89. },
  90. false
  91. );
  92. resetPanZoomControl.addEventListener(
  93. "touchstart",
  94. function() {
  95. instance.getPublicInstance().reset();
  96. },
  97. false
  98. );
  99. var resetPanZoomControlBackground = document.createElementNS(
  100. SvgUtils.svgNS,
  101. "rect"
  102. ); // TODO change these background space fillers to rounded rectangles so they look prettier
  103. resetPanZoomControlBackground.setAttribute("x", "2");
  104. resetPanZoomControlBackground.setAttribute("y", "2");
  105. resetPanZoomControlBackground.setAttribute("width", "182"); // larger than expected because the whole group is transformed to scale down
  106. resetPanZoomControlBackground.setAttribute("height", "58");
  107. resetPanZoomControlBackground.setAttribute(
  108. "class",
  109. "svg-pan-zoom-control-background"
  110. );
  111. resetPanZoomControl.appendChild(resetPanZoomControlBackground);
  112. var resetPanZoomControlShape1 = document.createElementNS(
  113. SvgUtils.svgNS,
  114. "path"
  115. );
  116. resetPanZoomControlShape1.setAttribute(
  117. "d",
  118. "M33.051,20.632c-0.742-0.406-1.854-0.609-3.338-0.609h-7.969v9.281h7.769c1.543,0,2.701-0.188,3.473-0.562c1.365-0.656,2.048-1.953,2.048-3.891C35.032,22.757,34.372,21.351,33.051,20.632z"
  119. );
  120. resetPanZoomControlShape1.setAttribute(
  121. "class",
  122. "svg-pan-zoom-control-element"
  123. );
  124. resetPanZoomControl.appendChild(resetPanZoomControlShape1);
  125. var resetPanZoomControlShape2 = document.createElementNS(
  126. SvgUtils.svgNS,
  127. "path"
  128. );
  129. resetPanZoomControlShape2.setAttribute(
  130. "d",
  131. "M170.231,0.5H15.847C7.102,0.5,0.5,5.708,0.5,11.84v38.861C0.5,56.833,7.102,61.5,15.847,61.5h154.384c8.745,0,15.269-4.667,15.269-10.798V11.84C185.5,5.708,178.976,0.5,170.231,0.5z M42.837,48.569h-7.969c-0.219-0.766-0.375-1.383-0.469-1.852c-0.188-0.969-0.289-1.961-0.305-2.977l-0.047-3.211c-0.03-2.203-0.41-3.672-1.142-4.406c-0.732-0.734-2.103-1.102-4.113-1.102h-7.05v13.547h-7.055V14.022h16.524c2.361,0.047,4.178,0.344,5.45,0.891c1.272,0.547,2.351,1.352,3.234,2.414c0.731,0.875,1.31,1.844,1.737,2.906s0.64,2.273,0.64,3.633c0,1.641-0.414,3.254-1.242,4.84s-2.195,2.707-4.102,3.363c1.594,0.641,2.723,1.551,3.387,2.73s0.996,2.98,0.996,5.402v2.32c0,1.578,0.063,2.648,0.19,3.211c0.19,0.891,0.635,1.547,1.333,1.969V48.569z M75.579,48.569h-26.18V14.022h25.336v6.117H56.454v7.336h16.781v6H56.454v8.883h19.125V48.569z M104.497,46.331c-2.44,2.086-5.887,3.129-10.34,3.129c-4.548,0-8.125-1.027-10.731-3.082s-3.909-4.879-3.909-8.473h6.891c0.224,1.578,0.662,2.758,1.316,3.539c1.196,1.422,3.246,2.133,6.15,2.133c1.739,0,3.151-0.188,4.236-0.562c2.058-0.719,3.087-2.055,3.087-4.008c0-1.141-0.504-2.023-1.512-2.648c-1.008-0.609-2.607-1.148-4.796-1.617l-3.74-0.82c-3.676-0.812-6.201-1.695-7.576-2.648c-2.328-1.594-3.492-4.086-3.492-7.477c0-3.094,1.139-5.664,3.417-7.711s5.623-3.07,10.036-3.07c3.685,0,6.829,0.965,9.431,2.895c2.602,1.93,3.966,4.73,4.093,8.402h-6.938c-0.128-2.078-1.057-3.555-2.787-4.43c-1.154-0.578-2.587-0.867-4.301-0.867c-1.907,0-3.428,0.375-4.565,1.125c-1.138,0.75-1.706,1.797-1.706,3.141c0,1.234,0.561,2.156,1.682,2.766c0.721,0.406,2.25,0.883,4.589,1.43l6.063,1.43c2.657,0.625,4.648,1.461,5.975,2.508c2.059,1.625,3.089,3.977,3.089,7.055C108.157,41.624,106.937,44.245,104.497,46.331z M139.61,48.569h-26.18V14.022h25.336v6.117h-18.281v7.336h16.781v6h-16.781v8.883h19.125V48.569z M170.337,20.14h-10.336v28.43h-7.266V20.14h-10.383v-6.117h27.984V20.14z"
  132. );
  133. resetPanZoomControlShape2.setAttribute(
  134. "class",
  135. "svg-pan-zoom-control-element"
  136. );
  137. resetPanZoomControl.appendChild(resetPanZoomControlShape2);
  138. return resetPanZoomControl;
  139. },
  140. _createZoomOut: function(instance) {
  141. // zoom out
  142. var zoomOut = document.createElementNS(SvgUtils.svgNS, "g");
  143. zoomOut.setAttribute("id", "svg-pan-zoom-zoom-out");
  144. zoomOut.setAttribute("transform", "translate(30.5 70) scale(0.015)");
  145. zoomOut.setAttribute("class", "svg-pan-zoom-control");
  146. zoomOut.addEventListener(
  147. "click",
  148. function() {
  149. instance.getPublicInstance().zoomOut();
  150. },
  151. false
  152. );
  153. zoomOut.addEventListener(
  154. "touchstart",
  155. function() {
  156. instance.getPublicInstance().zoomOut();
  157. },
  158. false
  159. );
  160. var zoomOutBackground = document.createElementNS(SvgUtils.svgNS, "rect"); // TODO change these background space fillers to rounded rectangles so they look prettier
  161. zoomOutBackground.setAttribute("x", "0");
  162. zoomOutBackground.setAttribute("y", "0");
  163. zoomOutBackground.setAttribute("width", "1500"); // larger than expected because the whole group is transformed to scale down
  164. zoomOutBackground.setAttribute("height", "1400");
  165. zoomOutBackground.setAttribute("class", "svg-pan-zoom-control-background");
  166. zoomOut.appendChild(zoomOutBackground);
  167. var zoomOutShape = document.createElementNS(SvgUtils.svgNS, "path");
  168. zoomOutShape.setAttribute(
  169. "d",
  170. "M1280 576v128q0 26 -19 45t-45 19h-896q-26 0 -45 -19t-19 -45v-128q0 -26 19 -45t45 -19h896q26 0 45 19t19 45zM1536 1120v-960q0 -119 -84.5 -203.5t-203.5 -84.5h-960q-119 0 -203.5 84.5t-84.5 203.5v960q0 119 84.5 203.5t203.5 84.5h960q119 0 203.5 -84.5 t84.5 -203.5z"
  171. );
  172. zoomOutShape.setAttribute("class", "svg-pan-zoom-control-element");
  173. zoomOut.appendChild(zoomOutShape);
  174. return zoomOut;
  175. },
  176. disable: function(instance) {
  177. if (instance.controlIcons) {
  178. instance.controlIcons.parentNode.removeChild(instance.controlIcons);
  179. instance.controlIcons = null;
  180. }
  181. }
  182. };
  183. },{"./svg-utilities":5}],2:[function(require,module,exports){
  184. var SvgUtils = require("./svg-utilities"),
  185. Utils = require("./utilities");
  186. var ShadowViewport = function(viewport, options) {
  187. this.init(viewport, options);
  188. };
  189. /**
  190. * Initialization
  191. *
  192. * @param {SVGElement} viewport
  193. * @param {Object} options
  194. */
  195. ShadowViewport.prototype.init = function(viewport, options) {
  196. // DOM Elements
  197. this.viewport = viewport;
  198. this.options = options;
  199. // State cache
  200. this.originalState = { zoom: 1, x: 0, y: 0 };
  201. this.activeState = { zoom: 1, x: 0, y: 0 };
  202. this.updateCTMCached = Utils.proxy(this.updateCTM, this);
  203. // Create a custom requestAnimationFrame taking in account refreshRate
  204. this.requestAnimationFrame = Utils.createRequestAnimationFrame(
  205. this.options.refreshRate
  206. );
  207. // ViewBox
  208. this.viewBox = { x: 0, y: 0, width: 0, height: 0 };
  209. this.cacheViewBox();
  210. // Process CTM
  211. var newCTM = this.processCTM();
  212. // Update viewport CTM and cache zoom and pan
  213. this.setCTM(newCTM);
  214. // Update CTM in this frame
  215. this.updateCTM();
  216. };
  217. /**
  218. * Cache initial viewBox value
  219. * If no viewBox is defined, then use viewport size/position instead for viewBox values
  220. */
  221. ShadowViewport.prototype.cacheViewBox = function() {
  222. var svgViewBox = this.options.svg.getAttribute("viewBox");
  223. if (svgViewBox) {
  224. var viewBoxValues = svgViewBox
  225. .split(/[\s\,]/)
  226. .filter(function(v) {
  227. return v;
  228. })
  229. .map(parseFloat);
  230. // Cache viewbox x and y offset
  231. this.viewBox.x = viewBoxValues[0];
  232. this.viewBox.y = viewBoxValues[1];
  233. this.viewBox.width = viewBoxValues[2];
  234. this.viewBox.height = viewBoxValues[3];
  235. var zoom = Math.min(
  236. this.options.width / this.viewBox.width,
  237. this.options.height / this.viewBox.height
  238. );
  239. // Update active state
  240. this.activeState.zoom = zoom;
  241. this.activeState.x = (this.options.width - this.viewBox.width * zoom) / 2;
  242. this.activeState.y = (this.options.height - this.viewBox.height * zoom) / 2;
  243. // Force updating CTM
  244. this.updateCTMOnNextFrame();
  245. this.options.svg.removeAttribute("viewBox");
  246. } else {
  247. this.simpleViewBoxCache();
  248. }
  249. };
  250. /**
  251. * Recalculate viewport sizes and update viewBox cache
  252. */
  253. ShadowViewport.prototype.simpleViewBoxCache = function() {
  254. var bBox = this.viewport.getBBox();
  255. this.viewBox.x = bBox.x;
  256. this.viewBox.y = bBox.y;
  257. this.viewBox.width = bBox.width;
  258. this.viewBox.height = bBox.height;
  259. };
  260. /**
  261. * Returns a viewbox object. Safe to alter
  262. *
  263. * @return {Object} viewbox object
  264. */
  265. ShadowViewport.prototype.getViewBox = function() {
  266. return Utils.extend({}, this.viewBox);
  267. };
  268. /**
  269. * Get initial zoom and pan values. Save them into originalState
  270. * Parses viewBox attribute to alter initial sizes
  271. *
  272. * @return {CTM} CTM object based on options
  273. */
  274. ShadowViewport.prototype.processCTM = function() {
  275. var newCTM = this.getCTM();
  276. if (this.options.fit || this.options.contain) {
  277. var newScale;
  278. if (this.options.fit) {
  279. newScale = Math.min(
  280. this.options.width / this.viewBox.width,
  281. this.options.height / this.viewBox.height
  282. );
  283. } else {
  284. newScale = Math.max(
  285. this.options.width / this.viewBox.width,
  286. this.options.height / this.viewBox.height
  287. );
  288. }
  289. newCTM.a = newScale; //x-scale
  290. newCTM.d = newScale; //y-scale
  291. newCTM.e = -this.viewBox.x * newScale; //x-transform
  292. newCTM.f = -this.viewBox.y * newScale; //y-transform
  293. }
  294. if (this.options.center) {
  295. var offsetX =
  296. (this.options.width -
  297. (this.viewBox.width + this.viewBox.x * 2) * newCTM.a) *
  298. 0.5,
  299. offsetY =
  300. (this.options.height -
  301. (this.viewBox.height + this.viewBox.y * 2) * newCTM.a) *
  302. 0.5;
  303. newCTM.e = offsetX;
  304. newCTM.f = offsetY;
  305. }
  306. // Cache initial values. Based on activeState and fix+center opitons
  307. this.originalState.zoom = newCTM.a;
  308. this.originalState.x = newCTM.e;
  309. this.originalState.y = newCTM.f;
  310. return newCTM;
  311. };
  312. /**
  313. * Return originalState object. Safe to alter
  314. *
  315. * @return {Object}
  316. */
  317. ShadowViewport.prototype.getOriginalState = function() {
  318. return Utils.extend({}, this.originalState);
  319. };
  320. /**
  321. * Return actualState object. Safe to alter
  322. *
  323. * @return {Object}
  324. */
  325. ShadowViewport.prototype.getState = function() {
  326. return Utils.extend({}, this.activeState);
  327. };
  328. /**
  329. * Get zoom scale
  330. *
  331. * @return {Float} zoom scale
  332. */
  333. ShadowViewport.prototype.getZoom = function() {
  334. return this.activeState.zoom;
  335. };
  336. /**
  337. * Get zoom scale for pubilc usage
  338. *
  339. * @return {Float} zoom scale
  340. */
  341. ShadowViewport.prototype.getRelativeZoom = function() {
  342. return this.activeState.zoom / this.originalState.zoom;
  343. };
  344. /**
  345. * Compute zoom scale for pubilc usage
  346. *
  347. * @return {Float} zoom scale
  348. */
  349. ShadowViewport.prototype.computeRelativeZoom = function(scale) {
  350. return scale / this.originalState.zoom;
  351. };
  352. /**
  353. * Get pan
  354. *
  355. * @return {Object}
  356. */
  357. ShadowViewport.prototype.getPan = function() {
  358. return { x: this.activeState.x, y: this.activeState.y };
  359. };
  360. /**
  361. * Return cached viewport CTM value that can be safely modified
  362. *
  363. * @return {SVGMatrix}
  364. */
  365. ShadowViewport.prototype.getCTM = function() {
  366. var safeCTM = this.options.svg.createSVGMatrix();
  367. // Copy values manually as in FF they are not itterable
  368. safeCTM.a = this.activeState.zoom;
  369. safeCTM.b = 0;
  370. safeCTM.c = 0;
  371. safeCTM.d = this.activeState.zoom;
  372. safeCTM.e = this.activeState.x;
  373. safeCTM.f = this.activeState.y;
  374. return safeCTM;
  375. };
  376. /**
  377. * Set a new CTM
  378. *
  379. * @param {SVGMatrix} newCTM
  380. */
  381. ShadowViewport.prototype.setCTM = function(newCTM) {
  382. var willZoom = this.isZoomDifferent(newCTM),
  383. willPan = this.isPanDifferent(newCTM);
  384. if (willZoom || willPan) {
  385. // Before zoom
  386. if (willZoom) {
  387. // If returns false then cancel zooming
  388. if (
  389. this.options.beforeZoom(
  390. this.getRelativeZoom(),
  391. this.computeRelativeZoom(newCTM.a)
  392. ) === false
  393. ) {
  394. newCTM.a = newCTM.d = this.activeState.zoom;
  395. willZoom = false;
  396. } else {
  397. this.updateCache(newCTM);
  398. this.options.onZoom(this.getRelativeZoom());
  399. }
  400. }
  401. // Before pan
  402. if (willPan) {
  403. var preventPan = this.options.beforePan(this.getPan(), {
  404. x: newCTM.e,
  405. y: newCTM.f
  406. }),
  407. // If prevent pan is an object
  408. preventPanX = false,
  409. preventPanY = false;
  410. // If prevent pan is Boolean false
  411. if (preventPan === false) {
  412. // Set x and y same as before
  413. newCTM.e = this.getPan().x;
  414. newCTM.f = this.getPan().y;
  415. preventPanX = preventPanY = true;
  416. } else if (Utils.isObject(preventPan)) {
  417. // Check for X axes attribute
  418. if (preventPan.x === false) {
  419. // Prevent panning on x axes
  420. newCTM.e = this.getPan().x;
  421. preventPanX = true;
  422. } else if (Utils.isNumber(preventPan.x)) {
  423. // Set a custom pan value
  424. newCTM.e = preventPan.x;
  425. }
  426. // Check for Y axes attribute
  427. if (preventPan.y === false) {
  428. // Prevent panning on x axes
  429. newCTM.f = this.getPan().y;
  430. preventPanY = true;
  431. } else if (Utils.isNumber(preventPan.y)) {
  432. // Set a custom pan value
  433. newCTM.f = preventPan.y;
  434. }
  435. }
  436. // Update willPan flag
  437. // Check if newCTM is still different
  438. if ((preventPanX && preventPanY) || !this.isPanDifferent(newCTM)) {
  439. willPan = false;
  440. } else {
  441. this.updateCache(newCTM);
  442. this.options.onPan(this.getPan());
  443. }
  444. }
  445. // Check again if should zoom or pan
  446. if (willZoom || willPan) {
  447. this.updateCTMOnNextFrame();
  448. }
  449. }
  450. };
  451. ShadowViewport.prototype.isZoomDifferent = function(newCTM) {
  452. return this.activeState.zoom !== newCTM.a;
  453. };
  454. ShadowViewport.prototype.isPanDifferent = function(newCTM) {
  455. return this.activeState.x !== newCTM.e || this.activeState.y !== newCTM.f;
  456. };
  457. /**
  458. * Update cached CTM and active state
  459. *
  460. * @param {SVGMatrix} newCTM
  461. */
  462. ShadowViewport.prototype.updateCache = function(newCTM) {
  463. this.activeState.zoom = newCTM.a;
  464. this.activeState.x = newCTM.e;
  465. this.activeState.y = newCTM.f;
  466. };
  467. ShadowViewport.prototype.pendingUpdate = false;
  468. /**
  469. * Place a request to update CTM on next Frame
  470. */
  471. ShadowViewport.prototype.updateCTMOnNextFrame = function() {
  472. if (!this.pendingUpdate) {
  473. // Lock
  474. this.pendingUpdate = true;
  475. // Throttle next update
  476. this.requestAnimationFrame.call(window, this.updateCTMCached);
  477. }
  478. };
  479. /**
  480. * Update viewport CTM with cached CTM
  481. */
  482. ShadowViewport.prototype.updateCTM = function() {
  483. var ctm = this.getCTM();
  484. // Updates SVG element
  485. SvgUtils.setCTM(this.viewport, ctm, this.defs);
  486. // Free the lock
  487. this.pendingUpdate = false;
  488. // Notify about the update
  489. if (this.options.onUpdatedCTM) {
  490. this.options.onUpdatedCTM(ctm);
  491. }
  492. };
  493. module.exports = function(viewport, options) {
  494. return new ShadowViewport(viewport, options);
  495. };
  496. },{"./svg-utilities":5,"./utilities":7}],3:[function(require,module,exports){
  497. var svgPanZoom = require("./svg-pan-zoom.js");
  498. // UMD module definition
  499. (function(window, document) {
  500. // AMD
  501. if (typeof define === "function" && define.amd) {
  502. define("svg-pan-zoom", function() {
  503. return svgPanZoom;
  504. });
  505. // CMD
  506. } else if (typeof module !== "undefined" && module.exports) {
  507. module.exports = svgPanZoom;
  508. // Browser
  509. // Keep exporting globally as module.exports is available because of browserify
  510. window.svgPanZoom = svgPanZoom;
  511. }
  512. })(window, document);
  513. },{"./svg-pan-zoom.js":4}],4:[function(require,module,exports){
  514. var Wheel = require("./uniwheel"),
  515. ControlIcons = require("./control-icons"),
  516. Utils = require("./utilities"),
  517. SvgUtils = require("./svg-utilities"),
  518. ShadowViewport = require("./shadow-viewport");
  519. var SvgPanZoom = function(svg, options) {
  520. this.init(svg, options);
  521. };
  522. var optionsDefaults = {
  523. viewportSelector: ".svg-pan-zoom_viewport", // Viewport selector. Can be querySelector string or SVGElement
  524. panEnabled: true, // enable or disable panning (default enabled)
  525. controlIconsEnabled: false, // insert icons to give user an option in addition to mouse events to control pan/zoom (default disabled)
  526. zoomEnabled: true, // enable or disable zooming (default enabled)
  527. dblClickZoomEnabled: true, // enable or disable zooming by double clicking (default enabled)
  528. mouseWheelZoomEnabled: true, // enable or disable zooming by mouse wheel (default enabled)
  529. preventMouseEventsDefault: true, // enable or disable preventDefault for mouse events
  530. zoomScaleSensitivity: 0.1, // Zoom sensitivity
  531. minZoom: 0.5, // Minimum Zoom level
  532. maxZoom: 10, // Maximum Zoom level
  533. fit: true, // enable or disable viewport fit in SVG (default true)
  534. contain: false, // enable or disable viewport contain the svg (default false)
  535. center: true, // enable or disable viewport centering in SVG (default true)
  536. refreshRate: "auto", // Maximum number of frames per second (altering SVG's viewport)
  537. beforeZoom: null,
  538. onZoom: null,
  539. beforePan: null,
  540. onPan: null,
  541. customEventsHandler: null,
  542. eventsListenerElement: null,
  543. onUpdatedCTM: null
  544. };
  545. var passiveListenerOption = { passive: true };
  546. SvgPanZoom.prototype.init = function(svg, options) {
  547. var that = this;
  548. this.svg = svg;
  549. this.defs = svg.querySelector("defs");
  550. // Add default attributes to SVG
  551. SvgUtils.setupSvgAttributes(this.svg);
  552. // Set options
  553. this.options = Utils.extend(Utils.extend({}, optionsDefaults), options);
  554. // Set default state
  555. this.state = "none";
  556. // Get dimensions
  557. var boundingClientRectNormalized = SvgUtils.getBoundingClientRectNormalized(
  558. svg
  559. );
  560. this.width = boundingClientRectNormalized.width;
  561. this.height = boundingClientRectNormalized.height;
  562. // Init shadow viewport
  563. this.viewport = ShadowViewport(
  564. SvgUtils.getOrCreateViewport(this.svg, this.options.viewportSelector),
  565. {
  566. svg: this.svg,
  567. width: this.width,
  568. height: this.height,
  569. fit: this.options.fit,
  570. contain: this.options.contain,
  571. center: this.options.center,
  572. refreshRate: this.options.refreshRate,
  573. // Put callbacks into functions as they can change through time
  574. beforeZoom: function(oldScale, newScale) {
  575. if (that.viewport && that.options.beforeZoom) {
  576. return that.options.beforeZoom(oldScale, newScale);
  577. }
  578. },
  579. onZoom: function(scale) {
  580. if (that.viewport && that.options.onZoom) {
  581. return that.options.onZoom(scale);
  582. }
  583. },
  584. beforePan: function(oldPoint, newPoint) {
  585. if (that.viewport && that.options.beforePan) {
  586. return that.options.beforePan(oldPoint, newPoint);
  587. }
  588. },
  589. onPan: function(point) {
  590. if (that.viewport && that.options.onPan) {
  591. return that.options.onPan(point);
  592. }
  593. },
  594. onUpdatedCTM: function(ctm) {
  595. if (that.viewport && that.options.onUpdatedCTM) {
  596. return that.options.onUpdatedCTM(ctm);
  597. }
  598. }
  599. }
  600. );
  601. // Wrap callbacks into public API context
  602. var publicInstance = this.getPublicInstance();
  603. publicInstance.setBeforeZoom(this.options.beforeZoom);
  604. publicInstance.setOnZoom(this.options.onZoom);
  605. publicInstance.setBeforePan(this.options.beforePan);
  606. publicInstance.setOnPan(this.options.onPan);
  607. publicInstance.setOnUpdatedCTM(this.options.onUpdatedCTM);
  608. if (this.options.controlIconsEnabled) {
  609. ControlIcons.enable(this);
  610. }
  611. // Init events handlers
  612. this.lastMouseWheelEventTime = Date.now();
  613. this.setupHandlers();
  614. };
  615. /**
  616. * Register event handlers
  617. */
  618. SvgPanZoom.prototype.setupHandlers = function() {
  619. var that = this,
  620. prevEvt = null; // use for touchstart event to detect double tap
  621. this.eventListeners = {
  622. // Mouse down group
  623. mousedown: function(evt) {
  624. var result = that.handleMouseDown(evt, prevEvt);
  625. prevEvt = evt;
  626. return result;
  627. },
  628. touchstart: function(evt) {
  629. var result = that.handleMouseDown(evt, prevEvt);
  630. prevEvt = evt;
  631. return result;
  632. },
  633. // Mouse up group
  634. mouseup: function(evt) {
  635. return that.handleMouseUp(evt);
  636. },
  637. touchend: function(evt) {
  638. return that.handleMouseUp(evt);
  639. },
  640. // Mouse move group
  641. mousemove: function(evt) {
  642. return that.handleMouseMove(evt);
  643. },
  644. touchmove: function(evt) {
  645. return that.handleMouseMove(evt);
  646. },
  647. // Mouse leave group
  648. mouseleave: function(evt) {
  649. return that.handleMouseUp(evt);
  650. },
  651. touchleave: function(evt) {
  652. return that.handleMouseUp(evt);
  653. },
  654. touchcancel: function(evt) {
  655. return that.handleMouseUp(evt);
  656. }
  657. };
  658. // Init custom events handler if available
  659. // eslint-disable-next-line eqeqeq
  660. if (this.options.customEventsHandler != null) {
  661. this.options.customEventsHandler.init({
  662. svgElement: this.svg,
  663. eventsListenerElement: this.options.eventsListenerElement,
  664. instance: this.getPublicInstance()
  665. });
  666. // Custom event handler may halt builtin listeners
  667. var haltEventListeners = this.options.customEventsHandler
  668. .haltEventListeners;
  669. if (haltEventListeners && haltEventListeners.length) {
  670. for (var i = haltEventListeners.length - 1; i >= 0; i--) {
  671. if (this.eventListeners.hasOwnProperty(haltEventListeners[i])) {
  672. delete this.eventListeners[haltEventListeners[i]];
  673. }
  674. }
  675. }
  676. }
  677. // Bind eventListeners
  678. for (var event in this.eventListeners) {
  679. // Attach event to eventsListenerElement or SVG if not available
  680. (this.options.eventsListenerElement || this.svg).addEventListener(
  681. event,
  682. this.eventListeners[event],
  683. !this.options.preventMouseEventsDefault ? passiveListenerOption : false
  684. );
  685. }
  686. // Zoom using mouse wheel
  687. if (this.options.mouseWheelZoomEnabled) {
  688. this.options.mouseWheelZoomEnabled = false; // set to false as enable will set it back to true
  689. this.enableMouseWheelZoom();
  690. }
  691. };
  692. /**
  693. * Enable ability to zoom using mouse wheel
  694. */
  695. SvgPanZoom.prototype.enableMouseWheelZoom = function() {
  696. if (!this.options.mouseWheelZoomEnabled) {
  697. var that = this;
  698. // Mouse wheel listener
  699. this.wheelListener = function(evt) {
  700. return that.handleMouseWheel(evt);
  701. };
  702. // Bind wheelListener
  703. var isPassiveListener = !this.options.preventMouseEventsDefault;
  704. Wheel.on(
  705. this.options.eventsListenerElement || this.svg,
  706. this.wheelListener,
  707. isPassiveListener
  708. );
  709. this.options.mouseWheelZoomEnabled = true;
  710. }
  711. };
  712. /**
  713. * Disable ability to zoom using mouse wheel
  714. */
  715. SvgPanZoom.prototype.disableMouseWheelZoom = function() {
  716. if (this.options.mouseWheelZoomEnabled) {
  717. var isPassiveListener = !this.options.preventMouseEventsDefault;
  718. Wheel.off(
  719. this.options.eventsListenerElement || this.svg,
  720. this.wheelListener,
  721. isPassiveListener
  722. );
  723. this.options.mouseWheelZoomEnabled = false;
  724. }
  725. };
  726. /**
  727. * Handle mouse wheel event
  728. *
  729. * @param {Event} evt
  730. */
  731. SvgPanZoom.prototype.handleMouseWheel = function(evt) {
  732. if (!this.options.zoomEnabled || this.state !== "none") {
  733. return;
  734. }
  735. if (this.options.preventMouseEventsDefault) {
  736. if (evt.preventDefault) {
  737. evt.preventDefault();
  738. } else {
  739. evt.returnValue = false;
  740. }
  741. }
  742. // Default delta in case that deltaY is not available
  743. var delta = evt.deltaY || 1,
  744. timeDelta = Date.now() - this.lastMouseWheelEventTime,
  745. divider = 3 + Math.max(0, 30 - timeDelta);
  746. // Update cache
  747. this.lastMouseWheelEventTime = Date.now();
  748. // Make empirical adjustments for browsers that give deltaY in pixels (deltaMode=0)
  749. if ("deltaMode" in evt && evt.deltaMode === 0 && evt.wheelDelta) {
  750. delta = evt.deltaY === 0 ? 0 : Math.abs(evt.wheelDelta) / evt.deltaY;
  751. }
  752. delta =
  753. -0.3 < delta && delta < 0.3
  754. ? delta
  755. : ((delta > 0 ? 1 : -1) * Math.log(Math.abs(delta) + 10)) / divider;
  756. var inversedScreenCTM = this.svg.getScreenCTM().inverse(),
  757. relativeMousePoint = SvgUtils.getEventPoint(evt, this.svg).matrixTransform(
  758. inversedScreenCTM
  759. ),
  760. zoom = Math.pow(1 + this.options.zoomScaleSensitivity, -1 * delta); // multiplying by neg. 1 so as to make zoom in/out behavior match Google maps behavior
  761. this.zoomAtPoint(zoom, relativeMousePoint);
  762. };
  763. /**
  764. * Zoom in at a SVG point
  765. *
  766. * @param {SVGPoint} point
  767. * @param {Float} zoomScale Number representing how much to zoom
  768. * @param {Boolean} zoomAbsolute Default false. If true, zoomScale is treated as an absolute value.
  769. * Otherwise, zoomScale is treated as a multiplied (e.g. 1.10 would zoom in 10%)
  770. */
  771. SvgPanZoom.prototype.zoomAtPoint = function(zoomScale, point, zoomAbsolute) {
  772. var originalState = this.viewport.getOriginalState();
  773. if (!zoomAbsolute) {
  774. // Fit zoomScale in set bounds
  775. if (
  776. this.getZoom() * zoomScale <
  777. this.options.minZoom * originalState.zoom
  778. ) {
  779. zoomScale = (this.options.minZoom * originalState.zoom) / this.getZoom();
  780. } else if (
  781. this.getZoom() * zoomScale >
  782. this.options.maxZoom * originalState.zoom
  783. ) {
  784. zoomScale = (this.options.maxZoom * originalState.zoom) / this.getZoom();
  785. }
  786. } else {
  787. // Fit zoomScale in set bounds
  788. zoomScale = Math.max(
  789. this.options.minZoom * originalState.zoom,
  790. Math.min(this.options.maxZoom * originalState.zoom, zoomScale)
  791. );
  792. // Find relative scale to achieve desired scale
  793. zoomScale = zoomScale / this.getZoom();
  794. }
  795. var oldCTM = this.viewport.getCTM(),
  796. relativePoint = point.matrixTransform(oldCTM.inverse()),
  797. modifier = this.svg
  798. .createSVGMatrix()
  799. .translate(relativePoint.x, relativePoint.y)
  800. .scale(zoomScale)
  801. .translate(-relativePoint.x, -relativePoint.y),
  802. newCTM = oldCTM.multiply(modifier);
  803. if (newCTM.a !== oldCTM.a) {
  804. this.viewport.setCTM(newCTM);
  805. }
  806. };
  807. /**
  808. * Zoom at center point
  809. *
  810. * @param {Float} scale
  811. * @param {Boolean} absolute Marks zoom scale as relative or absolute
  812. */
  813. SvgPanZoom.prototype.zoom = function(scale, absolute) {
  814. this.zoomAtPoint(
  815. scale,
  816. SvgUtils.getSvgCenterPoint(this.svg, this.width, this.height),
  817. absolute
  818. );
  819. };
  820. /**
  821. * Zoom used by public instance
  822. *
  823. * @param {Float} scale
  824. * @param {Boolean} absolute Marks zoom scale as relative or absolute
  825. */
  826. SvgPanZoom.prototype.publicZoom = function(scale, absolute) {
  827. if (absolute) {
  828. scale = this.computeFromRelativeZoom(scale);
  829. }
  830. this.zoom(scale, absolute);
  831. };
  832. /**
  833. * Zoom at point used by public instance
  834. *
  835. * @param {Float} scale
  836. * @param {SVGPoint|Object} point An object that has x and y attributes
  837. * @param {Boolean} absolute Marks zoom scale as relative or absolute
  838. */
  839. SvgPanZoom.prototype.publicZoomAtPoint = function(scale, point, absolute) {
  840. if (absolute) {
  841. // Transform zoom into a relative value
  842. scale = this.computeFromRelativeZoom(scale);
  843. }
  844. // If not a SVGPoint but has x and y then create a SVGPoint
  845. if (Utils.getType(point) !== "SVGPoint") {
  846. if ("x" in point && "y" in point) {
  847. point = SvgUtils.createSVGPoint(this.svg, point.x, point.y);
  848. } else {
  849. throw new Error("Given point is invalid");
  850. }
  851. }
  852. this.zoomAtPoint(scale, point, absolute);
  853. };
  854. /**
  855. * Get zoom scale
  856. *
  857. * @return {Float} zoom scale
  858. */
  859. SvgPanZoom.prototype.getZoom = function() {
  860. return this.viewport.getZoom();
  861. };
  862. /**
  863. * Get zoom scale for public usage
  864. *
  865. * @return {Float} zoom scale
  866. */
  867. SvgPanZoom.prototype.getRelativeZoom = function() {
  868. return this.viewport.getRelativeZoom();
  869. };
  870. /**
  871. * Compute actual zoom from public zoom
  872. *
  873. * @param {Float} zoom
  874. * @return {Float} zoom scale
  875. */
  876. SvgPanZoom.prototype.computeFromRelativeZoom = function(zoom) {
  877. return zoom * this.viewport.getOriginalState().zoom;
  878. };
  879. /**
  880. * Set zoom to initial state
  881. */
  882. SvgPanZoom.prototype.resetZoom = function() {
  883. var originalState = this.viewport.getOriginalState();
  884. this.zoom(originalState.zoom, true);
  885. };
  886. /**
  887. * Set pan to initial state
  888. */
  889. SvgPanZoom.prototype.resetPan = function() {
  890. this.pan(this.viewport.getOriginalState());
  891. };
  892. /**
  893. * Set pan and zoom to initial state
  894. */
  895. SvgPanZoom.prototype.reset = function() {
  896. this.resetZoom();
  897. this.resetPan();
  898. };
  899. /**
  900. * Handle double click event
  901. * See handleMouseDown() for alternate detection method
  902. *
  903. * @param {Event} evt
  904. */
  905. SvgPanZoom.prototype.handleDblClick = function(evt) {
  906. if (this.options.preventMouseEventsDefault) {
  907. if (evt.preventDefault) {
  908. evt.preventDefault();
  909. } else {
  910. evt.returnValue = false;
  911. }
  912. }
  913. // Check if target was a control button
  914. if (this.options.controlIconsEnabled) {
  915. var targetClass = evt.target.getAttribute("class") || "";
  916. if (targetClass.indexOf("svg-pan-zoom-control") > -1) {
  917. return false;
  918. }
  919. }
  920. var zoomFactor;
  921. if (evt.shiftKey) {
  922. zoomFactor = 1 / ((1 + this.options.zoomScaleSensitivity) * 2); // zoom out when shift key pressed
  923. } else {
  924. zoomFactor = (1 + this.options.zoomScaleSensitivity) * 2;
  925. }
  926. var point = SvgUtils.getEventPoint(evt, this.svg).matrixTransform(
  927. this.svg.getScreenCTM().inverse()
  928. );
  929. this.zoomAtPoint(zoomFactor, point);
  930. };
  931. /**
  932. * Handle click event
  933. *
  934. * @param {Event} evt
  935. */
  936. SvgPanZoom.prototype.handleMouseDown = function(evt, prevEvt) {
  937. if (this.options.preventMouseEventsDefault) {
  938. if (evt.preventDefault) {
  939. evt.preventDefault();
  940. } else {
  941. evt.returnValue = false;
  942. }
  943. }
  944. Utils.mouseAndTouchNormalize(evt, this.svg);
  945. // Double click detection; more consistent than ondblclick
  946. if (this.options.dblClickZoomEnabled && Utils.isDblClick(evt, prevEvt)) {
  947. this.handleDblClick(evt);
  948. } else {
  949. // Pan mode
  950. this.state = "pan";
  951. this.firstEventCTM = this.viewport.getCTM();
  952. this.stateOrigin = SvgUtils.getEventPoint(evt, this.svg).matrixTransform(
  953. this.firstEventCTM.inverse()
  954. );
  955. }
  956. };
  957. /**
  958. * Handle mouse move event
  959. *
  960. * @param {Event} evt
  961. */
  962. SvgPanZoom.prototype.handleMouseMove = function(evt) {
  963. if (this.options.preventMouseEventsDefault) {
  964. if (evt.preventDefault) {
  965. evt.preventDefault();
  966. } else {
  967. evt.returnValue = false;
  968. }
  969. }
  970. if (this.state === "pan" && this.options.panEnabled) {
  971. // Pan mode
  972. var point = SvgUtils.getEventPoint(evt, this.svg).matrixTransform(
  973. this.firstEventCTM.inverse()
  974. ),
  975. viewportCTM = this.firstEventCTM.translate(
  976. point.x - this.stateOrigin.x,
  977. point.y - this.stateOrigin.y
  978. );
  979. this.viewport.setCTM(viewportCTM);
  980. }
  981. };
  982. /**
  983. * Handle mouse button release event
  984. *
  985. * @param {Event} evt
  986. */
  987. SvgPanZoom.prototype.handleMouseUp = function(evt) {
  988. if (this.options.preventMouseEventsDefault) {
  989. if (evt.preventDefault) {
  990. evt.preventDefault();
  991. } else {
  992. evt.returnValue = false;
  993. }
  994. }
  995. if (this.state === "pan") {
  996. // Quit pan mode
  997. this.state = "none";
  998. }
  999. };
  1000. /**
  1001. * Adjust viewport size (only) so it will fit in SVG
  1002. * Does not center image
  1003. */
  1004. SvgPanZoom.prototype.fit = function() {
  1005. var viewBox = this.viewport.getViewBox(),
  1006. newScale = Math.min(
  1007. this.width / viewBox.width,
  1008. this.height / viewBox.height
  1009. );
  1010. this.zoom(newScale, true);
  1011. };
  1012. /**
  1013. * Adjust viewport size (only) so it will contain the SVG
  1014. * Does not center image
  1015. */
  1016. SvgPanZoom.prototype.contain = function() {
  1017. var viewBox = this.viewport.getViewBox(),
  1018. newScale = Math.max(
  1019. this.width / viewBox.width,
  1020. this.height / viewBox.height
  1021. );
  1022. this.zoom(newScale, true);
  1023. };
  1024. /**
  1025. * Adjust viewport pan (only) so it will be centered in SVG
  1026. * Does not zoom/fit/contain image
  1027. */
  1028. SvgPanZoom.prototype.center = function() {
  1029. var viewBox = this.viewport.getViewBox(),
  1030. offsetX =
  1031. (this.width - (viewBox.width + viewBox.x * 2) * this.getZoom()) * 0.5,
  1032. offsetY =
  1033. (this.height - (viewBox.height + viewBox.y * 2) * this.getZoom()) * 0.5;
  1034. this.getPublicInstance().pan({ x: offsetX, y: offsetY });
  1035. };
  1036. /**
  1037. * Update content cached BorderBox
  1038. * Use when viewport contents change
  1039. */
  1040. SvgPanZoom.prototype.updateBBox = function() {
  1041. this.viewport.simpleViewBoxCache();
  1042. };
  1043. /**
  1044. * Pan to a rendered position
  1045. *
  1046. * @param {Object} point {x: 0, y: 0}
  1047. */
  1048. SvgPanZoom.prototype.pan = function(point) {
  1049. var viewportCTM = this.viewport.getCTM();
  1050. viewportCTM.e = point.x;
  1051. viewportCTM.f = point.y;
  1052. this.viewport.setCTM(viewportCTM);
  1053. };
  1054. /**
  1055. * Relatively pan the graph by a specified rendered position vector
  1056. *
  1057. * @param {Object} point {x: 0, y: 0}
  1058. */
  1059. SvgPanZoom.prototype.panBy = function(point) {
  1060. var viewportCTM = this.viewport.getCTM();
  1061. viewportCTM.e += point.x;
  1062. viewportCTM.f += point.y;
  1063. this.viewport.setCTM(viewportCTM);
  1064. };
  1065. /**
  1066. * Get pan vector
  1067. *
  1068. * @return {Object} {x: 0, y: 0}
  1069. */
  1070. SvgPanZoom.prototype.getPan = function() {
  1071. var state = this.viewport.getState();
  1072. return { x: state.x, y: state.y };
  1073. };
  1074. /**
  1075. * Recalculates cached svg dimensions and controls position
  1076. */
  1077. SvgPanZoom.prototype.resize = function() {
  1078. // Get dimensions
  1079. var boundingClientRectNormalized = SvgUtils.getBoundingClientRectNormalized(
  1080. this.svg
  1081. );
  1082. this.width = boundingClientRectNormalized.width;
  1083. this.height = boundingClientRectNormalized.height;
  1084. // Recalculate original state
  1085. var viewport = this.viewport;
  1086. viewport.options.width = this.width;
  1087. viewport.options.height = this.height;
  1088. viewport.processCTM();
  1089. // Reposition control icons by re-enabling them
  1090. if (this.options.controlIconsEnabled) {
  1091. this.getPublicInstance().disableControlIcons();
  1092. this.getPublicInstance().enableControlIcons();
  1093. }
  1094. };
  1095. /**
  1096. * Unbind mouse events, free callbacks and destroy public instance
  1097. */
  1098. SvgPanZoom.prototype.destroy = function() {
  1099. var that = this;
  1100. // Free callbacks
  1101. this.beforeZoom = null;
  1102. this.onZoom = null;
  1103. this.beforePan = null;
  1104. this.onPan = null;
  1105. this.onUpdatedCTM = null;
  1106. // Destroy custom event handlers
  1107. // eslint-disable-next-line eqeqeq
  1108. if (this.options.customEventsHandler != null) {
  1109. this.options.customEventsHandler.destroy({
  1110. svgElement: this.svg,
  1111. eventsListenerElement: this.options.eventsListenerElement,
  1112. instance: this.getPublicInstance()
  1113. });
  1114. }
  1115. // Unbind eventListeners
  1116. for (var event in this.eventListeners) {
  1117. (this.options.eventsListenerElement || this.svg).removeEventListener(
  1118. event,
  1119. this.eventListeners[event],
  1120. !this.options.preventMouseEventsDefault ? passiveListenerOption : false
  1121. );
  1122. }
  1123. // Unbind wheelListener
  1124. this.disableMouseWheelZoom();
  1125. // Remove control icons
  1126. this.getPublicInstance().disableControlIcons();
  1127. // Reset zoom and pan
  1128. this.reset();
  1129. // Remove instance from instancesStore
  1130. instancesStore = instancesStore.filter(function(instance) {
  1131. return instance.svg !== that.svg;
  1132. });
  1133. // Delete options and its contents
  1134. delete this.options;
  1135. // Delete viewport to make public shadow viewport functions uncallable
  1136. delete this.viewport;
  1137. // Destroy public instance and rewrite getPublicInstance
  1138. delete this.publicInstance;
  1139. delete this.pi;
  1140. this.getPublicInstance = function() {
  1141. return null;
  1142. };
  1143. };
  1144. /**
  1145. * Returns a public instance object
  1146. *
  1147. * @return {Object} Public instance object
  1148. */
  1149. SvgPanZoom.prototype.getPublicInstance = function() {
  1150. var that = this;
  1151. // Create cache
  1152. if (!this.publicInstance) {
  1153. this.publicInstance = this.pi = {
  1154. // Pan
  1155. enablePan: function() {
  1156. that.options.panEnabled = true;
  1157. return that.pi;
  1158. },
  1159. disablePan: function() {
  1160. that.options.panEnabled = false;
  1161. return that.pi;
  1162. },
  1163. isPanEnabled: function() {
  1164. return !!that.options.panEnabled;
  1165. },
  1166. pan: function(point) {
  1167. that.pan(point);
  1168. return that.pi;
  1169. },
  1170. panBy: function(point) {
  1171. that.panBy(point);
  1172. return that.pi;
  1173. },
  1174. getPan: function() {
  1175. return that.getPan();
  1176. },
  1177. // Pan event
  1178. setBeforePan: function(fn) {
  1179. that.options.beforePan =
  1180. fn === null ? null : Utils.proxy(fn, that.publicInstance);
  1181. return that.pi;
  1182. },
  1183. setOnPan: function(fn) {
  1184. that.options.onPan =
  1185. fn === null ? null : Utils.proxy(fn, that.publicInstance);
  1186. return that.pi;
  1187. },
  1188. // Zoom and Control Icons
  1189. enableZoom: function() {
  1190. that.options.zoomEnabled = true;
  1191. return that.pi;
  1192. },
  1193. disableZoom: function() {
  1194. that.options.zoomEnabled = false;
  1195. return that.pi;
  1196. },
  1197. isZoomEnabled: function() {
  1198. return !!that.options.zoomEnabled;
  1199. },
  1200. enableControlIcons: function() {
  1201. if (!that.options.controlIconsEnabled) {
  1202. that.options.controlIconsEnabled = true;
  1203. ControlIcons.enable(that);
  1204. }
  1205. return that.pi;
  1206. },
  1207. disableControlIcons: function() {
  1208. if (that.options.controlIconsEnabled) {
  1209. that.options.controlIconsEnabled = false;
  1210. ControlIcons.disable(that);
  1211. }
  1212. return that.pi;
  1213. },
  1214. isControlIconsEnabled: function() {
  1215. return !!that.options.controlIconsEnabled;
  1216. },
  1217. // Double click zoom
  1218. enableDblClickZoom: function() {
  1219. that.options.dblClickZoomEnabled = true;
  1220. return that.pi;
  1221. },
  1222. disableDblClickZoom: function() {
  1223. that.options.dblClickZoomEnabled = false;
  1224. return that.pi;
  1225. },
  1226. isDblClickZoomEnabled: function() {
  1227. return !!that.options.dblClickZoomEnabled;
  1228. },
  1229. // Mouse wheel zoom
  1230. enableMouseWheelZoom: function() {
  1231. that.enableMouseWheelZoom();
  1232. return that.pi;
  1233. },
  1234. disableMouseWheelZoom: function() {
  1235. that.disableMouseWheelZoom();
  1236. return that.pi;
  1237. },
  1238. isMouseWheelZoomEnabled: function() {
  1239. return !!that.options.mouseWheelZoomEnabled;
  1240. },
  1241. // Zoom scale and bounds
  1242. setZoomScaleSensitivity: function(scale) {
  1243. that.options.zoomScaleSensitivity = scale;
  1244. return that.pi;
  1245. },
  1246. setMinZoom: function(zoom) {
  1247. that.options.minZoom = zoom;
  1248. return that.pi;
  1249. },
  1250. setMaxZoom: function(zoom) {
  1251. that.options.maxZoom = zoom;
  1252. return that.pi;
  1253. },
  1254. // Zoom event
  1255. setBeforeZoom: function(fn) {
  1256. that.options.beforeZoom =
  1257. fn === null ? null : Utils.proxy(fn, that.publicInstance);
  1258. return that.pi;
  1259. },
  1260. setOnZoom: function(fn) {
  1261. that.options.onZoom =
  1262. fn === null ? null : Utils.proxy(fn, that.publicInstance);
  1263. return that.pi;
  1264. },
  1265. // Zooming
  1266. zoom: function(scale) {
  1267. that.publicZoom(scale, true);
  1268. return that.pi;
  1269. },
  1270. zoomBy: function(scale) {
  1271. that.publicZoom(scale, false);
  1272. return that.pi;
  1273. },
  1274. zoomAtPoint: function(scale, point) {
  1275. that.publicZoomAtPoint(scale, point, true);
  1276. return that.pi;
  1277. },
  1278. zoomAtPointBy: function(scale, point) {
  1279. that.publicZoomAtPoint(scale, point, false);
  1280. return that.pi;
  1281. },
  1282. zoomIn: function() {
  1283. this.zoomBy(1 + that.options.zoomScaleSensitivity);
  1284. return that.pi;
  1285. },
  1286. zoomOut: function() {
  1287. this.zoomBy(1 / (1 + that.options.zoomScaleSensitivity));
  1288. return that.pi;
  1289. },
  1290. getZoom: function() {
  1291. return that.getRelativeZoom();
  1292. },
  1293. // CTM update
  1294. setOnUpdatedCTM: function(fn) {
  1295. that.options.onUpdatedCTM =
  1296. fn === null ? null : Utils.proxy(fn, that.publicInstance);
  1297. return that.pi;
  1298. },
  1299. // Reset
  1300. resetZoom: function() {
  1301. that.resetZoom();
  1302. return that.pi;
  1303. },
  1304. resetPan: function() {
  1305. that.resetPan();
  1306. return that.pi;
  1307. },
  1308. reset: function() {
  1309. that.reset();
  1310. return that.pi;
  1311. },
  1312. // Fit, Contain and Center
  1313. fit: function() {
  1314. that.fit();
  1315. return that.pi;
  1316. },
  1317. contain: function() {
  1318. that.contain();
  1319. return that.pi;
  1320. },
  1321. center: function() {
  1322. that.center();
  1323. return that.pi;
  1324. },
  1325. // Size and Resize
  1326. updateBBox: function() {
  1327. that.updateBBox();
  1328. return that.pi;
  1329. },
  1330. resize: function() {
  1331. that.resize();
  1332. return that.pi;
  1333. },
  1334. getSizes: function() {
  1335. return {
  1336. width: that.width,
  1337. height: that.height,
  1338. realZoom: that.getZoom(),
  1339. viewBox: that.viewport.getViewBox()
  1340. };
  1341. },
  1342. // Destroy
  1343. destroy: function() {
  1344. that.destroy();
  1345. return that.pi;
  1346. }
  1347. };
  1348. }
  1349. return this.publicInstance;
  1350. };
  1351. /**
  1352. * Stores pairs of instances of SvgPanZoom and SVG
  1353. * Each pair is represented by an object {svg: SVGSVGElement, instance: SvgPanZoom}
  1354. *
  1355. * @type {Array}
  1356. */
  1357. var instancesStore = [];
  1358. var svgPanZoom = function(elementOrSelector, options) {
  1359. var svg = Utils.getSvg(elementOrSelector);
  1360. if (svg === null) {
  1361. return null;
  1362. } else {
  1363. // Look for existent instance
  1364. for (var i = instancesStore.length - 1; i >= 0; i--) {
  1365. if (instancesStore[i].svg === svg) {
  1366. return instancesStore[i].instance.getPublicInstance();
  1367. }
  1368. }
  1369. // If instance not found - create one
  1370. instancesStore.push({
  1371. svg: svg,
  1372. instance: new SvgPanZoom(svg, options)
  1373. });
  1374. // Return just pushed instance
  1375. return instancesStore[
  1376. instancesStore.length - 1
  1377. ].instance.getPublicInstance();
  1378. }
  1379. };
  1380. module.exports = svgPanZoom;
  1381. },{"./control-icons":1,"./shadow-viewport":2,"./svg-utilities":5,"./uniwheel":6,"./utilities":7}],5:[function(require,module,exports){
  1382. var Utils = require("./utilities"),
  1383. _browser = "unknown";
  1384. // http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
  1385. if (/*@cc_on!@*/ false || !!document.documentMode) {
  1386. // internet explorer
  1387. _browser = "ie";
  1388. }
  1389. module.exports = {
  1390. svgNS: "http://www.w3.org/2000/svg",
  1391. xmlNS: "http://www.w3.org/XML/1998/namespace",
  1392. xmlnsNS: "http://www.w3.org/2000/xmlns/",
  1393. xlinkNS: "http://www.w3.org/1999/xlink",
  1394. evNS: "http://www.w3.org/2001/xml-events",
  1395. /**
  1396. * Get svg dimensions: width and height
  1397. *
  1398. * @param {SVGSVGElement} svg
  1399. * @return {Object} {width: 0, height: 0}
  1400. */
  1401. getBoundingClientRectNormalized: function(svg) {
  1402. if (svg.clientWidth && svg.clientHeight) {
  1403. return { width: svg.clientWidth, height: svg.clientHeight };
  1404. } else if (!!svg.getBoundingClientRect()) {
  1405. return svg.getBoundingClientRect();
  1406. } else {
  1407. throw new Error("Cannot get BoundingClientRect for SVG.");
  1408. }
  1409. },
  1410. /**
  1411. * Gets g element with class of "viewport" or creates it if it doesn't exist
  1412. *
  1413. * @param {SVGSVGElement} svg
  1414. * @return {SVGElement} g (group) element
  1415. */
  1416. getOrCreateViewport: function(svg, selector) {
  1417. var viewport = null;
  1418. if (Utils.isElement(selector)) {
  1419. viewport = selector;
  1420. } else {
  1421. viewport = svg.querySelector(selector);
  1422. }
  1423. // Check if there is just one main group in SVG
  1424. if (!viewport) {
  1425. var childNodes = Array.prototype.slice
  1426. .call(svg.childNodes || svg.children)
  1427. .filter(function(el) {
  1428. return el.nodeName !== "defs" && el.nodeName !== "#text";
  1429. });
  1430. // Node name should be SVGGElement and should have no transform attribute
  1431. // Groups with transform are not used as viewport because it involves parsing of all transform possibilities
  1432. if (
  1433. childNodes.length === 1 &&
  1434. childNodes[0].nodeName === "g" &&
  1435. childNodes[0].getAttribute("transform") === null
  1436. ) {
  1437. viewport = childNodes[0];
  1438. }
  1439. }
  1440. // If no favorable group element exists then create one
  1441. if (!viewport) {
  1442. var viewportId =
  1443. "viewport-" + new Date().toISOString().replace(/\D/g, "");
  1444. viewport = document.createElementNS(this.svgNS, "g");
  1445. viewport.setAttribute("id", viewportId);
  1446. // Internet Explorer (all versions?) can't use childNodes, but other browsers prefer (require?) using childNodes
  1447. var svgChildren = svg.childNodes || svg.children;
  1448. if (!!svgChildren && svgChildren.length > 0) {
  1449. for (var i = svgChildren.length; i > 0; i--) {
  1450. // Move everything into viewport except defs
  1451. if (svgChildren[svgChildren.length - i].nodeName !== "defs") {
  1452. viewport.appendChild(svgChildren[svgChildren.length - i]);
  1453. }
  1454. }
  1455. }
  1456. svg.appendChild(viewport);
  1457. }
  1458. // Parse class names
  1459. var classNames = [];
  1460. if (viewport.getAttribute("class")) {
  1461. classNames = viewport.getAttribute("class").split(" ");
  1462. }
  1463. // Set class (if not set already)
  1464. if (!~classNames.indexOf("svg-pan-zoom_viewport")) {
  1465. classNames.push("svg-pan-zoom_viewport");
  1466. viewport.setAttribute("class", classNames.join(" "));
  1467. }
  1468. return viewport;
  1469. },
  1470. /**
  1471. * Set SVG attributes
  1472. *
  1473. * @param {SVGSVGElement} svg
  1474. */
  1475. setupSvgAttributes: function(svg) {
  1476. // Setting default attributes
  1477. svg.setAttribute("xmlns", this.svgNS);
  1478. svg.setAttributeNS(this.xmlnsNS, "xmlns:xlink", this.xlinkNS);
  1479. svg.setAttributeNS(this.xmlnsNS, "xmlns:ev", this.evNS);
  1480. // Needed for Internet Explorer, otherwise the viewport overflows
  1481. if (svg.parentNode !== null) {
  1482. var style = svg.getAttribute("style") || "";
  1483. if (style.toLowerCase().indexOf("overflow") === -1) {
  1484. svg.setAttribute("style", "overflow: hidden; " + style);
  1485. }
  1486. }
  1487. },
  1488. /**
  1489. * How long Internet Explorer takes to finish updating its display (ms).
  1490. */
  1491. internetExplorerRedisplayInterval: 300,
  1492. /**
  1493. * Forces the browser to redisplay all SVG elements that rely on an
  1494. * element defined in a 'defs' section. It works globally, for every
  1495. * available defs element on the page.
  1496. * The throttling is intentionally global.
  1497. *
  1498. * This is only needed for IE. It is as a hack to make markers (and 'use' elements?)
  1499. * visible after pan/zoom when there are multiple SVGs on the page.
  1500. * See bug report: https://connect.microsoft.com/IE/feedback/details/781964/
  1501. * also see svg-pan-zoom issue: https://github.com/bumbu/svg-pan-zoom/issues/62
  1502. */
  1503. refreshDefsGlobal: Utils.throttle(
  1504. function() {
  1505. var allDefs = document.querySelectorAll("defs");
  1506. var allDefsCount = allDefs.length;
  1507. for (var i = 0; i < allDefsCount; i++) {
  1508. var thisDefs = allDefs[i];
  1509. thisDefs.parentNode.insertBefore(thisDefs, thisDefs);
  1510. }
  1511. },
  1512. this ? this.internetExplorerRedisplayInterval : null
  1513. ),
  1514. /**
  1515. * Sets the current transform matrix of an element
  1516. *
  1517. * @param {SVGElement} element
  1518. * @param {SVGMatrix} matrix CTM
  1519. * @param {SVGElement} defs
  1520. */
  1521. setCTM: function(element, matrix, defs) {
  1522. var that = this,
  1523. s =
  1524. "matrix(" +
  1525. matrix.a +
  1526. "," +
  1527. matrix.b +
  1528. "," +
  1529. matrix.c +
  1530. "," +
  1531. matrix.d +
  1532. "," +
  1533. matrix.e +
  1534. "," +
  1535. matrix.f +
  1536. ")";
  1537. element.setAttributeNS(null, "transform", s);
  1538. if ("transform" in element.style) {
  1539. element.style.transform = s;
  1540. } else if ("-ms-transform" in element.style) {
  1541. element.style["-ms-transform"] = s;
  1542. } else if ("-webkit-transform" in element.style) {
  1543. element.style["-webkit-transform"] = s;
  1544. }
  1545. // IE has a bug that makes markers disappear on zoom (when the matrix "a" and/or "d" elements change)
  1546. // see http://stackoverflow.com/questions/17654578/svg-marker-does-not-work-in-ie9-10
  1547. // and http://srndolha.wordpress.com/2013/11/25/svg-line-markers-may-disappear-in-internet-explorer-11/
  1548. if (_browser === "ie" && !!defs) {
  1549. // this refresh is intended for redisplaying the SVG during zooming
  1550. defs.parentNode.insertBefore(defs, defs);
  1551. // this refresh is intended for redisplaying the other SVGs on a page when panning a given SVG
  1552. // it is also needed for the given SVG itself, on zoomEnd, if the SVG contains any markers that
  1553. // are located under any other element(s).
  1554. window.setTimeout(function() {
  1555. that.refreshDefsGlobal();
  1556. }, that.internetExplorerRedisplayInterval);
  1557. }
  1558. },
  1559. /**
  1560. * Instantiate an SVGPoint object with given event coordinates
  1561. *
  1562. * @param {Event} evt
  1563. * @param {SVGSVGElement} svg
  1564. * @return {SVGPoint} point
  1565. */
  1566. getEventPoint: function(evt, svg) {
  1567. var point = svg.createSVGPoint();
  1568. Utils.mouseAndTouchNormalize(evt, svg);
  1569. point.x = evt.clientX;
  1570. point.y = evt.clientY;
  1571. return point;
  1572. },
  1573. /**
  1574. * Get SVG center point
  1575. *
  1576. * @param {SVGSVGElement} svg
  1577. * @return {SVGPoint}
  1578. */
  1579. getSvgCenterPoint: function(svg, width, height) {
  1580. return this.createSVGPoint(svg, width / 2, height / 2);
  1581. },
  1582. /**
  1583. * Create a SVGPoint with given x and y
  1584. *
  1585. * @param {SVGSVGElement} svg
  1586. * @param {Number} x
  1587. * @param {Number} y
  1588. * @return {SVGPoint}
  1589. */
  1590. createSVGPoint: function(svg, x, y) {
  1591. var point = svg.createSVGPoint();
  1592. point.x = x;
  1593. point.y = y;
  1594. return point;
  1595. }
  1596. };
  1597. },{"./utilities":7}],6:[function(require,module,exports){
  1598. // uniwheel 0.1.2 (customized)
  1599. // A unified cross browser mouse wheel event handler
  1600. // https://github.com/teemualap/uniwheel
  1601. module.exports = (function(){
  1602. //Full details: https://developer.mozilla.org/en-US/docs/Web/Reference/Events/wheel
  1603. var prefix = "", _addEventListener, _removeEventListener, support, fns = [];
  1604. var passiveListenerOption = {passive: true};
  1605. var activeListenerOption = {passive: false};
  1606. // detect event model
  1607. if ( window.addEventListener ) {
  1608. _addEventListener = "addEventListener";
  1609. _removeEventListener = "removeEventListener";
  1610. } else {
  1611. _addEventListener = "attachEvent";
  1612. _removeEventListener = "detachEvent";
  1613. prefix = "on";
  1614. }
  1615. // detect available wheel event
  1616. support = "onwheel" in document.createElement("div") ? "wheel" : // Modern browsers support "wheel"
  1617. document.onmousewheel !== undefined ? "mousewheel" : // Webkit and IE support at least "mousewheel"
  1618. "DOMMouseScroll"; // let's assume that remaining browsers are older Firefox
  1619. function createCallback(element,callback) {
  1620. var fn = function(originalEvent) {
  1621. !originalEvent && ( originalEvent = window.event );
  1622. // create a normalized event object
  1623. var event = {
  1624. // keep a ref to the original event object
  1625. originalEvent: originalEvent,
  1626. target: originalEvent.target || originalEvent.srcElement,
  1627. type: "wheel",
  1628. deltaMode: originalEvent.type == "MozMousePixelScroll" ? 0 : 1,
  1629. deltaX: 0,
  1630. delatZ: 0,
  1631. preventDefault: function() {
  1632. originalEvent.preventDefault ?
  1633. originalEvent.preventDefault() :
  1634. originalEvent.returnValue = false;
  1635. }
  1636. };
  1637. // calculate deltaY (and deltaX) according to the event
  1638. if ( support == "mousewheel" ) {
  1639. event.deltaY = - 1/40 * originalEvent.wheelDelta;
  1640. // Webkit also support wheelDeltaX
  1641. originalEvent.wheelDeltaX && ( event.deltaX = - 1/40 * originalEvent.wheelDeltaX );
  1642. } else {
  1643. event.deltaY = originalEvent.detail;
  1644. }
  1645. // it's time to fire the callback
  1646. return callback( event );
  1647. };
  1648. fns.push({
  1649. element: element,
  1650. fn: fn,
  1651. });
  1652. return fn;
  1653. }
  1654. function getCallback(element) {
  1655. for (var i = 0; i < fns.length; i++) {
  1656. if (fns[i].element === element) {
  1657. return fns[i].fn;
  1658. }
  1659. }
  1660. return function(){};
  1661. }
  1662. function removeCallback(element) {
  1663. for (var i = 0; i < fns.length; i++) {
  1664. if (fns[i].element === element) {
  1665. return fns.splice(i,1);
  1666. }
  1667. }
  1668. }
  1669. function _addWheelListener(elem, eventName, callback, isPassiveListener ) {
  1670. var cb;
  1671. if (support === "wheel") {
  1672. cb = callback;
  1673. } else {
  1674. cb = createCallback(elem, callback);
  1675. }
  1676. elem[_addEventListener](
  1677. prefix + eventName,
  1678. cb,
  1679. isPassiveListener ? passiveListenerOption : activeListenerOption
  1680. );
  1681. }
  1682. function _removeWheelListener(elem, eventName, callback, isPassiveListener ) {
  1683. var cb;
  1684. if (support === "wheel") {
  1685. cb = callback;
  1686. } else {
  1687. cb = getCallback(elem);
  1688. }
  1689. elem[_removeEventListener](
  1690. prefix + eventName,
  1691. cb,
  1692. isPassiveListener ? passiveListenerOption : activeListenerOption
  1693. );
  1694. removeCallback(elem);
  1695. }
  1696. function addWheelListener( elem, callback, isPassiveListener ) {
  1697. _addWheelListener(elem, support, callback, isPassiveListener );
  1698. // handle MozMousePixelScroll in older Firefox
  1699. if( support == "DOMMouseScroll" ) {
  1700. _addWheelListener(elem, "MozMousePixelScroll", callback, isPassiveListener );
  1701. }
  1702. }
  1703. function removeWheelListener(elem, callback, isPassiveListener){
  1704. _removeWheelListener(elem, support, callback, isPassiveListener);
  1705. // handle MozMousePixelScroll in older Firefox
  1706. if( support == "DOMMouseScroll" ) {
  1707. _removeWheelListener(elem, "MozMousePixelScroll", callback, isPassiveListener);
  1708. }
  1709. }
  1710. return {
  1711. on: addWheelListener,
  1712. off: removeWheelListener
  1713. };
  1714. })();
  1715. },{}],7:[function(require,module,exports){
  1716. module.exports = {
  1717. /**
  1718. * Extends an object
  1719. *
  1720. * @param {Object} target object to extend
  1721. * @param {Object} source object to take properties from
  1722. * @return {Object} extended object
  1723. */
  1724. extend: function(target, source) {
  1725. target = target || {};
  1726. for (var prop in source) {
  1727. // Go recursively
  1728. if (this.isObject(source[prop])) {
  1729. target[prop] = this.extend(target[prop], source[prop]);
  1730. } else {
  1731. target[prop] = source[prop];
  1732. }
  1733. }
  1734. return target;
  1735. },
  1736. /**
  1737. * Checks if an object is a DOM element
  1738. *
  1739. * @param {Object} o HTML element or String
  1740. * @return {Boolean} returns true if object is a DOM element
  1741. */
  1742. isElement: function(o) {
  1743. return (
  1744. o instanceof HTMLElement ||
  1745. o instanceof SVGElement ||
  1746. o instanceof SVGSVGElement || //DOM2
  1747. (o &&
  1748. typeof o === "object" &&
  1749. o !== null &&
  1750. o.nodeType === 1 &&
  1751. typeof o.nodeName === "string")
  1752. );
  1753. },
  1754. /**
  1755. * Checks if an object is an Object
  1756. *
  1757. * @param {Object} o Object
  1758. * @return {Boolean} returns true if object is an Object
  1759. */
  1760. isObject: function(o) {
  1761. return Object.prototype.toString.call(o) === "[object Object]";
  1762. },
  1763. /**
  1764. * Checks if variable is Number
  1765. *
  1766. * @param {Integer|Float} n
  1767. * @return {Boolean} returns true if variable is Number
  1768. */
  1769. isNumber: function(n) {
  1770. return !isNaN(parseFloat(n)) && isFinite(n);
  1771. },
  1772. /**
  1773. * Search for an SVG element
  1774. *
  1775. * @param {Object|String} elementOrSelector DOM Element or selector String
  1776. * @return {Object|Null} SVG or null
  1777. */
  1778. getSvg: function(elementOrSelector) {
  1779. var element, svg;
  1780. if (!this.isElement(elementOrSelector)) {
  1781. // If selector provided
  1782. if (
  1783. typeof elementOrSelector === "string" ||
  1784. elementOrSelector instanceof String
  1785. ) {
  1786. // Try to find the element
  1787. element = document.querySelector(elementOrSelector);
  1788. if (!element) {
  1789. throw new Error(
  1790. "Provided selector did not find any elements. Selector: " +
  1791. elementOrSelector
  1792. );
  1793. return null;
  1794. }
  1795. } else {
  1796. throw new Error("Provided selector is not an HTML object nor String");
  1797. return null;
  1798. }
  1799. } else {
  1800. element = elementOrSelector;
  1801. }
  1802. if (element.tagName.toLowerCase() === "svg") {
  1803. svg = element;
  1804. } else {
  1805. if (element.tagName.toLowerCase() === "object") {
  1806. svg = element.contentDocument.documentElement;
  1807. } else {
  1808. if (element.tagName.toLowerCase() === "embed") {
  1809. svg = element.getSVGDocument().documentElement;
  1810. } else {
  1811. if (element.tagName.toLowerCase() === "img") {
  1812. throw new Error(
  1813. 'Cannot script an SVG in an "img" element. Please use an "object" element or an in-line SVG.'
  1814. );
  1815. } else {
  1816. throw new Error("Cannot get SVG.");
  1817. }
  1818. return null;
  1819. }
  1820. }
  1821. }
  1822. return svg;
  1823. },
  1824. /**
  1825. * Attach a given context to a function
  1826. * @param {Function} fn Function
  1827. * @param {Object} context Context
  1828. * @return {Function} Function with certain context
  1829. */
  1830. proxy: function(fn, context) {
  1831. return function() {
  1832. return fn.apply(context, arguments);
  1833. };
  1834. },
  1835. /**
  1836. * Returns object type
  1837. * Uses toString that returns [object SVGPoint]
  1838. * And than parses object type from string
  1839. *
  1840. * @param {Object} o Any object
  1841. * @return {String} Object type
  1842. */
  1843. getType: function(o) {
  1844. return Object.prototype.toString
  1845. .apply(o)
  1846. .replace(/^\[object\s/, "")
  1847. .replace(/\]$/, "");
  1848. },
  1849. /**
  1850. * If it is a touch event than add clientX and clientY to event object
  1851. *
  1852. * @param {Event} evt
  1853. * @param {SVGSVGElement} svg
  1854. */
  1855. mouseAndTouchNormalize: function(evt, svg) {
  1856. // If no clientX then fallback
  1857. if (evt.clientX === void 0 || evt.clientX === null) {
  1858. // Fallback
  1859. evt.clientX = 0;
  1860. evt.clientY = 0;
  1861. // If it is a touch event
  1862. if (evt.touches !== void 0 && evt.touches.length) {
  1863. if (evt.touches[0].clientX !== void 0) {
  1864. evt.clientX = evt.touches[0].clientX;
  1865. evt.clientY = evt.touches[0].clientY;
  1866. } else if (evt.touches[0].pageX !== void 0) {
  1867. var rect = svg.getBoundingClientRect();
  1868. evt.clientX = evt.touches[0].pageX - rect.left;
  1869. evt.clientY = evt.touches[0].pageY - rect.top;
  1870. }
  1871. // If it is a custom event
  1872. } else if (evt.originalEvent !== void 0) {
  1873. if (evt.originalEvent.clientX !== void 0) {
  1874. evt.clientX = evt.originalEvent.clientX;
  1875. evt.clientY = evt.originalEvent.clientY;
  1876. }
  1877. }
  1878. }
  1879. },
  1880. /**
  1881. * Check if an event is a double click/tap
  1882. * TODO: For touch gestures use a library (hammer.js) that takes in account other events
  1883. * (touchmove and touchend). It should take in account tap duration and traveled distance
  1884. *
  1885. * @param {Event} evt
  1886. * @param {Event} prevEvt Previous Event
  1887. * @return {Boolean}
  1888. */
  1889. isDblClick: function(evt, prevEvt) {
  1890. // Double click detected by browser
  1891. if (evt.detail === 2) {
  1892. return true;
  1893. }
  1894. // Try to compare events
  1895. else if (prevEvt !== void 0 && prevEvt !== null) {
  1896. var timeStampDiff = evt.timeStamp - prevEvt.timeStamp, // should be lower than 250 ms
  1897. touchesDistance = Math.sqrt(
  1898. Math.pow(evt.clientX - prevEvt.clientX, 2) +
  1899. Math.pow(evt.clientY - prevEvt.clientY, 2)
  1900. );
  1901. return timeStampDiff < 250 && touchesDistance < 10;
  1902. }
  1903. // Nothing found
  1904. return false;
  1905. },
  1906. /**
  1907. * Returns current timestamp as an integer
  1908. *
  1909. * @return {Number}
  1910. */
  1911. now:
  1912. Date.now ||
  1913. function() {
  1914. return new Date().getTime();
  1915. },
  1916. // From underscore.
  1917. // Returns a function, that, when invoked, will only be triggered at most once
  1918. // during a given window of time. Normally, the throttled function will run
  1919. // as much as it can, without ever going more than once per `wait` duration;
  1920. // but if you'd like to disable the execution on the leading edge, pass
  1921. // `{leading: false}`. To disable execution on the trailing edge, ditto.
  1922. throttle: function(func, wait, options) {
  1923. var that = this;
  1924. var context, args, result;
  1925. var timeout = null;
  1926. var previous = 0;
  1927. if (!options) {
  1928. options = {};
  1929. }
  1930. var later = function() {
  1931. previous = options.leading === false ? 0 : that.now();
  1932. timeout = null;
  1933. result = func.apply(context, args);
  1934. if (!timeout) {
  1935. context = args = null;
  1936. }
  1937. };
  1938. return function() {
  1939. var now = that.now();
  1940. if (!previous && options.leading === false) {
  1941. previous = now;
  1942. }
  1943. var remaining = wait - (now - previous);
  1944. context = this; // eslint-disable-line consistent-this
  1945. args = arguments;
  1946. if (remaining <= 0 || remaining > wait) {
  1947. clearTimeout(timeout);
  1948. timeout = null;
  1949. previous = now;
  1950. result = func.apply(context, args);
  1951. if (!timeout) {
  1952. context = args = null;
  1953. }
  1954. } else if (!timeout && options.trailing !== false) {
  1955. timeout = setTimeout(later, remaining);
  1956. }
  1957. return result;
  1958. };
  1959. },
  1960. /**
  1961. * Create a requestAnimationFrame simulation
  1962. *
  1963. * @param {Number|String} refreshRate
  1964. * @return {Function}
  1965. */
  1966. createRequestAnimationFrame: function(refreshRate) {
  1967. var timeout = null;
  1968. // Convert refreshRate to timeout
  1969. if (refreshRate !== "auto" && refreshRate < 60 && refreshRate > 1) {
  1970. timeout = Math.floor(1000 / refreshRate);
  1971. }
  1972. if (timeout === null) {
  1973. return window.requestAnimationFrame || requestTimeout(33);
  1974. } else {
  1975. return requestTimeout(timeout);
  1976. }
  1977. }
  1978. };
  1979. /**
  1980. * Create a callback that will execute after a given timeout
  1981. *
  1982. * @param {Function} timeout
  1983. * @return {Function}
  1984. */
  1985. function requestTimeout(timeout) {
  1986. return function(callback) {
  1987. window.setTimeout(callback, timeout);
  1988. };
  1989. }
  1990. },{}]},{},[3]);