2 * jQuery UI Accordion 1.10.2
5 * Copyright 2013 jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
9 * http://api.jqueryui.com/accordion/
15 (function( $, undefined ) {
21 hideProps.height = hideProps.paddingTop = hideProps.paddingBottom =
22 hideProps.borderTopWidth = hideProps.borderBottomWidth = "hide";
23 showProps.height = showProps.paddingTop = showProps.paddingBottom =
24 showProps.borderTopWidth = showProps.borderBottomWidth = "show";
26 $.widget( "ui.accordion", {
33 header: "> li > :first-child,> :not(li):even",
36 activeHeader: "ui-icon-triangle-1-s",
37 header: "ui-icon-triangle-1-e"
46 var options = this.options;
47 this.prevShow = this.prevHide = $();
48 this.element.addClass( "ui-accordion ui-widget ui-helper-reset" )
50 .attr( "role", "tablist" );
52 // don't allow collapsible: false and active: false / null
53 if ( !options.collapsible && (options.active === false || options.active == null) ) {
57 this._processPanels();
58 // handle negative values
59 if ( options.active < 0 ) {
60 options.active += this.headers.length;
65 _getCreateEventData: function() {
68 panel: !this.active.length ? $() : this.active.next(),
69 content: !this.active.length ? $() : this.active.next()
73 _createIcons: function() {
74 var icons = this.options.icons;
77 .addClass( "ui-accordion-header-icon ui-icon " + icons.header )
78 .prependTo( this.headers );
79 this.active.children( ".ui-accordion-header-icon" )
80 .removeClass( icons.header )
81 .addClass( icons.activeHeader );
82 this.headers.addClass( "ui-accordion-icons" );
86 _destroyIcons: function() {
88 .removeClass( "ui-accordion-icons" )
89 .children( ".ui-accordion-header-icon" )
93 _destroy: function() {
96 // clean up main element
98 .removeClass( "ui-accordion ui-widget ui-helper-reset" )
99 .removeAttr( "role" );
103 .removeClass( "ui-accordion-header ui-accordion-header-active ui-helper-reset ui-state-default ui-corner-all ui-state-active ui-state-disabled ui-corner-top" )
104 .removeAttr( "role" )
105 .removeAttr( "aria-selected" )
106 .removeAttr( "aria-controls" )
107 .removeAttr( "tabIndex" )
109 if ( /^ui-accordion/.test( this.id ) ) {
110 this.removeAttribute( "id" );
113 this._destroyIcons();
115 // clean up content panels
116 contents = this.headers.next()
117 .css( "display", "" )
118 .removeAttr( "role" )
119 .removeAttr( "aria-expanded" )
120 .removeAttr( "aria-hidden" )
121 .removeAttr( "aria-labelledby" )
122 .removeClass( "ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content ui-accordion-content-active ui-state-disabled" )
124 if ( /^ui-accordion/.test( this.id ) ) {
125 this.removeAttribute( "id" );
128 if ( this.options.heightStyle !== "content" ) {
129 contents.css( "height", "" );
133 _setOption: function( key, value ) {
134 if ( key === "active" ) {
135 // _activate() will handle invalid values and update this.options
136 this._activate( value );
140 if ( key === "event" ) {
141 if ( this.options.event ) {
142 this._off( this.headers, this.options.event );
144 this._setupEvents( value );
147 this._super( key, value );
149 // setting collapsible: false while collapsed; open first panel
150 if ( key === "collapsible" && !value && this.options.active === false ) {
154 if ( key === "icons" ) {
155 this._destroyIcons();
161 // #5332 - opacity doesn't cascade to positioned elements in IE
162 // so we need to add the disabled class to the headers and panels
163 if ( key === "disabled" ) {
164 this.headers.add( this.headers.next() )
165 .toggleClass( "ui-state-disabled", !!value );
169 _keydown: function( event ) {
170 /*jshint maxcomplexity:15*/
171 if ( event.altKey || event.ctrlKey ) {
175 var keyCode = $.ui.keyCode,
176 length = this.headers.length,
177 currentIndex = this.headers.index( event.target ),
180 switch ( event.keyCode ) {
183 toFocus = this.headers[ ( currentIndex + 1 ) % length ];
187 toFocus = this.headers[ ( currentIndex - 1 + length ) % length ];
191 this._eventHandler( event );
194 toFocus = this.headers[ 0 ];
197 toFocus = this.headers[ length - 1 ];
202 $( event.target ).attr( "tabIndex", -1 );
203 $( toFocus ).attr( "tabIndex", 0 );
205 event.preventDefault();
209 _panelKeyDown : function( event ) {
210 if ( event.keyCode === $.ui.keyCode.UP && event.ctrlKey ) {
211 $( event.currentTarget ).prev().focus();
215 refresh: function() {
216 var options = this.options;
217 this._processPanels();
219 // was collapsed or no panel
220 if ( ( options.active === false && options.collapsible === true ) || !this.headers.length ) {
221 options.active = false;
223 // active false only when collapsible is true
224 } if ( options.active === false ) {
226 // was active, but active panel is gone
227 } else if ( this.active.length && !$.contains( this.element[ 0 ], this.active[ 0 ] ) ) {
228 // all remaining panel are disabled
229 if ( this.headers.length === this.headers.find(".ui-state-disabled").length ) {
230 options.active = false;
232 // activate previous panel
234 this._activate( Math.max( 0, options.active - 1 ) );
236 // was active, active panel still exists
238 // make sure active index is correct
239 options.active = this.headers.index( this.active );
242 this._destroyIcons();
247 _processPanels: function() {
248 this.headers = this.element.find( this.options.header )
249 .addClass( "ui-accordion-header ui-helper-reset ui-state-default ui-corner-all" );
252 .addClass( "ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" )
253 .filter(":not(.ui-accordion-content-active)")
257 _refresh: function() {
259 options = this.options,
260 heightStyle = options.heightStyle,
261 parent = this.element.parent(),
262 accordionId = this.accordionId = "ui-accordion-" +
263 (this.element.attr( "id" ) || ++uid);
265 this.active = this._findActive( options.active )
266 .addClass( "ui-accordion-header-active ui-state-active ui-corner-top" )
267 .removeClass( "ui-corner-all" );
269 .addClass( "ui-accordion-content-active" )
273 .attr( "role", "tab" )
274 .each(function( i ) {
275 var header = $( this ),
276 headerId = header.attr( "id" ),
277 panel = header.next(),
278 panelId = panel.attr( "id" );
280 headerId = accordionId + "-header-" + i;
281 header.attr( "id", headerId );
284 panelId = accordionId + "-panel-" + i;
285 panel.attr( "id", panelId );
287 header.attr( "aria-controls", panelId );
288 panel.attr( "aria-labelledby", headerId );
291 .attr( "role", "tabpanel" );
296 "aria-selected": "false",
301 "aria-expanded": "false",
302 "aria-hidden": "true"
306 // make sure at least one header is in the tab order
307 if ( !this.active.length ) {
308 this.headers.eq( 0 ).attr( "tabIndex", 0 );
311 "aria-selected": "true",
316 "aria-expanded": "true",
317 "aria-hidden": "false"
323 this._setupEvents( options.event );
325 if ( heightStyle === "fill" ) {
326 maxHeight = parent.height();
327 this.element.siblings( ":visible" ).each(function() {
328 var elem = $( this ),
329 position = elem.css( "position" );
331 if ( position === "absolute" || position === "fixed" ) {
334 maxHeight -= elem.outerHeight( true );
337 this.headers.each(function() {
338 maxHeight -= $( this ).outerHeight( true );
343 $( this ).height( Math.max( 0, maxHeight -
344 $( this ).innerHeight() + $( this ).height() ) );
346 .css( "overflow", "auto" );
347 } else if ( heightStyle === "auto" ) {
351 maxHeight = Math.max( maxHeight, $( this ).css( "height", "" ).height() );
353 .height( maxHeight );
357 _activate: function( index ) {
358 var active = this._findActive( index )[ 0 ];
360 // trying to activate the already active panel
361 if ( active === this.active[ 0 ] ) {
365 // trying to collapse, simulate a click on the currently active header
366 active = active || this.active[ 0 ];
370 currentTarget: active,
371 preventDefault: $.noop
375 _findActive: function( selector ) {
376 return typeof selector === "number" ? this.headers.eq( selector ) : $();
379 _setupEvents: function( event ) {
384 $.each( event.split(" "), function( index, eventName ) {
385 events[ eventName ] = "_eventHandler";
389 this._off( this.headers.add( this.headers.next() ) );
390 this._on( this.headers, events );
391 this._on( this.headers.next(), { keydown: "_panelKeyDown" });
392 this._hoverable( this.headers );
393 this._focusable( this.headers );
396 _eventHandler: function( event ) {
397 var options = this.options,
398 active = this.active,
399 clicked = $( event.currentTarget ),
400 clickedIsActive = clicked[ 0 ] === active[ 0 ],
401 collapsing = clickedIsActive && options.collapsible,
402 toShow = collapsing ? $() : clicked.next(),
403 toHide = active.next(),
407 newHeader: collapsing ? $() : clicked,
411 event.preventDefault();
414 // click on active header, but not collapsible
415 ( clickedIsActive && !options.collapsible ) ||
416 // allow canceling activation
417 ( this._trigger( "beforeActivate", event, eventData ) === false ) ) {
421 options.active = collapsing ? false : this.headers.index( clicked );
423 // when the call to ._toggle() comes after the class changes
424 // it causes a very odd bug in IE 8 (see #6720)
425 this.active = clickedIsActive ? $() : clicked;
426 this._toggle( eventData );
429 // corner classes on the previously active header stay after the animation
430 active.removeClass( "ui-accordion-header-active ui-state-active" );
431 if ( options.icons ) {
432 active.children( ".ui-accordion-header-icon" )
433 .removeClass( options.icons.activeHeader )
434 .addClass( options.icons.header );
437 if ( !clickedIsActive ) {
439 .removeClass( "ui-corner-all" )
440 .addClass( "ui-accordion-header-active ui-state-active ui-corner-top" );
441 if ( options.icons ) {
442 clicked.children( ".ui-accordion-header-icon" )
443 .removeClass( options.icons.header )
444 .addClass( options.icons.activeHeader );
449 .addClass( "ui-accordion-content-active" );
453 _toggle: function( data ) {
454 var toShow = data.newPanel,
455 toHide = this.prevShow.length ? this.prevShow : data.oldPanel;
457 // handle activating a panel during the animation for another activation
458 this.prevShow.add( this.prevHide ).stop( true, true );
459 this.prevShow = toShow;
460 this.prevHide = toHide;
462 if ( this.options.animate ) {
463 this._animate( toShow, toHide, data );
467 this._toggleComplete( data );
471 "aria-expanded": "false",
472 "aria-hidden": "true"
474 toHide.prev().attr( "aria-selected", "false" );
475 // if we're switching panels, remove the old header from the tab order
476 // if we're opening from collapsed state, remove the previous header from the tab order
477 // if we're collapsing, then keep the collapsing header in the tab order
478 if ( toShow.length && toHide.length ) {
479 toHide.prev().attr( "tabIndex", -1 );
480 } else if ( toShow.length ) {
481 this.headers.filter(function() {
482 return $( this ).attr( "tabIndex" ) === 0;
484 .attr( "tabIndex", -1 );
489 "aria-expanded": "true",
490 "aria-hidden": "false"
494 "aria-selected": "true",
499 _animate: function( toShow, toHide, data ) {
500 var total, easing, duration,
503 down = toShow.length &&
504 ( !toHide.length || ( toShow.index() < toHide.index() ) ),
505 animate = this.options.animate || {},
506 options = down && animate.down || animate,
507 complete = function() {
508 that._toggleComplete( data );
511 if ( typeof options === "number" ) {
514 if ( typeof options === "string" ) {
517 // fall back from options to animation in case of partial down settings
518 easing = easing || options.easing || animate.easing;
519 duration = duration || options.duration || animate.duration;
521 if ( !toHide.length ) {
522 return toShow.animate( showProps, duration, easing, complete );
524 if ( !toShow.length ) {
525 return toHide.animate( hideProps, duration, easing, complete );
528 total = toShow.show().outerHeight();
529 toHide.animate( hideProps, {
532 step: function( now, fx ) {
533 fx.now = Math.round( now );
538 .animate( showProps, {
542 step: function( now, fx ) {
543 fx.now = Math.round( now );
544 if ( fx.prop !== "height" ) {
546 } else if ( that.options.heightStyle !== "content" ) {
547 fx.now = Math.round( total - toHide.outerHeight() - adjust );
554 _toggleComplete: function( data ) {
555 var toHide = data.oldPanel;
558 .removeClass( "ui-accordion-content-active" )
560 .removeClass( "ui-corner-top" )
561 .addClass( "ui-corner-all" );
563 // Work around for rendering bug in IE (#5421)
564 if ( toHide.length ) {
565 toHide.parent()[0].className = toHide.parent()[0].className;
568 this._trigger( "activate", null, data );