Tabs: Coding standards.

This commit is contained in:
Scott González 2012-04-03 19:59:08 -04:00
parent 96f9c84b7b
commit a505192420

70
ui/jquery.ui.tabs.js vendored
View File

@ -95,8 +95,8 @@ $.widget( "ui.tabs", {
// into account and update option properly. // into account and update option properly.
if ( $.isArray( options.disabled ) ) { if ( $.isArray( options.disabled ) ) {
options.disabled = $.unique( options.disabled.concat( options.disabled = $.unique( options.disabled.concat(
$.map( this.lis.filter( ".ui-state-disabled" ), function( n, i ) { $.map( this.lis.filter( ".ui-state-disabled" ), function( li ) {
return that.lis.index( n ); return that.lis.index( li );
}) })
) ).sort(); ) ).sort();
} }
@ -162,13 +162,12 @@ $.widget( "ui.tabs", {
}, },
_sanitizeSelector: function( hash ) { _sanitizeSelector: function( hash ) {
// we need this because an id may contain a ":"
return hash ? hash.replace( /[!"$%&'()*+,.\/:;<=>?@[\]^`{|}~]/g, "\\$&" ) : ""; return hash ? hash.replace( /[!"$%&'()*+,.\/:;<=>?@[\]^`{|}~]/g, "\\$&" ) : "";
}, },
refresh: function() { refresh: function() {
var next, var next,
self = this, that = this,
options = this.options, options = this.options,
lis = this.list.children( ":has(a[href])" ); lis = this.list.children( ":has(a[href])" );
@ -216,14 +215,14 @@ $.widget( "ui.tabs", {
}, },
_processTabs: function() { _processTabs: function() {
var self = this; var that = this;
this.list = this._getList(); this.list = this._getList();
this.lis = $( " > li:has(a[href])", this.list ); this.lis = this.list.find( "> li:has(a[href])" );
this.anchors = this.lis.map(function() { this.anchors = this.lis.map(function() {
return $( "a", this )[ 0 ]; return $( "a", this )[ 0 ];
}); });
this.panels = $( [] ); this.panels = $();
this.anchors.each(function( i, a ) { this.anchors.each(function( i, a ) {
var selector, panel, id; var selector, panel, id;
@ -231,20 +230,20 @@ $.widget( "ui.tabs", {
// inline tab // inline tab
if ( isLocal( a ) ) { if ( isLocal( a ) ) {
selector = a.hash; selector = a.hash;
panel = self.element.find( self._sanitizeSelector( selector ) ); panel = that.element.find( that._sanitizeSelector( selector ) );
// remote tab // remote tab
} else { } else {
id = self._tabId( a ); id = that._tabId( a );
selector = "#" + id; selector = "#" + id;
panel = self.element.find( selector ); panel = that.element.find( selector );
if ( !panel.length ) { if ( !panel.length ) {
panel = self._createPanel( id ); panel = that._createPanel( id );
panel.insertAfter( self.panels[ i - 1 ] || self.list ); panel.insertAfter( that.panels[ i - 1 ] || that.list );
} }
} }
if ( panel.length) { if ( panel.length) {
self.panels = self.panels.add( panel ); that.panels = that.panels.add( panel );
} }
$( a ).attr( "aria-controls", selector.substring( 1 ) ); $( a ).attr( "aria-controls", selector.substring( 1 ) );
}); });
@ -256,10 +255,10 @@ $.widget( "ui.tabs", {
}, },
_createPanel: function( id ) { _createPanel: function( id ) {
return $( "<div></div>" ) return $( "<div>" )
.attr( "id", id ) .attr( "id", id )
.addClass( "ui-tabs-panel ui-widget-content ui-corner-bottom" ) .addClass( "ui-tabs-panel ui-widget-content ui-corner-bottom" )
.data( "ui-tabs-destroy", true ); .data( "ui-tabs-destroy", true );
}, },
_setupDisabled: function( disabled ) { _setupDisabled: function( disabled ) {
@ -273,7 +272,8 @@ $.widget( "ui.tabs", {
// disable tabs // disable tabs
for ( var i = 0, li; ( li = this.lis[ i ] ); i++ ) { for ( var i = 0, li; ( li = this.lis[ i ] ); i++ ) {
$( li ).toggleClass( "ui-state-disabled", ( disabled === true || $.inArray( i, disabled ) !== -1 ) ); $( li ).toggleClass( "ui-state-disabled",
( disabled === true || $.inArray( i, disabled ) !== -1 ) );
} }
this.options.disabled = disabled; this.options.disabled = disabled;
@ -302,11 +302,13 @@ $.widget( "ui.tabs", {
// attach tab event handler, unbind to avoid duplicates from former tabifying... // attach tab event handler, unbind to avoid duplicates from former tabifying...
this.anchors.unbind( ".tabs" ); this.anchors.unbind( ".tabs" );
// TODO: use event delegation via _bind()
if ( event ) { if ( event ) {
this.anchors.bind( event.split( " " ).join( ".tabs " ) + ".tabs", this.anchors.bind( event.split( " " ).join( ".tabs " ) + ".tabs",
$.proxy( this, "_eventHandler" ) ); $.proxy( this, "_eventHandler" ) );
} }
// TODO: use event delegation via _bind()
// disable click in any case // disable click in any case
this.anchors.bind( "click.tabs", function( event ){ this.anchors.bind( "click.tabs", function( event ){
event.preventDefault(); event.preventDefault();
@ -353,14 +355,12 @@ $.widget( "ui.tabs", {
} }
if ( !toHide.length && !toShow.length ) { if ( !toHide.length && !toShow.length ) {
throw "jQuery UI Tabs: Mismatching fragment identifier."; jQuery.error( "jQuery UI Tabs: Mismatching fragment identifier." );
} }
if ( toShow.length ) { if ( toShow.length ) {
// TODO make passing in node possible // TODO make passing in node possible
that.load( that.anchors.index( clicked ), event ); that.load( that.anchors.index( clicked ), event );
clicked[ 0 ].blur(); clicked[ 0 ].blur();
} }
that._toggle( event, eventData ); that._toggle( event, eventData );
@ -429,7 +429,7 @@ $.widget( "ui.tabs", {
_findActive: function( selector ) { _findActive: function( selector ) {
return typeof selector === "number" ? this.anchors.eq( selector ) : return typeof selector === "number" ? this.anchors.eq( selector ) :
typeof selector === "string" ? this.anchors.filter( "[href$='" + selector + "']" ) : $(); typeof selector === "string" ? this.anchors.filter( "[href$='" + selector + "']" ) : $();
}, },
_getIndex: function( index ) { _getIndex: function( index ) {
@ -525,10 +525,10 @@ $.widget( "ui.tabs", {
load: function( index, event ) { load: function( index, event ) {
index = this._getIndex( index ); index = this._getIndex( index );
var self = this, var that = this,
options = this.options, options = this.options,
anchor = this.anchors.eq( index ), anchor = this.anchors.eq( index ),
panel = self._getPanelForTab( anchor ), panel = that._getPanelForTab( anchor ),
eventData = { eventData = {
tab: anchor, tab: anchor,
panel: panel panel: panel
@ -542,7 +542,7 @@ $.widget( "ui.tabs", {
this.xhr = $.ajax({ this.xhr = $.ajax({
url: anchor.attr( "href" ), url: anchor.attr( "href" ),
beforeSend: function( jqXHR, settings ) { beforeSend: function( jqXHR, settings ) {
return self._trigger( "beforeLoad", event, return that._trigger( "beforeLoad", event,
$.extend( { jqXHR : jqXHR, ajaxSettings: settings }, eventData ) ); $.extend( { jqXHR : jqXHR, ajaxSettings: settings }, eventData ) );
} }
}); });
@ -556,7 +556,7 @@ $.widget( "ui.tabs", {
// remove when core #10467 is fixed // remove when core #10467 is fixed
setTimeout(function() { setTimeout(function() {
panel.html( response ); panel.html( response );
self._trigger( "load", event, eventData ); that._trigger( "load", event, eventData );
}, 1 ); }, 1 );
}) })
.complete(function( jqXHR, status ) { .complete(function( jqXHR, status ) {
@ -564,19 +564,17 @@ $.widget( "ui.tabs", {
// remove when core #10467 is fixed // remove when core #10467 is fixed
setTimeout(function() { setTimeout(function() {
if ( status === "abort" ) { if ( status === "abort" ) {
self.panels.stop( false, true ); that.panels.stop( false, true );
} }
self.lis.eq( index ).removeClass( "ui-tabs-loading" ); that.lis.eq( index ).removeClass( "ui-tabs-loading" );
if ( jqXHR === self.xhr ) { if ( jqXHR === that.xhr ) {
delete self.xhr; delete that.xhr;
} }
}, 1 ); }, 1 );
}); });
} }
return this;
}, },
_getPanelForTab: function( tab ) { _getPanelForTab: function( tab ) {
@ -614,7 +612,7 @@ if ( $.uiBackCompat !== false ) {
_create: function() { _create: function() {
this._super(); this._super();
var self = this; var that = this;
this.element.bind( "tabsbeforeload.tabs", function( event, ui ) { this.element.bind( "tabsbeforeload.tabs", function( event, ui ) {
// tab is already cached // tab is already cached
@ -623,21 +621,21 @@ if ( $.uiBackCompat !== false ) {
return; return;
} }
$.extend( ui.ajaxSettings, self.options.ajaxOptions, { $.extend( ui.ajaxSettings, that.options.ajaxOptions, {
error: function( xhr, s, e ) { error: function( xhr, s, e ) {
try { try {
// Passing index avoid a race condition when this method is // Passing index avoid a race condition when this method is
// called after the user has selected another tab. // called after the user has selected another tab.
// Pass the anchor that initiated this request allows // Pass the anchor that initiated this request allows
// loadError to manipulate the tab content panel via $(a.hash) // loadError to manipulate the tab content panel via $(a.hash)
self.options.ajaxOptions.error( xhr, s, ui.tab.closest( "li" ).index(), ui.tab[ 0 ] ); that.options.ajaxOptions.error( xhr, s, ui.tab.closest( "li" ).index(), ui.tab[ 0 ] );
} }
catch ( e ) {} catch ( e ) {}
} }
}); });
ui.jqXHR.success(function() { ui.jqXHR.success(function() {
if ( self.options.cache ) { if ( that.options.cache ) {
$.data( ui.tab[ 0 ], "cache.tabs", true ); $.data( ui.tab[ 0 ], "cache.tabs", true );
} }
}); });