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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* *
 *
 *  (c) 2009-2019 Øystein Moseng
 *
 *  Accessibility component for exporting menu.
 *
 *  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 }; }
 
/**
 * Show the export menu and focus the first item (if exists).
 *
 * @private
 * @function Highcharts.Chart#showExportMenu
 */
_Globals2.default.Chart.prototype.showExportMenu = function () {
    if (this.exportSVGElements && this.exportSVGElements[0]) {
        this.exportSVGElements[0].element.onclick();
        this.highlightExportItem(0);
    }
};
 
/**
 * Hide export menu.
 *
 * @private
 * @function Highcharts.Chart#hideExportMenu
 */
_Globals2.default.Chart.prototype.hideExportMenu = function () {
    var chart = this,
        exportList = chart.exportDivElements;
 
    if (exportList && chart.exportContextMenu) {
        // Reset hover states etc.
        exportList.forEach(function (el) {
            if (el.className === 'highcharts-menu-item' && el.onmouseout) {
                el.onmouseout();
            }
        });
        chart.highlightedExportItemIx = 0;
        // Hide the menu div
        chart.exportContextMenu.hideMenu();
        // Make sure the chart has focus and can capture keyboard events
        chart.container.focus();
    }
};
 
/**
 * Highlight export menu item by index.
 *
 * @private
 * @function Highcharts.Chart#highlightExportItem
 *
 * @param {number} ix
 *
 * @return {true|undefined}
 */
_Globals2.default.Chart.prototype.highlightExportItem = function (ix) {
    var listItem = this.exportDivElements && this.exportDivElements[ix],
        curHighlighted = this.exportDivElements && this.exportDivElements[this.highlightedExportItemIx],
        hasSVGFocusSupport;
 
    if (listItem && listItem.tagName === 'DIV' && !(listItem.children && listItem.children.length)) {
        // Test if we have focus support for SVG elements
        hasSVGFocusSupport = !!(this.renderTo.getElementsByTagName('g')[0] || {}).focus;
 
        // Only focus if we can set focus back to the elements after
        // destroying the menu (#7422)
        if (listItem.focus && hasSVGFocusSupport) {
            listItem.focus();
        }
        if (curHighlighted && curHighlighted.onmouseout) {
            curHighlighted.onmouseout();
        }
        if (listItem.onmouseover) {
            listItem.onmouseover();
        }
        this.highlightedExportItemIx = ix;
        return true;
    }
};
 
/**
 * Try to highlight the last valid export menu item.
 *
 * @private
 * @function Highcharts.Chart#highlightLastExportItem
 */
_Globals2.default.Chart.prototype.highlightLastExportItem = function () {
    var chart = this,
        i;
 
    if (chart.exportDivElements) {
        i = chart.exportDivElements.length;
        while (i--) {
            if (chart.highlightExportItem(i)) {
                return true;
            }
        }
    }
    return false;
};
 
/**
 * The MenuComponent class
 *
 * @private
 * @class
 * @name Highcharts.MenuComponent
 * @param {Highcharts.Chart} chart
 *        Chart object
 */
