| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- /* *
- *
- * (c) 2009-2020 Øystein Moseng
- *
- * Accessibility component for the range selector.
- *
- * License: www.highcharts.com/license
- *
- * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
- *
- * */
- 'use strict';
- import H from '../../Core/Globals.js';
- import U from '../../Core/Utilities.js';
- var extend = U.extend;
- import AccessibilityComponent from '../AccessibilityComponent.js';
- import KeyboardNavigationHandler from '../KeyboardNavigationHandler.js';
- import ChartUtilities from '../Utils/ChartUtilities.js';
- var unhideChartElementFromAT = ChartUtilities.unhideChartElementFromAT;
- import HTMLUtilities from '../Utils/HTMLUtilities.js';
- var setElAttrs = HTMLUtilities.setElAttrs;
- /* eslint-disable no-invalid-this, valid-jsdoc */
- /**
- * @private
- */
- function shouldRunInputNavigation(chart) {
- var inputVisible = (chart.rangeSelector &&
- chart.rangeSelector.inputGroup &&
- chart.rangeSelector.inputGroup.element
- .getAttribute('visibility') !== 'hidden');
- return (inputVisible &&
- chart.options.rangeSelector.inputEnabled !== false &&
- chart.rangeSelector.minInput &&
- chart.rangeSelector.maxInput);
- }
- /**
- * Highlight range selector button by index.
- *
- * @private
- * @function Highcharts.Chart#highlightRangeSelectorButton
- *
- * @param {number} ix
- *
- * @return {boolean}
- */
- H.Chart.prototype.highlightRangeSelectorButton = function (ix) {
- var buttons = this.rangeSelector.buttons, curSelectedIx = this.highlightedRangeSelectorItemIx;
- // Deselect old
- if (typeof curSelectedIx !== 'undefined' && buttons[curSelectedIx]) {
- buttons[curSelectedIx].setState(this.oldRangeSelectorItemState || 0);
- }
- // Select new
- this.highlightedRangeSelectorItemIx = ix;
- if (buttons[ix]) {
- this.setFocusToElement(buttons[ix].box, buttons[ix].element);
- this.oldRangeSelectorItemState = buttons[ix].state;
- buttons[ix].setState(2);
- return true;
- }
- return false;
- };
- /**
- * The RangeSelectorComponent class
- *
- * @private
- * @class
- * @name Highcharts.RangeSelectorComponent
- */
- var RangeSelectorComponent = function () { };
- RangeSelectorComponent.prototype = new AccessibilityComponent();
- extend(RangeSelectorComponent.prototype, /** @lends Highcharts.RangeSelectorComponent */ {
- /**
- * Called on first render/updates to the chart, including options changes.
- */
- onChartUpdate: function () {
- var chart = this.chart, component = this, rangeSelector = chart.rangeSelector;
- if (!rangeSelector) {
- return;
- }
- if (rangeSelector.buttons && rangeSelector.buttons.length) {
- rangeSelector.buttons.forEach(function (button) {
- unhideChartElementFromAT(chart, button.element);
- component.setRangeButtonAttrs(button);
- });
- }
- // Make sure input boxes are accessible and focusable
- if (rangeSelector.maxInput && rangeSelector.minInput) {
- ['minInput', 'maxInput'].forEach(function (key, i) {
- var input = rangeSelector[key];
- if (input) {
- unhideChartElementFromAT(chart, input);
- component.setRangeInputAttrs(input, 'accessibility.rangeSelector.' + (i ? 'max' : 'min') +
- 'InputLabel');
- }
- });
- }
- },
- /**
- * @private
- * @param {Highcharts.SVGElement} button
- */
- setRangeButtonAttrs: function (button) {
- var chart = this.chart, label = chart.langFormat('accessibility.rangeSelector.buttonText', {
- chart: chart,
- buttonText: button.text && button.text.textStr
- });
- setElAttrs(button.element, {
- tabindex: -1,
- role: 'button',
- 'aria-label': label
- });
- },
- /**
- * @private
- */
- setRangeInputAttrs: function (input, langKey) {
- var chart = this.chart;
- setElAttrs(input, {
- tabindex: -1,
- role: 'textbox',
- 'aria-label': chart.langFormat(langKey, { chart: chart })
- });
- },
- /**
- * Get navigation for the range selector buttons.
- * @private
- * @return {Highcharts.KeyboardNavigationHandler} The module object.
- */
- getRangeSelectorButtonNavigation: function () {
- var chart = this.chart, keys = this.keyCodes, component = this;
- return new KeyboardNavigationHandler(chart, {
- keyCodeMap: [
- [
- [keys.left, keys.right, keys.up, keys.down],
- function (keyCode) {
- return component.onButtonNavKbdArrowKey(this, keyCode);
- }
- ],
- [
- [keys.enter, keys.space],
- function () {
- return component.onButtonNavKbdClick(this);
- }
- ]
- ],
- validate: function () {
- var hasRangeSelector = chart.rangeSelector &&
- chart.rangeSelector.buttons &&
- chart.rangeSelector.buttons.length;
- return hasRangeSelector;
- },
- init: function (direction) {
- var lastButtonIx = (chart.rangeSelector.buttons.length - 1);
- chart.highlightRangeSelectorButton(direction > 0 ? 0 : lastButtonIx);
- }
- });
- },
- /**
- * @private
- * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
- * @param {number} keyCode
- * @return {number} Response code
- */
- onButtonNavKbdArrowKey: function (keyboardNavigationHandler, keyCode) {
- var response = keyboardNavigationHandler.response, keys = this.keyCodes, chart = this.chart, wrapAround = chart.options.accessibility
- .keyboardNavigation.wrapAround, direction = (keyCode === keys.left || keyCode === keys.up) ? -1 : 1, didHighlight = chart.highlightRangeSelectorButton(chart.highlightedRangeSelectorItemIx + direction);
- if (!didHighlight) {
- if (wrapAround) {
- keyboardNavigationHandler.init(direction);
- return response.success;
- }
- return response[direction > 0 ? 'next' : 'prev'];
- }
- return response.success;
- },
- /**
- * @private
- */
- onButtonNavKbdClick: function (keyboardNavigationHandler) {
- var response = keyboardNavigationHandler.response, chart = this.chart, wasDisabled = chart.oldRangeSelectorItemState === 3;
- if (!wasDisabled) {
- this.fakeClickEvent(chart.rangeSelector.buttons[chart.highlightedRangeSelectorItemIx].element);
- }
- return response.success;
- },
- /**
- * Get navigation for the range selector input boxes.
- * @private
- * @return {Highcharts.KeyboardNavigationHandler}
- * The module object.
- */
- getRangeSelectorInputNavigation: function () {
- var chart = this.chart, keys = this.keyCodes, component = this;
- return new KeyboardNavigationHandler(chart, {
- keyCodeMap: [
- [
- [
- keys.tab, keys.up, keys.down
- ],
- function (keyCode, e) {
- var direction = (keyCode === keys.tab && e.shiftKey ||
- keyCode === keys.up) ? -1 : 1;
- return component.onInputKbdMove(this, direction);
- }
- ]
- ],
- validate: function () {
- return shouldRunInputNavigation(chart);
- },
- init: function (direction) {
- component.onInputNavInit(direction);
- },
- terminate: function () {
- component.onInputNavTerminate();
- }
- });
- },
- /**
- * @private
- * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
- * @param {number} direction
- * @return {number} Response code
- */
- onInputKbdMove: function (keyboardNavigationHandler, direction) {
- var chart = this.chart, response = keyboardNavigationHandler.response, newIx = chart.highlightedInputRangeIx =
- chart.highlightedInputRangeIx + direction, newIxOutOfRange = newIx > 1 || newIx < 0;
- if (newIxOutOfRange) {
- return response[direction > 0 ? 'next' : 'prev'];
- }
- chart.rangeSelector[newIx ? 'maxInput' : 'minInput'].focus();
- return response.success;
- },
- /**
- * @private
- * @param {number} direction
- */
- onInputNavInit: function (direction) {
- var chart = this.chart, buttonIxToHighlight = direction > 0 ? 0 : 1;
- chart.highlightedInputRangeIx = buttonIxToHighlight;
- chart.rangeSelector[buttonIxToHighlight ? 'maxInput' : 'minInput'].focus();
- },
- /**
- * @private
- */
- onInputNavTerminate: function () {
- var rangeSel = (this.chart.rangeSelector || {});
- if (rangeSel.maxInput) {
- rangeSel.hideInput('max');
- }
- if (rangeSel.minInput) {
- rangeSel.hideInput('min');
- }
- },
- /**
- * Get keyboard navigation handlers for this component.
- * @return {Array<Highcharts.KeyboardNavigationHandler>}
- * List of module objects.
- */
- getKeyboardNavigation: function () {
- return [
- this.getRangeSelectorButtonNavigation(),
- this.getRangeSelectorInputNavigation()
- ];
- }
- });
- export default RangeSelectorComponent;
|