| 1 |
|
| 2 |
// Collapse Extension |
| 3 |
// =============================== |
| 4 |
|
| 5 |
var $colltabs = $('[data-toggle="collapse"]') |
| 6 |
$colltabs.each(function( index ) { |
| 7 |
var colltab = $(this) |
| 8 |
, collpanel = (colltab.attr('data-target')) ? $(colltab.attr('data-target')) : $(colltab.attr('href')) |
| 9 |
, parent = colltab.attr('data-parent') |
| 10 |
, collparent = parent && $(parent) |
| 11 |
, collid = colltab.attr('id') || uniqueId('ui-collapse') |
| 12 |
, parentpanel = collpanel.parent() // panel containing title and panel body |
| 13 |
, parentfirstchild = (collparent) ? collparent.find('.panel.panel-default:first-child') : null // first child of containing accordion |
| 14 |
, hasopenpanel = (collparent) ? collparent.find('.panel-collapse.in').length > 0 : false // true, if collapse parent has any panels with class 'in'; otherwise, false |
| 15 |
|
| 16 |
colltab.attr('id', collid) |
| 17 |
|
| 18 |
if(collparent){ |
| 19 |
colltab.attr({ 'aria-controls': collpanel.attr('id'), 'role':'tab', 'aria-selected':'false', 'aria-expanded':'false' }) |
| 20 |
$(collparent).find('div:not(.collapse,.panel-body), h4').attr('role','presentation') |
| 21 |
collparent.attr({ 'role' : 'tablist', 'aria-multiselectable' : 'true' }) |
| 22 |
collpanel.attr({ 'role':'tabpanel', 'aria-labelledby':collid }) |
| 23 |
|
| 24 |
if(!hasopenpanel && parentpanel.is(parentfirstchild)) { |
| 25 |
colltab.attr({ 'tabindex':'0' }) |
| 26 |
collpanel.attr({ 'tabindex':'-1' }) |
| 27 |
}else if(collpanel.hasClass('in')){ |
| 28 |
colltab.attr({ 'aria-selected':'true', 'aria-expanded':'true', 'tabindex':'0' }) |
| 29 |
collpanel.attr({ 'tabindex':'0', 'aria-hidden':'false' }) |
| 30 |
}else{ |
| 31 |
colltab.attr({ 'tabindex':'-1' }) |
| 32 |
collpanel.attr({ 'tabindex':'-1', 'aria-hidden':'true' }) |
| 33 |
} |
| 34 |
} |
| 35 |
}) |
| 36 |
|
| 37 |
var collToggle = $.fn.collapse.Constructor.prototype.toggle |
| 38 |
$.fn.collapse.Constructor.prototype.toggle = function(){ |
| 39 |
var prevTab = this.$parent && this.$parent.find('[aria-expanded="true"]') , href |
| 40 |
|
| 41 |
if(prevTab){ |
| 42 |
var prevPanel = prevTab.attr('data-target') || (href = prevTab.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') |
| 43 |
, $prevPanel = $(prevPanel) |
| 44 |
, $curPanel = this.$element |
| 45 |
, par = this.$parent |
| 46 |
, curTab |
| 47 |
|
| 48 |
if (this.$parent) curTab = this.$parent.find('[data-toggle=collapse][href="#' + this.$element.attr('id') + '"]') |
| 49 |
|
| 50 |
collToggle.apply(this, arguments) |
| 51 |
|
| 52 |
if ($.support.transition) { |
| 53 |
this.$element.one($.support.transition.end, function(){ |
| 54 |
|
| 55 |
prevTab.attr({ 'aria-selected':'false','aria-expanded':'false', 'tabIndex':'-1' }) |
| 56 |
$prevPanel.attr({ 'aria-hidden' : 'true','tabIndex' : '-1'}) |
| 57 |
|
| 58 |
curTab.attr({ 'aria-selected':'true','aria-expanded':'true', 'tabIndex':'0' }) |
| 59 |
|
| 60 |
if($curPanel.hasClass('in')){ |
| 61 |
$curPanel.attr({ 'aria-hidden' : 'false','tabIndex' : '0' }) |
| 62 |
}else{ |
| 63 |
curTab.attr({ 'aria-selected':'false','aria-expanded':'false'}) |
| 64 |
$curPanel.attr({ 'aria-hidden' : 'true','tabIndex' : '-1' }) |
| 65 |
} |
| 66 |
}) |
| 67 |
} |
| 68 |
}else{ |
| 69 |
collToggle.apply(this, arguments) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
$.fn.collapse.Constructor.prototype.keydown = function (e) { |
| 74 |
var $this = $(this) |
| 75 |
, $items |
| 76 |
, $tablist = $this.closest('div[role=tablist] ') |
| 77 |
, index |
| 78 |
, k = e.which || e.keyCode |
| 79 |
|
| 80 |
$this = $(this) |
| 81 |
if (!/(32|37|38|39|40)/.test(k)) return |
| 82 |
if(k==32) $this.click() |
| 83 |
|
| 84 |
$items = $tablist.find('[role=tab]') |
| 85 |
index = $items.index($items.filter(':focus')) |
| 86 |
|
| 87 |
if (k == 38 || k == 37) index-- // up & left |
| 88 |
if (k == 39 || k == 40) index++ // down & right |
| 89 |
if(index < 0) index = $items.length -1 |
| 90 |
if(index == $items.length) index = 0 |
| 91 |
|
| 92 |
$items.eq(index).focus() |
| 93 |
|
| 94 |
e.preventDefault() |
| 95 |
e.stopPropagation() |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
$(document).on('keydown.collapse.data-api','[data-toggle="collapse"]' , $.fn.collapse.Constructor.prototype.keydown); |
| 100 |
|