-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
236 lines (208 loc) · 6.76 KB
/
plugin.js
File metadata and controls
236 lines (208 loc) · 6.76 KB
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
/**
* CSSS A11y Plugin
*
* Helps to make slides more keyboard and screen reader friendly by
* providing the following:
*
* - Sets `role` for each slide for semantic context
* - Sets `aria-roledescription` for each slide for extra context
* - Sets `aria-label` for each slide to provide an accessible name
* - Outputs a link list for keyboard controls to load previous and next slides
* - Shifts focus to slide content container on link click
*
* @author Scott Vinkle <scott.vinkle@shopify.com>
* @version 0.1.1
* @license MIT License
*/
(function() {
var self = (window.a11y = {
// i18n
lang: document.documentElement.lang
? document.documentElement.lang.toLowerCase()
: "en",
strings: {
en: {
slide: "slide",
next: "Next",
prev: "Previous",
nextSlide: "Next slide",
prevSlide: "Previous slide"
},
fr: {
slide: "diaporama",
next: "Prochain",
prev: "Précédent",
nextSlide: "Diapositive suivante",
prevSlide: "Diapositive précédente"
}
},
/**
* Set slides to be more screen reader friendly.
*
* @return null
*/
setupSlides: function() {
// Check for `slideshow.slides` object
if (!window.slideshow.slides) {
return;
}
// Local slide object
var slide = {
all: window.slideshow.slides,
current: null,
index: 0,
next: null,
prev: null,
title: "",
total: window.slideshow.slides.length
};
// Iterarte over each slide available…
for (; slide.index < slide.total; slide.index++) {
// Cache current slide
slide.current = slide.all[slide.index];
// Previous slide
if (slide.index !== 0) {
slide.prev = slide.all[slide.index - 1];
}
// Next slide
slide.next = slide.all[slide.index + 1];
// Cache current slide title
slide.title = slide.current.getAttribute("data-title");
// Set current slide attributes
slide.current.setAttribute("role", "region");
slide.current.setAttribute(
"aria-roledescription",
self.strings[self.lang].slide
);
slide.current.setAttribute(
"aria-label",
slide.index + 1 + " - " + slide.title
);
// Setup controls for current slide
self.slideControls(slide.current, slide.prev, slide.next);
}
},
/**
* Create and output the list of links for keyboard and screen reader
* users.
*
* @param {Element} currentSlide Slide `section` HTML element
* @param {Element} prevSlide Slide `section` HTML element
* @param {Element} nextSlide Slide `section` HTML element
* @return null
*/
slideControls: function(currentSlide, prevSlide, nextSlide) {
// Create document fragment to hold list and controls
var fragment = document.createDocumentFragment();
// Create list and controls
var controls = {
list: document.createElement("ul"),
next: {
item: document.createElement("li"),
link: document.createElement("a")
},
prev: {
item: document.createElement("li"),
link: document.createElement("a")
}
};
// List classes
controls.list.setAttribute("role", "list");
controls.list.classList.add("a11y-controls__list");
// List items classes
controls.prev.item.classList.add("a11y-controls__item");
controls.next.item.classList.add("a11y-controls__item");
// Previous link classes
controls.prev.link.classList.add("a11y-controls__link");
controls.prev.link.classList.add("visuallyhidden");
controls.prev.link.classList.add("focusable");
// Next link classes
controls.next.link.classList.add("a11y-controls__link");
controls.next.link.classList.add("visuallyhidden");
controls.next.link.classList.add("focusable");
// Output previous link if available
if (prevSlide !== null) {
// Previous link attributes
controls.prev.link.href = "#" + prevSlide.id;
controls.prev.link.target = "_self";
controls.prev.link.innerHTML =
'<span aria-hidden="true">←</span> ' +
self.strings[self.lang].prev;
controls.prev.link.setAttribute(
"aria-label",
self.strings[self.lang].prevSlide
);
// Load previous slide on click
controls.prev.link.addEventListener(
"click",
function() {
self.shiftFocus(prevSlide);
},
false
);
// Output previous link within list item
controls.prev.item.appendChild(controls.prev.link);
controls.list.appendChild(controls.prev.item);
}
// Output next link if available
if (nextSlide !== undefined) {
// Next link attributes
controls.next.link.href = "#" + nextSlide.id;
controls.next.link.target = "_self";
controls.next.link.innerHTML =
self.strings[self.lang].next +
' <span aria-hidden="true">→</span>';
controls.next.link.setAttribute(
"aria-label",
self.strings[self.lang].nextSlide
);
// Load next slide on click
controls.next.link.addEventListener(
"click",
function() {
self.shiftFocus(nextSlide);
},
false
);
// Output next link within list item
controls.next.item.appendChild(controls.next.link);
controls.list.appendChild(controls.next.item);
}
// Output list and append to slide `section`
fragment.appendChild(controls.list);
currentSlide.appendChild(fragment);
},
/**
* Shift keyboard focus to the Slide on link click event. This helps
* to provide greater context for screen reader users for when a
* slide loads.
*
* @param {Element} slide Slide `section` HTML element
* @return null
*/
shiftFocus: function(slide) {
// Set `tabindex` on the slide in order for it to receive focus
slide.setAttribute("tabindex", -1);
// Send keyboard focus to the slide
slide.focus();
window.setTimeout(function() {
// Remove `tabindex` in order to not disrupt Slide keyboard navigation
slide.removeAttribute("tabindex");
}, 500);
},
/**
* Initialize the plugin by calling setupSlides method.
*
* @return null
*/
init: function() {
// Check for `slideshow` object
if (!window.slideshow) {
return;
}
self.setupSlides();
}
});
// Initialize the plugin when page content has loaded
document.addEventListener("DOMContentLoaded", self.init);
})();