whychdw
2019-09-09 c0b9ce437d409b89ca0e5388344ca9c5a56d70a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* *
 *
 *  (c) 2009-2019 Øystein Moseng
 *
 *  Accessibility component for chart zoom.
 *
 *  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);
 
var _KeyboardNavigationHandler = require('../KeyboardNavigationHandler.js');
 
var _KeyboardNavigationHandler2 = _interopRequireDefault(_KeyboardNavigationHandler);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
/**
 * 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]
 */
_Globals2.default.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
 * @param {Highcharts.Chart} chart
 *        Chart object
 */
var ZoomComponent = function ZoomComponent(chart) {
    this.initBase(chart);
    this.init();
};
ZoomComponent.prototype = new _AccessibilityComponent2.default();
_Globals2.default.extend(ZoomComponent.prototype, /** @lends Highcharts.ZoomComponent */{
 
    /**
     * Initialize the component
     */
    init: function init() {
        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 onChartUpdate() {
        var chart = this.chart,
            component = this;
 
        // Make map zoom buttons accessible
        if (chart.mapNavButtons) {
            chart.mapNavButtons.forEach(function (button, i) {
                component.unhideElementFromScreenReaders(button.element);
                button.element.setAttribute('tabindex', -1);
                button.element.setAttribute('role', 'button');
                button.element.setAttribute('aria-label', chart.langFormat('accessibility.mapZoom' + (i ? 'Out' : 'In'), { chart: chart }));
            });
        }
    },
 
    /**
     * Update the proxy overlays on every new render to ensure positions are
     * correct.
     */
    onChartRender: function onChartRender() {
        this.updateProxyOverlays();
    },
 
    /**
     * Update proxy overlays, recreating the buttons.
     */
    updateProxyOverlays: function updateProxyOverlays() {
        var component = this,
            chart = this.chart,
            proxyButton = function proxyButton(buttonEl, buttonProp, groupProp, label) {
            component.removeElement(component[groupProp]);
            component[groupProp] = component.addProxyGroup();
            component[buttonProp] = component.createProxyButton(buttonEl, component[groupProp], {
                'aria-label': label,
                tabindex: -1
            });
        };
 
        // Always start with a clean slate
        component.removeElement(component.drillUpProxyGroup);
        component.removeElement(component.resetZoomProxyGroup);
 
        if (chart.resetZoomButton) {
            proxyButton(chart.resetZoomButton, 'resetZoomProxyButton', 'resetZoomProxyGroup', chart.langFormat('accessibility.resetZoomButton', { chart: chart }));
        }
 
        if (chart.drillUpButton) {
            proxyButton(chart.drillUpButton, 'drillUpProxyButton', 'drillUpProxyGroup', chart.langFormat('accessibility.drillUpButton', {
                chart: chart,
                buttonText: chart.getDrilldownBackText()
            }));
        }
    },
 
    /**
     * Get keyboard navigation handler for map zoom.
     * @private
     * @return {Highcharts.KeyboardNavigationHandler} The module object
     */
    getMapZoomNavigation: function getMapZoomNavigation() {
        var keys = this.keyCodes,
            chart = this.chart,
            component = this;
 
        return new _KeyboardNavigationHandler2.default(chart, {
            keyCodeMap: [
            // Arrow keys
            [[keys.up, keys.down, keys.left, keys.right], function (keyCode) {
                chart[keyCode === keys.up || keyCode === keys.down ? 'yAxis' : 'xAxis'][0].panStep(keyCode === keys.left || keyCode === keys.up ? -1 : 1);
                return this.response.success;
            }],
 
            // Tabs
            [[keys.tab], function (keyCode, e) {
                var button;
 
                // Deselect old
                chart.mapNavButtons[component.focusedMapNavButtonIx].setState(0);
 
                // Trying to go somewhere we can't?
                if (e.shiftKey && !component.focusedMapNavButtonIx || !e.shiftKey && component.focusedMapNavButtonIx) {
                    chart.mapZoom(); // Reset zoom
                    // Nowhere to go, go to prev/next module
                    return this.response[e.shiftKey ? 'prev' : 'next'];
                }
 
                // Select other button
                component.focusedMapNavButtonIx += e.shiftKey ? -1 : 1;
                button = chart.mapNavButtons[component.focusedMapNavButtonIx];
                chart.setFocusToElement(button.box, button.element);
                button.setState(2);
 
                return this.response.success;
            }],
 
            // Press button
            [[keys.space, keys.enter], function () {
                component.fakeClickEvent(chart.mapNavButtons[component.focusedMapNavButtonIx].element);
                return this.response.success;
            }]],
 
            // Only run this module if we have map zoom on the chart
            validate: function validate() {
                return chart.mapZoom && chart.mapNavButtons && chart.mapNavButtons.length === 2;
            },
 
            // Make zoom buttons do their magic
            init: function init(direction) {
                var zoomIn = chart.mapNavButtons[0],
                    zoomOut = chart.mapNavButtons[1],
                    initialButton = direction > 0 ? zoomIn : zoomOut;
                chart.setFocusToElement(initialButton.box, initialButton.element);
                initialButton.setState(2);
                component.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 simpleButtonNavigation(buttonProp, proxyProp, onClick) {
        var keys = this.keyCodes,
            component = this,
            chart = this.chart;
 
        return new _KeyboardNavigationHandler2.default(chart, {
            keyCodeMap: [
            // Arrow/tab just move
            [[keys.tab, keys.up, keys.down, keys.left, keys.right], function (keyCode, e) {
                return this.response[keyCode === this.tab && e.shiftKey || keyCode === keys.left || keyCode === keys.up ? 'prev' : 'next'];
            }],
 
            // Select to click
            [[keys.space, keys.enter], function () {
                onClick(chart);
                return this.response.success;
            }]],
 
            // Only run if we have the button
            validate: function validate() {
                return chart[buttonProp] && chart[buttonProp].box && component[proxyProp];
            },
 
            // Focus button initially
            init: function init() {
                chart.setFocusToElement(chart[buttonProp].box, component[proxyProp]);
            }
        });
    },
 
    /**
     * Get keyboard navigation handlers for this component.
     * @return {Array<Highcharts.KeyboardNavigationHandler>}
     *         List of module objects
     */
    getKeyboardNavigation: function getKeyboardNavigation() {
        return [this.simpleButtonNavigation('resetZoomButton', 'resetZoomProxyButton', function (chart) {
            chart.zoomOut();
        }), this.simpleButtonNavigation('drillUpButton', 'drillUpProxyButton', function (chart) {
            chart.drillUp();
        }), this.getMapZoomNavigation()];
    }
 
});
 
exports.default = ZoomComponent;