MenuComponent.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* *
  2. *
  3. * (c) 2009-2021 Øystein Moseng
  4. *
  5. * Accessibility component for exporting menu.
  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 H from '../../Core/Globals.js';
  14. import U from '../../Core/Utilities.js';
  15. var extend = U.extend;
  16. import AccessibilityComponent from '../AccessibilityComponent.js';
  17. import KeyboardNavigationHandler from '../KeyboardNavigationHandler.js';
  18. import ChartUtilities from '../Utils/ChartUtilities.js';
  19. var unhideChartElementFromAT = ChartUtilities.unhideChartElementFromAT;
  20. import HTMLUtilities from '../Utils/HTMLUtilities.js';
  21. var removeElement = HTMLUtilities.removeElement, getFakeMouseEvent = HTMLUtilities.getFakeMouseEvent;
  22. /* eslint-disable no-invalid-this, valid-jsdoc */
  23. /**
  24. * Get the wrapped export button element of a chart.
  25. *
  26. * @private
  27. * @param {Highcharts.Chart} chart
  28. * @returns {Highcharts.SVGElement}
  29. */
  30. function getExportMenuButtonElement(chart) {
  31. return chart.exportSVGElements && chart.exportSVGElements[0];
  32. }
  33. /**
  34. * Show the export menu and focus the first item (if exists).
  35. *
  36. * @private
  37. * @function Highcharts.Chart#showExportMenu
  38. */
  39. H.Chart.prototype.showExportMenu = function () {
  40. var exportButton = getExportMenuButtonElement(this);
  41. if (exportButton) {
  42. var el = exportButton.element;
  43. if (el.onclick) {
  44. el.onclick(getFakeMouseEvent('click'));
  45. }
  46. }
  47. };
  48. /**
  49. * @private
  50. * @function Highcharts.Chart#hideExportMenu
  51. */
  52. H.Chart.prototype.hideExportMenu = function () {
  53. var chart = this, exportList = chart.exportDivElements;
  54. if (exportList && chart.exportContextMenu) {
  55. // Reset hover states etc.
  56. exportList.forEach(function (el) {
  57. if (el.className === 'highcharts-menu-item' && el.onmouseout) {
  58. el.onmouseout(getFakeMouseEvent('mouseout'));
  59. }
  60. });
  61. chart.highlightedExportItemIx = 0;
  62. // Hide the menu div
  63. chart.exportContextMenu.hideMenu();
  64. // Make sure the chart has focus and can capture keyboard events
  65. chart.container.focus();
  66. }
  67. };
  68. /**
  69. * Highlight export menu item by index.
  70. *
  71. * @private
  72. * @function Highcharts.Chart#highlightExportItem
  73. *
  74. * @param {number} ix
  75. *
  76. * @return {boolean}
  77. */
  78. H.Chart.prototype.highlightExportItem = function (ix) {
  79. var listItem = this.exportDivElements && this.exportDivElements[ix], curHighlighted = this.exportDivElements &&
  80. this.exportDivElements[this.highlightedExportItemIx], hasSVGFocusSupport;
  81. if (listItem &&
  82. listItem.tagName === 'LI' &&
  83. !(listItem.children && listItem.children.length)) {
  84. // Test if we have focus support for SVG elements
  85. hasSVGFocusSupport = !!(this.renderTo.getElementsByTagName('g')[0] || {}).focus;
  86. // Only focus if we can set focus back to the elements after
  87. // destroying the menu (#7422)
  88. if (listItem.focus && hasSVGFocusSupport) {
  89. listItem.focus();
  90. }
  91. if (curHighlighted && curHighlighted.onmouseout) {
  92. curHighlighted.onmouseout(getFakeMouseEvent('mouseout'));
  93. }
  94. if (listItem.onmouseover) {
  95. listItem.onmouseover(getFakeMouseEvent('mouseover'));
  96. }
  97. this.highlightedExportItemIx = ix;
  98. return true;
  99. }
  100. return false;
  101. };
  102. /**
  103. * Try to highlight the last valid export menu item.
  104. *
  105. * @private
  106. * @function Highcharts.Chart#highlightLastExportItem
  107. * @return {boolean}
  108. */
  109. H.Chart.prototype.highlightLastExportItem = function () {
  110. var chart = this, i;
  111. if (chart.exportDivElements) {
  112. i = chart.exportDivElements.length;
  113. while (i--) {
  114. if (chart.highlightExportItem(i)) {
  115. return true;
  116. }
  117. }
  118. }
  119. return false;
  120. };
  121. /**
  122. * @private
  123. * @param {Highcharts.Chart} chart
  124. */
  125. function exportingShouldHaveA11y(chart) {
  126. var exportingOpts = chart.options.exporting, exportButton = getExportMenuButtonElement(chart);
  127. return !!(exportingOpts &&
  128. exportingOpts.enabled !== false &&
  129. exportingOpts.accessibility &&
  130. exportingOpts.accessibility.enabled &&
  131. exportButton &&
  132. exportButton.element);
  133. }
  134. /**
  135. * The MenuComponent class
  136. *
  137. * @private
  138. * @class
  139. * @name Highcharts.MenuComponent
  140. */
  141. var MenuComponent = function () { };
  142. MenuComponent.prototype = new AccessibilityComponent();
  143. extend(MenuComponent.prototype, /** @lends Highcharts.MenuComponent */ {
  144. /**
  145. * Init the component
  146. */
  147. init: function () {
  148. var chart = this.chart, component = this;
  149. this.addEvent(chart, 'exportMenuShown', function () {
  150. component.onMenuShown();
  151. });
  152. this.addEvent(chart, 'exportMenuHidden', function () {
  153. component.onMenuHidden();
  154. });
  155. },
  156. /**
  157. * @private
  158. */
  159. onMenuHidden: function () {
  160. var menu = this.chart.exportContextMenu;
  161. if (menu) {
  162. menu.setAttribute('aria-hidden', 'true');
  163. }
  164. this.isExportMenuShown = false;
  165. this.setExportButtonExpandedState('false');
  166. },
  167. /**
  168. * @private
  169. */
  170. onMenuShown: function () {
  171. var chart = this.chart, menu = chart.exportContextMenu;
  172. if (menu) {
  173. this.addAccessibleContextMenuAttribs();
  174. unhideChartElementFromAT(chart, menu);
  175. }
  176. this.isExportMenuShown = true;
  177. this.setExportButtonExpandedState('true');
  178. },
  179. /**
  180. * @private
  181. * @param {string} stateStr
  182. */
  183. setExportButtonExpandedState: function (stateStr) {
  184. var button = this.exportButtonProxy;
  185. if (button) {
  186. button.setAttribute('aria-expanded', stateStr);
  187. }
  188. },
  189. /**
  190. * Called on each render of the chart. We need to update positioning of the
  191. * proxy overlay.
  192. */
  193. onChartRender: function () {
  194. var chart = this.chart, a11yOptions = chart.options.accessibility;
  195. // Always start with a clean slate
  196. removeElement(this.exportProxyGroup);
  197. // Set screen reader properties on export menu
  198. if (exportingShouldHaveA11y(chart)) {
  199. // Proxy button and group
  200. this.exportProxyGroup = this.addProxyGroup(
  201. // Wrap in a region div if verbosity is high
  202. a11yOptions.landmarkVerbosity === 'all' ? {
  203. 'aria-label': chart.langFormat('accessibility.exporting.exportRegionLabel', { chart: chart }),
  204. 'role': 'region'
  205. } : {});
  206. var button = getExportMenuButtonElement(this.chart);
  207. this.exportButtonProxy = this.createProxyButton(button, this.exportProxyGroup, {
  208. 'aria-label': chart.langFormat('accessibility.exporting.menuButtonLabel', { chart: chart }),
  209. 'aria-expanded': 'false'
  210. });
  211. }
  212. },
  213. /**
  214. * @private
  215. */
  216. addAccessibleContextMenuAttribs: function () {
  217. var chart = this.chart, exportList = chart.exportDivElements;
  218. if (exportList && exportList.length) {
  219. // Set tabindex on the menu items to allow focusing by script
  220. // Set role to give screen readers a chance to pick up the contents
  221. exportList.forEach(function (item) {
  222. if (item.tagName === 'LI' &&
  223. !(item.children && item.children.length)) {
  224. item.setAttribute('tabindex', -1);
  225. }
  226. else {
  227. item.setAttribute('aria-hidden', 'true');
  228. }
  229. });
  230. // Set accessibility properties on parent div
  231. var parentDiv = exportList[0].parentNode;
  232. parentDiv.removeAttribute('aria-hidden');
  233. parentDiv.setAttribute('aria-label', chart.langFormat('accessibility.exporting.chartMenuLabel', { chart: chart }));
  234. }
  235. },
  236. /**
  237. * Get keyboard navigation handler for this component.
  238. * @return {Highcharts.KeyboardNavigationHandler}
  239. */
  240. getKeyboardNavigation: function () {
  241. var keys = this.keyCodes, chart = this.chart, component = this;
  242. return new KeyboardNavigationHandler(chart, {
  243. keyCodeMap: [
  244. // Arrow prev handler
  245. [
  246. [keys.left, keys.up],
  247. function () {
  248. return component.onKbdPrevious(this);
  249. }
  250. ],
  251. // Arrow next handler
  252. [
  253. [keys.right, keys.down],
  254. function () {
  255. return component.onKbdNext(this);
  256. }
  257. ],
  258. // Click handler
  259. [
  260. [keys.enter, keys.space],
  261. function () {
  262. return component.onKbdClick(this);
  263. }
  264. ]
  265. ],
  266. // Only run exporting navigation if exporting support exists and is
  267. // enabled on chart
  268. validate: function () {
  269. return chart.exportChart &&
  270. chart.options.exporting.enabled !== false &&
  271. chart.options.exporting.accessibility.enabled !==
  272. false;
  273. },
  274. // Focus export menu button
  275. init: function () {
  276. var exportBtn = component.exportButtonProxy, exportGroup = chart.exportingGroup;
  277. if (exportGroup && exportBtn) {
  278. chart.setFocusToElement(exportGroup, exportBtn);
  279. }
  280. },
  281. // Hide the menu
  282. terminate: function () {
  283. chart.hideExportMenu();
  284. }
  285. });
  286. },
  287. /**
  288. * @private
  289. * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
  290. * @return {number}
  291. * Response code
  292. */
  293. onKbdPrevious: function (keyboardNavigationHandler) {
  294. var chart = this.chart, a11yOptions = chart.options.accessibility, response = keyboardNavigationHandler.response, i = chart.highlightedExportItemIx || 0;
  295. // Try to highlight prev item in list. Highlighting e.g.
  296. // separators will fail.
  297. while (i--) {
  298. if (chart.highlightExportItem(i)) {
  299. return response.success;
  300. }
  301. }
  302. // We failed, so wrap around or move to prev module
  303. if (a11yOptions.keyboardNavigation.wrapAround) {
  304. chart.highlightLastExportItem();
  305. return response.success;
  306. }
  307. return response.prev;
  308. },
  309. /**
  310. * @private
  311. * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
  312. * @return {number}
  313. * Response code
  314. */
  315. onKbdNext: function (keyboardNavigationHandler) {
  316. var chart = this.chart, a11yOptions = chart.options.accessibility, response = keyboardNavigationHandler.response, i = (chart.highlightedExportItemIx || 0) + 1;
  317. // Try to highlight next item in list. Highlighting e.g.
  318. // separators will fail.
  319. for (; i < chart.exportDivElements.length; ++i) {
  320. if (chart.highlightExportItem(i)) {
  321. return response.success;
  322. }
  323. }
  324. // We failed, so wrap around or move to next module
  325. if (a11yOptions.keyboardNavigation.wrapAround) {
  326. chart.highlightExportItem(0);
  327. return response.success;
  328. }
  329. return response.next;
  330. },
  331. /**
  332. * @private
  333. * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
  334. * @return {number}
  335. * Response code
  336. */
  337. onKbdClick: function (keyboardNavigationHandler) {
  338. var chart = this.chart, curHighlightedItem = chart.exportDivElements[chart.highlightedExportItemIx], exportButtonElement = getExportMenuButtonElement(chart).element;
  339. if (this.isExportMenuShown) {
  340. this.fakeClickEvent(curHighlightedItem);
  341. }
  342. else {
  343. this.fakeClickEvent(exportButtonElement);
  344. chart.highlightExportItem(0);
  345. }
  346. return keyboardNavigationHandler.response.success;
  347. }
  348. });
  349. export default MenuComponent;