MapNavigation.js 12 KB

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