Debian Codegraph @

inst/lib/bs-a11y-p/plugins/js/bootstrap-accessibility.js

raw
1 /* ========================================================================
2 * Extends Bootstrap v3.1.1
3
4 * Copyright (c) <2015> PayPal
5
6 * All rights reserved.
7
8 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
11
12 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
13
14 * Neither the name of PayPal or any of its subsidiaries or affiliates nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17
18 * ======================================================================== */
19
20
21 $(function() {
22 "use strict";
23
24 // GENERAL UTILITY FUNCTIONS
25 // ===============================
26
27 var uniqueId = function(prefix) {
28 return (prefix || 'ui-id') + '-' + Math.floor((Math.random()*1000)+1)
29 }
30
31
32 var removeMultiValAttributes = function (el, attr, val) {
33 var describedby = (el.attr( attr ) || "").split( /\s+/ )
34 , index = $.inArray(val, describedby)
35 if ( index !== -1 ) {
36 describedby.splice( index, 1 )
37 }
38 describedby = $.trim( describedby.join( " " ) )
39 if (describedby ) {
40 el.attr( attr, describedby )
41 } else {
42 el.removeAttr( attr )
43 }
44 }
45
46 // selectors Courtesy: https://github.com/jquery/jquery-ui/blob/master/ui/focusable.js and tabbable.js
47 /*
48 Copyright jQuery Foundation and other contributors, https://jquery.org/
49
50 This software consists of voluntary contributions made by many
51 individuals. For exact contribution history, see the revision history
52 available at https://github.com/jquery/jquery-ui
53
54 The following license applies to all parts of this software except as
55 documented below:
56
57 ====
58
59 Permission is hereby granted, free of charge, to any person obtaining
60 a copy of this software and associated documentation files (the
61 "Software"), to deal in the Software without restriction, including
62 without limitation the rights to use, copy, modify, merge, publish,
63 distribute, sublicense, and/or sell copies of the Software, and to
64 permit persons to whom the Software is furnished to do so, subject to
65 the following conditions:
66
67 The above copyright notice and this permission notice shall be
68 included in all copies or substantial portions of the Software.
69
70 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
71 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
72 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
73 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
74 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
75 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
76 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
77
78 ====
79
80 Copyright and related rights for sample code are waived via CC0. Sample
81 code is defined as all source code contained within the demos directory.
82
83 CC0: http://creativecommons.org/publicdomain/zero/1.0/
84
85 ====
86 */
87
88 var focusable = function ( element, isTabIndexNotNaN ) {
89 var map, mapName, img,
90 nodeName = element.nodeName.toLowerCase();
91 if ( "area" === nodeName ) {
92 map = element.parentNode;
93 mapName = map.name;
94 if ( !element.href || !mapName || map.nodeName.toLowerCase() !== "map" ) {
95 return false;
96 }
97 img = $( "img[usemap='#" + mapName + "']" )[ 0 ];
98 return !!img && visible( img );
99 }
100 return ( /input|select|textarea|button|object/.test( nodeName ) ?
101 !element.disabled :
102 "a" === nodeName ?
103 element.href || isTabIndexNotNaN :isTabIndexNotNaN) && visible( element ); // the element and all of its ancestors must be visible
104 }
105 var visible = function ( element ) {
106 return $.expr.filters.visible( element ) &&
107 !$( element ).parents().addBack().filter(function() {
108 return $.css( this, "visibility" ) === "hidden";
109 }).length;
110 }
111
112 $.extend( $.expr[ ":" ], {
113 data: $.expr.createPseudo ?
114 $.expr.createPseudo(function( dataName ) {
115 return function( elem ) {
116 return !!$.data( elem, dataName );
117 };
118 }) :
119 // support: jQuery <1.8
120 function( elem, i, match ) {
121 return !!$.data( elem, match[ 3 ] );
122 },
123
124 focusable: function( element ) {
125 return focusable( element, !isNaN( $.attr( element, "tabindex" ) ) );
126 },
127
128 tabbable: function( element ) {
129 var tabIndex = $.attr( element, "tabindex" ),
130 isTabIndexNaN = isNaN( tabIndex );
131 return ( isTabIndexNaN || tabIndex >= 0 ) && focusable( element, !isTabIndexNaN );
132 }
133 });
134
135 // Modal Extension
136 // ===============================
137
138 $('.modal-dialog').attr( {'role' : 'document'})
139 var modalhide = $.fn.modal.Constructor.prototype.hide
140 $.fn.modal.Constructor.prototype.hide = function(){
141 modalhide.apply(this, arguments)
142 $(document).off('keydown.bs.modal')
143 }
144
145 var modalfocus = $.fn.modal.Constructor.prototype.enforceFocus
146 $.fn.modal.Constructor.prototype.enforceFocus = function(){
147 var $content = this.$element.find(".modal-content")
148 var focEls = $content.find(":tabbable")
149 , $lastEl = $(focEls[focEls.length-1])
150 , $firstEl = $(focEls[0])
151 $lastEl.on('keydown.bs.modal', $.proxy(function (ev) {
152 if(ev.keyCode === 9 && !(ev.shiftKey | ev.ctrlKey | ev.metaKey | ev.altKey)) { // TAB pressed
153 ev.preventDefault();
154 $firstEl.focus();
155 }
156 }, this))
157 $firstEl.on('keydown.bs.modal', $.proxy(function (ev) {
158 if(ev.keyCode === 9 && ev.shiftKey) { // SHIFT-TAB pressed
159 ev.preventDefault();
160 $lastEl.focus();
161 }
162 }, this))
163 modalfocus.apply(this, arguments)
164 }
165
166 // DROPDOWN Extension
167 // ===============================
168
169 var toggle = '[data-toggle=dropdown]'
170 , $par
171 , firstItem
172 , focusDelay = 200
173 , menus = $(toggle).parent().find('ul').attr('role','menu')
174 , lis = menus.find('li').attr('role','presentation')
175
176 // add menuitem role and tabIndex to dropdown links
177 lis.find('a').attr({'role':'menuitem', 'tabIndex':'-1'})
178 // add aria attributes to dropdown toggle
179 $(toggle).attr({ 'aria-haspopup':'true', 'aria-expanded': 'false'})
180
181 $(toggle).parent()
182 // Update aria-expanded when open
183 .on('shown.bs.dropdown',function(e){
184 $par = $(this)
185 var $toggle = $par.find(toggle)
186 $toggle.attr('aria-expanded','true')
187 $toggle.on('keydown.bs.dropdown', $.proxy(function (ev) {
188 setTimeout(function() {
189 firstItem = $('.dropdown-menu [role=menuitem]:visible', $par)[0]
190 try{ firstItem.focus()} catch(ex) {}
191 }, focusDelay)
192 }, this))
193
194 })
195 // Update aria-expanded when closed
196 .on('hidden.bs.dropdown',function(e){
197 $par = $(this)
198 var $toggle = $par.find(toggle)
199 $toggle.attr('aria-expanded','false')
200 })
201
202 // Close the dropdown if tabbed away from
203 $(document)
204 .on('focusout.dropdown.data-api', '.dropdown-menu', function(e){
205 var $this = $(this)
206 , that = this;
207 // since we're trying to close when appropriate,
208 // make sure the dropdown is open
209 if (!$this.parent().hasClass('open')) {
210 return;
211 }
212 setTimeout(function() {
213 if(!$.contains(that, document.activeElement)){
214 $this.parent().find('[data-toggle=dropdown]').dropdown('hide')
215 }
216 }, 150)
217 })
218 .on('keydown.bs.dropdown.data-api', toggle + ', [role=menu]' , $.fn.dropdown.Constructor.prototype.keydown);
219
220 // Tab Extension
221 // ===============================
222
223 var $tablist = $('.nav-tabs, .nav-pills')
224 , $lis = $tablist.children('li')
225 , $tabs = $tablist.find('[data-toggle="tab"], [data-toggle="pill"]')
226
227 if($tabs){
228 $tablist.attr('role', 'tablist')
229 $lis.attr('role', 'presentation')
230 $tabs.attr('role', 'tab')
231 }
232
233 $tabs.each(function( index ) {
234 var tabpanel = $($(this).attr('href'))
235 , tab = $(this)
236 , tabid = tab.attr('id') || uniqueId('ui-tab')
237
238 tab.attr('id', tabid)
239
240 if(tab.parent().hasClass('active')){
241 tab.attr( { 'tabIndex' : '0', 'aria-selected' : 'true', 'aria-controls': tab.attr('href').substr(1) } )
242 tabpanel.attr({ 'role' : 'tabpanel', 'tabIndex' : '0', 'aria-hidden' : 'false', 'aria-labelledby':tabid })
243 }else{
244 tab.attr( { 'tabIndex' : '-1', 'aria-selected' : 'false', 'aria-controls': tab.attr('href').substr(1) } )
245 tabpanel.attr( { 'role' : 'tabpanel', 'tabIndex' : '-1', 'aria-hidden' : 'true', 'aria-labelledby':tabid } )
246 }
247 })
248
249 $.fn.tab.Constructor.prototype.keydown = function (e) {
250 var $this = $(this)
251 , $items
252 , $ul = $this.closest('ul[role=tablist] ')
253 , index
254 , k = e.which || e.keyCode
255
256 $this = $(this)
257 if (!/(37|38|39|40)/.test(k)) return
258
259 $items = $ul.find('[role=tab]:visible')
260 index = $items.index($items.filter(':focus'))
261
262 if (k == 38 || k == 37) index-- // up & left
263 if (k == 39 || k == 40) index++ // down & right
264
265
266 if(index < 0) index = $items.length -1
267 if(index == $items.length) index = 0
268
269 var nextTab = $items.eq(index)
270 if(nextTab.attr('role') ==='tab'){
271
272 nextTab.tab('show') //Comment this line for dynamically loaded tabPabels, to save Ajax requests on arrow key navigation
273 .focus()
274 }
275 // nextTab.focus()
276
277 e.preventDefault()
278 e.stopPropagation()
279 }
280
281 $(document).on('keydown.tab.data-api','[data-toggle="tab"], [data-toggle="pill"]' , $.fn.tab.Constructor.prototype.keydown)
282
283 var tabactivate = $.fn.tab.Constructor.prototype.activate;
284 $.fn.tab.Constructor.prototype.activate = function (element, container, callback) {
285 var $active = container.find('> .active')
286 $active.find('[data-toggle=tab], [data-toggle=pill]').attr({ 'tabIndex' : '-1','aria-selected' : false })
287 $active.filter('.tab-pane').attr({ 'aria-hidden' : true,'tabIndex' : '-1' })
288
289 tabactivate.apply(this, arguments)
290
291 element.addClass('active')
292 element.find('[data-toggle=tab], [data-toggle=pill]').attr({ 'tabIndex' : '0','aria-selected' : true })
293 element.filter('.tab-pane').attr({ 'aria-hidden' : false,'tabIndex' : '0' })
294 }
295
296 // Collapse Extension
297 // ===============================
298
299 var $colltabs = $('[data-toggle="collapse"]')
300 $colltabs.each(function( index ) {
301 var colltab = $(this)
302 , collpanel = (colltab.attr('data-target')) ? $(colltab.attr('data-target')) : $(colltab.attr('href'))
303 , parent = colltab.attr('data-parent')
304 , collparent = parent && $(parent)
305 , collid = colltab.attr('id') || uniqueId('ui-collapse')
306 , parentpanel = collpanel.parent() // panel containing title and panel body
307 , parentfirstchild = (collparent) ? collparent.find('.panel.panel-default:first-child') : null // first child of containing accordion
308 , hasopenpanel = (collparent) ? collparent.find('.panel-collapse.in').length > 0 : false // true, if collapse parent has any panels with class 'in'; otherwise, false
309
310 colltab.attr('id', collid)
311
312 if(collparent){
313 colltab.attr({ 'aria-controls': collpanel.attr('id'), 'role':'tab', 'aria-selected':'false', 'aria-expanded':'false' })
314 $(collparent).find('div:not(.collapse,.panel-body), h4').attr('role','presentation')
315 collparent.attr({ 'role' : 'tablist', 'aria-multiselectable' : 'true' })
316 collpanel.attr({ 'role':'tabpanel', 'aria-labelledby':collid })
317
318 if(!hasopenpanel && parentpanel.is(parentfirstchild)) {
319 colltab.attr({ 'tabindex':'0' })
320 collpanel.attr({ 'tabindex':'-1' })
321 }else if(collpanel.hasClass('in')){
322 colltab.attr({ 'aria-selected':'true', 'aria-expanded':'true', 'tabindex':'0' })
323 collpanel.attr({ 'tabindex':'0', 'aria-hidden':'false' })
324 }else{
325 colltab.attr({ 'tabindex':'-1' })
326 collpanel.attr({ 'tabindex':'-1', 'aria-hidden':'true' })
327 }
328 }
329 })
330
331 var collToggle = $.fn.collapse.Constructor.prototype.toggle
332 $.fn.collapse.Constructor.prototype.toggle = function(){
333 var prevTab = this.$parent && this.$parent.find('[aria-expanded="true"]') , href
334
335 if(prevTab){
336 var prevPanel = prevTab.attr('data-target') || (href = prevTab.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')
337 , $prevPanel = $(prevPanel)
338 , $curPanel = this.$element
339 , par = this.$parent
340 , curTab
341
342 if (this.$parent) curTab = this.$parent.find('[data-toggle=collapse][href="#' + this.$element.attr('id') + '"]')
343
344 collToggle.apply(this, arguments)
345
346 if ($.support.transition) {
347 this.$element.one($.support.transition.end, function(){
348
349 prevTab.attr({ 'aria-selected':'false','aria-expanded':'false', 'tabIndex':'-1' })
350 $prevPanel.attr({ 'aria-hidden' : 'true','tabIndex' : '-1'})
351
352 curTab.attr({ 'aria-selected':'true','aria-expanded':'true', 'tabIndex':'0' })
353
354 if($curPanel.hasClass('in')){
355 $curPanel.attr({ 'aria-hidden' : 'false','tabIndex' : '0' })
356 }else{
357 curTab.attr({ 'aria-selected':'false','aria-expanded':'false'})
358 $curPanel.attr({ 'aria-hidden' : 'true','tabIndex' : '-1' })
359 }
360 })
361 }
362 }else{
363 collToggle.apply(this, arguments)
364 }
365 }
366
367 $.fn.collapse.Constructor.prototype.keydown = function (e) {
368 var $this = $(this)
369 , $items
370 , $tablist = $this.closest('div[role=tablist] ')
371 , index
372 , k = e.which || e.keyCode
373
374 $this = $(this)
375 if (!/(32|37|38|39|40)/.test(k)) return
376 if(k==32) $this.click()
377
378 $items = $tablist.find('[role=tab]')
379 index = $items.index($items.filter(':focus'))
380
381 if (k == 38 || k == 37) index-- // up & left
382 if (k == 39 || k == 40) index++ // down & right
383 if(index < 0) index = $items.length -1
384 if(index == $items.length) index = 0
385
386 $items.eq(index).focus()
387
388 e.preventDefault()
389 e.stopPropagation()
390
391 }
392
393 $(document).on('keydown.collapse.data-api','[data-toggle="collapse"]' , $.fn.collapse.Constructor.prototype.keydown);
394
395
396 // Carousel Extension
397 // ===============================
398
399 $('.carousel').each(function (index) {
400
401 // This function positions a highlight box around the tabs in the tablist to use in focus styling
402
403 function setTablistHighlightBox() {
404
405 var $tab
406 , offset
407 , height
408 , width
409 , highlightBox = {}
410
411 highlightBox.top = 0
412 highlightBox.left = 32000
413 highlightBox.height = 0
414 highlightBox.width = 0
415
416 for (var i = 0; i < $tabs.length; i++) {
417 $tab = $tabs[i]
418 offset = $($tab).offset()
419 height = $($tab).height()
420 width = $($tab).width()
421
422 // console.log(" Top: " + offset.top + " Left: " + offset.left + " Height: " + height + " Width: " + width)
423
424 if (highlightBox.top < offset.top) {
425 highlightBox.top = Math.round(offset.top)
426 }
427
428 if (highlightBox.height < height) {
429 highlightBox.height = Math.round(height)
430 }
431
432 if (highlightBox.left > offset.left) {
433 highlightBox.left = Math.round(offset.left)
434 }
435
436 var w = (offset.left - highlightBox.left) + Math.round(width)
437
438 if (highlightBox.width < w) {
439 highlightBox.width = w
440 }
441
442 } // end for
443
444 // console.log("[HIGHLIGHT] Top: " + highlightBox.top + " Left: " + highlightBox.left + " Height: " + highlightBox.height + " Width: " + highlightBox.width)
445
446 $tablistHighlight.style.top = (highlightBox.top - 2) + 'px'
447 $tablistHighlight.style.left = (highlightBox.left - 2) + 'px'
448 $tablistHighlight.style.height = (highlightBox.height + 7) + 'px'
449 $tablistHighlight.style.width = (highlightBox.width + 8) + 'px'
450
451 } // end function
452
453 var $this = $(this)
454 , $prev = $this.find('[data-slide="prev"]')
455 , $next = $this.find('[data-slide="next"]')
456 , $tablist = $this.find('.carousel-indicators')
457 , $tabs = $this.find('.carousel-indicators li')
458 , $tabpanels = $this.find('.item')
459 , $tabpanel
460 , $tablistHighlight
461 , $pauseCarousel
462 , $complementaryLandmark
463 , $tab
464 , $is_paused = false
465 , offset
466 , height
467 , width
468 , i
469 , id_title = 'id_title'
470 , id_desc = 'id_desc'
471
472
473 $tablist.attr('role', 'tablist')
474
475 $tabs.focus(function() {
476 $this.carousel('pause')
477 $is_paused = true
478 $pauseCarousel.innerHTML = "Play Carousel"
479 $(this).parent().addClass('active');
480 // $(this).addClass('focus')
481 setTablistHighlightBox()
482 $($tablistHighlight).addClass('focus')
483 $(this).parents('.carousel').addClass('contrast')
484 })
485
486 $tabs.blur(function(event) {
487 $(this).parent().removeClass('active');
488 // $(this).removeClass('focus')
489 $($tablistHighlight).removeClass('focus')
490 $(this).parents('.carousel').removeClass('contrast')
491 })
492
493
494 for (i = 0; i < $tabpanels.length; i++) {
495 $tabpanel = $tabpanels[i]
496 $tabpanel.setAttribute('role', 'tabpanel')
497 $tabpanel.setAttribute('id', 'tabpanel-' + index + '-' + i)
498 $tabpanel.setAttribute('aria-labelledby', 'tab-' + index + '-' + i)
499 }
500
501 if (typeof $this.attr('role') !== 'string') {
502 $this.attr('role', 'complementary');
503 $this.attr('aria-labelledby', id_title);
504 $this.attr('aria-describedby', id_desc);
505 $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>')
506 $this.prepend('<h2 id="' + id_title + '" class="sr-only">Carousel content with ' + $tabpanels.length + ' slides.</h2>')
507 }
508
509
510 for (i = 0; i < $tabs.length; i++) {
511 $tab = $tabs[i]
512
513 $tab.setAttribute('role', 'tab')
514 $tab.setAttribute('id', 'tab-' + index + '-' + i)
515 $tab.setAttribute('aria-controls', 'tabpanel-' + index + '-' + i)
516
517 var tpId = '#tabpanel-' + index + '-' + i
518 var caption = $this.find(tpId).find('h1').text()
519
520 if ((typeof caption !== 'string') || (caption.length === 0)) caption = $this.find(tpId).text()
521 if ((typeof caption !== 'string') || (caption.length === 0)) caption = $this.find(tpId).find('h3').text()
522 if ((typeof caption !== 'string') || (caption.length === 0)) caption = $this.find(tpId).find('h4').text()
523 if ((typeof caption !== 'string') || (caption.length === 0)) caption = $this.find(tpId).find('h5').text()
524 if ((typeof caption !== 'string') || (caption.length === 0)) caption = $this.find(tpId).find('h6').text()
525 if ((typeof caption !== 'string') || (caption.length === 0)) caption = "no title";
526
527 // console.log("CAPTION: " + caption )
528
529 var tabName = document.createElement('span')
530 tabName.setAttribute('class', 'sr-only')
531 tabName.innerHTML='Slide ' + (i+1)
532 if (caption) tabName.innerHTML += ": " + caption
533 $tab.appendChild(tabName)
534
535 }
536
537 // create div for focus styling of tablist
538 $tablistHighlight = document.createElement('div')
539 $tablistHighlight.className = 'carousel-tablist-highlight'
540 document.body.appendChild($tablistHighlight)
541
542 // create button for screen reader users to stop rotation of carousel
543
544 // create button for screen reader users to pause carousel for virtual mode review
545 $complementaryLandmark = document.createElement('aside')
546 $complementaryLandmark.setAttribute('class', 'carousel-aside-pause')
547 $complementaryLandmark.setAttribute('aria-label', 'carousel pause/play control')
548 $this.prepend($complementaryLandmark)
549
550 $pauseCarousel = document.createElement('button')
551 $pauseCarousel.className = "carousel-pause-button"
552 $pauseCarousel.innerHTML = "Pause Carousel"
553 $pauseCarousel.setAttribute('title', "Pause/Play carousel button can be used by screen reader users to stop carousel animations")
554 $($complementaryLandmark).append($pauseCarousel)
555
556 $($pauseCarousel).click(function() {
557 if ($is_paused) {
558 $pauseCarousel.innerHTML = "Pause Carousel"
559 $this.carousel('cycle')
560 $is_paused = false
561 }
562 else {
563 $pauseCarousel.innerHTML = "Play Carousel"
564 $this.carousel('pause')
565 $is_paused = true
566 }
567 })
568 $($pauseCarousel).focus(function() {
569 $(this).addClass('focus')
570 })
571
572 $($pauseCarousel).blur(function() {
573 $(this).removeClass('focus')
574 })
575
576 setTablistHighlightBox()
577
578 $( window ).resize(function() {
579 setTablistHighlightBox()
580 })
581
582 // Add space bar behavior to prev and next buttons for SR compatibility
583 $prev.attr('aria-label', 'Previous Slide')
584 $prev.keydown(function(e) {
585 var k = e.which || e.keyCode
586 if (/(13|32)/.test(k)) {
587 e.preventDefault()
588 e.stopPropagation()
589 $prev.trigger('click');
590 }
591 });
592
593 $prev.focus(function() {
594 $(this).parents('.carousel').addClass('contrast')
595 })
596
597 $prev.blur(function() {
598 $(this).parents('.carousel').removeClass('contrast')
599 })
600
601 $next.attr('aria-label', 'Next Slide')
602 $next.keydown(function(e) {
603 var k = e.which || e.keyCode
604 if (/(13|32)/.test(k)) {
605 e.preventDefault()
606 e.stopPropagation()
607 $next.trigger('click');
608 }
609 });
610
611 $next.focus(function() {
612 $(this).parents('.carousel').addClass('contrast')
613 })
614
615 $next.blur(function() {
616 $(this).parents('.carousel').removeClass('contrast')
617 })
618
619 $('.carousel-inner a').focus(function() {
620 $(this).parents('.carousel').addClass('contrast')
621 })
622
623 $('.carousel-inner a').blur(function() {
624 $(this).parents('.carousel').removeClass('contrast')
625 })
626
627 $tabs.each(function () {
628 var item = $(this)
629 if(item.hasClass('active')) {
630 item.attr({ 'aria-selected': 'true', 'tabindex' : '0' })
631 }else{
632 item.attr({ 'aria-selected': 'false', 'tabindex' : '-1' })
633 }
634 })
635 })
636
637 var slideCarousel = $.fn.carousel.Constructor.prototype.slide
638 $.fn.carousel.Constructor.prototype.slide = function (type, next) {
639 var $element = this.$element
640 , $active = $element.find('[role=tabpanel].active')
641 , $next = next || $active[type]()
642 , $tab
643 , $tab_count = $element.find('[role=tabpanel]').length
644 , $prev_side = $element.find('[data-slide="prev"]')
645 , $next_side = $element.find('[data-slide="next"]')
646 , $index = 0
647 , $prev_index = $tab_count -1
648 , $next_index = 1
649 , $id
650
651 if ($next && $next.attr('id')) {
652 $id = $next.attr('id')
653 $index = $id.lastIndexOf("-")
654 if ($index >= 0) $index = parseInt($id.substring($index+1), 10)
655
656 $prev_index = $index - 1
657 if ($prev_index < 1) $prev_index = $tab_count - 1
658
659 $next_index = $index + 1
660 if ($next_index >= $tab_count) $next_index = 0
661 }
662
663 $prev_side.attr('aria-label', 'Show slide ' + ($prev_index+1) + ' of ' + $tab_count)
664 $next_side.attr('aria-label', 'Show slide ' + ($next_index+1) + ' of ' + $tab_count)
665
666
667 slideCarousel.apply(this, arguments)
668
669 $active
670 .one('bsTransitionEnd', function () {
671 var $tab
672
673 $tab = $element.find('li[aria-controls="' + $active.attr('id') + '"]')
674 if ($tab) $tab.attr({'aria-selected':false, 'tabIndex': '-1'})
675
676 $tab = $element.find('li[aria-controls="' + $next.attr('id') + '"]')
677 if ($tab) $tab.attr({'aria-selected': true, 'tabIndex': '0'})
678
679 })
680 }
681
682 var $this;
683 $.fn.carousel.Constructor.prototype.keydown = function (e) {
684
685 $this = $this || $(this)
686 if(this instanceof Node) $this = $(this)
687
688 function selectTab(index) {
689 if (index >= $tabs.length) return
690 if (index < 0) return
691
692 $carousel.carousel(index)
693 setTimeout(function () {
694 $tabs[index].focus()
695 // $this.prev().focus()
696 }, 150)
697 }
698
699 var $carousel = $(e.target).closest('.carousel')
700 , $tabs = $carousel.find('[role=tab]')
701 , k = e.which || e.keyCode
702 , index
703
704 if (!/(37|38|39|40)/.test(k)) return
705
706 index = $tabs.index($tabs.filter('.active'))
707 if (k == 37 || k == 38) { // Up
708 index--
709 selectTab(index);
710 }
711
712 if (k == 39 || k == 40) { // Down
713 index++
714 selectTab(index);
715 }
716
717 e.preventDefault()
718 e.stopPropagation()
719 }
720 $(document).on('keydown.carousel.data-api', 'li[role=tab]', $.fn.carousel.Constructor.prototype.keydown);
721
722
723 });