ZoomComponent.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* *
  2. *
  3. * (c) 2009-2021 Øystein Moseng
  4. *
  5. * Accessibility component for chart zoom.
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. 'use strict';
  13. import AccessibilityComponent from '../AccessibilityComponent.js';
  14. import ChartUtilities from '../Utils/ChartUtilities.js';
  15. var unhideChartElementFromAT = ChartUtilities.unhideChartElementFromAT;
  16. import H from '../../Core/Globals.js';
  17. var noop = H.noop;
  18. import HTMLUtilities from '../Utils/HTMLUtilities.js';
  19. var removeElement = HTMLUtilities.removeElement, setElAttrs = HTMLUtilities.setElAttrs;
  20. import KeyboardNavigationHandler from '../KeyboardNavigationHandler.js';
  21. import U from '../../Core/Utilities.js';
  22. var extend = U.extend, pick = U.pick;
  23. /* eslint-disable no-invalid-this, valid-jsdoc */
  24. /**
  25. * @private
  26. */
  27. function chartHasMapZoom(chart) {
  28. return !!(chart.mapZoom &&
  29. chart.mapNavButtons &&
  30. chart.mapNavButtons.length);
  31. }
  32. /**
  33. * Pan along axis in a direction (1 or -1), optionally with a defined
  34. * granularity (number of steps it takes to walk across current view)
  35. *
  36. * @private
  37. * @function Highcharts.Axis#panStep
  38. *
  39. * @param {number} direction
  40. * @param {number} [granularity]
  41. */
  42. H.Axis.prototype.panStep = function (direction, granularity) {
  43. var gran = granularity || 3, extremes = this.getExtremes(), step = (extremes.max - extremes.min) / gran * direction, newMax = extremes.max + step, newMin = extremes.min + step, size = newMax - newMin;
  44. if (direction < 0 && newMin < extremes.dataMin) {
  45. newMin = extremes.dataMin;
  46. newMax = newMin + size;
  47. }
  48. else if (direction > 0 && newMax > extremes.dataMax) {
  49. newMax = extremes.dataMax;
  50. newMin = newMax - size;
  51. }
  52. this.setExtremes(newMin, newMax);
  53. };
  54. /**
  55. * The ZoomComponent class
  56. *
  57. * @private
  58. * @class
  59. * @name Highcharts.ZoomComponent
  60. */
  61. var ZoomComponent = noop;
  62. ZoomComponent.prototype = new AccessibilityComponent();
  63. extend(ZoomComponent.prototype, /** @lends Highcharts.ZoomComponent */ {
  64. /**
  65. * Initialize the component
  66. */
  67. init: function () {
  68. var component = this, chart = this.chart;
  69. [
  70. 'afterShowResetZoom', 'afterDrilldown', 'drillupall'
  71. ].forEach(function (eventType) {
  72. component.addEvent(chart, eventType, function () {
  73. component.updateProxyOverlays();
  74. });
  75. });
  76. },
  77. /**
  78. * Called when chart is updated
  79. */
  80. onChartUpdate: function () {
  81. var chart = this.chart, component = this;
  82. // Make map zoom buttons accessible
  83. if (chart.mapNavButtons) {
  84. chart.mapNavButtons.forEach(function (button, i) {
  85. unhideChartElementFromAT(chart, button.element);
  86. component.setMapNavButtonAttrs(button.element, 'accessibility.zoom.mapZoom' + (i ? 'Out' : 'In'));
  87. });
  88. }
  89. },
  90. /**
  91. * @private
  92. * @param {Highcharts.HTMLDOMElement|Highcharts.SVGDOMElement} button
  93. * @param {string} labelFormatKey
  94. */
  95. setMapNavButtonAttrs: function (button, labelFormatKey) {
  96. var chart = this.chart, label = chart.langFormat(labelFormatKey, { chart: chart });
  97. setElAttrs(button, {
  98. tabindex: -1,
  99. role: 'button',
  100. 'aria-label': label
  101. });
  102. },
  103. /**
  104. * Update the proxy overlays on every new render to ensure positions are
  105. * correct.
  106. */
  107. onChartRender: function () {
  108. this.updateProxyOverlays();
  109. },
  110. /**
  111. * Update proxy overlays, recreating the buttons.
  112. */
  113. updateProxyOverlays: function () {
  114. var chart = this.chart;
  115. // Always start with a clean slate
  116. removeElement(this.drillUpProxyGroup);
  117. removeElement(this.resetZoomProxyGroup);
  118. if (chart.resetZoomButton) {
  119. this.recreateProxyButtonAndGroup(chart.resetZoomButton, 'resetZoomProxyButton', 'resetZoomProxyGroup', chart.langFormat('accessibility.zoom.resetZoomButton', { chart: chart }));
  120. }
  121. if (chart.drillUpButton) {
  122. this.recreateProxyButtonAndGroup(chart.drillUpButton, 'drillUpProxyButton', 'drillUpProxyGroup', chart.langFormat('accessibility.drillUpButton', {
  123. chart: chart,
  124. buttonText: chart.getDrilldownBackText()
  125. }));
  126. }
  127. },
  128. /**
  129. * @private
  130. * @param {Highcharts.SVGElement} buttonEl
  131. * @param {string} buttonProp
  132. * @param {string} groupProp
  133. * @param {string} label
  134. */
  135. recreateProxyButtonAndGroup: function (buttonEl, buttonProp, groupProp, label) {
  136. removeElement(this[groupProp]);
  137. this[groupProp] = this.addProxyGroup();
  138. this[buttonProp] = this.createProxyButton(buttonEl, this[groupProp], { 'aria-label': label, tabindex: -1 });
  139. },
  140. /**
  141. * Get keyboard navigation handler for map zoom.
  142. * @private
  143. * @return {Highcharts.KeyboardNavigationHandler} The module object
  144. */
  145. getMapZoomNavigation: function () {
  146. var keys = this.keyCodes, chart = this.chart, component = this;
  147. return new KeyboardNavigationHandler(chart, {
  148. keyCodeMap: [
  149. [
  150. [keys.up, keys.down, keys.left, keys.right],
  151. function (keyCode) {
  152. return component.onMapKbdArrow(this, keyCode);
  153. }
  154. ],
  155. [
  156. [keys.tab],
  157. function (_keyCode, e) {
  158. return component.onMapKbdTab(this, e);
  159. }
  160. ],
  161. [
  162. [keys.space, keys.enter],
  163. function () {
  164. return component.onMapKbdClick(this);
  165. }
  166. ]
  167. ],
  168. validate: function () {
  169. return chartHasMapZoom(chart);
  170. },
  171. init: function (direction) {
  172. return component.onMapNavInit(direction);
  173. }
  174. });
  175. },
  176. /**
  177. * @private
  178. * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
  179. * @param {number} keyCode
  180. * @return {number} Response code
  181. */
  182. onMapKbdArrow: function (keyboardNavigationHandler, keyCode) {
  183. var keys = this.keyCodes, panAxis = (keyCode === keys.up || keyCode === keys.down) ?
  184. 'yAxis' : 'xAxis', stepDirection = (keyCode === keys.left || keyCode === keys.up) ?
  185. -1 : 1;
  186. this.chart[panAxis][0].panStep(stepDirection);
  187. return keyboardNavigationHandler.response.success;
  188. },
  189. /**
  190. * @private
  191. * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
  192. * @param {global.KeyboardEvent} event
  193. * @return {number} Response code
  194. */
  195. onMapKbdTab: function (keyboardNavigationHandler, event) {
  196. var button, chart = this.chart, response = keyboardNavigationHandler.response, isBackwards = event.shiftKey, isMoveOutOfRange = isBackwards && !this.focusedMapNavButtonIx ||
  197. !isBackwards && this.focusedMapNavButtonIx;
  198. // Deselect old
  199. chart.mapNavButtons[this.focusedMapNavButtonIx].setState(0);
  200. if (isMoveOutOfRange) {
  201. chart.mapZoom(); // Reset zoom
  202. return response[isBackwards ? 'prev' : 'next'];
  203. }
  204. // Select other button
  205. this.focusedMapNavButtonIx += isBackwards ? -1 : 1;
  206. button = chart.mapNavButtons[this.focusedMapNavButtonIx];
  207. chart.setFocusToElement(button.box, button.element);
  208. button.setState(2);
  209. return response.success;
  210. },
  211. /**
  212. * @private
  213. * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
  214. * @return {number} Response code
  215. */
  216. onMapKbdClick: function (keyboardNavigationHandler) {
  217. this.fakeClickEvent(this.chart.mapNavButtons[this.focusedMapNavButtonIx]
  218. .element);
  219. return keyboardNavigationHandler.response.success;
  220. },
  221. /**
  222. * @private
  223. * @param {number} direction
  224. */
  225. onMapNavInit: function (direction) {
  226. var chart = this.chart, zoomIn = chart.mapNavButtons[0], zoomOut = chart.mapNavButtons[1], initialButton = direction > 0 ? zoomIn : zoomOut;
  227. chart.setFocusToElement(initialButton.box, initialButton.element);
  228. initialButton.setState(2);
  229. this.focusedMapNavButtonIx = direction > 0 ? 0 : 1;
  230. },
  231. /**
  232. * Get keyboard navigation handler for a simple chart button. Provide the
  233. * button reference for the chart, and a function to call on click.
  234. *
  235. * @private
  236. * @param {string} buttonProp The property on chart referencing the button.
  237. * @return {Highcharts.KeyboardNavigationHandler} The module object
  238. */
  239. simpleButtonNavigation: function (buttonProp, proxyProp, onClick) {
  240. var keys = this.keyCodes, component = this, chart = this.chart;
  241. return new KeyboardNavigationHandler(chart, {
  242. keyCodeMap: [
  243. [
  244. [keys.tab, keys.up, keys.down, keys.left, keys.right],
  245. function (keyCode, e) {
  246. var isBackwards = keyCode === keys.tab && e.shiftKey ||
  247. keyCode === keys.left || keyCode === keys.up;
  248. // Arrow/tab => just move
  249. return this.response[isBackwards ? 'prev' : 'next'];
  250. }
  251. ],
  252. [
  253. [keys.space, keys.enter],
  254. function () {
  255. var res = onClick(this, chart);
  256. return pick(res, this.response.success);
  257. }
  258. ]
  259. ],
  260. validate: function () {
  261. var hasButton = (chart[buttonProp] &&
  262. chart[buttonProp].box &&
  263. component[proxyProp]);
  264. return hasButton;
  265. },
  266. init: function () {
  267. chart.setFocusToElement(chart[buttonProp].box, component[proxyProp]);
  268. }
  269. });
  270. },
  271. /**
  272. * Get keyboard navigation handlers for this component.
  273. * @return {Array<Highcharts.KeyboardNavigationHandler>}
  274. * List of module objects
  275. */
  276. getKeyboardNavigation: function () {
  277. return [
  278. this.simpleButtonNavigation('resetZoomButton', 'resetZoomProxyButton', function (_handler, chart) {
  279. chart.zoomOut();
  280. }),
  281. this.simpleButtonNavigation('drillUpButton', 'drillUpProxyButton', function (handler, chart) {
  282. chart.drillUp();
  283. return handler.response.prev;
  284. }),
  285. this.getMapZoomNavigation()
  286. ];
  287. }
  288. });
  289. export default ZoomComponent;