MapNavigation.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* *
  2. *
  3. * (c) 2010-2020 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. import Chart from '../Core/Chart/Chart.js';
  11. import H from '../Core/Globals.js';
  12. var doc = H.doc;
  13. import U from '../Core/Utilities.js';
  14. var addEvent = U.addEvent, extend = U.extend, merge = U.merge, objectEach = U.objectEach, pick = U.pick;
  15. /* eslint-disable no-invalid-this, valid-jsdoc */
  16. /**
  17. * @private
  18. */
  19. function stopEvent(e) {
  20. if (e) {
  21. if (e.preventDefault) {
  22. e.preventDefault();
  23. }
  24. if (e.stopPropagation) {
  25. e.stopPropagation();
  26. }
  27. e.cancelBubble = true;
  28. }
  29. }
  30. /**
  31. * The MapNavigation handles buttons for navigation in addition to mousewheel
  32. * and doubleclick handlers for chart zooming.
  33. *
  34. * @private
  35. * @class
  36. * @name MapNavigation
  37. *
  38. * @param {Highcharts.Chart} chart
  39. * The Chart instance.
  40. */
  41. function MapNavigation(chart) {
  42. this.init(chart);
  43. }
  44. /**
  45. * Initialize function.
  46. *
  47. * @function MapNavigation#init
  48. *
  49. * @param {Highcharts.Chart} chart
  50. * The Chart instance.
  51. *
  52. * @return {void}
  53. */
  54. MapNavigation.prototype.init = function (chart) {
  55. this.chart = chart;
  56. chart.mapNavButtons = [];
  57. };
  58. /**
  59. * Update the map navigation with new options. Calling this is the same as
  60. * calling `chart.update({ mapNavigation: {} })`.
  61. *
  62. * @function MapNavigation#update
  63. *
  64. * @param {Highcharts.MapNavigationOptions} [options]
  65. * New options for the map navigation.
  66. *
  67. * @return {void}
  68. */
  69. MapNavigation.prototype.update = function (options) {
  70. var chart = this.chart, o = chart.options.mapNavigation, buttonOptions, attr, states, hoverStates, selectStates, outerHandler = function (e) {
  71. this.handler.call(chart, e);
  72. stopEvent(e); // Stop default click event (#4444)
  73. }, mapNavButtons = chart.mapNavButtons;
  74. // Merge in new options in case of update, and register back to chart
  75. // options.
  76. if (options) {
  77. o = chart.options.mapNavigation =
  78. merge(chart.options.mapNavigation, options);
  79. }
  80. // Destroy buttons in case of dynamic update
  81. while (mapNavButtons.length) {
  82. mapNavButtons.pop().destroy();
  83. }
  84. if (pick(o.enableButtons, o.enabled) && !chart.renderer.forExport) {
  85. objectEach(o.buttons, function (button, n) {
  86. buttonOptions = merge(o.buttonOptions, button);
  87. // Presentational
  88. if (!chart.styledMode) {
  89. attr = buttonOptions.theme;
  90. attr.style = merge(buttonOptions.theme.style, buttonOptions.style // #3203
  91. );
  92. states = attr.states;
  93. hoverStates = states && states.hover;
  94. selectStates = states && states.select;
  95. }
  96. button = chart.renderer
  97. .button(buttonOptions.text, 0, 0, outerHandler, attr, hoverStates, selectStates, 0, n === 'zoomIn' ? 'topbutton' : 'bottombutton')
  98. .addClass('highcharts-map-navigation highcharts-' + {
  99. zoomIn: 'zoom-in',
  100. zoomOut: 'zoom-out'
  101. }[n])
  102. .attr({
  103. width: buttonOptions.width,
  104. height: buttonOptions.height,
  105. title: chart.options.lang[n],
  106. padding: buttonOptions.padding,
  107. zIndex: 5
  108. })
  109. .add();
  110. button.handler = buttonOptions.onclick;
  111. // Stop double click event (#4444)
  112. addEvent(button.element, 'dblclick', stopEvent);
  113. mapNavButtons.push(button);
  114. // Align it after the plotBox is known (#12776)
  115. var bo = buttonOptions;
  116. var un = addEvent(chart, 'load', function () {
  117. button.align(extend(bo, {
  118. width: button.width,
  119. height: 2 * button.height
  120. }), null, bo.alignTo);
  121. un();
  122. });
  123. });
  124. }
  125. this.updateEvents(o);
  126. };
  127. /**
  128. * Update events, called internally from the update function. Add new event
  129. * handlers, or unbinds events if disabled.
  130. *
  131. * @function MapNavigation#updateEvents
  132. *
  133. * @param {Highcharts.MapNavigationOptions} options
  134. * Options for map navigation.
  135. *
  136. * @return {void}
  137. */
  138. MapNavigation.prototype.updateEvents = function (options) {
  139. var chart = this.chart;
  140. // Add the double click event
  141. if (pick(options.enableDoubleClickZoom, options.enabled) ||
  142. options.enableDoubleClickZoomTo) {
  143. this.unbindDblClick = this.unbindDblClick || addEvent(chart.container, 'dblclick', function (e) {
  144. chart.pointer.onContainerDblClick(e);
  145. });
  146. }
  147. else if (this.unbindDblClick) {
  148. // Unbind and set unbinder to undefined
  149. this.unbindDblClick = this.unbindDblClick();
  150. }
  151. // Add the mousewheel event
  152. if (pick(options.enableMouseWheelZoom, options.enabled)) {
  153. this.unbindMouseWheel = this.unbindMouseWheel || addEvent(chart.container, typeof doc.onmousewheel === 'undefined' ?
  154. 'DOMMouseScroll' : 'mousewheel', function (e) {
  155. chart.pointer.onContainerMouseWheel(e);
  156. // Issue #5011, returning false from non-jQuery event does
  157. // not prevent default
  158. stopEvent(e);
  159. return false;
  160. });
  161. }
  162. else if (this.unbindMouseWheel) {
  163. // Unbind and set unbinder to undefined
  164. this.unbindMouseWheel = this.unbindMouseWheel();
  165. }
  166. };
  167. // Add events to the Chart object itself
  168. extend(Chart.prototype, /** @lends Chart.prototype */ {
  169. /**
  170. * Fit an inner box to an outer. If the inner box overflows left or right,
  171. * align it to the sides of the outer. If it overflows both sides, fit it
  172. * within the outer. This is a pattern that occurs more places in
  173. * Highcharts, perhaps it should be elevated to a common utility function.
  174. *
  175. * @ignore
  176. * @function Highcharts.Chart#fitToBox
  177. *
  178. * @param {Highcharts.BBoxObject} inner
  179. *
  180. * @param {Highcharts.BBoxObject} outer
  181. *
  182. * @return {Highcharts.BBoxObject}
  183. * The inner box
  184. */
  185. fitToBox: function (inner, outer) {
  186. [['x', 'width'], ['y', 'height']].forEach(function (dim) {
  187. var pos = dim[0], size = dim[1];
  188. if (inner[pos] + inner[size] >
  189. outer[pos] + outer[size]) { // right
  190. // the general size is greater, fit fully to outer
  191. if (inner[size] > outer[size]) {
  192. inner[size] = outer[size];
  193. inner[pos] = outer[pos];
  194. }
  195. else { // align right
  196. inner[pos] = outer[pos] +
  197. outer[size] - inner[size];
  198. }
  199. }
  200. if (inner[size] > outer[size]) {
  201. inner[size] = outer[size];
  202. }
  203. if (inner[pos] < outer[pos]) {
  204. inner[pos] = outer[pos];
  205. }
  206. });
  207. return inner;
  208. },
  209. /**
  210. * Highmaps only. Zoom in or out of the map. See also {@link Point#zoomTo}.
  211. * See {@link Chart#fromLatLonToPoint} for how to get the `centerX` and
  212. * `centerY` parameters for a geographic location.
  213. *
  214. * @function Highcharts.Chart#mapZoom
  215. *
  216. * @param {number} [howMuch]
  217. * How much to zoom the map. Values less than 1 zooms in. 0.5 zooms
  218. * in to half the current view. 2 zooms to twice the current view. If
  219. * omitted, the zoom is reset.
  220. *
  221. * @param {number} [centerX]
  222. * The X axis position to center around if available space.
  223. *
  224. * @param {number} [centerY]
  225. * The Y axis position to center around if available space.
  226. *
  227. * @param {number} [mouseX]
  228. * Fix the zoom to this position if possible. This is used for
  229. * example in mousewheel events, where the area under the mouse
  230. * should be fixed as we zoom in.
  231. *
  232. * @param {number} [mouseY]
  233. * Fix the zoom to this position if possible.
  234. *
  235. * @return {void}
  236. */
  237. mapZoom: function (howMuch, centerXArg, centerYArg, mouseX, mouseY) {
  238. var chart = this, xAxis = chart.xAxis[0], xRange = xAxis.max - xAxis.min, centerX = pick(centerXArg, xAxis.min + xRange / 2), newXRange = xRange * howMuch, yAxis = chart.yAxis[0], yRange = yAxis.max - yAxis.min, centerY = pick(centerYArg, yAxis.min + yRange / 2), newYRange = yRange * howMuch, fixToX = mouseX ? ((mouseX - xAxis.pos) / xAxis.len) : 0.5, fixToY = mouseY ? ((mouseY - yAxis.pos) / yAxis.len) : 0.5, newXMin = centerX - newXRange * fixToX, newYMin = centerY - newYRange * fixToY, newExt = chart.fitToBox({
  239. x: newXMin,
  240. y: newYMin,
  241. width: newXRange,
  242. height: newYRange
  243. }, {
  244. x: xAxis.dataMin,
  245. y: yAxis.dataMin,
  246. width: xAxis.dataMax - xAxis.dataMin,
  247. height: yAxis.dataMax - yAxis.dataMin
  248. }), zoomOut = (newExt.x <= xAxis.dataMin &&
  249. newExt.width >=
  250. xAxis.dataMax - xAxis.dataMin &&
  251. newExt.y <= yAxis.dataMin &&
  252. newExt.height >= yAxis.dataMax - yAxis.dataMin);
  253. // When mousewheel zooming, fix the point under the mouse
  254. if (mouseX && xAxis.mapAxis) {
  255. xAxis.mapAxis.fixTo = [mouseX - xAxis.pos, centerXArg];
  256. }
  257. if (mouseY && yAxis.mapAxis) {
  258. yAxis.mapAxis.fixTo = [mouseY - yAxis.pos, centerYArg];
  259. }
  260. // Zoom
  261. if (typeof howMuch !== 'undefined' && !zoomOut) {
  262. xAxis.setExtremes(newExt.x, newExt.x + newExt.width, false);
  263. yAxis.setExtremes(newExt.y, newExt.y + newExt.height, false);
  264. // Reset zoom
  265. }
  266. else {
  267. xAxis.setExtremes(void 0, void 0, false);
  268. yAxis.setExtremes(void 0, void 0, false);
  269. }
  270. // Prevent zooming until this one is finished animating
  271. /*
  272. chart.holdMapZoom = true;
  273. setTimeout(function () {
  274. chart.holdMapZoom = false;
  275. }, 200);
  276. */
  277. /*
  278. delay = animation ? animation.duration || 500 : 0;
  279. if (delay) {
  280. chart.isMapZooming = true;
  281. setTimeout(function () {
  282. chart.isMapZooming = false;
  283. if (chart.mapZoomQueue) {
  284. chart.mapZoom.apply(chart, chart.mapZoomQueue);
  285. }
  286. chart.mapZoomQueue = null;
  287. }, delay);
  288. }
  289. */
  290. chart.redraw();
  291. }
  292. });
  293. // Extend the Chart.render method to add zooming and panning
  294. addEvent(Chart, 'beforeRender', function () {
  295. // Render the plus and minus buttons. Doing this before the shapes makes
  296. // getBBox much quicker, at least in Chrome.
  297. this.mapNavigation = new MapNavigation(this);
  298. this.mapNavigation.update();
  299. });
  300. H.MapNavigation = MapNavigation;