LegendComponent.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* *
  2. *
  3. * (c) 2009-2021 Øystein Moseng
  4. *
  5. * Accessibility component for chart legend.
  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 Legend from '../../Core/Legend.js';
  15. import U from '../../Core/Utilities.js';
  16. var addEvent = U.addEvent, extend = U.extend, find = U.find, fireEvent = U.fireEvent;
  17. import AccessibilityComponent from '../AccessibilityComponent.js';
  18. import KeyboardNavigationHandler from '../KeyboardNavigationHandler.js';
  19. import HTMLUtilities from '../Utils/HTMLUtilities.js';
  20. var removeElement = HTMLUtilities.removeElement, stripHTMLTags = HTMLUtilities.stripHTMLTagsFromString;
  21. /* eslint-disable no-invalid-this, valid-jsdoc */
  22. /**
  23. * @private
  24. */
  25. function scrollLegendToItem(legend, itemIx) {
  26. var itemPage = legend.allItems[itemIx].pageIx, curPage = legend.currentPage;
  27. if (typeof itemPage !== 'undefined' && itemPage + 1 !== curPage) {
  28. legend.scroll(1 + itemPage - curPage);
  29. }
  30. }
  31. /**
  32. * @private
  33. */
  34. function shouldDoLegendA11y(chart) {
  35. var items = chart.legend && chart.legend.allItems, legendA11yOptions = (chart.options.legend.accessibility || {});
  36. return !!(items && items.length &&
  37. !(chart.colorAxis && chart.colorAxis.length) &&
  38. legendA11yOptions.enabled !== false);
  39. }
  40. /**
  41. * Highlight legend item by index.
  42. *
  43. * @private
  44. * @function Highcharts.Chart#highlightLegendItem
  45. *
  46. * @param {number} ix
  47. *
  48. * @return {boolean}
  49. */
  50. H.Chart.prototype.highlightLegendItem = function (ix) {
  51. var items = this.legend.allItems, oldIx = this.highlightedLegendItemIx;
  52. if (items[ix]) {
  53. if (items[oldIx]) {
  54. fireEvent(items[oldIx].legendGroup.element, 'mouseout');
  55. }
  56. scrollLegendToItem(this.legend, ix);
  57. this.setFocusToElement(items[ix].legendItem, items[ix].a11yProxyElement);
  58. fireEvent(items[ix].legendGroup.element, 'mouseover');
  59. return true;
  60. }
  61. return false;
  62. };
  63. // Keep track of pressed state for legend items
  64. addEvent(Legend, 'afterColorizeItem', function (e) {
  65. var chart = this.chart, a11yOptions = chart.options.accessibility, legendItem = e.item;
  66. if (a11yOptions.enabled && legendItem && legendItem.a11yProxyElement) {
  67. legendItem.a11yProxyElement.setAttribute('aria-pressed', e.visible ? 'true' : 'false');
  68. }
  69. });
  70. /**
  71. * The LegendComponent class
  72. *
  73. * @private
  74. * @class
  75. * @name Highcharts.LegendComponent
  76. */
  77. var LegendComponent = function () { };
  78. LegendComponent.prototype = new AccessibilityComponent();
  79. extend(LegendComponent.prototype, /** @lends Highcharts.LegendComponent */ {
  80. /**
  81. * Init the component
  82. * @private
  83. */
  84. init: function () {
  85. var component = this;
  86. this.proxyElementsList = [];
  87. this.recreateProxies();
  88. // Note: Chart could create legend dynamically, so events can not be
  89. // tied to the component's chart's current legend.
  90. this.addEvent(Legend, 'afterScroll', function () {
  91. if (this.chart === component.chart) {
  92. component.updateProxiesPositions();
  93. component.updateLegendItemProxyVisibility();
  94. this.chart.highlightLegendItem(component.highlightedLegendItemIx);
  95. }
  96. });
  97. this.addEvent(Legend, 'afterPositionItem', function (e) {
  98. if (this.chart === component.chart && this.chart.renderer) {
  99. component.updateProxyPositionForItem(e.item);
  100. }
  101. });
  102. },
  103. /**
  104. * @private
  105. */
  106. updateLegendItemProxyVisibility: function () {
  107. var legend = this.chart.legend, items = legend.allItems || [], curPage = legend.currentPage || 1, clipHeight = legend.clipHeight || 0;
  108. items.forEach(function (item) {
  109. var itemPage = item.pageIx || 0, y = item._legendItemPos ? item._legendItemPos[1] : 0, h = item.legendItem ? Math.round(item.legendItem.getBBox().height) : 0, hide = y + h - legend.pages[itemPage] > clipHeight || itemPage !== curPage - 1;
  110. if (item.a11yProxyElement) {
  111. item.a11yProxyElement.style.visibility = hide ?
  112. 'hidden' : 'visible';
  113. }
  114. });
  115. },
  116. /**
  117. * The legend needs updates on every render, in order to update positioning
  118. * of the proxy overlays.
  119. */
  120. onChartRender: function () {
  121. if (shouldDoLegendA11y(this.chart)) {
  122. this.updateProxiesPositions();
  123. }
  124. else {
  125. this.removeProxies();
  126. }
  127. },
  128. /**
  129. * @private
  130. */
  131. onChartUpdate: function () {
  132. this.updateLegendTitle();
  133. },
  134. /**
  135. * @private
  136. */
  137. updateProxiesPositions: function () {
  138. for (var _i = 0, _a = this.proxyElementsList; _i < _a.length; _i++) {
  139. var _b = _a[_i], element = _b.element, posElement = _b.posElement;
  140. this.updateProxyButtonPosition(element, posElement);
  141. }
  142. },
  143. /**
  144. * @private
  145. */
  146. updateProxyPositionForItem: function (item) {
  147. var proxyRef = find(this.proxyElementsList, function (ref) { return ref.item === item; });
  148. if (proxyRef) {
  149. this.updateProxyButtonPosition(proxyRef.element, proxyRef.posElement);
  150. }
  151. },
  152. /**
  153. * @private
  154. */
  155. recreateProxies: function () {
  156. this.removeProxies();
  157. if (shouldDoLegendA11y(this.chart)) {
  158. this.addLegendProxyGroup();
  159. this.proxyLegendItems();
  160. this.updateLegendItemProxyVisibility();
  161. }
  162. },
  163. /**
  164. * @private
  165. */
  166. removeProxies: function () {
  167. removeElement(this.legendProxyGroup);
  168. this.proxyElementsList = [];
  169. },
  170. /**
  171. * @private
  172. */
  173. updateLegendTitle: function () {
  174. var chart = this.chart;
  175. var legendTitle = stripHTMLTags((chart.legend &&
  176. chart.legend.options.title &&
  177. chart.legend.options.title.text ||
  178. '').replace(/<br ?\/?>/g, ' '));
  179. var legendLabel = chart.langFormat('accessibility.legend.legendLabel' + (legendTitle ? '' : 'NoTitle'), {
  180. chart: chart,
  181. legendTitle: legendTitle
  182. });
  183. if (this.legendProxyGroup) {
  184. this.legendProxyGroup.setAttribute('aria-label', legendLabel);
  185. }
  186. },
  187. /**
  188. * @private
  189. */
  190. addLegendProxyGroup: function () {
  191. var a11yOptions = this.chart.options.accessibility, groupRole = a11yOptions.landmarkVerbosity === 'all' ?
  192. 'region' : null;
  193. this.legendProxyGroup = this.addProxyGroup({
  194. 'aria-label': '_placeholder_',
  195. role: groupRole
  196. });
  197. },
  198. /**
  199. * @private
  200. */
  201. proxyLegendItems: function () {
  202. var component = this, items = (this.chart.legend &&
  203. this.chart.legend.allItems || []);
  204. items.forEach(function (item) {
  205. if (item.legendItem && item.legendItem.element) {
  206. component.proxyLegendItem(item);
  207. }
  208. });
  209. },
  210. /**
  211. * @private
  212. * @param {Highcharts.BubbleLegend|Point|Highcharts.Series} item
  213. */
  214. proxyLegendItem: function (item) {
  215. if (!item.legendItem || !item.legendGroup) {
  216. return;
  217. }
  218. var itemLabel = this.chart.langFormat('accessibility.legend.legendItem', {
  219. chart: this.chart,
  220. itemName: stripHTMLTags(item.name),
  221. item: item
  222. }), attribs = {
  223. tabindex: -1,
  224. 'aria-pressed': item.visible,
  225. 'aria-label': itemLabel
  226. },
  227. // Considers useHTML
  228. proxyPositioningElement = item.legendGroup.div ?
  229. item.legendItem : item.legendGroup;
  230. item.a11yProxyElement = this.createProxyButton(item.legendItem, this.legendProxyGroup, attribs, proxyPositioningElement);
  231. this.proxyElementsList.push({
  232. item: item,
  233. element: item.a11yProxyElement,
  234. posElement: proxyPositioningElement
  235. });
  236. },
  237. /**
  238. * Get keyboard navigation handler for this component.
  239. * @return {Highcharts.KeyboardNavigationHandler}
  240. */
  241. getKeyboardNavigation: function () {
  242. var keys = this.keyCodes, component = this, chart = this.chart;
  243. return new KeyboardNavigationHandler(chart, {
  244. keyCodeMap: [
  245. [
  246. [keys.left, keys.right, keys.up, keys.down],
  247. function (keyCode) {
  248. return component.onKbdArrowKey(this, keyCode);
  249. }
  250. ],
  251. [
  252. [keys.enter, keys.space],
  253. function (keyCode) {
  254. if (H.isFirefox && keyCode === keys.space) { // #15520
  255. return this.response.success;
  256. }
  257. return component.onKbdClick(this);
  258. }
  259. ]
  260. ],
  261. validate: function () {
  262. return component.shouldHaveLegendNavigation();
  263. },
  264. init: function (direction) {
  265. return component.onKbdNavigationInit(direction);
  266. }
  267. });
  268. },
  269. /**
  270. * @private
  271. * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
  272. * @param {number} keyCode
  273. * @return {number}
  274. * Response code
  275. */
  276. onKbdArrowKey: function (keyboardNavigationHandler, keyCode) {
  277. var keys = this.keyCodes, response = keyboardNavigationHandler.response, chart = this.chart, a11yOptions = chart.options.accessibility, numItems = chart.legend.allItems.length, direction = (keyCode === keys.left || keyCode === keys.up) ? -1 : 1;
  278. var res = chart.highlightLegendItem(this.highlightedLegendItemIx + direction);
  279. if (res) {
  280. this.highlightedLegendItemIx += direction;
  281. return response.success;
  282. }
  283. if (numItems > 1 &&
  284. a11yOptions.keyboardNavigation.wrapAround) {
  285. keyboardNavigationHandler.init(direction);
  286. return response.success;
  287. }
  288. // No wrap, move
  289. return response[direction > 0 ? 'next' : 'prev'];
  290. },
  291. /**
  292. * @private
  293. * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
  294. * @return {number}
  295. * Response code
  296. */
  297. onKbdClick: function (keyboardNavigationHandler) {
  298. var legendItem = this.chart.legend.allItems[this.highlightedLegendItemIx];
  299. if (legendItem && legendItem.a11yProxyElement) {
  300. fireEvent(legendItem.a11yProxyElement, 'click');
  301. }
  302. return keyboardNavigationHandler.response.success;
  303. },
  304. /**
  305. * @private
  306. * @return {boolean|undefined}
  307. */
  308. shouldHaveLegendNavigation: function () {
  309. var chart = this.chart, legendOptions = chart.options.legend || {}, hasLegend = chart.legend && chart.legend.allItems, hasColorAxis = chart.colorAxis && chart.colorAxis.length, legendA11yOptions = (legendOptions.accessibility || {});
  310. return !!(hasLegend &&
  311. chart.legend.display &&
  312. !hasColorAxis &&
  313. legendA11yOptions.enabled &&
  314. legendA11yOptions.keyboardNavigation &&
  315. legendA11yOptions.keyboardNavigation.enabled);
  316. },
  317. /**
  318. * @private
  319. * @param {number} direction
  320. */
  321. onKbdNavigationInit: function (direction) {
  322. var chart = this.chart, lastIx = chart.legend.allItems.length - 1, ixToHighlight = direction > 0 ? 0 : lastIx;
  323. chart.highlightLegendItem(ixToHighlight);
  324. this.highlightedLegendItemIx = ixToHighlight;
  325. }
  326. });
  327. export default LegendComponent;