var MenuComponent = function MenuComponent(chart) {
    this.initBase(chart);
    this.init();
};
MenuComponent.prototype = new _AccessibilityComponent2.default();
_Globals2.default.extend(MenuComponent.prototype, /** @lends Highcharts.MenuComponent */{
 
    /**
     * Init the component
     */
    init: function init() {
        var chart = this.chart;
        // Hide the export menu from screen readers when it is hidden visually
        this.addEvent(chart, 'exportMenuHidden', function () {
            var menu = this.exportContextMenu;
            if (menu) {
                menu.setAttribute('aria-hidden', true);
            }
        });
    },
 
    /**
     * Called on each render of the chart. We need to update positioning of the
     * proxy overlay.
     */
    onChartRender: function onChartRender() {
        var component = this,
            chart = this.chart,
            a11yOptions = chart.options.accessibility;
 
        // Always start with a clean slate
        this.removeElement(this.exportProxyGroup);
 
        // Set screen reader properties on export menu
        if (chart.options.exporting && chart.options.exporting.enabled !== false && chart.options.exporting.accessibility && chart.options.exporting.accessibility.enabled && chart.exportSVGElements && chart.exportSVGElements[0] && chart.exportSVGElements[0].element) {
            // Set event handler on button if not already done
            var button = chart.exportSVGElements[0],
                buttonElement = button.element,
                oldExportCallback = buttonElement.onclick;
            if (this.wrappedButton !== buttonElement) {
                buttonElement.onclick = function () {
                    oldExportCallback.apply(this, Array.prototype.slice.call(arguments));
                    component.addAccessibleContextMenuAttribs();
                    chart.highlightExportItem(0);
                };
                this.wrappedButton = buttonElement;
            }
 
            // Proxy button and group
            this.exportProxyGroup = this.addProxyGroup(
            // Wrap in a region div if verbosity is high
            a11yOptions.landmarkVerbosity === 'all' ? {
                'aria-label': chart.langFormat('accessibility.exporting.exportRegionLabel', { chart: chart }),
                'role': 'region'
            } : null);
 
            this.exportButtonProxy = this.createProxyButton(button, this.exportProxyGroup, {
                'aria-label': chart.langFormat('accessibility.exporting.menuButtonLabel', { chart: chart })
            });
        }
    },
 
    /**
     * Add ARIA to context menu
     * @private
     */
    addAccessibleContextMenuAttribs: function addAccessibleContextMenuAttribs() {
        var chart = this.chart,
            exportList = chart.exportDivElements,
            contextMenu = chart.exportContextMenu;
 
        if (exportList && exportList.length) {
            // Set tabindex on the menu items to allow focusing by script
            // Set role to give screen readers a chance to pick up the contents
            exportList.forEach(function (item) {
                if (item.tagName === 'DIV' && !(item.children && item.children.length)) {
                    item.setAttribute('role', 'menuitem');
                    item.setAttribute('tabindex', -1);
                }
            });
            // Set accessibility properties on parent div
            exportList[0].parentNode.setAttribute('role', 'menu');
            exportList[0].parentNode.setAttribute('aria-label', chart.langFormat('accessibility.exporting.chartMenuLabel', { chart: chart }));
        }
        if (contextMenu) {
            this.unhideElementFromScreenReaders(contextMenu);
        }
    },
 
    /**
     * Get keyboard navigation handler for this component.
     * @return {Highcharts.KeyboardNavigationHandler}
     */
    getKeyboardNavigation: function getKeyboardNavigation() {
        var keys = this.keyCodes,
            chart = this.chart,
            a11yOptions = chart.options.accessibility,
            component = this;
 
        return new _KeyboardNavigationHandler2.default(chart, {
            keyCodeMap: [
            // Arrow prev handler
            [[keys.left, keys.up], function () {
                var i = chart.highlightedExportItemIx || 0;
 
                // Try to highlight prev item in list. Highlighting e.g.
                // separators will fail.
                while (i--) {
                    if (chart.highlightExportItem(i)) {
                        return this.response.success;
                    }
                }
 
                // We failed, so wrap around or move to prev module
                if (a11yOptions.keyboardNavigation.wrapAround) {
                    chart.highlightLastExportItem();
                    return this.response.success;
                }
                return this.response.prev;
            }],
 
            // Arrow next handler
            [[keys.right, keys.down], function () {
                var i = (chart.highlightedExportItemIx || 0) + 1;
 
                // Try to highlight next item in list. Highlighting e.g.
                // separators will fail.
                for (; i < chart.exportDivElements.length; ++i) {
                    if (chart.highlightExportItem(i)) {
                        return this.response.success;
                    }
                }
 
                // We failed, so wrap around or move to next module
                if (a11yOptions.keyboardNavigation.wrapAround) {
                    chart.highlightExportItem(0);
                    return this.response.success;
                }
                return this.response.next;
            }],
 
            // Click handler
            [[keys.enter, keys.space], function () {
                component.fakeClickEvent(chart.exportDivElements[chart.highlightedExportItemIx]);
                return this.response.success;
            }],
 
            // ESC handler
            [[keys.esc], function () {
                return this.response.prev;
            }]],
 
            // Only run exporting navigation if exporting support exists and is
            // enabled on chart
            validate: function validate() {
                return chart.exportChart && chart.options.exporting.enabled !== false && chart.options.exporting.accessibility.enabled !== false;
            },
 
            // Show export menu
            init: function init(direction) {
                chart.showExportMenu();
 
                // If coming back to export menu from other module, try to
                // highlight last item in menu
                if (direction < 0) {
                    chart.highlightLastExportItem();
                }
            },
 
            // Hide the menu
            terminate: function terminate() {
                chart.hideExportMenu();
            }
        });
    }
 
});
 
exports.default = MenuComponent;