Merge branch 'accordion-activate'

This commit is contained in:
Scott González 2011-01-13 14:56:29 -05:00
commit 599e1434c5
5 changed files with 78 additions and 67 deletions

View File

@ -22,7 +22,7 @@ test("ui-accordion-heading class added to headers anchor", function() {
test("accessibility", function () { test("accessibility", function () {
expect(9); expect(9);
var ac = $('#list1').accordion().accordion("activate", 1); var ac = $('#list1').accordion().accordion("option", "active", 1);
var headers = $(".ui-accordion-header"); var headers = $(".ui-accordion-header");
equals( headers.eq(1).attr("tabindex"), "0", "active header should have tabindex=0"); equals( headers.eq(1).attr("tabindex"), "0", "active header should have tabindex=0");
@ -32,7 +32,7 @@ test("accessibility", function () {
equals( headers.next().attr("role"), "tabpanel", "tabpanel roles"); equals( headers.next().attr("role"), "tabpanel", "tabpanel roles");
equals( headers.eq(1).attr("aria-expanded"), "true", "active tab has aria-expanded"); equals( headers.eq(1).attr("aria-expanded"), "true", "active tab has aria-expanded");
equals( headers.eq(0).attr("aria-expanded"), "false", "inactive tab has aria-expanded"); equals( headers.eq(0).attr("aria-expanded"), "false", "inactive tab has aria-expanded");
ac.accordion("activate", 0); ac.accordion("option", "active", 0);
equals( headers.eq(0).attr("aria-expanded"), "true", "newly active tab has aria-expanded"); equals( headers.eq(0).attr("aria-expanded"), "true", "newly active tab has aria-expanded");
equals( headers.eq(1).attr("aria-expanded"), "false", "newly inactive tab has aria-expanded"); equals( headers.eq(1).attr("aria-expanded"), "false", "newly inactive tab has aria-expanded");
}); });

View File

@ -17,14 +17,14 @@ test("accordionchange event, open closed and close again", function() {
equals( ui.newHeader.size(), 1 ); equals( ui.newHeader.size(), 1 );
equals( ui.newContent.size(), 1 ); equals( ui.newContent.size(), 1 );
}) })
.accordion("activate", 0) .accordion("option", "active", 0)
.one("accordionchange", function(event, ui) { .one("accordionchange", function(event, ui) {
equals( ui.oldHeader.size(), 1 ); equals( ui.oldHeader.size(), 1 );
equals( ui.oldContent.size(), 1 ); equals( ui.oldContent.size(), 1 );
equals( ui.newHeader.size(), 0 ); equals( ui.newHeader.size(), 0 );
equals( ui.newContent.size(), 0 ); equals( ui.newContent.size(), 0 );
}) })
.accordion("activate", 0); .accordion("option", "active", false);
}); });
})(jQuery); })(jQuery);

View File

@ -50,10 +50,10 @@ test("disable", function() {
equals(actual, expected, 'disable is chainable'); equals(actual, expected, 'disable is chainable');
state(expected, 1, 0, 0) state(expected, 1, 0, 0)
expected.accordion("activate", 1); expected.accordion("option", "active", 1);
state(expected, 1, 0, 0) state(expected, 1, 0, 0)
expected.accordion("enable"); expected.accordion("enable");
expected.accordion("activate", 1); expected.accordion("option", "active", 1);
state(expected, 0, 1, 0) state(expected, 0, 1, 0)
}); });

View File

@ -61,7 +61,7 @@ test("{ active: Number }", function() {
$('.ui-accordion-header:eq(2)', '#list1').click(); $('.ui-accordion-header:eq(2)', '#list1').click();
equals( $("#list1").accordion('option', 'active'), 2); equals( $("#list1").accordion('option', 'active'), 2);
$("#list1").accordion('activate', 0); $("#list1").accordion('option', 'active', 0);
equals( $("#list1").accordion('option', 'active'), 0); equals( $("#list1").accordion('option', 'active'), 0);
}); });
@ -96,7 +96,7 @@ test("{ heightStyle: 'content' }", function() {
}); });
test("{ collapsible: false }, default", function() { test("{ collapsible: false }, default", function() {
var ac = $("#list1").accordion(); var ac = $("#list1").accordion();
ac.accordion("activate", false); ac.accordion("option", "active", false);
state(ac, 1, 0, 0); state(ac, 1, 0, 0);
}); });

View File

