| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- /* *
- *
- * (c) 2009-2021 Øystein Moseng
- *
- * Accessibility component for chart zoom.
- *
- * License: www.highcharts.com/license
- *
- * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
- *
- * */
- 'use strict';
- import AccessibilityComponent from '../AccessibilityComponent.js';
- import ChartUtilities from '../Utils/ChartUtilities.js';
- var unhideChartElementFromAT = ChartUtilities.unhideChartElementFromAT;
- import H from '../../Core/Globals.js';
- var noop = H.noop;
- import HTMLUtilities from '../Utils/HTMLUtilities.js';
- var removeElement = HTMLUtilities.removeElement, setElAttrs = HTMLUtilities.setElAttrs;
- import KeyboardNavigationHandler from '../KeyboardNavigationHandler.js';
- import U from '../../Core/Utilities.js';
- var extend = U.extend, pick = U.pick;
- /* eslint-disable no-invalid-this, valid-jsdoc */
- /**
- * @private
- */
- function chartHasMapZoom(chart) {
- return !!(chart.mapZoom &&
- chart.mapNavButtons &&
- chart.mapNavButtons.length);
- }
- /**
- * Pan along axis in a direction (1 or -1), optionally with a defined
- * granularity (number of steps it takes to walk across current view)
- *
- * @private
- * @function Highcharts.Axis#panStep
- *
- * @param {number} direction
- * @param {number} [granularity]
- */
- H.Axis.prototype.panStep = function (direction, granularity) {
- var gran = granularity || 3, extremes = this.getExtremes(), step = (extremes.max - extremes.min) / gran * direction, newMax = extremes.max + step, newMin = extremes.min + step, size = newMax - newMin;
- if (direction < 0 && newMin < extremes.dataMin) {
- newMin = extremes.dataMin;
- newMax = newMin + size;
- }
- else if (direction > 0 && newMax > extremes.dataMax) {
- newMax = extremes.dataMax;
- newMin = newMax - size;
- }
- this.setExtremes(newMin, newMax);
- };
- /**
- * The ZoomComponent class
- *
- * @private
- * @class
- * @name Highcharts.ZoomComponent
- */
- var ZoomComponent = noop;
- ZoomComponent.prototype = new AccessibilityComponent();
- extend(ZoomComponent.prototype, /** @lends Highcharts.ZoomComponent */ {
- /**
- * Initialize the component
- */
- init: function () {
- var component = this, chart = this.chart;
- [
- 'afterShowResetZoom', 'afterDrilldown', 'drillupall'
- ].forEach(function (eventType) {
- component.addEvent(chart, eventType, function () {
- component.updateProxyOverlays();
- });
- });
- },
- /**
- * Called when chart is updated
- */
- onChartUpdate: function () {
- var chart = this.chart, component = this;
- // Make map zoom buttons accessible
- if (chart.mapNavButtons) {
- chart.mapNavButtons.forEach(function (button, i) {
- unhideChartElementFromAT(chart, button.element);
- component.setMapNavButtonAttrs(button.element, 'accessibility.zoom.mapZoom' + (i ? 'Out' : 'In'));
- });
- }
- },
- /**
- * @private
- * @param {Highcharts.HTMLDOMElement|Highcharts.SVGDOMElement} button
- * @param {string} labelFormatKey
- */
- setMapNavButtonAttrs: function (button, labelFormatKey) {
- var chart = this.chart, label = chart.langFormat(labelFormatKey, { chart: chart });
- setElAttrs(button, {
- tabindex: -1,
- role: 'button',
- 'aria-label': label
- });
- },
- /**
- * Update the proxy overlays on every new render to ensure positions are
- * correct.
- */
- onChartRender: function () {
- this.updateProxyOverlays();
- },
- /**
- * Update proxy overlays, recreating the buttons.
- */
- updateProxyOverlays: function () {
- var chart = this.chart;
- // Always start with a clean slate
- removeElement(this.drillUpProxyGroup);
- removeElement(this.resetZoomProxyGroup);
- if (chart.resetZoomButton) {
- this.recreateProxyButtonAndGroup(chart.resetZoomButton, 'resetZoomProxyButton', 'resetZoomProxyGroup', chart.langFormat('accessibility.zoom.resetZoomButton', { chart: chart }));
- }
- if (chart.drillUpButton) {
- this.recreateProxyButtonAndGroup(chart.drillUpButton, 'drillUpProxyButton', 'drillUpProxyGroup', chart.langFormat('accessibility.drillUpButton', {
- chart: chart,
- buttonText: chart.getDrilldownBackText()
- }));
- }
- },
- /**
- * @private
- * @param {Highcharts.SVGElement} buttonEl
- * @param {string} buttonProp
- * @param {string} groupProp
- * @param {string} label
- */
- recreateProxyButtonAndGroup: function (buttonEl, buttonProp, groupProp, label) {
- removeElement(this[groupProp]);
- this[groupProp] = this.addProxyGroup();
- this[buttonProp] = this.createProxyButton(buttonEl, this[groupProp], { 'aria-label': label, tabindex: -1 });
- },
- /**
- * Get keyboard navigation handler for map zoom.
- * @private
- * @return {Highcharts.KeyboardNavigationHandler} The module object
- */
- getMapZoomNavigation: function () {
- var keys = this.keyCodes, chart = this.chart, component = this;
- return new KeyboardNavigationHandler(chart, {
- keyCodeMap: [
- [
- [keys.up, keys.down, keys.left, keys.right],
- function (keyCode) {
- return component.onMapKbdArrow(this, keyCode);
- }
- ],
- [
- [keys.tab],
- function (_keyCode, e) {
- return component.onMapKbdTab(this, e);
- }
- ],
- [
- [keys.space, keys.enter],
- function () {
- return component.onMapKbdClick(this);
- }
- ]
- ],
- validate: function () {
- return chartHasMapZoom(chart);
- },
- init: function (direction) {
- return component.onMapNavInit(direction);
- }
- });
- },
- /**
- * @private
- * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
- * @param {number} keyCode
- * @return {number} Response code
- */
- onMapKbdArrow: function (keyboardNavigationHandler, keyCode) {
- var keys = this.keyCodes, panAxis = (keyCode === keys.up || keyCode === keys.down) ?
- 'yAxis' : 'xAxis', stepDirection = (keyCode === keys.left || keyCode === keys.up) ?
- -1 : 1;
- this.chart[panAxis][0].panStep(stepDirection);
- return keyboardNavigationHandler.response.success;
- },
- /**
- * @private
- * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
- * @param {global.KeyboardEvent} event
- * @return {number} Response code
- */
- onMapKbdTab: function (keyboardNavigationHandler, event) {
- var button, chart = this.chart, response = keyboardNavigationHandler.response, isBackwards = event.shiftKey, isMoveOutOfRange = isBackwards && !this.focusedMapNavButtonIx ||
- !isBackwards && this.focusedMapNavButtonIx;
- // Deselect old
- chart.mapNavButtons[this.focusedMapNavButtonIx].setState(0);
- if (isMoveOutOfRange) {
- chart.mapZoom(); // Reset zoom
- return response[isBackwards ? 'prev' : 'next'];
- }
- // Select other button
- this.focusedMapNavButtonIx += isBackwards ? -1 : 1;
- button = chart.mapNavButtons[this.focusedMapNavButtonIx];
- chart.setFocusToElement(button.box, button.element);
- button.setState(2);
- return response.success;
- },
- /**
- * @private
- * @param {Highcharts.KeyboardNavigationHandler} keyboardNavigationHandler
- * @return {number} Response code
- */
- onMapKbdClick: function (keyboardNavigationHandler) {
- this.fakeClickEvent(this.chart.mapNavButtons[this.focusedMapNavButtonIx]
- .element);
- return keyboardNavigationHandler.response.success;
- },
- /**
- * @private
- * @param {number} direction
- */
- onMapNavInit: function (direction) {
- var chart = this.chart, zoomIn = chart.mapNavButtons[0], zoomOut = chart.mapNavButtons[1], initialButton = direction > 0 ? zoomIn : zoomOut;
- chart.setFocusToElement(initialButton.box, initialButton.element);
- initialButton.setState(2);
- this.focusedMapNavButtonIx = direction > 0 ? 0 : 1;
- },
- /**
- * Get keyboard navigation handler for a simple chart button. Provide the
- * button reference for the chart, and a function to call on click.
- *
- * @private
- * @param {string} buttonProp The property on chart referencing the button.
- * @return {Highcharts.KeyboardNavigationHandler} The module object
- */
- simpleButtonNavigation: function (buttonProp, proxyProp, onClick) {
- var keys = this.keyCodes, component = this, chart = this.chart;
- return new KeyboardNavigationHandler(chart, {
- keyCodeMap: [
- [
- [keys.tab, keys.up, keys.down, keys.left, keys.right],
- function (keyCode, e) {
- var isBackwards = keyCode === keys.tab && e.shiftKey ||
- keyCode === keys.left || keyCode === keys.up;
- // Arrow/tab => just move
- return this.response[isBackwards ? 'prev' : 'next'];
- }
- ],
- [
- [keys.space, keys.enter],
- function () {
- var res = onClick(this, chart);
- return pick(res, this.response.success);
- }
- ]
- ],
- validate: function () {
- var hasButton = (chart[buttonProp] &&
- chart[buttonProp].box &&
- component[proxyProp]);
- return hasButton;
- },
- init: function () {
- chart.setFocusToElement(chart[buttonProp].box, component[proxyProp]);
- }
- });
- },
- /**
- * Get keyboard navigation handlers for this component.
- * @return {Array<Highcharts.KeyboardNavigationHandler>}
- * List of module objects
- */
- getKeyboardNavigation: function () {
- return [
- this.simpleButtonNavigation('resetZoomButton', 'resetZoomProxyButton', function (_handler, chart) {
- chart.zoomOut();
- }),
- this.simpleButtonNavigation('drillUpButton', 'drillUpProxyButton', function (handler, chart) {
- chart.drillUp();
- return handler.response.prev;
- }),
- this.getMapZoomNavigation()
- ];
- }
- });
- export default ZoomComponent;
|