You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1901 lines
61 KiB

  1. // svg-pan-zoom v3.5.1
  2. // https://github.com/ariutta/svg-pan-zoom
  3. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  4. var svgPanZoom = require('./svg-pan-zoom.js');
  5. // UMD module definition
  6. (function(window, document){
  7. // AMD
  8. if (typeof define === 'function' && define.amd) {
  9. define('svg-pan-zoom', function () {
  10. return svgPanZoom;
  11. });
  12. // CMD
  13. } else if (typeof module !== 'undefined' && module.exports) {
  14. module.exports = svgPanZoom;
  15. // Browser
  16. // Keep exporting globally as module.exports is available because of browserify
  17. window.svgPanZoom = svgPanZoom;
  18. }
  19. })(window, document)
  20. },{"./svg-pan-zoom.js":4}],2:[function(require,module,exports){
  21. var SvgUtils = require('./svg-utilities');
  22. module.exports = {
  23. enable: function(instance) {
  24. // Select (and create if necessary) defs
  25. var defs = instance.svg.querySelector('defs')
  26. if (!defs) {
  27. defs = document.createElementNS(SvgUtils.svgNS, 'defs')
  28. instance.svg.appendChild(defs)
  29. }
  30. // Check for style element, and create it if it doesn't exist
  31. var styleEl = defs.querySelector('style#svg-pan-zoom-controls-styles');
  32. if (!styleEl) {
  33. var style = document.createElementNS(SvgUtils.svgNS, 'style')
  34. style.setAttribute('id', 'svg-pan-zoom-controls-styles')
  35. style.setAttribute('type', 'text/css')
  36. style.textContent = '.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; }'
  37. defs.appendChild(style)
  38. }
  39. // Zoom Group
  40. var zoomGroup = document.createElementNS(SvgUtils.svgNS, 'g');
  41. zoomGroup.setAttribute('id', 'svg-pan-zoom-controls');
  42. zoomGroup.setAttribute('transform', 'translate(' + ( instance.width - 70 ) + ' ' + ( instance.height - 76 ) + ') scale(0.75)');
  43. zoomGroup.setAttribute('class', 'svg-pan-zoom-control');
  44. // Control elements
  45. zoomGroup.appendChild(this._createZoomIn(instance))
  46. zoomGroup.appendChild(this._createZoomReset(instance))
  47. zoomGroup.appendChild(this._createZoomOut(instance))
  48. // Finally append created element
  49. instance.svg.appendChild(zoomGroup)
  50. // Cache control instance
  51. instance.controlIcons = zoomGroup
  52. }
  53. , _createZoomIn: function(instance) {
  54. var zoomIn = document.createElementNS(SvgUtils.svgNS, 'g');
  55. zoomIn.setAttribute('id', 'svg-pan-zoom-zoom-in');
  56. zoomIn.setAttribute('transform', 'translate(30.5 5) scale(0.015)');
  57. zoomIn.setAttribute('class', 'svg-pan-zoom-control');
  58. zoomIn.addEventListener('click', function() {instance.getPublicInstance().zoomIn()}, false)
  59. zoomIn.addEventListener('touchstart', function() {instance.getPublicInstance().zoomIn()}, false)
  60. var zoomInBackground = document.createElementNS(SvgUtils.svgNS, 'rect'); // TODO change these background space fillers to rounded rectangles so they look prettier
  61. zoomInBackground.setAttribute('x', '0');
  62. zoomInBackground.setAttribute('y', '0');
  63. zoomInBackground.setAttribute('width', '1500'); // larger than expected because the whole group is transformed to scale down
  64. zoomInBackground.setAttribute('height', '1400');
  65. zoomInBackground.setAttribute('class', 'svg-pan-zoom-control-background');
  66. zoomIn.appendChild(zoomInBackground);
  67. var zoomInShape = document.createElementNS(SvgUtils.svgNS, 'path');
  68. zoomInShape.setAttribute('d', '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');
  69. zoomInShape.setAttribute('class', 'svg-pan-zoom-control-element');
  70. zoomIn.appendChild(zoomInShape);
  71. return zoomIn
  72. }
  73. , _createZoomReset: function(instance){
  74. // reset
  75. var resetPanZoomControl = document.createElementNS(SvgUtils.svgNS, 'g');
  76. resetPanZoomControl.setAttribute('id', 'svg-pan-zoom-reset-pan-zoom');
  77. resetPanZoomControl.setAttribute('transform', 'translate(5 35) scale(0.4)');
  78. resetPanZoomControl.setAttribute('class', 'svg-pan-zoom-control');
  79. resetPanZoomControl.addEventListener('click', function() {instance.getPublicInstance().reset()}, false);
  80. resetPanZoomControl.addEventListener('touchstart', function() {instance.getPublicInstance().reset()}, false);
  81. var resetPanZoomControlBackground = document.createElementNS(SvgUtils.svgNS, 'rect'); // TODO change these background space fillers to rounded rectangles so they look prettier
  82. resetPanZoomControlBackground.setAttribute('x', '2');
  83. resetPanZoomControlBackground.setAttribute('y', '2');
  84. resetPanZoomControlBackground.setAttribute('width', '182'); // larger than expected because the whole group is transformed to scale down
  85. resetPanZoomControlBackground.setAttribute('height', '58');
  86. resetPanZoomControlBackground.setAttribute('class', 'svg-pan-zoom-control-background');
  87. resetPanZoomControl.appendChild(resetPanZoomControlBackground);
  88. var resetPanZoomControlShape1 = document.createElementNS(SvgUtils.svgNS, 'path');
  89. resetPanZoomControlShape1.setAttribute('d', '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');
  90. resetPanZoomControlShape1.setAttribute('class', 'svg-pan-zoom-control-element');
  91. resetPanZoomControl.appendChild(resetPanZoomControlShape1);
  92. var resetPanZoomControlShape2 = document.createElementNS(SvgUtils.svgNS, 'path');
  93. resetPanZoomControlShape2.setAttribute('d', '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');
  94. resetPanZoomControlShape2.setAttribute('class', 'svg-pan-zoom-control-element');
  95. resetPanZoomControl.appendChild(resetPanZoomControlShape2);
  96. return resetPanZoomControl
  97. }
  98. , _createZoomOut: function(instance){
  99. // zoom out
  100. var zoomOut = document.createElementNS(SvgUtils.svgNS, 'g');
  101. zoomOut.setAttribute('id', 'svg-pan-zoom-zoom-out');
  102. zoomOut.setAttribute('transform', 'translate(30.5 70) scale(0.015)');
  103. zoomOut.setAttribute('class', 'svg-pan-zoom-control');
  104. zoomOut.addEventListener('click', function() {instance.getPublicInstance().zoomOut()}, false);
  105. zoomOut.addEventListener('touchstart', function() {instance.getPublicInstance().zoomOut()}, false);
  106. var zoomOutBackground = document.createElementNS(SvgUtils.svgNS, 'rect'); // TODO change these background space fillers to rounded rectangles so they look prettier
  107. zoomOutBackground.setAttribute('x', '0');
  108. zoomOutBackground.setAttribute('y', '0');
  109. zoomOutBackground.setAttribute('width', '1500'); // larger than expected because the whole group is transformed to scale down
  110. zoomOutBackground.setAttribute('height', '1400');
  111. zoomOutBackground.setAttribute('class', 'svg-pan-zoom-control-background');
  112. zoomOut.appendChild(zoomOutBackground);
  113. var zoomOutShape = document.createElementNS(SvgUtils.svgNS, 'path');
  114. zoomOutShape.setAttribute('d', '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');
  115. zoomOutShape.setAttribute('class', 'svg-pan-zoom-control-element');
  116. zoomOut.appendChild(zoomOutShape);
  117. return zoomOut
  118. }
  119. , disable: function(instance) {
  120. if (instance.controlIcons) {
  121. instance.controlIcons.parentNode.removeChild(instance.controlIcons)
  122. instance.controlIcons = null
  123. }
  124. }
  125. }
  126. },{"./svg-utilities":5}],3:[function(require,module,exports){
  127. var SvgUtils = require('./svg-utilities')
  128. , Utils = require('./utilities')
  129. ;
  130. var ShadowViewport = function(viewport, options){
  131. this.init(viewport, options)
  132. }
  133. /**
  134. * Initialization
  135. *
  136. * @param {SVGElement} viewport
  137. * @param {Object} options
  138. */
  139. ShadowViewport.prototype.init = function(viewport, options) {
  140. // DOM Elements
  141. this.viewport = viewport
  142. this.options = options
  143. // State cache
  144. this.originalState = {zoom: 1, x: 0, y: 0}
  145. this.activeState = {zoom: 1, x: 0, y: 0}
  146. this.updateCTMCached = Utils.proxy(this.updateCTM, this)
  147. // Create a custom requestAnimationFrame taking in account refreshRate
  148. this.requestAnimationFrame = Utils.createRequestAnimationFrame(this.options.refreshRate)
  149. // ViewBox
  150. this.viewBox = {x: 0, y: 0, width: 0, height: 0}
  151. this.cacheViewBox()
  152. // Process CTM
  153. var newCTM = this.processCTM()
  154. // Update viewport CTM and cache zoom and pan
  155. this.setCTM(newCTM)
  156. // Update CTM in this frame
  157. this.updateCTM()
  158. }
  159. /**
  160. * Cache initial viewBox value
  161. * If no viewBox is defined, then use viewport size/position instead for viewBox values
  162. */
  163. ShadowViewport.prototype.cacheViewBox = function() {
  164. var svgViewBox = this.options.svg.getAttribute('viewBox')
  165. if (svgViewBox) {
  166. var viewBoxValues = svgViewBox.split(/[\s\,]/).filter(function(v){return v}).map(parseFloat)
  167. // Cache viewbox x and y offset
  168. this.viewBox.x = viewBoxValues[0]
  169. this.viewBox.y = viewBoxValues[1]
  170. this.viewBox.width = viewBoxValues[2]
  171. this.viewBox.height = viewBoxValues[3]
  172. var zoom = Math.min(this.options.width / this.viewBox.width, this.options.height / this.viewBox.height)
  173. // Update active state
  174. this.activeState.zoom = zoom
  175. this.activeState.x = (this.options.width - this.viewBox.width * zoom) / 2
  176. this.activeState.y = (this.options.height - this.viewBox.height * zoom) / 2
  177. // Force updating CTM
  178. this.updateCTMOnNextFrame()
  179. this.options.svg.removeAttribute('viewBox')
  180. } else {
  181. this.simpleViewBoxCache()
  182. }
  183. }
  184. /**
  185. * Recalculate viewport sizes and update viewBox cache
  186. */
  187. ShadowViewport.prototype.simpleViewBoxCache = function() {
  188. var bBox = this.viewport.getBBox()
  189. this.viewBox.x = bBox.x
  190. this.viewBox.y = bBox.y
  191. this.viewBox.width = bBox.width
  192. this.viewBox.height = bBox.height
  193. }
  194. /**
  195. * Returns a viewbox object. Safe to alter
  196. *
  197. * @return {Object} viewbox object
  198. */
  199. ShadowViewport.prototype.getViewBox = function() {
  200. return Utils.extend({}, this.viewBox)
  201. }
  202. /**
  203. * Get initial zoom and pan values. Save them into originalState
  204. * Parses viewBox attribute to alter initial sizes
  205. *
  206. * @return {CTM} CTM object based on options
  207. */
  208. ShadowViewport.prototype.processCTM = function() {
  209. var newCTM = this.getCTM()
  210. if (this.options.fit || this.options.contain) {
  211. var newScale;
  212. if (this.options.fit) {
  213. newScale = Math.min(this.options.width/this.viewBox.width, this.options.height/this.viewBox.height);
  214. } else {
  215. newScale = Math.max(this.options.width/this.viewBox.width, this.options.height/this.viewBox.height);
  216. }
  217. newCTM.a = newScale; //x-scale
  218. newCTM.d = newScale; //y-scale
  219. newCTM.e = -this.viewBox.x * newScale; //x-transform
  220. newCTM.f = -this.viewBox.y * newScale; //y-transform
  221. }
  222. if (this.options.center) {
  223. var offsetX = (this.options.width - (this.viewBox.width + this.viewBox.x * 2) * newCTM.a) * 0.5
  224. , offsetY = (this.options.height - (this.viewBox.height + this.viewBox.y * 2) * newCTM.a) * 0.5
  225. newCTM.e = offsetX
  226. newCTM.f = offsetY
  227. }
  228. // Cache initial values. Based on activeState and fix+center opitons
  229. this.originalState.zoom = newCTM.a
  230. this.originalState.x = newCTM.e
  231. this.originalState.y = newCTM.f
  232. return newCTM
  233. }
  234. /**
  235. * Return originalState object. Safe to alter
  236. *
  237. * @return {Object}
  238. */
  239. ShadowViewport.prototype.getOriginalState = function() {
  240. return Utils.extend({}, this.originalState)
  241. }
  242. /**
  243. * Return actualState object. Safe to alter
  244. *
  245. * @return {Object}
  246. */
  247. ShadowViewport.prototype.getState = function() {
  248. return Utils.extend({}, this.activeState)
  249. }
  250. /**
  251. * Get zoom scale
  252. *
  253. * @return {Float} zoom scale
  254. */
  255. ShadowViewport.prototype.getZoom = function() {
  256. return this.activeState.zoom
  257. }
  258. /**
  259. * Get zoom scale for pubilc usage
  260. *
  261. * @return {Float} zoom scale
  262. */
  263. ShadowViewport.prototype.getRelativeZoom = function() {
  264. return this.activeState.zoom / this.originalState.zoom
  265. }
  266. /**
  267. * Compute zoom scale for pubilc usage
  268. *
  269. * @return {Float} zoom scale
  270. */
  271. ShadowViewport.prototype.computeRelativeZoom = function(scale) {
  272. return scale / this.originalState.zoom
  273. }
  274. /**
  275. * Get pan
  276. *
  277. * @return {Object}
  278. */
  279. ShadowViewport.prototype.getPan = function() {
  280. return {x: this.activeState.x, y: this.activeState.y}
  281. }
  282. /**
  283. * Return cached viewport CTM value that can be safely modified
  284. *
  285. * @return {SVGMatrix}
  286. */
  287. ShadowViewport.prototype.getCTM = function() {
  288. var safeCTM = this.options.svg.createSVGMatrix()
  289. // Copy values manually as in FF they are not itterable
  290. safeCTM.a = this.activeState.zoom
  291. safeCTM.b = 0
  292. safeCTM.c = 0
  293. safeCTM.d = this.activeState.zoom
  294. safeCTM.e = this.activeState.x
  295. safeCTM.f = this.activeState.y
  296. return safeCTM
  297. }
  298. /**
  299. * Set a new CTM
  300. *
  301. * @param {SVGMatrix} newCTM
  302. */
  303. ShadowViewport.prototype.setCTM = function(newCTM) {
  304. var willZoom = this.isZoomDifferent(newCTM)
  305. , willPan = this.isPanDifferent(newCTM)
  306. if (willZoom || willPan) {
  307. // Before zoom
  308. if (willZoom) {
  309. // If returns false then cancel zooming
  310. if (this.options.beforeZoom(this.getRelativeZoom(), this.computeRelativeZoom(newCTM.a)) === false) {
  311. newCTM.a = newCTM.d = this.activeState.zoom
  312. willZoom = false
  313. } else {
  314. this.updateCache(newCTM);
  315. this.options.onZoom(this.getRelativeZoom())
  316. }
  317. }
  318. // Before pan
  319. if (willPan) {
  320. var preventPan = this.options.beforePan(this.getPan(), {x: newCTM.e, y: newCTM.f})
  321. // If prevent pan is an object
  322. , preventPanX = false
  323. , preventPanY = false
  324. // If prevent pan is Boolean false
  325. if (preventPan === false) {
  326. // Set x and y same as before
  327. newCTM.e = this.getPan().x
  328. newCTM.f = this.getPan().y
  329. preventPanX = preventPanY = true
  330. } else if (Utils.isObject(preventPan)) {
  331. // Check for X axes attribute
  332. if (preventPan.x === false) {
  333. // Prevent panning on x axes
  334. newCTM.e = this.getPan().x
  335. preventPanX = true
  336. } else if (Utils.isNumber(preventPan.x)) {
  337. // Set a custom pan value
  338. newCTM.e = preventPan.x
  339. }
  340. // Check for Y axes attribute
  341. if (preventPan.y === false) {
  342. // Prevent panning on x axes
  343. newCTM.f = this.getPan().y
  344. preventPanY = true
  345. } else if (Utils.isNumber(preventPan.y)) {
  346. // Set a custom pan value
  347. newCTM.f = preventPan.y
  348. }
  349. }
  350. // Update willPan flag
  351. // Check if newCTM is still different
  352. if ((preventPanX && preventPanY) || !this.isPanDifferent(newCTM)) {
  353. willPan = false
  354. } else {
  355. this.updateCache(newCTM);
  356. this.options.onPan(this.getPan());
  357. }
  358. }
  359. // Check again if should zoom or pan
  360. if (willZoom || willPan) {
  361. this.updateCTMOnNextFrame()
  362. }
  363. }
  364. }
  365. ShadowViewport.prototype.isZoomDifferent = function(newCTM) {
  366. return this.activeState.zoom !== newCTM.a
  367. }
  368. ShadowViewport.prototype.isPanDifferent = function(newCTM) {
  369. return this.activeState.x !== newCTM.e || this.activeState.y !== newCTM.f
  370. }
  371. /**
  372. * Update cached CTM and active state
  373. *
  374. * @param {SVGMatrix} newCTM
  375. */
  376. ShadowViewport.prototype.updateCache = function(newCTM) {
  377. this.activeState.zoom = newCTM.a
  378. this.activeState.x = newCTM.e
  379. this.activeState.y = newCTM.f
  380. }
  381. ShadowViewport.prototype.pendingUpdate = false
  382. /**
  383. * Place a request to update CTM on next Frame
  384. */
  385. ShadowViewport.prototype.updateCTMOnNextFrame = function() {
  386. if (!this.pendingUpdate) {
  387. // Lock
  388. this.pendingUpdate = true
  389. // Throttle next update
  390. this.requestAnimationFrame.call(window, this.updateCTMCached)
  391. }
  392. }
  393. /**
  394. * Update viewport CTM with cached CTM
  395. */
  396. ShadowViewport.prototype.updateCTM = function() {
  397. var ctm = this.getCTM()
  398. // Updates SVG element
  399. SvgUtils.setCTM(this.viewport, ctm, this.defs)
  400. // Free the lock
  401. this.pendingUpdate = false
  402. // Notify about the update
  403. if(this.options.onUpdatedCTM) {
  404. this.options.onUpdatedCTM(ctm)
  405. }
  406. }
  407. module.exports = function(viewport, options){
  408. return new ShadowViewport(viewport, options)
  409. }
  410. },{"./svg-utilities":5,"./utilities":7}],4:[function(require,module,exports){
  411. var Wheel = require('./uniwheel')
  412. , ControlIcons = require('./control-icons')
  413. , Utils = require('./utilities')
  414. , SvgUtils = require('./svg-utilities')
  415. , ShadowViewport = require('./shadow-viewport')
  416. var SvgPanZoom = function(svg, options) {
  417. this.init(svg, options)
  418. }
  419. var optionsDefaults = {
  420. viewportSelector: '.svg-pan-zoom_viewport' // Viewport selector. Can be querySelector string or SVGElement
  421. , panEnabled: true // enable or disable panning (default enabled)
  422. , controlIconsEnabled: false // insert icons to give user an option in addition to mouse events to control pan/zoom (default disabled)
  423. , zoomEnabled: true // enable or disable zooming (default enabled)
  424. , dblClickZoomEnabled: true // enable or disable zooming by double clicking (default enabled)
  425. , mouseWheelZoomEnabled: true // enable or disable zooming by mouse wheel (default enabled)
  426. , preventMouseEventsDefault: true // enable or disable preventDefault for mouse events
  427. , zoomScaleSensitivity: 0.1 // Zoom sensitivity
  428. , minZoom: 0.5 // Minimum Zoom level
  429. , maxZoom: 10 // Maximum Zoom level
  430. , fit: true // enable or disable viewport fit in SVG (default true)
  431. , contain: false // enable or disable viewport contain the svg (default false)
  432. , center: true // enable or disable viewport centering in SVG (default true)
  433. , refreshRate: 'auto' // Maximum number of frames per second (altering SVG's viewport)
  434. , beforeZoom: null
  435. , onZoom: null
  436. , beforePan: null
  437. , onPan: null
  438. , customEventsHandler: null
  439. , eventsListenerElement: null
  440. , onUpdatedCTM: null
  441. }
  442. SvgPanZoom.prototype.init = function(svg, options) {
  443. var that = this
  444. this.svg = svg
  445. this.defs = svg.querySelector('defs')
  446. // Add default attributes to SVG
  447. SvgUtils.setupSvgAttributes(this.svg)
  448. // Set options
  449. this.options = Utils.extend(Utils.extend({}, optionsDefaults), options)
  450. // Set default state
  451. this.state = 'none'
  452. // Get dimensions
  453. var boundingClientRectNormalized = SvgUtils.getBoundingClientRectNormalized(svg)
  454. this.width = boundingClientRectNormalized.width
  455. this.height = boundingClientRectNormalized.height
  456. // Init shadow viewport
  457. this.viewport = ShadowViewport(SvgUtils.getOrCreateViewport(this.svg, this.options.viewportSelector), {
  458. svg: this.svg
  459. , width: this.width
  460. , height: this.height
  461. , fit: this.options.fit
  462. , contain: this.options.contain
  463. , center: this.options.center
  464. , refreshRate: this.options.refreshRate
  465. // Put callbacks into functions as they can change through time
  466. , beforeZoom: function(oldScale, newScale) {
  467. if (that.viewport && that.options.beforeZoom) {return that.options.beforeZoom(oldScale, newScale)}
  468. }
  469. , onZoom: function(scale) {
  470. if (that.viewport && that.options.onZoom) {return that.options.onZoom(scale)}
  471. }
  472. , beforePan: function(oldPoint, newPoint) {
  473. if (that.viewport && that.options.beforePan) {return that.options.beforePan(oldPoint, newPoint)}
  474. }
  475. , onPan: function(point) {
  476. if (that.viewport && that.options.onPan) {return that.options.onPan(point)}
  477. }
  478. , onUpdatedCTM: function(ctm) {
  479. if (that.viewport && that.options.onUpdatedCTM) {return that.options.onUpdatedCTM(ctm)}
  480. }
  481. })
  482. // Wrap callbacks into public API context
  483. var publicInstance = this.getPublicInstance()
  484. publicInstance.setBeforeZoom(this.options.beforeZoom)
  485. publicInstance.setOnZoom(this.options.onZoom)
  486. publicInstance.setBeforePan(this.options.beforePan)
  487. publicInstance.setOnPan(this.options.onPan)
  488. publicInstance.setOnUpdatedCTM(this.options.onUpdatedCTM)
  489. if (this.options.controlIconsEnabled) {
  490. ControlIcons.enable(this)
  491. }
  492. // Init events handlers
  493. this.lastMouseWheelEventTime = Date.now()
  494. this.setupHandlers()
  495. }
  496. /**
  497. * Register event handlers
  498. */
  499. SvgPanZoom.prototype.setupHandlers = function() {
  500. var that = this
  501. , prevEvt = null // use for touchstart event to detect double tap
  502. ;
  503. this.eventListeners = {
  504. // Mouse down group
  505. mousedown: function(evt) {
  506. var result = that.handleMouseDown(evt, prevEvt);
  507. prevEvt = evt
  508. return result;
  509. }
  510. , touchstart: function(evt) {
  511. var result = that.handleMouseDown(evt, prevEvt);
  512. prevEvt = evt
  513. return result;
  514. }
  515. // Mouse up group
  516. , mouseup: function(evt) {
  517. return that.handleMouseUp(evt);
  518. }
  519. , touchend: function(evt) {
  520. return that.handleMouseUp(evt);
  521. }
  522. // Mouse move group
  523. , mousemove: function(evt) {
  524. return that.handleMouseMove(evt);
  525. }
  526. , touchmove: function(evt) {
  527. return that.handleMouseMove(evt);
  528. }
  529. // Mouse leave group
  530. , mouseleave: function(evt) {
  531. return that.handleMouseUp(evt);
  532. }
  533. , touchleave: function(evt) {
  534. return that.handleMouseUp(evt);
  535. }
  536. , touchcancel: function(evt) {
  537. return that.handleMouseUp(evt);
  538. }
  539. }
  540. // Init custom events handler if available
  541. if (this.options.customEventsHandler != null) { // jshint ignore:line
  542. this.options.customEventsHandler.init({
  543. svgElement: this.svg
  544. , eventsListenerElement: this.options.eventsListenerElement
  545. , instance: this.getPublicInstance()
  546. })
  547. // Custom event handler may halt builtin listeners
  548. var haltEventListeners = this.options.customEventsHandler.haltEventListeners
  549. if (haltEventListeners && haltEventListeners.length) {
  550. for (var i = haltEventListeners.length - 1; i >= 0; i--) {
  551. if (this.eventListeners.hasOwnProperty(haltEventListeners[i])) {
  552. delete this.eventListeners[haltEventListeners[i]]
  553. }
  554. }
  555. }
  556. }
  557. // Bind eventListeners
  558. for (var event in this.eventListeners) {
  559. // Attach event to eventsListenerElement or SVG if not available
  560. (this.options.eventsListenerElement || this.svg)
  561. .addEventListener(event, this.eventListeners[event], false)
  562. }
  563. // Zoom using mouse wheel
  564. if (this.options.mouseWheelZoomEnabled) {
  565. this.options.mouseWheelZoomEnabled = false // set to false as enable will set it back to true
  566. this.enableMouseWheelZoom()
  567. }
  568. }
  569. /**
  570. * Enable ability to zoom using mouse wheel
  571. */
  572. SvgPanZoom.prototype.enableMouseWheelZoom = function() {
  573. if (!this.options.mouseWheelZoomEnabled) {
  574. var that = this
  575. // Mouse wheel listener
  576. this.wheelListener = function(evt) {
  577. return that.handleMouseWheel(evt);
  578. }
  579. // Bind wheelListener
  580. Wheel.on(this.options.eventsListenerElement || this.svg, this.wheelListener, false)
  581. this.options.mouseWheelZoomEnabled = true
  582. }
  583. }
  584. /**
  585. * Disable ability to zoom using mouse wheel
  586. */
  587. SvgPanZoom.prototype.disableMouseWheelZoom = function() {
  588. if (this.options.mouseWheelZoomEnabled) {
  589. Wheel.off(this.options.eventsListenerElement || this.svg, this.wheelListener, false)
  590. this.options.mouseWheelZoomEnabled = false
  591. }
  592. }
  593. /**
  594. * Handle mouse wheel event
  595. *
  596. * @param {Event} evt
  597. */
  598. SvgPanZoom.prototype.handleMouseWheel = function(evt) {
  599. if (!this.options.zoomEnabled || this.state !== 'none') {
  600. return;
  601. }
  602. if (this.options.preventMouseEventsDefault){
  603. if (evt.preventDefault) {
  604. evt.preventDefault();
  605. } else {
  606. evt.returnValue = false;
  607. }
  608. }
  609. // Default delta in case that deltaY is not available
  610. var delta = evt.deltaY || 1
  611. , timeDelta = Date.now() - this.lastMouseWheelEventTime
  612. , divider = 3 + Math.max(0, 30 - timeDelta)
  613. // Update cache
  614. this.lastMouseWheelEventTime = Date.now()
  615. // Make empirical adjustments for browsers that give deltaY in pixels (deltaMode=0)
  616. if ('deltaMode' in evt && evt.deltaMode === 0 && evt.wheelDelta) {
  617. delta = evt.deltaY === 0 ? 0 : Math.abs(evt.wheelDelta) / evt.deltaY
  618. }
  619. delta = -0.3 < delta && delta < 0.3 ? delta : (delta > 0 ? 1 : -1) * Math.log(Math.abs(delta) + 10) / divider
  620. var inversedScreenCTM = this.svg.getScreenCTM().inverse()
  621. , relativeMousePoint = SvgUtils.getEventPoint(evt, this.svg).matrixTransform(inversedScreenCTM)
  622. , 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
  623. this.zoomAtPoint(zoom, relativeMousePoint)
  624. }
  625. /**
  626. * Zoom in at a SVG point
  627. *
  628. * @param {SVGPoint} point
  629. * @param {Float} zoomScale Number representing how much to zoom
  630. * @param {Boolean} zoomAbsolute Default false. If true, zoomScale is treated as an absolute value.
  631. * Otherwise, zoomScale is treated as a multiplied (e.g. 1.10 would zoom in 10%)
  632. */
  633. SvgPanZoom.prototype.zoomAtPoint = function(zoomScale, point, zoomAbsolute) {
  634. var originalState = this.viewport.getOriginalState()
  635. if (!zoomAbsolute) {
  636. // Fit zoomScale in set bounds
  637. if (this.getZoom() * zoomScale < this.options.minZoom * originalState.zoom) {
  638. zoomScale = (this.options.minZoom * originalState.zoom) / this.getZoom()
  639. } else if (this.getZoom() * zoomScale > this.options.maxZoom * originalState.zoom) {
  640. zoomScale = (this.options.maxZoom * originalState.zoom) / this.getZoom()
  641. }
  642. } else {
  643. // Fit zoomScale in set bounds
  644. zoomScale = Math.max(this.options.minZoom * originalState.zoom, Math.min(this.options.maxZoom * originalState.zoom, zoomScale))
  645. // Find relative scale to achieve desired scale
  646. zoomScale = zoomScale/this.getZoom()
  647. }
  648. var oldCTM = this.viewport.getCTM()
  649. , relativePoint = point.matrixTransform(oldCTM.inverse())
  650. , modifier = this.svg.createSVGMatrix().translate(relativePoint.x, relativePoint.y).scale(zoomScale).translate(-relativePoint.x, -relativePoint.y)
  651. , newCTM = oldCTM.multiply(modifier)
  652. if (newCTM.a !== oldCTM.a) {
  653. this.viewport.setCTM(newCTM)
  654. }
  655. }
  656. /**
  657. * Zoom at center point
  658. *
  659. * @param {Float} scale
  660. * @param {Boolean} absolute Marks zoom scale as relative or absolute
  661. */
  662. SvgPanZoom.prototype.zoom = function(scale, absolute) {
  663. this.zoomAtPoint(scale, SvgUtils.getSvgCenterPoint(this.svg, this.width, this.height), absolute)
  664. }
  665. /**
  666. * Zoom used by public instance
  667. *
  668. * @param {Float} scale
  669. * @param {Boolean} absolute Marks zoom scale as relative or absolute
  670. */
  671. SvgPanZoom.prototype.publicZoom = function(scale, absolute) {
  672. if (absolute) {
  673. scale = this.computeFromRelativeZoom(scale)
  674. }
  675. this.zoom(scale, absolute)
  676. }
  677. /**
  678. * Zoom at point used by public instance
  679. *
  680. * @param {Float} scale
  681. * @param {SVGPoint|Object} point An object that has x and y attributes
  682. * @param {Boolean} absolute Marks zoom scale as relative or absolute
  683. */
  684. SvgPanZoom.prototype.publicZoomAtPoint = function(scale, point, absolute) {
  685. if (absolute) {
  686. // Transform zoom into a relative value
  687. scale = this.computeFromRelativeZoom(scale)
  688. }
  689. // If not a SVGPoint but has x and y then create a SVGPoint
  690. if (Utils.getType(point) !== 'SVGPoint') {
  691. if('x' in point && 'y' in point) {
  692. point = SvgUtils.createSVGPoint(this.svg, point.x, point.y)
  693. } else {
  694. throw new Error('Given point is invalid')
  695. }
  696. }
  697. this.zoomAtPoint(scale, point, absolute)
  698. }
  699. /**
  700. * Get zoom scale
  701. *
  702. * @return {Float} zoom scale
  703. */
  704. SvgPanZoom.prototype.getZoom = function() {
  705. return this.viewport.getZoom()
  706. }
  707. /**
  708. * Get zoom scale for public usage
  709. *
  710. * @return {Float} zoom scale
  711. */
  712. SvgPanZoom.prototype.getRelativeZoom = function() {
  713. return this.viewport.getRelativeZoom()
  714. }
  715. /**
  716. * Compute actual zoom from public zoom
  717. *
  718. * @param {Float} zoom
  719. * @return {Float} zoom scale
  720. */
  721. SvgPanZoom.prototype.computeFromRelativeZoom = function(zoom) {
  722. return zoom * this.viewport.getOriginalState().zoom
  723. }
  724. /**
  725. * Set zoom to initial state
  726. */
  727. SvgPanZoom.prototype.resetZoom = function() {
  728. var originalState = this.viewport.getOriginalState()
  729. this.zoom(originalState.zoom, true);
  730. }
  731. /**
  732. * Set pan to initial state
  733. */
  734. SvgPanZoom.prototype.resetPan = function() {
  735. this.pan(this.viewport.getOriginalState());
  736. }
  737. /**
  738. * Set pan and zoom to initial state
  739. */
  740. SvgPanZoom.prototype.reset = function() {
  741. this.resetZoom()
  742. this.resetPan()
  743. }
  744. /**
  745. * Handle double click event
  746. * See handleMouseDown() for alternate detection method
  747. *
  748. * @param {Event} evt
  749. */
  750. SvgPanZoom.prototype.handleDblClick = function(evt) {
  751. if (this.options.preventMouseEventsDefault) {
  752. if (evt.preventDefault) {
  753. evt.preventDefault()
  754. } else {
  755. evt.returnValue = false
  756. }
  757. }
  758. // Check if target was a control button
  759. if (this.options.controlIconsEnabled) {
  760. var targetClass = evt.target.getAttribute('class') || ''
  761. if (targetClass.indexOf('svg-pan-zoom-control') > -1) {
  762. return false
  763. }
  764. }
  765. var zoomFactor
  766. if (evt.shiftKey) {
  767. zoomFactor = 1/((1 + this.options.zoomScaleSensitivity) * 2) // zoom out when shift key pressed
  768. } else {
  769. zoomFactor = (1 + this.options.zoomScaleSensitivity) * 2
  770. }
  771. var point = SvgUtils.getEventPoint(evt, this.svg).matrixTransform(this.svg.getScreenCTM().inverse())
  772. this.zoomAtPoint(zoomFactor, point)
  773. }
  774. /**
  775. * Handle click event
  776. *
  777. * @param {Event} evt
  778. */
  779. SvgPanZoom.prototype.handleMouseDown = function(evt, prevEvt) {
  780. if (this.options.preventMouseEventsDefault) {
  781. if (evt.preventDefault) {
  782. evt.preventDefault()
  783. } else {
  784. evt.returnValue = false
  785. }
  786. }
  787. Utils.mouseAndTouchNormalize(evt, this.svg)
  788. // Double click detection; more consistent than ondblclick
  789. if (this.options.dblClickZoomEnabled && Utils.isDblClick(evt, prevEvt)){
  790. this.handleDblClick(evt)
  791. } else {
  792. // Pan mode
  793. this.state = 'pan'
  794. this.firstEventCTM = this.viewport.getCTM()
  795. this.stateOrigin = SvgUtils.getEventPoint(evt, this.svg).matrixTransform(this.firstEventCTM.inverse())
  796. }
  797. }
  798. /**
  799. * Handle mouse move event
  800. *
  801. * @param {Event} evt
  802. */
  803. SvgPanZoom.prototype.handleMouseMove = function(evt) {
  804. if (this.options.preventMouseEventsDefault) {
  805. if (evt.preventDefault) {
  806. evt.preventDefault()
  807. } else {
  808. evt.returnValue = false
  809. }
  810. }
  811. if (this.state === 'pan' && this.options.panEnabled) {
  812. // Pan mode
  813. var point = SvgUtils.getEventPoint(evt, this.svg).matrixTransform(this.firstEventCTM.inverse())
  814. , viewportCTM = this.firstEventCTM.translate(point.x - this.stateOrigin.x, point.y - this.stateOrigin.y)
  815. this.viewport.setCTM(viewportCTM)
  816. }
  817. }
  818. /**
  819. * Handle mouse button release event
  820. *
  821. * @param {Event} evt
  822. */
  823. SvgPanZoom.prototype.handleMouseUp = function(evt) {
  824. if (this.options.preventMouseEventsDefault) {
  825. if (evt.preventDefault) {
  826. evt.preventDefault()
  827. } else {
  828. evt.returnValue = false
  829. }
  830. }
  831. if (this.state === 'pan') {
  832. // Quit pan mode
  833. this.state = 'none'
  834. }
  835. }
  836. /**
  837. * Adjust viewport size (only) so it will fit in SVG
  838. * Does not center image
  839. */
  840. SvgPanZoom.prototype.fit = function() {
  841. var viewBox = this.viewport.getViewBox()
  842. , newScale = Math.min(this.width/viewBox.width, this.height/viewBox.height)
  843. this.zoom(newScale, true)
  844. }
  845. /**
  846. * Adjust viewport size (only) so it will contain the SVG
  847. * Does not center image
  848. */
  849. SvgPanZoom.prototype.contain = function() {
  850. var viewBox = this.viewport.getViewBox()
  851. , newScale = Math.max(this.width/viewBox.width, this.height/viewBox.height)
  852. this.zoom(newScale, true)
  853. }
  854. /**
  855. * Adjust viewport pan (only) so it will be centered in SVG
  856. * Does not zoom/fit/contain image
  857. */
  858. SvgPanZoom.prototype.center = function() {
  859. var viewBox = this.viewport.getViewBox()
  860. , offsetX = (this.width - (viewBox.width + viewBox.x * 2) * this.getZoom()) * 0.5
  861. , offsetY = (this.height - (viewBox.height + viewBox.y * 2) * this.getZoom()) * 0.5
  862. this.getPublicInstance().pan({x: offsetX, y: offsetY})
  863. }
  864. /**
  865. * Update content cached BorderBox
  866. * Use when viewport contents change
  867. */
  868. SvgPanZoom.prototype.updateBBox = function() {
  869. this.viewport.simpleViewBoxCache()
  870. }
  871. /**
  872. * Pan to a rendered position
  873. *
  874. * @param {Object} point {x: 0, y: 0}
  875. */
  876. SvgPanZoom.prototype.pan = function(point) {
  877. var viewportCTM = this.viewport.getCTM()
  878. viewportCTM.e = point.x
  879. viewportCTM.f = point.y
  880. this.viewport.setCTM(viewportCTM)
  881. }
  882. /**
  883. * Relatively pan the graph by a specified rendered position vector
  884. *
  885. * @param {Object} point {x: 0, y: 0}
  886. */
  887. SvgPanZoom.prototype.panBy = function(point) {
  888. var viewportCTM = this.viewport.getCTM()
  889. viewportCTM.e += point.x
  890. viewportCTM.f += point.y
  891. this.viewport.setCTM(viewportCTM)
  892. }
  893. /**
  894. * Get pan vector
  895. *
  896. * @return {Object} {x: 0, y: 0}
  897. */
  898. SvgPanZoom.prototype.getPan = function() {
  899. var state = this.viewport.getState()
  900. return {x: state.x, y: state.y}
  901. }
  902. /**
  903. * Recalculates cached svg dimensions and controls position
  904. */
  905. SvgPanZoom.prototype.resize = function() {
  906. // Get dimensions
  907. var boundingClientRectNormalized = SvgUtils.getBoundingClientRectNormalized(this.svg)
  908. this.width = boundingClientRectNormalized.width
  909. this.height = boundingClientRectNormalized.height
  910. // Recalculate original state
  911. var viewport = this.viewport
  912. viewport.options.width = this.width
  913. viewport.options.height = this.height
  914. viewport.processCTM()
  915. // Reposition control icons by re-enabling them
  916. if (this.options.controlIconsEnabled) {
  917. this.getPublicInstance().disableControlIcons()
  918. this.getPublicInstance().enableControlIcons()
  919. }
  920. }
  921. /**
  922. * Unbind mouse events, free callbacks and destroy public instance
  923. */
  924. SvgPanZoom.prototype.destroy = function() {
  925. var that = this
  926. // Free callbacks
  927. this.beforeZoom = null
  928. this.onZoom = null
  929. this.beforePan = null
  930. this.onPan = null
  931. this.onUpdatedCTM = null
  932. // Destroy custom event handlers
  933. if (this.options.customEventsHandler != null) { // jshint ignore:line
  934. this.options.customEventsHandler.destroy({
  935. svgElement: this.svg
  936. , eventsListenerElement: this.options.eventsListenerElement
  937. , instance: this.getPublicInstance()
  938. })
  939. }
  940. // Unbind eventListeners
  941. for (var event in this.eventListeners) {
  942. (this.options.eventsListenerElement || this.svg)
  943. .removeEventListener(event, this.eventListeners[event], false)
  944. }
  945. // Unbind wheelListener
  946. this.disableMouseWheelZoom()
  947. // Remove control icons
  948. this.getPublicInstance().disableControlIcons()
  949. // Reset zoom and pan
  950. this.reset()
  951. // Remove instance from instancesStore
  952. instancesStore = instancesStore.filter(function(instance){
  953. return instance.svg !== that.svg
  954. })
  955. // Delete options and its contents
  956. delete this.options
  957. // Delete viewport to make public shadow viewport functions uncallable
  958. delete this.viewport
  959. // Destroy public instance and rewrite getPublicInstance
  960. delete this.publicInstance
  961. delete this.pi
  962. this.getPublicInstance = function(){
  963. return null
  964. }
  965. }
  966. /**
  967. * Returns a public instance object
  968. *
  969. * @return {Object} Public instance object
  970. */
  971. SvgPanZoom.prototype.getPublicInstance = function() {
  972. var that = this
  973. // Create cache
  974. if (!this.publicInstance) {
  975. this.publicInstance = this.pi = {
  976. // Pan
  977. enablePan: function() {that.options.panEnabled = true; return that.pi}
  978. , disablePan: function() {that.options.panEnabled = false; return that.pi}
  979. , isPanEnabled: function() {return !!that.options.panEnabled}
  980. , pan: function(point) {that.pan(point); return that.pi}
  981. , panBy: function(point) {that.panBy(point); return that.pi}
  982. , getPan: function() {return that.getPan()}
  983. // Pan event
  984. , setBeforePan: function(fn) {that.options.beforePan = fn === null ? null : Utils.proxy(fn, that.publicInstance); return that.pi}
  985. , setOnPan: function(fn) {that.options.onPan = fn === null ? null : Utils.proxy(fn, that.publicInstance); return that.pi}
  986. // Zoom and Control Icons
  987. , enableZoom: function() {that.options.zoomEnabled = true; return that.pi}
  988. , disableZoom: function() {that.options.zoomEnabled = false; return that.pi}
  989. , isZoomEnabled: function() {return !!that.options.zoomEnabled}
  990. , enableControlIcons: function() {
  991. if (!that.options.controlIconsEnabled) {
  992. that.options.controlIconsEnabled = true
  993. ControlIcons.enable(that)
  994. }
  995. return that.pi
  996. }
  997. , disableControlIcons: function() {
  998. if (that.options.controlIconsEnabled) {
  999. that.options.controlIconsEnabled = false;
  1000. ControlIcons.disable(that)
  1001. }
  1002. return that.pi
  1003. }
  1004. , isControlIconsEnabled: function() {return !!that.options.controlIconsEnabled}
  1005. // Double click zoom
  1006. , enableDblClickZoom: function() {that.options.dblClickZoomEnabled = true; return that.pi}
  1007. , disableDblClickZoom: function() {that.options.dblClickZoomEnabled = false; return that.pi}
  1008. , isDblClickZoomEnabled: function() {return !!that.options.dblClickZoomEnabled}
  1009. // Mouse wheel zoom
  1010. , enableMouseWheelZoom: function() {that.enableMouseWheelZoom(); return that.pi}
  1011. , disableMouseWheelZoom: function() {that.disableMouseWheelZoom(); return that.pi}
  1012. , isMouseWheelZoomEnabled: function() {return !!that.options.mouseWheelZoomEnabled}
  1013. // Zoom scale and bounds
  1014. , setZoomScaleSensitivity: function(scale) {that.options.zoomScaleSensitivity = scale; return that.pi}
  1015. , setMinZoom: function(zoom) {that.options.minZoom = zoom; return that.pi}
  1016. , setMaxZoom: function(zoom) {that.options.maxZoom = zoom; return that.pi}
  1017. // Zoom event
  1018. , setBeforeZoom: function(fn) {that.options.beforeZoom = fn === null ? null : Utils.proxy(fn, that.publicInstance); return that.pi}
  1019. , setOnZoom: function(fn) {that.options.onZoom = fn === null ? null : Utils.proxy(fn, that.publicInstance); return that.pi}
  1020. // Zooming
  1021. , zoom: function(scale) {that.publicZoom(scale, true); return that.pi}
  1022. , zoomBy: function(scale) {that.publicZoom(scale, false); return that.pi}
  1023. , zoomAtPoint: function(scale, point) {that.publicZoomAtPoint(scale, point, true); return that.pi}
  1024. , zoomAtPointBy: function(scale, point) {that.publicZoomAtPoint(scale, point, false); return that.pi}
  1025. , zoomIn: function() {this.zoomBy(1 + that.options.zoomScaleSensitivity); return that.pi}
  1026. , zoomOut: function() {this.zoomBy(1 / (1 + that.options.zoomScaleSensitivity)); return that.pi}
  1027. , getZoom: function() {return that.getRelativeZoom()}
  1028. // CTM update
  1029. , setOnUpdatedCTM: function(fn) {that.options.onUpdatedCTM = fn === null ? null : Utils.proxy(fn, that.publicInstance); return that.pi}
  1030. // Reset
  1031. , resetZoom: function() {that.resetZoom(); return that.pi}
  1032. , resetPan: function() {that.resetPan(); return that.pi}
  1033. , reset: function() {that.reset(); return that.pi}
  1034. // Fit, Contain and Center
  1035. , fit: function() {that.fit(); return that.pi}
  1036. , contain: function() {that.contain(); return that.pi}
  1037. , center: function() {that.center(); return that.pi}
  1038. // Size and Resize
  1039. , updateBBox: function() {that.updateBBox(); return that.pi}
  1040. , resize: function() {that.resize(); return that.pi}
  1041. , getSizes: function() {
  1042. return {
  1043. width: that.width
  1044. , height: that.height
  1045. , realZoom: that.getZoom()
  1046. , viewBox: that.viewport.getViewBox()
  1047. }
  1048. }
  1049. // Destroy
  1050. , destroy: function() {that.destroy(); return that.pi}
  1051. }
  1052. }
  1053. return this.publicInstance
  1054. }
  1055. /**
  1056. * Stores pairs of instances of SvgPanZoom and SVG
  1057. * Each pair is represented by an object {svg: SVGSVGElement, instance: SvgPanZoom}
  1058. *
  1059. * @type {Array}
  1060. */
  1061. var instancesStore = []
  1062. var svgPanZoom = function(elementOrSelector, options){
  1063. var svg = Utils.getSvg(elementOrSelector)
  1064. if (svg === null) {
  1065. return null
  1066. } else {
  1067. // Look for existent instance
  1068. for(var i = instancesStore.length - 1; i >= 0; i--) {
  1069. if (instancesStore[i].svg === svg) {
  1070. return instancesStore[i].instance.getPublicInstance()
  1071. }
  1072. }
  1073. // If instance not found - create one
  1074. instancesStore.push({
  1075. svg: svg
  1076. , instance: new SvgPanZoom(svg, options)
  1077. })
  1078. // Return just pushed instance
  1079. return instancesStore[instancesStore.length - 1].instance.getPublicInstance()
  1080. }
  1081. }
  1082. module.exports = svgPanZoom;
  1083. },{"./control-icons":2,"./shadow-viewport":3,"./svg-utilities":5,"./uniwheel":6,"./utilities":7}],5:[function(require,module,exports){
  1084. var Utils = require('./utilities')
  1085. , _browser = 'unknown'
  1086. ;
  1087. // http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
  1088. if (/*@cc_on!@*/false || !!document.documentMode) { // internet explorer
  1089. _browser = 'ie';
  1090. }
  1091. module.exports = {
  1092. svgNS: 'http://www.w3.org/2000/svg'
  1093. , xmlNS: 'http://www.w3.org/XML/1998/namespace'
  1094. , xmlnsNS: 'http://www.w3.org/2000/xmlns/'
  1095. , xlinkNS: 'http://www.w3.org/1999/xlink'
  1096. , evNS: 'http://www.w3.org/2001/xml-events'
  1097. /**
  1098. * Get svg dimensions: width and height
  1099. *
  1100. * @param {SVGSVGElement} svg
  1101. * @return {Object} {width: 0, height: 0}
  1102. */
  1103. , getBoundingClientRectNormalized: function(svg) {
  1104. if (svg.clientWidth && svg.clientHeight) {
  1105. return {width: svg.clientWidth, height: svg.clientHeight}
  1106. } else if (!!svg.getBoundingClientRect()) {
  1107. return svg.getBoundingClientRect();
  1108. } else {
  1109. throw new Error('Cannot get BoundingClientRect for SVG.');
  1110. }
  1111. }
  1112. /**
  1113. * Gets g element with class of "viewport" or creates it if it doesn't exist
  1114. *
  1115. * @param {SVGSVGElement} svg
  1116. * @return {SVGElement} g (group) element
  1117. */
  1118. , getOrCreateViewport: function(svg, selector) {
  1119. var viewport = null
  1120. if (Utils.isElement(selector)) {
  1121. viewport = selector
  1122. } else {
  1123. viewport = svg.querySelector(selector)
  1124. }
  1125. // Check if there is just one main group in SVG
  1126. if (!viewport) {
  1127. var childNodes = Array.prototype.slice.call(svg.childNodes || svg.children).filter(function(el){
  1128. return el.nodeName !== 'defs' && el.nodeName !== '#text'
  1129. })
  1130. // Node name should be SVGGElement and should have no transform attribute
  1131. // Groups with transform are not used as viewport because it involves parsing of all transform possibilities
  1132. if (childNodes.length === 1 && childNodes[0].nodeName === 'g' && childNodes[0].getAttribute('transform') === null) {
  1133. viewport = childNodes[0]
  1134. }
  1135. }
  1136. // If no favorable group element exists then create one
  1137. if (!viewport) {
  1138. var viewportId = 'viewport-' + new Date().toISOString().replace(/\D/g, '');
  1139. viewport = document.createElementNS(this.svgNS, 'g');
  1140. viewport.setAttribute('id', viewportId);
  1141. // Internet Explorer (all versions?) can't use childNodes, but other browsers prefer (require?) using childNodes
  1142. var svgChildren = svg.childNodes || svg.children;
  1143. if (!!svgChildren && svgChildren.length > 0) {
  1144. for (var i = svgChildren.length; i > 0; i--) {
  1145. // Move everything into viewport except defs
  1146. if (svgChildren[svgChildren.length - i].nodeName !== 'defs') {
  1147. viewport.appendChild(svgChildren[svgChildren.length - i]);
  1148. }
  1149. }
  1150. }
  1151. svg.appendChild(viewport);
  1152. }
  1153. // Parse class names
  1154. var classNames = [];
  1155. if (viewport.getAttribute('class')) {
  1156. classNames = viewport.getAttribute('class').split(' ')
  1157. }
  1158. // Set class (if not set already)
  1159. if (!~classNames.indexOf('svg-pan-zoom_viewport')) {
  1160. classNames.push('svg-pan-zoom_viewport')
  1161. viewport.setAttribute('class', classNames.join(' '))
  1162. }
  1163. return viewport
  1164. }
  1165. /**
  1166. * Set SVG attributes
  1167. *
  1168. * @param {SVGSVGElement} svg
  1169. */
  1170. , setupSvgAttributes: function(svg) {
  1171. // Setting default attributes
  1172. svg.setAttribute('xmlns', this.svgNS);
  1173. svg.setAttributeNS(this.xmlnsNS, 'xmlns:xlink', this.xlinkNS);
  1174. svg.setAttributeNS(this.xmlnsNS, 'xmlns:ev', this.evNS);
  1175. // Needed for Internet Explorer, otherwise the viewport overflows
  1176. if (svg.parentNode !== null) {
  1177. var style = svg.getAttribute('style') || '';
  1178. if (style.toLowerCase().indexOf('overflow') === -1) {
  1179. svg.setAttribute('style', 'overflow: hidden; ' + style);
  1180. }
  1181. }
  1182. }
  1183. /**
  1184. * How long Internet Explorer takes to finish updating its display (ms).
  1185. */
  1186. , internetExplorerRedisplayInterval: 300
  1187. /**
  1188. * Forces the browser to redisplay all SVG elements that rely on an
  1189. * element defined in a 'defs' section. It works globally, for every
  1190. * available defs element on the page.
  1191. * The throttling is intentionally global.
  1192. *
  1193. * This is only needed for IE. It is as a hack to make markers (and 'use' elements?)
  1194. * visible after pan/zoom when there are multiple SVGs on the page.
  1195. * See bug report: https://connect.microsoft.com/IE/feedback/details/781964/
  1196. * also see svg-pan-zoom issue: https://github.com/ariutta/svg-pan-zoom/issues/62
  1197. */
  1198. , refreshDefsGlobal: Utils.throttle(function() {
  1199. var allDefs = document.querySelectorAll('defs');
  1200. var allDefsCount = allDefs.length;
  1201. for (var i = 0; i < allDefsCount; i++) {
  1202. var thisDefs = allDefs[i];
  1203. thisDefs.parentNode.insertBefore(thisDefs, thisDefs);
  1204. }
  1205. }, this.internetExplorerRedisplayInterval)
  1206. /**
  1207. * Sets the current transform matrix of an element
  1208. *
  1209. * @param {SVGElement} element
  1210. * @param {SVGMatrix} matrix CTM
  1211. * @param {SVGElement} defs
  1212. */
  1213. , setCTM: function(element, matrix, defs) {
  1214. var that = this
  1215. , s = 'matrix(' + matrix.a + ',' + matrix.b + ',' + matrix.c + ',' + matrix.d + ',' + matrix.e + ',' + matrix.f + ')';
  1216. element.setAttributeNS(null, 'transform', s);
  1217. if ('transform' in element.style) {
  1218. element.style.transform = s;
  1219. } else if ('-ms-transform' in element.style) {
  1220. element.style['-ms-transform'] = s;
  1221. } else if ('-webkit-transform' in element.style) {
  1222. element.style['-webkit-transform'] = s;
  1223. }
  1224. // IE has a bug that makes markers disappear on zoom (when the matrix "a" and/or "d" elements change)
  1225. // see http://stackoverflow.com/questions/17654578/svg-marker-does-not-work-in-ie9-10
  1226. // and http://srndolha.wordpress.com/2013/11/25/svg-line-markers-may-disappear-in-internet-explorer-11/
  1227. if (_browser === 'ie' && !!defs) {
  1228. // this refresh is intended for redisplaying the SVG during zooming
  1229. defs.parentNode.insertBefore(defs, defs);
  1230. // this refresh is intended for redisplaying the other SVGs on a page when panning a given SVG
  1231. // it is also needed for the given SVG itself, on zoomEnd, if the SVG contains any markers that
  1232. // are located under any other element(s).
  1233. window.setTimeout(function() {
  1234. that.refreshDefsGlobal();
  1235. }, that.internetExplorerRedisplayInterval);
  1236. }
  1237. }
  1238. /**
  1239. * Instantiate an SVGPoint object with given event coordinates
  1240. *
  1241. * @param {Event} evt
  1242. * @param {SVGSVGElement} svg
  1243. * @return {SVGPoint} point
  1244. */
  1245. , getEventPoint: function(evt, svg) {
  1246. var point = svg.createSVGPoint()
  1247. Utils.mouseAndTouchNormalize(evt, svg)
  1248. point.x = evt.clientX
  1249. point.y = evt.clientY
  1250. return point
  1251. }
  1252. /**
  1253. * Get SVG center point
  1254. *
  1255. * @param {SVGSVGElement} svg
  1256. * @return {SVGPoint}
  1257. */
  1258. , getSvgCenterPoint: function(svg, width, height) {
  1259. return this.createSVGPoint(svg, width / 2, height / 2)
  1260. }
  1261. /**
  1262. * Create a SVGPoint with given x and y
  1263. *
  1264. * @param {SVGSVGElement} svg
  1265. * @param {Number} x
  1266. * @param {Number} y
  1267. * @return {SVGPoint}
  1268. */
  1269. , createSVGPoint: function(svg, x, y) {
  1270. var point = svg.createSVGPoint()
  1271. point.x = x
  1272. point.y = y
  1273. return point
  1274. }
  1275. }
  1276. },{"./utilities":7}],6:[function(require,module,exports){
  1277. // uniwheel 0.1.2 (customized)
  1278. // A unified cross browser mouse wheel event handler
  1279. // https://github.com/teemualap/uniwheel
  1280. module.exports = (function(){
  1281. //Full details: https://developer.mozilla.org/en-US/docs/Web/Reference/Events/wheel
  1282. var prefix = "", _addEventListener, _removeEventListener, onwheel, support, fns = [];
  1283. // detect event model
  1284. if ( window.addEventListener ) {
  1285. _addEventListener = "addEventListener";
  1286. _removeEventListener = "removeEventListener";
  1287. } else {
  1288. _addEventListener = "attachEvent";
  1289. _removeEventListener = "detachEvent";
  1290. prefix = "on";
  1291. }
  1292. // detect available wheel event
  1293. support = "onwheel" in document.createElement("div") ? "wheel" : // Modern browsers support "wheel"
  1294. document.onmousewheel !== undefined ? "mousewheel" : // Webkit and IE support at least "mousewheel"
  1295. "DOMMouseScroll"; // let's assume that remaining browsers are older Firefox
  1296. function createCallback(element,callback,capture) {
  1297. var fn = function(originalEvent) {
  1298. !originalEvent && ( originalEvent = window.event );
  1299. // create a normalized event object
  1300. var event = {
  1301. // keep a ref to the original event object
  1302. originalEvent: originalEvent,
  1303. target: originalEvent.target || originalEvent.srcElement,
  1304. type: "wheel",
  1305. deltaMode: originalEvent.type == "MozMousePixelScroll" ? 0 : 1,
  1306. deltaX: 0,
  1307. delatZ: 0,
  1308. preventDefault: function() {
  1309. originalEvent.preventDefault ?
  1310. originalEvent.preventDefault() :
  1311. originalEvent.returnValue = false;
  1312. }
  1313. };
  1314. // calculate deltaY (and deltaX) according to the event
  1315. if ( support == "mousewheel" ) {
  1316. event.deltaY = - 1/40 * originalEvent.wheelDelta;
  1317. // Webkit also support wheelDeltaX
  1318. originalEvent.wheelDeltaX && ( event.deltaX = - 1/40 * originalEvent.wheelDeltaX );
  1319. } else {
  1320. event.deltaY = originalEvent.detail;
  1321. }
  1322. // it's time to fire the callback
  1323. return callback( event );
  1324. };
  1325. fns.push({
  1326. element: element,
  1327. fn: fn,
  1328. capture: capture
  1329. });
  1330. return fn;
  1331. }
  1332. function getCallback(element,capture) {
  1333. for (var i = 0; i < fns.length; i++) {
  1334. if (fns[i].element === element && fns[i].capture === capture) {
  1335. return fns[i].fn;
  1336. }
  1337. }
  1338. return function(){};
  1339. }
  1340. function removeCallback(element,capture) {
  1341. for (var i = 0; i < fns.length; i++) {
  1342. if (fns[i].element === element && fns[i].capture === capture) {
  1343. return fns.splice(i,1);
  1344. }
  1345. }
  1346. }
  1347. function _addWheelListener( elem, eventName, callback, useCapture ) {
  1348. var cb;
  1349. if (support === "wheel") {
  1350. cb = callback;
  1351. } else {
  1352. cb = createCallback(elem,callback,useCapture);
  1353. }
  1354. elem[ _addEventListener ]( prefix + eventName, cb, useCapture || false );
  1355. }
  1356. function _removeWheelListener( elem, eventName, callback, useCapture ) {
  1357. var cb;
  1358. if (support === "wheel") {
  1359. cb = callback;
  1360. } else {
  1361. cb = getCallback(elem,useCapture);
  1362. }
  1363. elem[ _removeEventListener ]( prefix + eventName, cb, useCapture || false );
  1364. removeCallback(elem,useCapture);
  1365. }
  1366. function addWheelListener( elem, callback, useCapture ) {
  1367. _addWheelListener( elem, support, callback, useCapture );
  1368. // handle MozMousePixelScroll in older Firefox
  1369. if( support == "DOMMouseScroll" ) {
  1370. _addWheelListener( elem, "MozMousePixelScroll", callback, useCapture);
  1371. }
  1372. }
  1373. function removeWheelListener(elem,callback,useCapture){
  1374. _removeWheelListener(elem,support,callback,useCapture);
  1375. // handle MozMousePixelScroll in older Firefox
  1376. if( support == "DOMMouseScroll" ) {
  1377. _removeWheelListener(elem, "MozMousePixelScroll", callback, useCapture);
  1378. }
  1379. }
  1380. return {
  1381. on: addWheelListener,
  1382. off: removeWheelListener
  1383. };
  1384. })();
  1385. },{}],7:[function(require,module,exports){
  1386. module.exports = {
  1387. /**
  1388. * Extends an object
  1389. *
  1390. * @param {Object} target object to extend
  1391. * @param {Object} source object to take properties from
  1392. * @return {Object} extended object
  1393. */
  1394. extend: function(target, source) {
  1395. target = target || {};
  1396. for (var prop in source) {
  1397. // Go recursively
  1398. if (this.isObject(source[prop])) {
  1399. target[prop] = this.extend(target[prop], source[prop])
  1400. } else {
  1401. target[prop] = source[prop]
  1402. }
  1403. }
  1404. return target;
  1405. }
  1406. /**
  1407. * Checks if an object is a DOM element
  1408. *
  1409. * @param {Object} o HTML element or String
  1410. * @return {Boolean} returns true if object is a DOM element
  1411. */
  1412. , isElement: function(o){
  1413. return (
  1414. o instanceof HTMLElement || o instanceof SVGElement || o instanceof SVGSVGElement || //DOM2
  1415. (o && typeof o === 'object' && o !== null && o.nodeType === 1 && typeof o.nodeName === 'string')
  1416. );
  1417. }
  1418. /**
  1419. * Checks if an object is an Object
  1420. *
  1421. * @param {Object} o Object
  1422. * @return {Boolean} returns true if object is an Object
  1423. */
  1424. , isObject: function(o){
  1425. return Object.prototype.toString.call(o) === '[object Object]';
  1426. }
  1427. /**
  1428. * Checks if variable is Number
  1429. *
  1430. * @param {Integer|Float} n
  1431. * @return {Boolean} returns true if variable is Number
  1432. */
  1433. , isNumber: function(n) {
  1434. return !isNaN(parseFloat(n)) && isFinite(n);
  1435. }
  1436. /**
  1437. * Search for an SVG element
  1438. *
  1439. * @param {Object|String} elementOrSelector DOM Element or selector String
  1440. * @return {Object|Null} SVG or null
  1441. */
  1442. , getSvg: function(elementOrSelector) {
  1443. var element
  1444. , svg;
  1445. if (!this.isElement(elementOrSelector)) {
  1446. // If selector provided
  1447. if (typeof elementOrSelector === 'string' || elementOrSelector instanceof String) {
  1448. // Try to find the element
  1449. element = document.querySelector(elementOrSelector)
  1450. if (!element) {
  1451. throw new Error('Provided selector did not find any elements. Selector: ' + elementOrSelector)
  1452. return null
  1453. }
  1454. } else {
  1455. throw new Error('Provided selector is not an HTML object nor String')
  1456. return null
  1457. }
  1458. } else {
  1459. element = elementOrSelector
  1460. }
  1461. if (element.tagName.toLowerCase() === 'svg') {
  1462. svg = element;
  1463. } else {
  1464. if (element.tagName.toLowerCase() === 'object') {
  1465. svg = element.contentDocument.documentElement;
  1466. } else {
  1467. if (element.tagName.toLowerCase() === 'embed') {
  1468. svg = element.getSVGDocument().documentElement;
  1469. } else {
  1470. if (element.tagName.toLowerCase() === 'img') {
  1471. throw new Error('Cannot script an SVG in an "img" element. Please use an "object" element or an in-line SVG.');
  1472. } else {
  1473. throw new Error('Cannot get SVG.');
  1474. }
  1475. return null
  1476. }
  1477. }
  1478. }
  1479. return svg
  1480. }
  1481. /**
  1482. * Attach a given context to a function
  1483. * @param {Function} fn Function
  1484. * @param {Object} context Context
  1485. * @return {Function} Function with certain context
  1486. */
  1487. , proxy: function(fn, context) {
  1488. return function() {
  1489. return fn.apply(context, arguments)
  1490. }
  1491. }
  1492. /**
  1493. * Returns object type
  1494. * Uses toString that returns [object SVGPoint]
  1495. * And than parses object type from string
  1496. *
  1497. * @param {Object} o Any object
  1498. * @return {String} Object type
  1499. */
  1500. , getType: function(o) {
  1501. return Object.prototype.toString.apply(o).replace(/^\[object\s/, '').replace(/\]$/, '')
  1502. }
  1503. /**
  1504. * If it is a touch event than add clientX and clientY to event object
  1505. *
  1506. * @param {Event} evt
  1507. * @param {SVGSVGElement} svg
  1508. */
  1509. , mouseAndTouchNormalize: function(evt, svg) {
  1510. // If no cilentX and but touch objects are available
  1511. if (evt.clientX === void 0 || evt.clientX === null) {
  1512. // Fallback
  1513. evt.clientX = 0
  1514. evt.clientY = 0
  1515. // If it is a touch event
  1516. if (evt.changedTouches !== void 0 && evt.changedTouches.length) {
  1517. // If touch event has changedTouches
  1518. if (evt.changedTouches[0].clientX !== void 0) {
  1519. evt.clientX = evt.changedTouches[0].clientX
  1520. evt.clientY = evt.changedTouches[0].clientY
  1521. }
  1522. // If changedTouches has pageX attribute
  1523. else if (evt.changedTouches[0].pageX !== void 0) {
  1524. var rect = svg.getBoundingClientRect();
  1525. evt.clientX = evt.changedTouches[0].pageX - rect.left
  1526. evt.clientY = evt.changedTouches[0].pageY - rect.top
  1527. }
  1528. // If it is a custom event
  1529. } else if (evt.originalEvent !== void 0) {
  1530. if (evt.originalEvent.clientX !== void 0) {
  1531. evt.clientX = evt.originalEvent.clientX
  1532. evt.clientY = evt.originalEvent.clientY
  1533. }
  1534. }
  1535. }
  1536. }
  1537. /**
  1538. * Check if an event is a double click/tap
  1539. * TODO: For touch gestures use a library (hammer.js) that takes in account other events
  1540. * (touchmove and touchend). It should take in account tap duration and traveled distance
  1541. *
  1542. * @param {Event} evt
  1543. * @param {Event} prevEvt Previous Event
  1544. * @return {Boolean}
  1545. */
  1546. , isDblClick: function(evt, prevEvt) {
  1547. // Double click detected by browser
  1548. if (evt.detail === 2) {
  1549. return true;
  1550. }
  1551. // Try to compare events
  1552. else if (prevEvt !== void 0 && prevEvt !== null) {
  1553. var timeStampDiff = evt.timeStamp - prevEvt.timeStamp // should be lower than 250 ms
  1554. , touchesDistance = Math.sqrt(Math.pow(evt.clientX - prevEvt.clientX, 2) + Math.pow(evt.clientY - prevEvt.clientY, 2))
  1555. return timeStampDiff < 250 && touchesDistance < 10
  1556. }
  1557. // Nothing found
  1558. return false;
  1559. }
  1560. /**
  1561. * Returns current timestamp as an integer
  1562. *
  1563. * @return {Number}
  1564. */
  1565. , now: Date.now || function() {
  1566. return new Date().getTime();
  1567. }
  1568. // From underscore.
  1569. // Returns a function, that, when invoked, will only be triggered at most once
  1570. // during a given window of time. Normally, the throttled function will run
  1571. // as much as it can, without ever going more than once per `wait` duration;
  1572. // but if you'd like to disable the execution on the leading edge, pass
  1573. // `{leading: false}`. To disable execution on the trailing edge, ditto.
  1574. // jscs:disable
  1575. // jshint ignore:start
  1576. , throttle: function(func, wait, options) {
  1577. var that = this;
  1578. var context, args, result;
  1579. var timeout = null;
  1580. var previous = 0;
  1581. if (!options) options = {};
  1582. var later = function() {
  1583. previous = options.leading === false ? 0 : that.now();
  1584. timeout = null;
  1585. result = func.apply(context, args);
  1586. if (!timeout) context = args = null;
  1587. };
  1588. return function() {
  1589. var now = that.now();
  1590. if (!previous && options.leading === false) previous = now;
  1591. var remaining = wait - (now - previous);
  1592. context = this;
  1593. args = arguments;
  1594. if (remaining <= 0 || remaining > wait) {
  1595. clearTimeout(timeout);
  1596. timeout = null;
  1597. previous = now;
  1598. result = func.apply(context, args);
  1599. if (!timeout) context = args = null;
  1600. } else if (!timeout && options.trailing !== false) {
  1601. timeout = setTimeout(later, remaining);
  1602. }
  1603. return result;
  1604. };
  1605. }
  1606. // jshint ignore:end
  1607. // jscs:enable
  1608. /**
  1609. * Create a requestAnimationFrame simulation
  1610. *
  1611. * @param {Number|String} refreshRate
  1612. * @return {Function}
  1613. */
  1614. , createRequestAnimationFrame: function(refreshRate) {
  1615. var timeout = null
  1616. // Convert refreshRate to timeout
  1617. if (refreshRate !== 'auto' && refreshRate < 60 && refreshRate > 1) {
  1618. timeout = Math.floor(1000 / refreshRate)
  1619. }
  1620. if (timeout === null) {
  1621. return window.requestAnimationFrame || requestTimeout(33)
  1622. } else {
  1623. return requestTimeout(timeout)
  1624. }
  1625. }
  1626. }
  1627. /**
  1628. * Create a callback that will execute after a given timeout
  1629. *
  1630. * @param {Function} timeout
  1631. * @return {Function}
  1632. */
  1633. function requestTimeout(timeout) {
  1634. return function(callback) {
  1635. window.setTimeout(callback, timeout)
  1636. }
  1637. }
  1638. },{}]},{},[1]);