Debian Codegraph @

inst/lib/bs-a11y-p/src/js/carousel.js

raw
1 // Carousel Extension
2 // ===============================
3
4 $('.carousel').each(function (index) {
5
6 // This function positions a highlight box around the tabs in the tablist to use in focus styling
7
8 function setTablistHighlightBox() {
9
10 var $tab
11 , offset
12 , height
13 , width
14 , highlightBox = {}
15
16 highlightBox.top = 0
17 highlightBox.left = 32000
18 highlightBox.height = 0
19 highlightBox.width = 0
20
21 for (var i = 0; i < $tabs.length; i++) {
22 $tab = $tabs[i]
23 offset = $($tab).offset()
24 height = $($tab).height()
25 width = $($tab).width()
26
27 // console.log(" Top: " + offset.top + " Left: " + offset.left + " Height: " + height + " Width: " + width)
28
29 if (highlightBox.top < offset.top) {
30 highlightBox.top = Math.round(offset.top)
31 }
32
33 if (highlightBox.height < height) {
34 highlightBox.height = Math.round(height)
35 }
36
37 if (highlightBox.left > offset.left) {
38 highlightBox.left = Math.round(offset.left)
39 }
40
41 var w = (offset.left - highlightBox.left) + Math.round(width)
42
43 if (highlightBox.width < w) {
44 highlightBox.width = w
45 }
46
47 } // end for
48
49 // console.log("[HIGHLIGHT] Top: " + highlightBox.top + " Left: " + highlightBox.left + " Height: " + highlightBox.height + " Width: " + highlightBox.width)
50
51 $tablistHighlight.style.top = (highlightBox.top - 2) + 'px'
52 $tablistHighlight.style.left = (highlightBox.left - 2) + 'px'
53 $tablistHighlight.style.height = (highlightBox.height + 7) + 'px'
54 $tablistHighlight.style.width = (highlightBox.width + 8) + 'px'
55
56 } // end function
57
58 var $this = $(this)
59 , $prev = $this.find('[data-slide="prev"]')
60 , $next = $this.find('[data-slide="next"]')
61 , $tablist = $this.find('.carousel-indicators')
62 , $tabs = $this.find('.carousel-indicators li')
63 , $tabpanels = $this.find('.item')
64 , $tabpanel
65 , $tablistHighlight
66 , $pauseCarousel
67 , $complementaryLandmark
68 , $tab
69 , $is_paused = false
70 , offset
71 , height
72 , width
73 , i
74 , id_title = 'id_title'
75 , id_desc = 'id_desc'
76
77
78 $tablist.attr('role', 'tablist')
79
80 $tabs.focus(function() {
81 $this.carousel('pause')
82 $is_paused = true
83 $pauseCarousel.innerHTML = "Play Carousel"
84 $(this).parent().addClass('active');
85 // $(this).addClass('focus')
86 setTablistHighlightBox()
87 $($tablistHighlight).addClass('focus')
88 $(this).parents('.carousel').addClass('contrast')
89 })
90
91 $tabs.blur(function(event) {
92 $(this).parent().removeClass('active');
93 // $(this).removeClass('focus')
94 $($tablistHighlight).removeClass('focus')
95 $(this).parents('.carousel').removeClass('contrast')
96 })
97
98
99 for (i = 0; i < $tabpanels.length; i++) {
100 $tabpanel = $tabpanels[i]
101 $tabpanel.setAttribute('role', 'tabpanel')
102 $tabpanel.setAttribute('id', 'tabpanel-' + index + '-' + i)
103 $tabpanel.setAttribute('aria-labelledby', 'tab-' + index + '-' + i)
104 }
105
106 if (typeof $this.attr('role') !== 'string') {
107 $this.attr('role', 'complementary');
108 $this.attr('aria-labelledby', id_title);
109 $this.attr('aria-describedby', id_desc);
110 $this.prepend('<p id="' + id_desc + '" class="sr-only">A carousel is a rotating set of images, rotation stops on keyboard focus on carousel tab controls or hovering the mouse pointer over images. Use the tabs or the previous and next buttons to change the displayed slide.</p>')
111 $this.prepend('<h2 id="' + id_title + '" class="sr-only">Carousel content with ' + $tabpanels.length + ' slides.</h2>')
112 }
113
114
115 for (i = 0; i < $tabs.length; i++) {
116 $tab = $tabs[i]
117
118 $tab.setAttribute('role', 'tab')
119 $tab.setAttribute('id', 'tab-' + index + '-' + i)
120 $tab.setAttribute('aria-controls', 'tabpanel-' + index + '-' + i)
121
122 var tpId = '#tabpanel-' + index + '-' + i
123 var caption = $this.find(tpId).find('h1').text()
124
125 if ((typeof caption !== 'string') || (caption.length === 0)) caption = $this.find(tpId).text()
126 if ((typeof caption !== 'string') || (caption.length === 0)) caption = $this.find(tpId).find('h3').text()
127 if ((typeof caption !== 'string') || (caption.length === 0)) caption = $this.find(tpId).find('h4').text()
128 if ((typeof caption !== 'string') || (caption.length === 0)) caption = $this.find(tpId).find('h5').text()
129 if ((typeof caption !== 'string') || (caption.length === 0)) caption = $this.find(tpId).find('h6').text()
130 if ((typeof caption !== 'string') || (caption.length === 0)) caption = "no title";
131
132 // console.log("CAPTION: " + caption )
133
134 var tabName = document.createElement('span')
135 tabName.setAttribute('class', 'sr-only')
136 tabName.innerHTML='Slide ' + (i+1)
137 if (caption) tabName.innerHTML += ": " + caption
138 $tab.appendChild(tabName)
139
140 }
141
142 // create div for focus styling of tablist
143 $tablistHighlight = document.createElement('div')
144 $tablistHighlight.className = 'carousel-tablist-highlight'
145 document.body.appendChild($tablistHighlight)
146
147 // create button for screen reader users to stop rotation of carousel
148
149 // create button for screen reader users to pause carousel for virtual mode review
150 $complementaryLandmark = document.createElement('aside')
151 $complementaryLandmark.setAttribute('class', 'carousel-aside-pause')
152 $complementaryLandmark.setAttribute('aria-label', 'carousel pause/play control')
153 $this.prepend($complementaryLandmark)
154
155 $pauseCarousel = document.createElement('button')
156 $pauseCarousel.className = "carousel-pause-button"
157 $pauseCarousel.innerHTML = "Pause Carousel"
158 $pauseCarousel.setAttribute('title', "Pause/Play carousel button can be used by screen reader users to stop carousel animations")
159 $($complementaryLandmark).append($pauseCarousel)
160
161 $($pauseCarousel).click(function() {
162 if ($is_paused) {
163 $pauseCarousel.innerHTML = "Pause Carousel"
164 $this.carousel('cycle')
165 $is_paused = false
166 }
167 else {
168 $pauseCarousel.innerHTML = "Play Carousel"
169 $this.carousel('pause')
170 $is_paused = true
171 }
172 })
173 $($pauseCarousel).focus(function() {
174 $(this).addClass('focus')
175 })
176
177 $($pauseCarousel).blur(function() {
178 $(this).removeClass('focus')
179 })
180
181 setTablistHighlightBox()
182
183 $( window ).resize(function() {
184 setTablistHighlightBox()
185 })
186
187 // Add space bar behavior to prev and next buttons for SR compatibility
188 $prev.attr('aria-label', 'Previous Slide')
189 $prev.keydown(function(e) {
190 var k = e.which || e.keyCode
191 if (/(13|32)/.test(k)) {
192 e.preventDefault()
193 e.stopPropagation()
194 $prev.trigger('click');
195 }
196 });
197
198 $prev.focus(function() {
199 $(this).parents('.carousel').addClass('contrast')
200 })
201
202 $prev.blur(function() {
203 $(this).parents('.carousel').removeClass('contrast')
204 })
205
206 $next.attr('aria-label', 'Next Slide')
207 $next.keydown(function(e) {
208 var k = e.which || e.keyCode
209 if (/(13|32)/.test(k)) {
210 e.preventDefault()
211 e.stopPropagation()
212 $next.trigger('click');
213 }
214 });
215
216 $next.focus(function() {
217 $(this).parents('.carousel').addClass('contrast')
218 })
219
220 $next.blur(function() {
221 $(this).parents('.carousel').removeClass('contrast')
222 })
223
224 $('.carousel-inner a').focus(function() {
225 $(this).parents('.carousel').addClass('contrast')
226 })
227
228 $('.carousel-inner a').blur(function() {
229 $(this).parents('.carousel').removeClass('contrast')
230 })
231
232 $tabs.each(function () {
233 var item = $(this)
234 if(item.hasClass('active')) {
235 item.attr({ 'aria-selected': 'true', 'tabindex' : '0' })
236 }else{
237 item.attr({ 'aria-selected': 'false', 'tabindex' : '-1' })
238 }
239 })
240 })
241
242 var slideCarousel = $.fn.carousel.Constructor.prototype.slide
243 $.fn.carousel.Constructor.prototype.slide = function (type, next) {
244 var $element = this.$element
245 , $active = $element.find('[role=tabpanel].active')
246 , $next = next || $active[type]()
247 , $tab
248 , $tab_count = $element.find('[role=tabpanel]').length
249 , $prev_side = $element.find('[data-slide="prev"]')
250 , $next_side = $element.find('[data-slide="next"]')
251 , $index = 0
252 , $prev_index = $tab_count -1
253 , $next_index = 1
254 , $id
255
256 if ($next && $next.attr('id')) {
257 $id = $next.attr('id')
258 $index = $id.lastIndexOf("-")
259 if ($index >= 0) $index = parseInt($id.substring($index+1), 10)
260
261 $prev_index = $index - 1
262 if ($prev_index < 1) $prev_index = $tab_count - 1
263
264 $next_index = $index + 1
265 if ($next_index >= $tab_count) $next_index = 0
266 }
267
268 $prev_side.attr('aria-label', 'Show slide ' + ($prev_index+1) + ' of ' + $tab_count)
269 $next_side.attr('aria-label', 'Show slide ' + ($next_index+1) + ' of ' + $tab_count)
270
271
272 slideCarousel.apply(this, arguments)
273
274 $active
275 .one('bsTransitionEnd', function () {
276 var $tab
277
278 $tab = $element.find('li[aria-controls="' + $active.attr('id') + '"]')
279 if ($tab) $tab.attr({'aria-selected':false, 'tabIndex': '-1'})
280
281 $tab = $element.find('li[aria-controls="' + $next.attr('id') + '"]')
282 if ($tab) $tab.attr({'aria-selected': true, 'tabIndex': '0'})
283
284 })
285 }
286
287 var $this;
288 $.fn.carousel.Constructor.prototype.keydown = function (e) {
289
290 $this = $this || $(this)
291 if(this instanceof Node) $this = $(this)
292
293 function selectTab(index) {
294 if (index >= $tabs.length) return
295 if (index < 0) return
296
297 $carousel.carousel(index)
298 setTimeout(function () {
299 $tabs[index].focus()
300 // $this.prev().focus()
301 }, 150)
302 }
303
304 var $carousel = $(e.target).closest('.carousel')
305 , $tabs = $carousel.find('[role=tab]')
306 , k = e.which || e.keyCode
307 , index
308
309 if (!/(37|38|39|40)/.test(k)) return
310
311 index = $tabs.index($tabs.filter('.active'))
312 if (k == 37 || k == 38) { // Up
313 index--
314 selectTab(index);
315 }
316
317 if (k == 39 || k == 40) { // Down
318 index++
319 selectTab(index);
320 }
321
322 e.preventDefault()
323 e.stopPropagation()
324 }
325 $(document).on('keydown.carousel.data-api', 'li[role=tab]', $.fn.carousel.Constructor.prototype.keydown);