LegendComponent.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* *
  2. *
  3. * (c) 2009-2020 Ø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 stripHTMLTags = HTMLUtilities.stripHTMLTagsFromString, removeElement = HTMLUtilities.removeElement;
  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 ? 'false' : 'true');
  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. updateProxiesPositions: function () {
  132. for (var _i = 0, _a = this.proxyElementsList; _i < _a.length; _i++) {
  133. var _b = _a[_i], element = _b.element, posElement = _b.posElement;
  134. this.updateProxyButtonPosition(element, posElement);
  135. }
  136. },
  137. /**
  138. * @private
  139. */
  140. updateProxyPositionForItem: function (item) {
  141. var proxyRef = find(this.proxyElementsList, function (ref) { return ref.item === item; });
  142. if (proxyRef) {
  143. this.updateProxyButtonPosition(proxyRef.element, proxyRef.posElement);
  144. }
  145. },
  146. /**
  147. * @private
  148. */
  149. recreateProxies: function () {
  150. this.removeProxies();
  151. if (shouldDoLegendA11y(this.chart)) {
  152. this.addLegendProxyGroup();
  153. this.proxyLegendItems();
  154. this.updateLegendItemProxyVisibility();
  155. }
  156. },
  157. /**
  158. * @private
  159. */
  160. removeProxies: function () {
  161. removeElement(this.legendProxyGroup);
  162. this.proxyElementsList = [];
  163. },
  164. /**
  165. * @private
  166. */
  167. addLegendProxyGroup: function () {
  168. var a11yOptions = this.chart.options.accessibility, groupLabel = this.chart.langFormat('accessibility.legend.legendLabel', {}), groupRole = a11yOptions.landmarkVerbosity === 'all' ?
  169. 'region' : null;
  170. this.legendProxyGroup = this.addProxyGroup({
  171. 'aria-label': groupLabel,
  172. 'role': groupRole
  173. });
  174. },
  175. /**
  176. * @private
  177. */
  178. proxyLegendItems: function () {
  179. var component = this, items = (this.chart.legend &&
  180. this.chart.legend.allItems || []);
  181. items.forEach(function (item) {
  182. if (item.legendItem && item.legendItem.element) {
  183. component.proxyLegendItem(item);
  184. }
  185. });
  186. },
  187. /**
  188. * @private
  189. * @param {Highcharts.BubbleLegend|Point|Highcharts.Series} item
  190. */
  191. proxyLegendItem: function (item) {
  192. if (!item.legendItem || !item.legendGroup) {
  193. return;
  194. }
  195. var itemLabel = this.chart.langFormat('accessibility.legend.legendItem', {
  196. chart: this.chart,
  197. itemName: stripHTMLTags(item.name)
  198. }), attribs = {
  199. tabindex: -1,
  200. 'aria-pressed': !item.visible,
  201. 'aria-label': itemLabel
  202. },
  203. // Considers useHTML
  204. proxyPositioningElement = item.legendGroup.div ?
  205. item.legendItem : item.legendGroup;
  206. item.a11yProxyElement = this.createProxyButton(item.legendItem, this.legendProxyGroup, attribs, proxyPositioningElement);
  207. this.proxyElementsList.push({
  208. item: item,
  209. element: item.a11yProxyElement,
  210. posElement: proxyPositioningElement
  211. });
  212. },
  213. /**
  214. * Get keyboard navigation handler for this component.
  215. * @return {Highcharts.KeyboardNavigationHandler}
  216. */
  217. getKeyboardNavigation: function () {
  218. var keys = this.keyCodes, component = this, chart = this.chart;
  219. return new KeyboardNavigationHandler(chart, {
  220. keyCodeMap: [
  221. [
  222. [keys.left, keys.right, keys.up, keys.down],
  223. function (keyCode) {
  224. return component.onKbdArrowKey(this, keyCode);
  225. }
  226. ],
  227. [
  228. [keys.enter, keys.space],
  229. function () {
  230. return component.onKbdClick(this);
  231. }
  232. ]
  233. ],
  234. validate: function () {
  235. return component.shouldHaveLegendNavigation();
  236. },
  237. init: function (direction) {
  238. return component.onKbdNavigationInit(direction);
  239. }
  240. });
  241. },
  242. /**
  243. * @private
  244. * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
  245. * @param {number} keyCode
  246. * @return {number}
  247. * Response code
  248. */
  249. onKbdArrowKey: function (keyboardNavigationHandler, keyCode) {
  250. 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;
  251. var res = chart.highlightLegendItem(this.highlightedLegendItemIx + direction);
  252. if (res) {
  253. this.highlightedLegendItemIx += direction;
  254. return response.success;
  255. }
  256. if (numItems > 1 &&
  257. a11yOptions.keyboardNavigation.wrapAround) {
  258. keyboardNavigationHandler.init(direction);
  259. return response.success;
  260. }
  261. // No wrap, move
  262. return response[direction > 0 ? 'next' : 'prev'];
  263. },
  264. /**
  265. * @private
  266. * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
  267. * @return {number}
  268. * Response code
  269. */
  270. onKbdClick: function (keyboardNavigationHandler) {
  271. var legendItem = this.chart.legend.allItems[this.highlightedLegendItemIx];
  272. if (legendItem && legendItem.a11yProxyElement) {
  273. fireEvent(legendItem.a11yProxyElement, 'click');
  274. }
  275. return keyboardNavigationHandler.response.success;
  276. },
  277. /**
  278. * @private
  279. * @return {boolean|undefined}
  280. */
  281. shouldHaveLegendNavigation: function () {
  282. var chart = this.chart, legendOptions = chart.options.legend || {}, hasLegend = chart.legend && chart.legend.allItems, hasColorAxis = chart.colorAxis && chart.colorAxis.length, legendA11yOptions = (legendOptions.accessibility || {});
  283. return !!(hasLegend &&
  284. chart.legend.display &&
  285. !hasColorAxis &&
  286. legendA11yOptions.enabled &&
  287. legendA11yOptions.keyboardNavigation &&
  288. legendA11yOptions.keyboardNavigation.enabled);
  289. },
  290. /**
  291. * @private
  292. * @param {number} direction
  293. */
  294. onKbdNavigationInit: function (direction) {
  295. var chart = this.chart, lastIx = chart.legend.allItems.length - 1, ixToHighlight = direction > 0 ? 0 : lastIx;
  296. chart.highlightLegendItem(ixToHighlight);
  297. this.highlightedLegendItemIx = ixToHighlight;
  298. }
  299. });
  300. export default LegendComponent;