Debian Codegraph @

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

raw
1 // Tab Extension
2 // ===============================
3
4 var $tablist = $('.nav-tabs, .nav-pills')
5 , $lis = $tablist.children('li')
6 , $tabs = $tablist.find('[data-toggle="tab"], [data-toggle="pill"]')
7
8 if($tabs){
9 $tablist.attr('role', 'tablist')
10 $lis.attr('role', 'presentation')
11 $tabs.attr('role', 'tab')
12 }
13
14 $tabs.each(function( index ) {
15 var tabpanel = $($(this).attr('href'))
16 , tab = $(this)
17 , tabid = tab.attr('id') || uniqueId('ui-tab')
18
19 tab.attr('id', tabid)
20
21 if(tab.parent().hasClass('active')){
22 tab.attr( { 'tabIndex' : '0', 'aria-selected' : 'true', 'aria-controls': tab.attr('href').substr(1) } )
23 tabpanel.attr({ 'role' : 'tabpanel', 'tabIndex' : '0', 'aria-hidden' : 'false', 'aria-labelledby':tabid })
24 }else{
25 tab.attr( { 'tabIndex' : '-1', 'aria-selected' : 'false', 'aria-controls': tab.attr('href').substr(1) } )
26 tabpanel.attr( { 'role' : 'tabpanel', 'tabIndex' : '-1', 'aria-hidden' : 'true', 'aria-labelledby':tabid } )
27 }
28 })
29
30 $.fn.tab.Constructor.prototype.keydown = function (e) {
31 var $this = $(this)
32 , $items
33 , $ul = $this.closest('ul[role=tablist] ')
34 , index
35 , k = e.which || e.keyCode
36
37 $this = $(this)
38 if (!/(37|38|39|40)/.test(k)) return
39
40 $items = $ul.find('[role=tab]:visible')
41 index = $items.index($items.filter(':focus'))
42
43 if (k == 38 || k == 37) index-- // up & left
44 if (k == 39 || k == 40) index++ // down & right
45
46
47 if(index < 0) index = $items.length -1
48 if(index == $items.length) index = 0
49
50 var nextTab = $items.eq(index)
51 if(nextTab.attr('role') ==='tab'){
52
53 nextTab.tab('show') //Comment this line for dynamically loaded tabPabels, to save Ajax requests on arrow key navigation
54 .focus()
55 }
56 // nextTab.focus()
57
58 e.preventDefault()
59 e.stopPropagation()
60 }
61
62 $(document).on('keydown.tab.data-api','[data-toggle="tab"], [data-toggle="pill"]' , $.fn.tab.Constructor.prototype.keydown)
63
64 var tabactivate = $.fn.tab.Constructor.prototype.activate;
65 $.fn.tab.Constructor.prototype.activate = function (element, container, callback) {
66 var $active = container.find('> .active')
67 $active.find('[data-toggle=tab], [data-toggle=pill]').attr({ 'tabIndex' : '-1','aria-selected' : false })
68 $active.filter('.tab-pane').attr({ 'aria-hidden' : true,'tabIndex' : '-1' })
69
70 tabactivate.apply(this, arguments)
71
72 element.addClass('active')
73 element.find('[data-toggle=tab], [data-toggle=pill]').attr({ 'tabIndex' : '0','aria-selected' : true })
74 element.filter('.tab-pane').attr({ 'aria-hidden' : false,'tabIndex' : '0' })
75 }