-
Notifications
You must be signed in to change notification settings - Fork 12
/
owlcarousel2-a11ylayer.js
289 lines (252 loc) · 7.67 KB
/
owlcarousel2-a11ylayer.js
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
/**
* Owl Carousel v2 Accessibility Plugin
* Version 0.2.1
* © Geoffrey Roberts 2016
*/
;(function($, window, document){
var Owl2A11y = function(carousel) {
this._core = carousel;
this._initialized = false;
this._core._options = $.extend(Owl2A11y.defaults, this._core.options);
this.$element = this._core.$element;
var setCurrent = $.proxy(function(e) {
this.setCurrent(e);
}, this);
this._handlers = {
'initialized.owl.carousel': $.proxy(function(e) {
this.setupRoot();
if (e.namespace && !this._initialized) {
this.setupFocus();
this.setupKeyboard();
}
this.setCurrent(e);
}, this),
'changed.owl.carousel': setCurrent,
'translated.owl.carousel': setCurrent,
'refreshed.owl.carousel': setCurrent,
'resized.owl.carousel': setCurrent
};
this.$element.on(this._handlers);
};
/* PREFERENCES */
/**
* Contains default parameters, if there were any.
*/
Owl2A11y.defaults = {};
/* EVENT HANDLERS */
/**
* Adds support for things that don't map nicely to the root object
* such as event handlers.
*/
Owl2A11y.eventHandlers = {};
/**
* Get a callback for keyup events within this carousel.
*
* @return callback
* An event callback that takes an Event as an argument.
*/
Owl2A11y.prototype.getDocumentKeyUp = function(){
var self = this;
return function(e) {
var eventTarg = $(e.target),
targ = self.focused(eventTarg),
action = null;
if (!!targ) {
if (e.keyCode == 37 || e.keyCode == 38) {
action = 'prev.owl.carousel';
}
else if (e.keyCode == 39 || e.keyCode == 40) {
action = 'next.owl.carousel';
}
else if (e.keyCode == 13) {
if (eventTarg.hasClass('owl-prev')) action = 'prev.owl.carousel';
else if (eventTarg.hasClass('owl-next')) action = 'next.owl.carousel';
else if (eventTarg.hasClass('owl-dot')) action = 'click';
}
if (!!action) targ.trigger(action);
}
};
};
/* SETUP AND TEAR DOWN */
/**
* Assign attributes to the root element.
*/
Owl2A11y.prototype.setupRoot = function() {
this.$element.attr({
role: 'listbox',
tabindex: '0'
});
};
/**
* Setup keyboard events for this carousel.
*/
Owl2A11y.prototype.setupKeyboard = function(){
// Only needed to initialise once for the entire document
if (!this.$element.attr('data-owl-access-keyup')) {
this.$element.bind('keyup', this.getDocumentKeyUp())
.attr('data-owl-access-keyup', '1');
}
this.$element.attr('data-owl-carousel-focusable', '1');
};
/**
* Setup focusing behaviour for the carousel.
*/
Owl2A11y.prototype.setupFocus = function(){
// Only needed to initialise once for the entire document
this.$element.bind('focusin', function(){
$(this).attr({
'data-owl-carousel-focused': '1',
'aria-live': 'polite'
}).trigger('stop.owl.autoplay');
}).bind('focusout', function(){
$(this).attr({
'data-owl-carousel-focused': '0',
'aria-live': 'off'
}).trigger('play.owl.autoplay');
});
// Add tabindex to allow navigation to be focused.
if (!!this._core._plugins.navigation) {
var navPlugin = this._core._plugins.navigation,
toFocus = [];
if (!!navPlugin._controls.$previous) {
toFocus.push(navPlugin._controls.$previous);
}
if (!!navPlugin._controls.$next) {
toFocus.push(navPlugin._controls.$next);
}
if (!!navPlugin._controls.$indicators) {
toFocus.push(navPlugin._controls.$indicators.children());
}
$.each(toFocus, function(){
this.attr('tabindex', '0');
});
}
};
/**
* Assign attributes to the root element.
*/
Owl2A11y.prototype.destroy = function() {
this.$element.unbind('keyup', this.eventHandlers.documentKeyUp)
.removeAttr('data-owl-access-keyup data-owl-carousel-focusable')
.unbind('focusin focusout');
};
/* HELPER FUNCTIONS */
/**
* Identifies all focusable elements within a given element.
*
* @param DOMElement elem
* A DOM element.
*
* @return jQuery
* A jQuery object that may refer to zero or more focusable elements.
*/
Owl2A11y.prototype.focusableElems = function(elem) {
return $(elem).find('a, input, select, button, *[tabindex]');
};
/**
* Identifies all focusable elements within a given element.
*
* @param jQeury elems
* A jQuery object that may refer to zero or more focusable elements.
* @param boolean enable
* Whether focus is to be enabled on these elements or not.
*/
Owl2A11y.prototype.adjustFocus = function(elems, enable){
elems.each(function(){
var item = $(this);
var newTabIndex = '0',
storeTabIndex = '0';
currentTabIndex = item.attr('tabindex'),
storedTabIndex = item.attr('data-owl-temp-tabindex');
if (enable) {
newTabIndex = (
typeof(storedTabIndex) != 'undefined' && (storedTabIndex != '-1') ?
item.attr('data-owl-temp-tabindex') :
'0'
);
storedTabIndex = newTabIndex;
}
else {
newTabIndex = '-1';
storedTabIndex = (
(typeof(currentTabIndex) != 'undefined') || (currentTabIndex != '-1') ?
currentTabIndex :
'0'
);
}
item.attr({
tabindex: newTabIndex,
'data-owl-temp-tabindex': storeTabIndex
});
});
};
/**
* Get the root element if we are focused within it.
*
* @param DOMElement targ
* An element that might be within this carousel.
*
* @return mixed
* Either the jQuery element containing the root element, or NULL.
*/
Owl2A11y.prototype.focused = function(targ){
var targ = $(targ);
if (targ.attr('data-owl-carousel-focused') == 1) {
return targ;
}
var closest = targ.closest('[data-owl-carousel-focused="1"]');
if (closest.length > 0) return closest;
return null;
};
/* UPDATE FUNCTIONS */
/**
* Identify active elements, set WAI-ARIA sttributes accordingly,
* scroll to show element if we need to, and set up focusing.
*
* @param Event e
* The triggering event.
*/
Owl2A11y.prototype.setCurrent = function(e) {
var targ = this.focused($(':focus')),
element = this._core.$element,
stage = this._core.$stage,
focusableElems = this.focusableElems,
adjustFocus = this.adjustFocus;
if (!!stage) {
var offs = stage.offset();
if (!!targ) {
window.scrollTo(
offs.left,
offs.top - parseInt($('body').css('padding-top'), 10)
);
}
this._core.$stage.children().each(function(i) {
var item = $(this);
var focusable = focusableElems(this);
// Use the active class to determine if we can see it or not.
// Pretty lazy, but the Owl API doesn't make it easy to tell
// from indices alone.
if (item.hasClass('active')) {
item.attr('aria-hidden', 'false');
adjustFocus(focusable, true);
}
else {
item.attr('aria-hidden', 'true');
adjustFocus(focusable, false);
}
});
if (!!targ) {
// Focus on the root element after we're done moving,
// but only if we're not using the controls.
setTimeout(function(){
var newFocus = element;
if ($(':focus').closest('.owl-controls').length) {
newFocus = $(':focus');
}
newFocus.focus();
}, 250);
}
}
};
$.fn.owlCarousel.Constructor.Plugins['Owl2A11y'] = Owl2A11y;
})(window.Zepto || window.jQuery, window, document);