/* * * * (c) 2009-2019 Øystein Moseng * * Accessibility component for chart info region and table. * * License: www.highcharts.com/license * * */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _Globals = require('../../../parts/Globals.js'); var _Globals2 = _interopRequireDefault(_Globals); var _AccessibilityComponent = require('../AccessibilityComponent.js'); var _AccessibilityComponent2 = _interopRequireDefault(_AccessibilityComponent); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var merge = _Globals2.default.merge, pick = _Globals2.default.pick; /** * Return simplified text description of chart type. Some types will not be * familiar to most users, but in those cases we try to add a description of the * type. * * @private * @function Highcharts.Chart#getTypeDescription * @param {Array} types The series types in this chart. * @return {string} The text description of the chart type. */ _Globals2.default.Chart.prototype.getTypeDescription = function (types) { var firstType = types[0], firstSeries = this.series && this.series[0] || {}, mapTitle = firstSeries.mapTitle, formatContext = { numSeries: this.series.length, numPoints: firstSeries.points && firstSeries.points.length, chart: this, mapTitle: mapTitle }; if (!firstType) { return this.langFormat('accessibility.chartTypes.emptyChart', formatContext); } if (firstType === 'map') { return mapTitle ? this.langFormat('accessibility.chartTypes.mapTypeDescription', formatContext) : this.langFormat('accessibility.chartTypes.unknownMap', formatContext); } if (this.types.length > 1) { return this.langFormat('accessibility.chartTypes.combinationChart', formatContext); } var typeDesc = this.langFormat('accessibility.seriesTypeDescriptions.' + firstType, { chart: this }), multi = this.series && this.series.length === 1 ? 'Single' : 'Multiple'; return (this.langFormat('accessibility.chartTypes.' + firstType + multi, formatContext) || this.langFormat('accessibility.chartTypes.default' + multi, formatContext)) + (typeDesc ? ' ' + typeDesc : ''); }; /** * The InfoRegionComponent class * * @private * @class * @name Highcharts.InfoRegionComponent * @param {Highcharts.Chart} chart * Chart object */ var InfoRegionComponent = function InfoRegionComponent(chart) { this.initBase(chart); this.init(); }; InfoRegionComponent.prototype = new _AccessibilityComponent2.default(); _Globals2.default.extend(InfoRegionComponent.prototype, /** @lends Highcharts.InfoRegionComponent */{ // eslint-disable-line /** * Init the component * @private */ init: function init() { // Add ID and summary attr to table HTML var chart = this.chart, component = this; this.addEvent(chart, 'afterGetTable', function (e) { if (chart.options.accessibility.enabled) { component.tableAnchor.setAttribute('aria-expanded', true); e.html = e.html.replace('' + (options.accessibility.typeDescription || chart.getTypeDescription(chartTypes)) + '' + (options.subtitle && options.subtitle.text ? '
' + this.htmlencode(options.subtitle.text) + '
' : '') + (options.accessibility.description ? '
' + options.accessibility.description + '
' : '') + (axesDesc.xAxis ? '
' + axesDesc.xAxis + '
' : '') + (axesDesc.yAxis ? '
' + axesDesc.yAxis + '
' : ''); }, /** * Return object with text description of each of the chart's axes. * @private * @return {object} */ getAxesDescription: function getAxesDescription() { var chart = this.chart, component = this, xAxes = chart.xAxis, // Figure out when to show axis info in the region showXAxes = xAxes.length > 1 || xAxes[0] && pick(xAxes[0].options.accessibility && xAxes[0].options.accessibility.enabled, !chart.angular && chart.hasCartesianSeries && chart.types.indexOf('map') < 0), yAxes = chart.yAxis, showYAxes = yAxes.length > 1 || yAxes[0] && pick(yAxes[0].options.accessibility && yAxes[0].options.accessibility.enabled, chart.hasCartesianSeries && chart.types.indexOf('map') < 0), desc = {}; if (showXAxes) { desc.xAxis = chart.langFormat('accessibility.axis.xAxisDescription' + (xAxes.length > 1 ? 'Plural' : 'Singular'), { chart: chart, names: chart.xAxis.map(function (axis) { return axis.getDescription(); }), ranges: chart.xAxis.map(function (axis) { return component.getAxisRangeDescription(axis); }), numAxes: xAxes.length }); } if (showYAxes) { desc.yAxis = chart.langFormat('accessibility.axis.yAxisDescription' + (yAxes.length > 1 ? 'Plural' : 'Singular'), { chart: chart, names: chart.yAxis.map(function (axis) { return axis.getDescription(); }), ranges: chart.yAxis.map(function (axis) { return component.getAxisRangeDescription(axis); }), numAxes: yAxes.length }); } return desc; }, /** * Return string with text description of the axis range. * @private * @param {Highcharts.Axis} axis The axis to get range desc of. * @return {string} A string with the range description for the axis. */ getAxisRangeDescription: function getAxisRangeDescription(axis) { var chart = this.chart, axisOptions = axis.options || {}; // Handle overridden range description if (axisOptions.accessibility && axisOptions.accessibility.rangeDescription !== undefined) { return axisOptions.accessibility.rangeDescription; } // Handle category axes if (axis.categories) { return chart.langFormat('accessibility.axis.rangeCategories', { chart: chart, axis: axis, numCategories: axis.dataMax - axis.dataMin + 1 }); } // Use range, not from-to? if (axis.isDatetimeAxis && (axis.min === 0 || axis.dataMin === 0)) { var range = {}, rangeUnit = 'Seconds'; range.Seconds = (axis.max - axis.min) / 1000; range.Minutes = range.Seconds / 60; range.Hours = range.Minutes / 60; range.Days = range.Hours / 24; ['Minutes', 'Hours', 'Days'].forEach(function (unit) { if (range[unit] > 2) { rangeUnit = unit; } }); range.value = range[rangeUnit].toFixed(rangeUnit !== 'Seconds' && rangeUnit !== 'Minutes' ? 1 : 0 // Use decimals for days/hours ); // We have the range and the unit to use, find the desc format return chart.langFormat('accessibility.axis.timeRange' + rangeUnit, { chart: chart, axis: axis, range: range.value.replace('.0', '') }); } // Just use from and to. // We have the range and the unit to use, find the desc format var a11yOptions = chart.options.accessibility; return chart.langFormat('accessibility.axis.rangeFromTo', { chart: chart, axis: axis, rangeFrom: axis.isDatetimeAxis ? chart.time.dateFormat(a11yOptions.axisRangeDateFormat, axis.min) : axis.min, rangeTo: axis.isDatetimeAxis ? chart.time.dateFormat(a11yOptions.axisRangeDateFormat, axis.max) : axis.max }); } }); exports.default = InfoRegionComponent;