Accessibility.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* *
  2. *
  3. * (c) 2009-2020 Øystein Moseng
  4. *
  5. * Accessibility module for Highcharts
  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 ChartUtilities from './Utils/ChartUtilities.js';
  14. import H from '../Core/Globals.js';
  15. var doc = H.doc;
  16. import KeyboardNavigationHandler from './KeyboardNavigationHandler.js';
  17. import CartesianSeries from '../Core/Series/CartesianSeries.js';
  18. import O from '../Core/Options.js';
  19. var defaultOptions = O.defaultOptions;
  20. import Point from '../Core/Series/Point.js';
  21. import U from '../Core/Utilities.js';
  22. var addEvent = U.addEvent, extend = U.extend, fireEvent = U.fireEvent, merge = U.merge;
  23. import AccessibilityComponent from './AccessibilityComponent.js';
  24. import KeyboardNavigation from './KeyboardNavigation.js';
  25. import LegendComponent from './Components/LegendComponent.js';
  26. import MenuComponent from './Components/MenuComponent.js';
  27. import SeriesComponent from './Components/SeriesComponent/SeriesComponent.js';
  28. import ZoomComponent from './Components/ZoomComponent.js';
  29. import RangeSelectorComponent from './Components/RangeSelectorComponent.js';
  30. import InfoRegionsComponent from './Components/InfoRegionsComponent.js';
  31. import ContainerComponent from './Components/ContainerComponent.js';
  32. import whcm from './HighContrastMode.js';
  33. import highContrastTheme from './HighContrastTheme.js';
  34. import defaultOptionsA11Y from './Options/Options.js';
  35. import defaultLangOptions from './Options/LangOptions.js';
  36. import copyDeprecatedOptions from './Options/DeprecatedOptions.js';
  37. import './A11yI18n.js';
  38. import './FocusBorder.js';
  39. // Add default options
  40. merge(true, defaultOptions, defaultOptionsA11Y, {
  41. accessibility: {
  42. highContrastTheme: highContrastTheme
  43. },
  44. lang: defaultLangOptions
  45. });
  46. // Expose functionality on Highcharts namespace
  47. H.A11yChartUtilities = ChartUtilities;
  48. H.KeyboardNavigationHandler = KeyboardNavigationHandler;
  49. H.AccessibilityComponent = AccessibilityComponent;
  50. /* eslint-disable no-invalid-this, valid-jsdoc */
  51. /**
  52. * The Accessibility class
  53. *
  54. * @private
  55. * @requires module:modules/accessibility
  56. *
  57. * @class
  58. * @name Highcharts.Accessibility
  59. *
  60. * @param {Highcharts.Chart} chart
  61. * Chart object
  62. */
  63. function Accessibility(chart) {
  64. this.init(chart);
  65. }
  66. Accessibility.prototype = {
  67. /**
  68. * Initialize the accessibility class
  69. * @private
  70. * @param {Highcharts.Chart} chart
  71. * Chart object
  72. */
  73. init: function (chart) {
  74. this.chart = chart;
  75. // Abort on old browsers
  76. if (!doc.addEventListener || !chart.renderer.isSVG) {
  77. chart.renderTo.setAttribute('aria-hidden', true);
  78. return;
  79. }
  80. // Copy over any deprecated options that are used. We could do this on
  81. // every update, but it is probably not needed.
  82. copyDeprecatedOptions(chart);
  83. this.initComponents();
  84. this.keyboardNavigation = new KeyboardNavigation(chart, this.components);
  85. this.update();
  86. },
  87. /**
  88. * @private
  89. */
  90. initComponents: function () {
  91. var chart = this.chart, a11yOptions = chart.options.accessibility;
  92. this.components = {
  93. container: new ContainerComponent(),
  94. infoRegions: new InfoRegionsComponent(),
  95. legend: new LegendComponent(),
  96. chartMenu: new MenuComponent(),
  97. rangeSelector: new RangeSelectorComponent(),
  98. series: new SeriesComponent(),
  99. zoom: new ZoomComponent()
  100. };
  101. if (a11yOptions.customComponents) {
  102. extend(this.components, a11yOptions.customComponents);
  103. }
  104. var components = this.components;
  105. this.getComponentOrder().forEach(function (componentName) {
  106. components[componentName].initBase(chart);
  107. components[componentName].init();
  108. });
  109. },
  110. /**
  111. * Get order to update components in.
  112. * @private
  113. */
  114. getComponentOrder: function () {
  115. if (!this.components) {
  116. return []; // For zombie accessibility object on old browsers
  117. }
  118. if (!this.components.series) {
  119. return Object.keys(this.components);
  120. }
  121. var componentsExceptSeries = Object.keys(this.components)
  122. .filter(function (c) { return c !== 'series'; });
  123. // Update series first, so that other components can read accessibility
  124. // info on points.
  125. return ['series'].concat(componentsExceptSeries);
  126. },
  127. /**
  128. * Update all components.
  129. */
  130. update: function () {
  131. var components = this.components, chart = this.chart, a11yOptions = chart.options.accessibility;
  132. fireEvent(chart, 'beforeA11yUpdate');
  133. // Update the chart type list as this is used by multiple modules
  134. chart.types = this.getChartTypes();
  135. // Update markup
  136. this.getComponentOrder().forEach(function (componentName) {
  137. components[componentName].onChartUpdate();
  138. fireEvent(chart, 'afterA11yComponentUpdate', {
  139. name: componentName,
  140. component: components[componentName]
  141. });
  142. });
  143. // Update keyboard navigation
  144. this.keyboardNavigation.update(a11yOptions.keyboardNavigation.order);
  145. // Handle high contrast mode
  146. if (!chart.highContrastModeActive && // Only do this once
  147. whcm.isHighContrastModeActive()) {
  148. whcm.setHighContrastTheme(chart);
  149. }
  150. fireEvent(chart, 'afterA11yUpdate', {
  151. accessibility: this
  152. });
  153. },
  154. /**
  155. * Destroy all elements.
  156. */
  157. destroy: function () {
  158. var chart = this.chart || {};
  159. // Destroy components
  160. var components = this.components;
  161. Object.keys(components).forEach(function (componentName) {
  162. components[componentName].destroy();
  163. components[componentName].destroyBase();
  164. });
  165. // Kill keyboard nav
  166. if (this.keyboardNavigation) {
  167. this.keyboardNavigation.destroy();
  168. }
  169. // Hide container from screen readers if it exists
  170. if (chart.renderTo) {
  171. chart.renderTo.setAttribute('aria-hidden', true);
  172. }
  173. // Remove focus border if it exists
  174. if (chart.focusElement) {
  175. chart.focusElement.removeFocusBorder();
  176. }
  177. },
  178. /**
  179. * Return a list of the types of series we have in the chart.
  180. * @private
  181. */
  182. getChartTypes: function () {
  183. var types = {};
  184. this.chart.series.forEach(function (series) {
  185. types[series.type] = 1;
  186. });
  187. return Object.keys(types);
  188. }
  189. };
  190. /**
  191. * @private
  192. */
  193. H.Chart.prototype.updateA11yEnabled = function () {
  194. var a11y = this.accessibility, accessibilityOptions = this.options.accessibility;
  195. if (accessibilityOptions && accessibilityOptions.enabled) {
  196. if (a11y) {
  197. a11y.update();
  198. }
  199. else {
  200. this.accessibility = a11y = new Accessibility(this);
  201. }
  202. }
  203. else if (a11y) {
  204. // Destroy if after update we have a11y and it is disabled
  205. if (a11y.destroy) {
  206. a11y.destroy();
  207. }
  208. delete this.accessibility;
  209. }
  210. else {
  211. // Just hide container
  212. this.renderTo.setAttribute('aria-hidden', true);
  213. }
  214. };
  215. // Handle updates to the module and send render updates to components
  216. addEvent(H.Chart, 'render', function (e) {
  217. // Update/destroy
  218. if (this.a11yDirty && this.renderTo) {
  219. delete this.a11yDirty;
  220. this.updateA11yEnabled();
  221. }
  222. var a11y = this.accessibility;
  223. if (a11y) {
  224. a11y.getComponentOrder().forEach(function (componentName) {
  225. a11y.components[componentName].onChartRender();
  226. });
  227. }
  228. });
  229. // Update with chart/series/point updates
  230. addEvent(H.Chart, 'update', function (e) {
  231. // Merge new options
  232. var newOptions = e.options.accessibility;
  233. if (newOptions) {
  234. // Handle custom component updating specifically
  235. if (newOptions.customComponents) {
  236. this.options.accessibility.customComponents =
  237. newOptions.customComponents;
  238. delete newOptions.customComponents;
  239. }
  240. merge(true, this.options.accessibility, newOptions);
  241. // Recreate from scratch
  242. if (this.accessibility && this.accessibility.destroy) {
  243. this.accessibility.destroy();
  244. delete this.accessibility;
  245. }
  246. }
  247. // Mark dirty for update
  248. this.a11yDirty = true;
  249. });
  250. // Mark dirty for update
  251. addEvent(Point, 'update', function () {
  252. if (this.series.chart.accessibility) {
  253. this.series.chart.a11yDirty = true;
  254. }
  255. });
  256. ['addSeries', 'init'].forEach(function (event) {
  257. addEvent(H.Chart, event, function () {
  258. this.a11yDirty = true;
  259. });
  260. });
  261. ['update', 'updatedData', 'remove'].forEach(function (event) {
  262. addEvent(CartesianSeries, event, function () {
  263. if (this.chart.accessibility) {
  264. this.chart.a11yDirty = true;
  265. }
  266. });
  267. });
  268. // Direct updates (events happen after render)
  269. [
  270. 'afterDrilldown', 'drillupall'
  271. ].forEach(function (event) {
  272. addEvent(H.Chart, event, function () {
  273. if (this.accessibility) {
  274. this.accessibility.update();
  275. }
  276. });
  277. });
  278. // Destroy with chart
  279. addEvent(H.Chart, 'destroy', function () {
  280. if (this.accessibility) {
  281. this.accessibility.destroy();
  282. }
  283. });