LegendComponent.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 _a, _b;
  175. var chart = this.chart;
  176. var legendTitle = stripHTMLTags((((_b = (_a = chart.legend) === null || _a === void 0 ? void 0 : _a.options.title) === null || _b === void 0 ? void 0 : _b.text) || '').replace(/<br ?\/?>/g, ' '));
  177. var legendLabel = chart.langFormat('accessibility.legend.legendLabel' + (legendTitle ? '' : 'NoTitle'), {
  178. chart: chart,
  179. legendTitle: legendTitle
  180. });
  181. if (this.legendProxyGroup) {
  182. this.legendProxyGroup.setAttribute('aria-label', legendLabel);
  183. }
  184. },
  185. /**
  186. * @private
  187. */
  188. addLegendProxyGroup: function () {
  189. var a11yOptions = this.chart.options.accessibility, groupRole = a11yOptions.landmarkVerbosity === 'all' ?
  190. 'region' : null;
  191. this.legendProxyGroup = this.addProxyGroup({
  192. 'aria-label': '_placeholder_',
  193. role: groupRole
  194. });
  195. },
  196. /**
  197. * @private
  198. */
  199. proxyLegendItems: function () {
  200. var component = this, items = (this.chart.legend &&
  201. this.chart.legend.allItems || []);
  202. items.forEach(function (item) {
  203. if (item.legendItem && item.legendItem.element) {
  204. component.proxyLegendItem(item);
  205. }
  206. });
  207. },
  208. /**
  209. * @private
  210. * @param {Highcharts.BubbleLegend|Point|Highcharts.Series} item
  211. */
  212. proxyLegendItem: function (item) {
  213. if (!item.legendItem || !item.legendGroup) {
  214. return;
  215. }
  216. var itemLabel = this.chart.langFormat('accessibility.legend.legendItem', {
  217. chart: this.chart,
  218. itemName: stripHTMLTags(item.name)
  219. }), attribs = {
  220. tabindex: -1,
  221. 'aria-pressed': item.visible,
  222. 'aria-label': itemLabel
  223. },
  224. // Considers useHTML
  225. proxyPositioningElement = item.legendGroup.div ?
  226. item.legendItem : item.legendGroup;
  227. item.a11yProxyElement = this.createProxyButton(item.legendItem, this.legendProxyGroup, attribs, proxyPositioningElement);
  228. this.proxyElementsList.push({
  229. item: item,
  230. element: item.a11yProxyElement,
  231. posElement: proxyPositioningElement
  232. });
  233. },
  234. /**
  235. * Get keyboard navigation handler for this component.
  236. * @return {Highcharts.KeyboardNavigationHandler}
  237. */
  238. getKeyboardNavigation: function () {
  239. var keys = this.keyCodes, component = this, chart = this.chart;
  240. return new KeyboardNavigationHandler(chart, {
  241. keyCodeMap: [
  242. [
  243. [keys.left, keys.right, keys.up, keys.down],
  244. function (keyCode) {
  245. return component.onKbdArrowKey(this, keyCode);
  246. }
  247. ],
  248. [
  249. [keys.enter, keys.space],
  250. function () {
  251. return component.onKbdClick(this);
  252. }
  253. ]
  254. ],
  255. validate: function () {
  256. return component.shouldHaveLegendNavigation();
  257. },
  258. init: function (direction) {
  259. return component.onKbdNavigationInit(direction);
  260. }
  261. });
  262. },
  263. /**
  264. * @private
  265. * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
  266. * @param {number} keyCode
  267. * @return {number}
  268. * Response code
  269. */
  270. onKbdArrowKey: function (keyboardNavigationHandler, keyCode) {
  271. 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;
  272. var res = chart.highlightLegendItem(this.highlightedLegendItemIx + direction);
  273. if (res) {
  274. this.highlightedLegendItemIx += direction;
  275. return response.success;
  276. }
  277. if (numItems > 1 &&
  278. a11yOptions.keyboardNavigation.wrapAround) {
  279. keyboardNavigationHandler.init(direction);
  280. return response.success;
  281. }
  282. // No wrap, move
  283. return response[direction > 0 ? 'next' : 'prev'];
  284. },
  285. /**
  286. * @private
  287. * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
  288. * @return {number}
  289. * Response code
  290. */
  291. onKbdClick: function (keyboardNavigationHandler) {
  292. var legendItem = this.chart.legend.allItems[this.highlightedLegendItemIx];
  293. if (legendItem && legendItem.a11yProxyElement) {
  294. fireEvent(legendItem.a11yProxyElement, 'click');
  295. }
  296. return keyboardNavigationHandler.response.success;
  297. },
  298. /**
  299. * @private
  300. * @return {boolean|undefined}
  301. */
  302. shouldHaveLegendNavigation: function () {
  303. var chart = this.chart, legendOptions = chart.options.legend || {}, hasLegend = chart.legend && chart.legend.allItems, hasColorAxis = chart.colorAxis && chart.colorAxis.length, legendA11yOptions = (legendOptions.accessibility || {});
  304. return !!(hasLegend &&
  305. chart.legend.display &&
  306. !hasColorAxis &&
  307. legendA11yOptions.enabled &&
  308. legendA11yOptions.keyboardNavigation &&
  309. legendA11yOptions.keyboardNavigation.enabled);
  310. },
  311. /**
  312. * @private
  313. * @param {number} direction
  314. */
  315. onKbdNavigationInit: function (direction) {
  316. var chart = this.chart, lastIx = chart.legend.allItems.length - 1, ixToHighlight = direction > 0 ? 0 : lastIx;
  317. chart.highlightLegendItem(ixToHighlight);
  318. this.highlightedLegendItemIx = ixToHighlight;
  319. }
  320. });
  321. export default LegendComponent;