ContainerComponent.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* *
  2. *
  3. * (c) 2009-2021 Øystein Moseng
  4. *
  5. * Accessibility component for chart container.
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. import AccessibilityComponent from '../AccessibilityComponent.js';
  13. import KeyboardNavigationHandler from '../KeyboardNavigationHandler.js';
  14. import ChartUtilities from '../Utils/ChartUtilities.js';
  15. var unhideChartElementFromAT = ChartUtilities.unhideChartElementFromAT, getChartTitle = ChartUtilities.getChartTitle;
  16. import H from '../../Core/Globals.js';
  17. var doc = H.doc;
  18. import HTMLUtilities from '../Utils/HTMLUtilities.js';
  19. var stripHTMLTags = HTMLUtilities.stripHTMLTagsFromString;
  20. import U from '../../Core/Utilities.js';
  21. var extend = U.extend;
  22. /* eslint-disable valid-jsdoc */
  23. /**
  24. * The ContainerComponent class
  25. *
  26. * @private
  27. * @class
  28. * @name Highcharts.ContainerComponent
  29. */
  30. var ContainerComponent = function () { };
  31. ContainerComponent.prototype = new AccessibilityComponent();
  32. extend(ContainerComponent.prototype, /** @lends Highcharts.ContainerComponent */ {
  33. /**
  34. * Called on first render/updates to the chart, including options changes.
  35. */
  36. onChartUpdate: function () {
  37. this.handleSVGTitleElement();
  38. this.setSVGContainerLabel();
  39. this.setGraphicContainerAttrs();
  40. this.setRenderToAttrs();
  41. this.makeCreditsAccessible();
  42. },
  43. /**
  44. * @private
  45. */
  46. handleSVGTitleElement: function () {
  47. var chart = this.chart, titleId = 'highcharts-title-' + chart.index, titleContents = stripHTMLTags(chart.langFormat('accessibility.svgContainerTitle', {
  48. chartTitle: getChartTitle(chart)
  49. }));
  50. if (titleContents.length) {
  51. var titleElement = this.svgTitleElement =
  52. this.svgTitleElement || doc.createElementNS('http://www.w3.org/2000/svg', 'title');
  53. titleElement.textContent = titleContents;
  54. titleElement.id = titleId;
  55. chart.renderTo.insertBefore(titleElement, chart.renderTo.firstChild);
  56. }
  57. },
  58. /**
  59. * @private
  60. */
  61. setSVGContainerLabel: function () {
  62. var chart = this.chart, svgContainerLabel = chart.langFormat('accessibility.svgContainerLabel', {
  63. chartTitle: getChartTitle(chart)
  64. });
  65. if (chart.renderer.box && svgContainerLabel.length) {
  66. chart.renderer.box.setAttribute('aria-label', svgContainerLabel);
  67. }
  68. },
  69. /**
  70. * @private
  71. */
  72. setGraphicContainerAttrs: function () {
  73. var chart = this.chart, label = chart.langFormat('accessibility.graphicContainerLabel', {
  74. chartTitle: getChartTitle(chart)
  75. });
  76. if (label.length) {
  77. chart.container.setAttribute('aria-label', label);
  78. }
  79. },
  80. /**
  81. * @private
  82. */
  83. setRenderToAttrs: function () {
  84. var chart = this.chart;
  85. if (chart.options.accessibility.landmarkVerbosity !== 'disabled') {
  86. chart.renderTo.setAttribute('role', 'region');
  87. }
  88. else {
  89. chart.renderTo.removeAttribute('role');
  90. }
  91. chart.renderTo.setAttribute('aria-label', chart.langFormat('accessibility.chartContainerLabel', {
  92. title: getChartTitle(chart),
  93. chart: chart
  94. }));
  95. },
  96. /**
  97. * @private
  98. */
  99. makeCreditsAccessible: function () {
  100. var chart = this.chart, credits = chart.credits;
  101. if (credits) {
  102. if (credits.textStr) {
  103. credits.element.setAttribute('aria-label', chart.langFormat('accessibility.credits', { creditsStr: stripHTMLTags(credits.textStr) }));
  104. }
  105. unhideChartElementFromAT(chart, credits.element);
  106. }
  107. },
  108. /**
  109. * Empty handler to just set focus on chart
  110. * @return {Highcharts.KeyboardNavigationHandler}
  111. */
  112. getKeyboardNavigation: function () {
  113. var chart = this.chart;
  114. return new KeyboardNavigationHandler(chart, {
  115. keyCodeMap: [],
  116. validate: function () {
  117. return true;
  118. },
  119. init: function () {
  120. var a11y = chart.accessibility;
  121. if (a11y) {
  122. a11y.keyboardNavigation.tabindexContainer.focus();
  123. }
  124. }
  125. });
  126. },
  127. /**
  128. * Accessibility disabled/chart destroyed.
  129. */
  130. destroy: function () {
  131. this.chart.renderTo.setAttribute('aria-hidden', true);
  132. }
  133. });
  134. export default ContainerComponent;