AnnotationsA11y.js 5.3 KB

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