@ -73,7 +73,7 @@ $.widget( "ui.accordion", {
.addClass( "ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" ); .addClass( "ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" );
self.headers.find( ":first-child" ).addClass( "ui-accordion-heading" ); self.headers.find( ":first-child" ).addClass( "ui-accordion-heading" );
self.active = self._findActive( self.active || options.active ) self.active = self._findActive( options.active )
.addClass( "ui-state-default ui-state-active" ) .addClass( "ui-state-default ui-state-active" )
.toggleClass( "ui-corner-all" ) .toggleClass( "ui-corner-all" )
.toggleClass( "ui-corner-top" ); .toggleClass( "ui-corner-top" );
@ -120,7 +120,7 @@ $.widget( "ui.accordion", {
if ( options.event ) { if ( options.event ) {
self.headers.bind( options.event.split(" ").join(".accordion ") + ".accordion", function(event) { self.headers.bind( options.event.split(" ").join(".accordion ") + ".accordion", function(event) {
self._clickHandler.call( self, event, this ); self._eventHandler( event );
event.preventDefault(); event.preventDefault();
}); });
} }
@ -173,11 +173,14 @@ $.widget( "ui.accordion", {
}, },
_setOption: function( key, value ) { _setOption: function( key, value ) {
if ( key == "active" ) {
// _activate() will handle invalid values and update this.options
this._activate( value );
return;
}
$.Widget.prototype._setOption.apply( this, arguments ); $.Widget.prototype._setOption.apply( this, arguments );
if ( key == "active" ) {
this.activate( value );
}
if ( key == "icons" ) { if ( key == "icons" ) {
this._destroyIcons(); this._destroyIcons();
if ( value ) { if ( value ) {
@ -213,7 +216,7 @@ $.widget( "ui.accordion", {
break; break;
case keyCode.SPACE: case keyCode.SPACE:
case keyCode.ENTER: case keyCode.ENTER:
this._clickHandler( { target: event.target }, event.target ); this._eventHandler( event );
event.preventDefault(); event.preventDefault();
} }
@ -272,73 +275,64 @@ $.widget( "ui.accordion", {
return this; return this;
}, },
activate: function( index ) { _activate: function( index ) {
// TODO this gets called on init, changing the option without an explicit call for that
this.options.active = index;
// call clickHandler with custom event
var active = this._findActive( index )[ 0 ]; var active = this._findActive( index )[ 0 ];
this._clickHandler( { target: active }, active );
return this; // we found a header to activate, just delegate to the event handler
}, if ( active ) {
if ( active !== this.active[ 0 ] ) {
_findActive: function( selector ) { this._eventHandler( { target: active, currentTarget: active } );
return selector }
? typeof selector === "number"
? this.headers.filter( ":eq(" + selector + ")" )
: this.headers.not( this.headers.not( selector ) )
: selector === false
? $( [] )
: this.headers.filter( ":eq(0)" );
},
// TODO isn't event.target enough? why the separate target argument?
_clickHandler: function( event, target ) {
var options = this.options;
if ( options.disabled ) {
return; return;
} }
// called only when using activate(false) to close all parts programmatically // no header to activate, check if we can collapse
if ( !event.target ) { if ( !this.options.collapsible ) {
if ( !options.collapsible ) {
return; return;
} }
this.active this.active
.removeClass( "ui-state-active ui-corner-top" ) .removeClass( "ui-state-active ui-corner-top" )
.addClass( "ui-state-default ui-corner-all" ) .addClass( "ui-state-default ui-corner-all" )
.children( ".ui-icon" ) .children( ".ui-icon" )
.removeClass( options.icons.activeHeader ) .removeClass( this.options.icons.activeHeader )
.addClass( options.icons.header ); .addClass( this.options.icons.header );
this.active.next().addClass( "ui-accordion-content-active" ); this.active.next().addClass( "ui-accordion-content-active" );
var toHide = this.active.next(), var toHide = this.active.next(),
data = { data = {
options: options, options: this.options,
newHeader: $( [] ), newHeader: $( [] ),
oldHeader: options.active, oldHeader: this.active,
newContent: $( [] ), newContent: $( [] ),
oldContent: toHide oldContent: toHide
}, },
toShow = ( this.active = $( [] ) ); toShow = ( this.active = $( [] ) );
this._toggle( toShow, toHide, data ); this._toggle( toShow, toHide, data );
return; },
}
// get the click target // TODO: add tests/docs for negative values in 2.0 (#6854)
var clicked = $( event.currentTarget || target ), _findActive: function( selector ) {
return typeof selector === "number" ? this.headers.eq( selector ) : $( [] );
},
_eventHandler: function( event ) {
var options = this.options,
clicked = $( event.currentTarget ),
clickedIsActive = clicked[0] === this.active[0]; clickedIsActive = clicked[0] === this.active[0];
// TODO the option is changed, is that correct? if ( options.disabled ) {
// TODO if it is correct, shouldn't that happen after determining that the click is valid? return;
options.active = options.collapsible && clickedIsActive ? }
false :
this.headers.index( clicked );
// if animations are still active, or the active header is the target, ignore click // if animations are still active, or the active header is the target, ignore click
if ( this.running || ( !options.collapsible && clickedIsActive ) ) { if ( this.running || ( !options.collapsible && clickedIsActive ) ) {
return; return;
} }
options.active = options.collapsible && clickedIsActive ?
false :
this.headers.index( clicked );
// find elements to show and hide // find elements to show and hide
var active = this.active, var active = this.active,
toShow = clicked.next(), toShow = clicked.next(),
@ -375,8 +369,6 @@ $.widget( "ui.accordion", {
.next() .next()
.addClass( "ui-accordion-content-active" ); .addClass( "ui-accordion-content-active" );
} }
return;
}, },
_toggle: function( toShow, toHide, data, clickedIsActive, down ) { _toggle: function( toShow, toHide, data, clickedIsActive, down ) {
@ -685,4 +677,23 @@ $.extend( $.ui.accordion, {
}; };
}( jQuery, jQuery.ui.accordion.prototype ) ); }( jQuery, jQuery.ui.accordion.prototype ) );
// expanded active option, activate method
(function( $, prototype ) {
prototype.activate = prototype._activate;
var _findActive = prototype._findActive;
prototype._findActive = function( index ) {
if ( index === -1 ) {
index = false;
}
if ( index && typeof index !== "number" ) {
index = this.headers.index( this.headers.filter( index ) );
if ( index === -1 ) {
index = false;
}
}
return _findActive.call( this, index );
};
}( jQuery, jQuery.ui.accordion.prototype ) );
})( jQuery ); })( jQuery );