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
/* *
 *
 *  (c) 2009-2019 Øystein Moseng
 *
 *  Accessibility component for chart legend.
 *
 *  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 }; }
 
/**
 * Highlight legend item by index.
 *
 * @private
 * @function Highcharts.Chart#highlightLegendItem
 *
 * @param {number} ix
 *
 * @return {boolean}
 */
_Globals2.default.Chart.prototype.highlightLegendItem = function (ix) {
    var items = this.legend.allItems,
        oldIx = this.highlightedLegendItemIx;
 
    if (items[ix]) {
        if (items[oldIx]) {
            _Globals2.default.fireEvent(items[oldIx].legendGroup.element, 'mouseout');
        }
        // Scroll if we have to
        if (items[ix].pageIx !== undefined && items[ix].pageIx + 1 !== this.legend.currentPage) {
            this.legend.scroll(1 + items[ix].pageIx - this.legend.currentPage);
        }
        // Focus
        this.setFocusToElement(items[ix].legendItem, items[ix].a11yProxyElement);
        _Globals2.default.fireEvent(items[ix].legendGroup.element, 'mouseover');
        return true;
    }
    return false;
};
 
// Keep track of pressed state for legend items
_Globals2.default.addEvent(_Globals2.default.Legend, 'afterColorizeItem', function (e) {
    var chart = this.chart,
        a11yOptions = chart.options.accessibility,
        legendItem = e.item;
    if (a11yOptions.enabled && legendItem && legendItem.a11yProxyElement) {
        legendItem.a11yProxyElement.setAttribute('aria-pressed', e.visible ? 'false' : 'true');
    }
});
 
/**
 * The LegendComponent class
 *
 * @private
 * @class
 * @name Highcharts.LegendComponent
 * @param {Highcharts.Chart} chart
 *        Chart object
 */
var LegendComponent = function LegendComponent(chart) {
    this.initBase(chart);
};
LegendComponent.prototype = new _AccessibilityComponent2.default();
_Globals2.default.extend(LegendComponent.prototype, /** @lends Highcharts.LegendComponent */{
 
    /**
     * The legend needs updates on every render, in order to update positioning
     * of the proxy overlays.
     */
    onChartRender: function onChartRender() {
        var chart = this.chart,
            a11yOptions = chart.options.accessibility,
            items = chart.legend && chart.legend.allItems,
            component = this;
 
        // Ignore render after proxy clicked. No need to destroy it, and
        // destroying also kills focus.
        if (component.legendProxyButtonClicked) {
            delete component.legendProxyButtonClicked;
            return;
        }
 
        // Always Remove group if exists
        this.removeElement(this.legendProxyGroup);
 
        // Skip everything if we do not have legend items, or if we have a
        // color axis
        if (!items || !items.length || chart.colorAxis && chart.colorAxis.length || !chart.options.legend.accessibility.enabled) {
            return;
        }
 
        // Add proxy group
        this.legendProxyGroup = this.addProxyGroup({
            'aria-label': chart.langFormat('accessibility.legendLabel'),
            'role': a11yOptions.landmarkVerbosity === 'all' ? 'region' : null
        });
 
        // Proxy the legend items
        items.forEach(function (item) {
            if (item.legendItem && item.legendItem.element) {
                item.a11yProxyElement = component.createProxyButton(item.legendItem, component.legendProxyGroup, {
                    tabindex: -1,
                    'aria-pressed': !item.visible,
                    'aria-label': chart.langFormat('accessibility.legendItem', {
                        chart: chart,
                        itemName: component.stripTags(item.name)
                    })
                },
                // Consider useHTML
                item.legendGroup.div ? item.legendItem : item.legendGroup,
                // Additional click event (fires first)
                function () {
                    // Keep track of when we should ignore next render
                    component.legendProxyButtonClicked = true;
                });
            }
        });
    },
 
    /**
     * Get keyboard navigation handler for this component.
     * @return {Highcharts.KeyboardNavigationHandler}
     */
    getKeyboardNavigation: function getKeyboardNavigation() {
        var keys = this.keyCodes,
            component = this,
            chart = this.chart,
            a11yOptions = chart.options.accessibility;
        return new _KeyboardNavigationHandler2.default(chart, {
            keyCodeMap: [
            // Arrow key handling
            [[keys.left, keys.right, keys.up, keys.down], function (keyCode) {
                var direction = keyCode === keys.left || keyCode === keys.up ? -1 : 1;
 
                // Try to highlight next/prev legend item
                var res = chart.highlightLegendItem(component.highlightedLegendItemIx + direction);
                if (res) {
                    component.highlightedLegendItemIx += direction;
                    return this.response.success;
                }
 
                // Failed, can we wrap around?
                if (chart.legend.allItems.length > 1 && a11yOptions.keyboardNavigation.wrapAround) {
                    // Wrap around if we failed and have more than 1 item
                    this.init(direction);
                    return this.response.success;
                }
 
                // No wrap, move
                return this.response[direction > 0 ? 'next' : 'prev'];
            }],
 
            // Click item
            [[keys.enter, keys.space], function () {
                var legendItem = chart.legend.allItems[component.highlightedLegendItemIx];
                if (legendItem && legendItem.a11yProxyElement) {
                    _Globals2.default.fireEvent(legendItem.a11yProxyElement, 'click');
                }
                return this.response.success;
            }]],
 
            // Only run this module if we have at least one legend - wait for
            // it - item. Don't run if the legend is populated by a colorAxis.
            // Don't run if legend navigation is disabled.
            validate: function validate() {
                var legendOptions = chart.options.legend;
                return chart.legend && chart.legend.allItems && chart.legend.display && !(chart.colorAxis && chart.colorAxis.length) && legendOptions && legendOptions.accessibility && legendOptions.accessibility.enabled && legendOptions.accessibility.keyboardNavigation && legendOptions.accessibility.keyboardNavigation.enabled;
            },
 
            // Focus first/last item
            init: function init(direction) {
                var ix = direction > 0 ? 0 : chart.legend.allItems.length - 1;
                chart.highlightLegendItem(ix);
                component.highlightedLegendItemIx = ix;
            }
        });
    }
 
});
 
exports.default = LegendComponent;