AnnotationsA11y.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 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. numPoints: numPoints,
  77. annotationPoint: pointValueDescriptions[0],
  78. additionalAnnotationPoints: pointValueDescriptions.slice(1)
  79. };
  80. return chart.langFormat(langFormatStr, context);
  81. }
  82. /**
  83. * Return array of HTML strings for each annotation label in the chart.
  84. *
  85. * @private
  86. * @param {Highcharts.Chart} chart The chart to get annotation info on.
  87. * @return {Array<string>} Array of strings with HTML content for each annotation label.
  88. */
  89. function getAnnotationListItems(chart) {
  90. var labels = getChartAnnotationLabels(chart);
  91. return labels.map(function (label) {
  92. var desc = escapeStringForHTML(stripHTMLTagsFromString(getAnnotationLabelDescription(label)));
  93. return desc ? "<li>" + desc + "</li>" : '';
  94. });
  95. }
  96. /**
  97. * Return the annotation info for a chart as string.
  98. *
  99. * @private
  100. * @param {Highcharts.Chart} chart The chart to get annotation info on.
  101. * @return {string} String with HTML content or empty string if no annotations.
  102. */
  103. function getAnnotationsInfoHTML(chart) {
  104. var annotations = chart.annotations;
  105. if (!(annotations && annotations.length)) {
  106. return '';
  107. }
  108. var annotationItems = getAnnotationListItems(chart);
  109. return "<ul>" + annotationItems.join(' ') + "</ul>";
  110. }
  111. /**
  112. * Return the texts for the annotation(s) connected to a point, or empty array
  113. * if none.
  114. *
  115. * @private
  116. * @param {Highcharts.Point} point The data point to get the annotation info from.
  117. * @return {Array<string>} Annotation texts
  118. */
  119. function getPointAnnotationTexts(point) {
  120. var labels = getChartAnnotationLabels(point.series.chart);
  121. var pointLabels = labels
  122. .filter(function (label) { return label.points.indexOf(point) > -1; });
  123. if (!pointLabels.length) {
  124. return [];
  125. }
  126. return pointLabels.map(function (label) { return "" + getLabelText(label); });
  127. }
  128. var AnnotationsA11y = {
  129. getAnnotationsInfoHTML: getAnnotationsInfoHTML,
  130. getAnnotationLabelDescription: getAnnotationLabelDescription,
  131. getAnnotationListItems: getAnnotationListItems,
  132. getPointAnnotationTexts: getPointAnnotationTexts
  133. };
  134. export default AnnotationsA11y;