AnnotationsA11y.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* *
  2. *
  3. * (c) 2009-2021 Øystein Moseng
  4. *
  5. * Annotations accessibility code.
  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 HTMLUtilities from '../Utils/HTMLUtilities.js';
  14. var escapeStringForHTML = HTMLUtilities.escapeStringForHTML, stripHTMLTagsFromString = HTMLUtilities.stripHTMLTagsFromString;
  15. /**
  16. * Get list of all annotation labels in the chart.
  17. *
  18. * @private
  19. * @param {Highcharts.Chart} chart The chart to get annotation info on.
  20. * @return {Array<object>} The labels, or empty array if none.
  21. */
  22. function getChartAnnotationLabels(chart) {
  23. var annotations = chart.annotations || [];
  24. return annotations.reduce(function (acc, cur) {
  25. var _a;
  26. if (((_a = cur.options) === null || _a === void 0 ? void 0 : _a.visible) !== false) {
  27. acc = acc.concat(cur.labels);
  28. }
  29. return acc;
  30. }, []);
  31. }
  32. /**
  33. * Get the text of an annotation label.
  34. *
  35. * @private
  36. * @param {object} label The annotation label object
  37. * @return {string} The text in the label.
  38. */
  39. function getLabelText(label) {
  40. var _a, _b, _c, _d;
  41. var a11yDesc = (_b = (_a = label.options) === null || _a === void 0 ? void 0 : _a.accessibility) === null || _b === void 0 ? void 0 : _b.description;
  42. return a11yDesc ? a11yDesc : ((_d = (_c = label.graphic) === null || _c === void 0 ? void 0 : _c.text) === null || _d === void 0 ? void 0 : _d.textStr) || '';
  43. }
  44. /**
  45. * Describe an annotation label.
  46. *
  47. * @private
  48. * @param {object} label The annotation label object to describe
  49. * @return {string} The description for the label.
  50. */
  51. function getAnnotationLabelDescription(label) {
  52. var _a, _b;
  53. var a11yDesc = (_b = (_a = label.options) === null || _a === void 0 ? void 0 : _a.accessibility) === null || _b === void 0 ? void 0 : _b.description;
  54. if (a11yDesc) {
  55. return a11yDesc;
  56. }
  57. var chart = label.chart;
  58. var labelText = getLabelText(label);
  59. var points = label.points;
  60. var getAriaLabel = function (point) { var _a, _b; return ((_b = (_a = point === null || point === void 0 ? void 0 : point.graphic) === null || _a === void 0 ? void 0 : _a.element) === null || _b === void 0 ? void 0 : _b.getAttribute('aria-label')) || ''; };
  61. var getValueDesc = function (point) {
  62. var _a;
  63. var valDesc = ((_a = point === null || point === void 0 ? void 0 : point.accessibility) === null || _a === void 0 ? void 0 : _a.valueDescription) || getAriaLabel(point);
  64. var seriesName = (point === null || point === void 0 ? void 0 : point.series.name) || '';
  65. return (seriesName ? seriesName + ', ' : '') + 'data point ' + valDesc;
  66. };
  67. var pointValueDescriptions = points
  68. .filter(function (p) { return !!p.graphic; }) // Filter out mock points
  69. .map(getValueDesc)
  70. .filter(function (desc) { return !!desc; }); // Filter out points we can't describe
  71. var numPoints = pointValueDescriptions.length;
  72. var pointsSelector = numPoints > 1 ? 'MultiplePoints' : numPoints ? 'SinglePoint' : 'NoPoints';
  73. var langFormatStr = 'accessibility.screenReaderSection.annotations.description' + pointsSelector;
  74. var context = {
  75. annotationText: labelText,
  76. annotation: label,
  77. numPoints: numPoints,
  78. annotationPoint: pointValueDescriptions[0],
  79. additionalAnnotationPoints: pointValueDescriptions.slice(1)
  80. };
  81. return chart.langFormat(langFormatStr, context);
  82. }
  83. /**
  84. * Return array of HTML strings for each annotation label in the chart.
  85. *
  86. * @private
  87. * @param {Highcharts.Chart} chart The chart to get annotation info on.
  88. * @return {Array<string>} Array of strings with HTML content for each annotation label.
  89. */
  90. function getAnnotationListItems(chart) {
  91. var labels = getChartAnnotationLabels(chart);
  92. return labels.map(function (label) {
  93. var desc = escapeStringForHTML(stripHTMLTagsFromString(getAnnotationLabelDescription(label)));
  94. return desc ? "<li>" + desc + "</li>" : '';
  95. });
  96. }
  97. /**
  98. * Return the annotation info for a chart as string.
  99. *
  100. * @private
  101. * @param {Highcharts.Chart} chart The chart to get annotation info on.
  102. * @return {string} String with HTML content or empty string if no annotations.
  103. */
  104. function getAnnotationsInfoHTML(chart) {
  105. var annotations = chart.annotations;
  106. if (!(annotations && annotations.length)) {
  107. return '';
  108. }
  109. var annotationItems = getAnnotationListItems(chart);
  110. return "<ul style=\"list-style-type: none\">" + annotationItems.join(' ') + "</ul>";
  111. }
  112. /**
  113. * Return the texts for the annotation(s) connected to a point, or empty array
  114. * if none.
  115. *
  116. * @private
  117. * @param {Highcharts.Point} point The data point to get the annotation info from.
  118. * @return {Array<string>} Annotation texts
  119. */
  120. function getPointAnnotationTexts(point) {
  121. var labels = getChartAnnotationLabels(point.series.chart);
  122. var pointLabels = labels
  123. .filter(function (label) { return label.points.indexOf(point) > -1; });
  124. if (!pointLabels.length) {
  125. return [];
  126. }
  127. return pointLabels.map(function (label) { return "" + getLabelText(label); });
  128. }
  129. var AnnotationsA11y = {
  130. getAnnotationsInfoHTML: getAnnotationsInfoHTML,
  131. getAnnotationLabelDescription: getAnnotationLabelDescription,
  132. getAnnotationListItems: getAnnotationListItems,
  133. getPointAnnotationTexts: getPointAnnotationTexts
  134. };
  135. export default AnnotationsA11y;