AccessibilityComponent.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* *
  2. *
  3. * (c) 2009-2020 Øystein Moseng
  4. *
  5. * Accessibility component class definition
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. import ChartUtilities from './Utils/ChartUtilities.js';
  13. var unhideChartElementFromAT = ChartUtilities.unhideChartElementFromAT;
  14. import DOMElementProvider from './Utils/DOMElementProvider.js';
  15. import EventProvider from './Utils/EventProvider.js';
  16. import H from '../Core/Globals.js';
  17. var doc = H.doc, win = H.win;
  18. import HTMLUtilities from './Utils/HTMLUtilities.js';
  19. var removeElement = HTMLUtilities.removeElement, getFakeMouseEvent = HTMLUtilities.getFakeMouseEvent;
  20. import U from '../Core/Utilities.js';
  21. var extend = U.extend, fireEvent = U.fireEvent, merge = U.merge;
  22. /* eslint-disable valid-jsdoc */
  23. /** @lends Highcharts.AccessibilityComponent */
  24. var functionsToOverrideByDerivedClasses = {
  25. /**
  26. * Called on component initialization.
  27. */
  28. init: function () { },
  29. /**
  30. * Get keyboard navigation handler for this component.
  31. * @return {Highcharts.KeyboardNavigationHandler}
  32. */
  33. getKeyboardNavigation: function () { },
  34. /**
  35. * Called on updates to the chart, including options changes.
  36. * Note that this is also called on first render of chart.
  37. */
  38. onChartUpdate: function () { },
  39. /**
  40. * Called on every chart render.
  41. */
  42. onChartRender: function () { },
  43. /**
  44. * Called when accessibility is disabled or chart is destroyed.
  45. */
  46. destroy: function () { }
  47. };
  48. /**
  49. * The AccessibilityComponent base class, representing a part of the chart that
  50. * has accessibility logic connected to it. This class can be inherited from to
  51. * create a custom accessibility component for a chart.
  52. *
  53. * Components should take care to destroy added elements and unregister event
  54. * handlers on destroy. This is handled automatically if using this.addEvent and
  55. * this.createElement.
  56. *
  57. * @sample highcharts/accessibility/custom-component
  58. * Custom accessibility component
  59. *
  60. * @requires module:modules/accessibility
  61. * @class
  62. * @name Highcharts.AccessibilityComponent
  63. */
  64. function AccessibilityComponent() { }
  65. /**
  66. * @lends Highcharts.AccessibilityComponent
  67. */
  68. AccessibilityComponent.prototype = {
  69. /**
  70. * Initialize the class
  71. * @private
  72. * @param {Highcharts.Chart} chart
  73. * Chart object
  74. */
  75. initBase: function (chart) {
  76. this.chart = chart;
  77. this.eventProvider = new EventProvider();
  78. this.domElementProvider = new DOMElementProvider();
  79. // Key code enum for common keys
  80. this.keyCodes = {
  81. left: 37,
  82. right: 39,
  83. up: 38,
  84. down: 40,
  85. enter: 13,
  86. space: 32,
  87. esc: 27,
  88. tab: 9
  89. };
  90. },
  91. /**
  92. * Add an event to an element and keep track of it for later removal.
  93. * See EventProvider for details.
  94. * @private
  95. */
  96. addEvent: function () {
  97. return this.eventProvider.addEvent
  98. .apply(this.eventProvider, arguments);
  99. },
  100. /**
  101. * Create an element and keep track of it for later removal.
  102. * See DOMElementProvider for details.
  103. * @private
  104. */
  105. createElement: function () {
  106. return this.domElementProvider.createElement.apply(this.domElementProvider, arguments);
  107. },
  108. /**
  109. * Fire an event on an element that is either wrapped by Highcharts,
  110. * or a DOM element
  111. * @private
  112. * @param {Highcharts.HTMLElement|Highcharts.HTMLDOMElement|
  113. * Highcharts.SVGDOMElement|Highcharts.SVGElement} el
  114. * @param {Event} eventObject
  115. */
  116. fireEventOnWrappedOrUnwrappedElement: function (el, eventObject) {
  117. var type = eventObject.type;
  118. if (doc.createEvent && (el.dispatchEvent || el.fireEvent)) {
  119. if (el.dispatchEvent) {
  120. el.dispatchEvent(eventObject);
  121. }
  122. else {
  123. el.fireEvent(type, eventObject);
  124. }
  125. }
  126. else {
  127. fireEvent(el, type, eventObject);
  128. }
  129. },
  130. /**
  131. * Utility function to attempt to fake a click event on an element.
  132. * @private
  133. * @param {Highcharts.HTMLDOMElement|Highcharts.SVGDOMElement} element
  134. */
  135. fakeClickEvent: function (element) {
  136. if (element) {
  137. var fakeEventObject = getFakeMouseEvent('click');
  138. this.fireEventOnWrappedOrUnwrappedElement(element, fakeEventObject);
  139. }
  140. },
  141. /**
  142. * Add a new proxy group to the proxy container. Creates the proxy container
  143. * if it does not exist.
  144. * @private
  145. * @param {Highcharts.HTMLAttributes} [attrs]
  146. * The attributes to set on the new group div.
  147. * @return {Highcharts.HTMLDOMElement}
  148. * The new proxy group element.
  149. */
  150. addProxyGroup: function (attrs) {
  151. this.createOrUpdateProxyContainer();
  152. var groupDiv = this.createElement('div');
  153. Object.keys(attrs || {}).forEach(function (prop) {
  154. if (attrs[prop] !== null) {
  155. groupDiv.setAttribute(prop, attrs[prop]);
  156. }
  157. });
  158. this.chart.a11yProxyContainer.appendChild(groupDiv);
  159. return groupDiv;
  160. },
  161. /**
  162. * Creates and updates DOM position of proxy container
  163. * @private
  164. */
  165. createOrUpdateProxyContainer: function () {
  166. var chart = this.chart, rendererSVGEl = chart.renderer.box;
  167. chart.a11yProxyContainer = chart.a11yProxyContainer ||
  168. this.createProxyContainerElement();
  169. if (rendererSVGEl.nextSibling !== chart.a11yProxyContainer) {
  170. chart.container.insertBefore(chart.a11yProxyContainer, rendererSVGEl.nextSibling);
  171. }
  172. },
  173. /**
  174. * @private
  175. * @return {Highcharts.HTMLDOMElement} element
  176. */
  177. createProxyContainerElement: function () {
  178. var pc = doc.createElement('div');
  179. pc.className = 'highcharts-a11y-proxy-container';
  180. return pc;
  181. },
  182. /**
  183. * Create an invisible proxy HTML button in the same position as an SVG
  184. * element
  185. * @private
  186. * @param {Highcharts.SVGElement} svgElement
  187. * The wrapped svg el to proxy.
  188. * @param {Highcharts.HTMLDOMElement} parentGroup
  189. * The proxy group element in the proxy container to add this button to.
  190. * @param {Highcharts.SVGAttributes} [attributes]
  191. * Additional attributes to set.
  192. * @param {Highcharts.SVGElement} [posElement]
  193. * Element to use for positioning instead of svgElement.
  194. * @param {Function} [preClickEvent]
  195. * Function to call before click event fires.
  196. *
  197. * @return {Highcharts.HTMLDOMElement} The proxy button.
  198. */
  199. createProxyButton: function (svgElement, parentGroup, attributes, posElement, preClickEvent) {
  200. var svgEl = svgElement.element, proxy = this.createElement('button'), attrs = merge({
  201. 'aria-label': svgEl.getAttribute('aria-label')
  202. }, attributes);
  203. Object.keys(attrs).forEach(function (prop) {
  204. if (attrs[prop] !== null) {
  205. proxy.setAttribute(prop, attrs[prop]);
  206. }
  207. });
  208. proxy.className = 'highcharts-a11y-proxy-button';
  209. if (preClickEvent) {
  210. this.addEvent(proxy, 'click', preClickEvent);
  211. }
  212. this.setProxyButtonStyle(proxy);
  213. this.updateProxyButtonPosition(proxy, posElement || svgElement);
  214. this.proxyMouseEventsForButton(svgEl, proxy);
  215. // Add to chart div and unhide from screen readers
  216. parentGroup.appendChild(proxy);
  217. if (!attrs['aria-hidden']) {
  218. unhideChartElementFromAT(this.chart, proxy);
  219. }
  220. return proxy;
  221. },
  222. /**
  223. * Get the position relative to chart container for a wrapped SVG element.
  224. * @private
  225. * @param {Highcharts.SVGElement} element
  226. * The element to calculate position for.
  227. * @return {Highcharts.BBoxObject}
  228. * Object with x and y props for the position.
  229. */
  230. getElementPosition: function (element) {
  231. var el = element.element, div = this.chart.renderTo;
  232. if (div && el && el.getBoundingClientRect) {
  233. var rectEl = el.getBoundingClientRect(), rectDiv = div.getBoundingClientRect();
  234. return {
  235. x: rectEl.left - rectDiv.left,
  236. y: rectEl.top - rectDiv.top,
  237. width: rectEl.right - rectEl.left,
  238. height: rectEl.bottom - rectEl.top
  239. };
  240. }
  241. return { x: 0, y: 0, width: 1, height: 1 };
  242. },
  243. /**
  244. * @private
  245. * @param {Highcharts.HTMLElement} button The proxy element.
  246. */
  247. setProxyButtonStyle: function (button) {
  248. merge(true, button.style, {
  249. 'border-width': 0,
  250. 'background-color': 'transparent',
  251. cursor: 'pointer',
  252. outline: 'none',
  253. opacity: 0.001,
  254. filter: 'alpha(opacity=1)',
  255. '-ms-filter': 'progid:DXImageTransform.Microsoft.Alpha(Opacity=1)',
  256. zIndex: 999,
  257. overflow: 'hidden',
  258. padding: 0,
  259. margin: 0,
  260. display: 'block',
  261. position: 'absolute'
  262. });
  263. },
  264. /**
  265. * @private
  266. * @param {Highcharts.HTMLElement} proxy The proxy to update position of.
  267. * @param {Highcharts.SVGElement} posElement The element to overlay and take position from.
  268. */
  269. updateProxyButtonPosition: function (proxy, posElement) {
  270. var bBox = this.getElementPosition(posElement);
  271. merge(true, proxy.style, {
  272. width: (bBox.width || 1) + 'px',
  273. height: (bBox.height || 1) + 'px',
  274. left: (bBox.x || 0) + 'px',
  275. top: (bBox.y || 0) + 'px'
  276. });
  277. },
  278. /**
  279. * @private
  280. * @param {Highcharts.HTMLElement|Highcharts.HTMLDOMElement|
  281. * Highcharts.SVGDOMElement|Highcharts.SVGElement} source
  282. * @param {Highcharts.HTMLElement} button
  283. */
  284. proxyMouseEventsForButton: function (source, button) {
  285. var component = this;
  286. [
  287. 'click', 'touchstart', 'touchend', 'touchcancel', 'touchmove',
  288. 'mouseover', 'mouseenter', 'mouseleave', 'mouseout'
  289. ].forEach(function (evtType) {
  290. var isTouchEvent = evtType.indexOf('touch') === 0;
  291. component.addEvent(button, evtType, function (e) {
  292. var clonedEvent = isTouchEvent ?
  293. component.cloneTouchEvent(e) :
  294. component.cloneMouseEvent(e);
  295. if (source) {
  296. component.fireEventOnWrappedOrUnwrappedElement(source, clonedEvent);
  297. }
  298. e.stopPropagation();
  299. e.preventDefault();
  300. });
  301. });
  302. },
  303. /**
  304. * Utility function to clone a mouse event for re-dispatching.
  305. * @private
  306. * @param {global.MouseEvent} e The event to clone.
  307. * @return {global.MouseEvent} The cloned event
  308. */
  309. cloneMouseEvent: function (e) {
  310. if (typeof win.MouseEvent === 'function') {
  311. return new win.MouseEvent(e.type, e);
  312. }
  313. // No MouseEvent support, try using initMouseEvent
  314. if (doc.createEvent) {
  315. var evt = doc.createEvent('MouseEvent');
  316. if (evt.initMouseEvent) {
  317. evt.initMouseEvent(e.type, e.bubbles, // #10561, #12161
  318. e.cancelable, e.view || win, e.detail, e.screenX, e.screenY, e.clientX, e.clientY, e.ctrlKey, e.altKey, e.shiftKey, e.metaKey, e.button, e.relatedTarget);
  319. return evt;
  320. }
  321. }
  322. return getFakeMouseEvent(e.type);
  323. },
  324. /**
  325. * Utility function to clone a touch event for re-dispatching.
  326. * @private
  327. * @param {global.TouchEvent} e The event to clone.
  328. * @return {global.TouchEvent} The cloned event
  329. */
  330. cloneTouchEvent: function (e) {
  331. var touchListToTouchArray = function (l) {
  332. var touchArray = [];
  333. for (var i = 0; i < l.length; ++i) {
  334. var item = l.item(i);
  335. if (item) {
  336. touchArray.push(item);
  337. }
  338. }
  339. return touchArray;
  340. };
  341. if (typeof win.TouchEvent === 'function') {
  342. var newEvent = new win.TouchEvent(e.type, {
  343. touches: touchListToTouchArray(e.touches),
  344. targetTouches: touchListToTouchArray(e.targetTouches),
  345. changedTouches: touchListToTouchArray(e.changedTouches),
  346. ctrlKey: e.ctrlKey,
  347. shiftKey: e.shiftKey,
  348. altKey: e.altKey,
  349. metaKey: e.metaKey,
  350. bubbles: e.bubbles,
  351. cancelable: e.cancelable,
  352. composed: e.composed,
  353. detail: e.detail,
  354. view: e.view
  355. });
  356. if (e.defaultPrevented) {
  357. newEvent.preventDefault();
  358. }
  359. return newEvent;
  360. }
  361. // Fallback to mouse event
  362. var fakeEvt = this.cloneMouseEvent(e);
  363. fakeEvt.touches = e.touches;
  364. fakeEvt.changedTouches = e.changedTouches;
  365. fakeEvt.targetTouches = e.targetTouches;
  366. return fakeEvt;
  367. },
  368. /**
  369. * Remove traces of the component.
  370. * @private
  371. */
  372. destroyBase: function () {
  373. removeElement(this.chart.a11yProxyContainer);
  374. this.domElementProvider.destroyCreatedElements();
  375. this.eventProvider.removeAddedEvents();
  376. }
  377. };
  378. extend(AccessibilityComponent.prototype, functionsToOverrideByDerivedClasses);
  379. export default AccessibilityComponent;