|
1 | 1 | define( [ |
2 | 2 | "../core", |
3 | 3 | "../var/rnotwhite", |
| 4 | + "../data/var/dataPriv", |
4 | 5 | "../core/init" |
5 | | -], function( jQuery, rnotwhite ) { |
| 6 | +], function( jQuery, rnotwhite, dataPriv ) { |
6 | 7 |
|
7 | 8 | var rclass = /[\t\r\n\f]/g; |
8 | 9 |
|
@@ -96,26 +97,60 @@ jQuery.fn.extend( { |
96 | 97 | }, |
97 | 98 |
|
98 | 99 | toggleClass: function( value, stateVal ) { |
99 | | - var type = typeof value, |
100 | | - classNames = type === "string" ? value.match( rnotwhite ) : []; |
| 100 | + var type = typeof value; |
101 | 101 |
|
102 | | - return this.each( function( i ) { |
103 | | - var className, |
104 | | - self = jQuery( this ), |
105 | | - c = 0; |
| 102 | + if ( typeof stateVal === "boolean" && type === "string" ) { |
| 103 | + return stateVal ? this.addClass( value ) : this.removeClass( value ); |
| 104 | + } |
106 | 105 |
|
107 | | - if ( type === "function" ) { |
108 | | - classNames = value.call( this, i, getClass( this ), stateVal ) |
109 | | - .match( rnotwhite ) || []; |
110 | | - } |
| 106 | + if ( jQuery.isFunction( value ) ) { |
| 107 | + return this.each( function( i ) { |
| 108 | + jQuery( this ).toggleClass( |
| 109 | + value.call( this, i, getClass( this ), stateVal ), |
| 110 | + stateVal |
| 111 | + ); |
| 112 | + } ); |
| 113 | + } |
| 114 | + |
| 115 | + return this.each( function() { |
| 116 | + var className, i, self, classNames; |
| 117 | + |
| 118 | + if ( type === "string" ) { |
111 | 119 |
|
112 | | - // Toggle individual class names based on presence or stateVal |
113 | | - while ( ( className = classNames[ c++ ] ) ) { |
| 120 | + // Toggle individual class names |
| 121 | + i = 0; |
| 122 | + self = jQuery( this ); |
| 123 | + classNames = value.match( rnotwhite ) || []; |
| 124 | + |
| 125 | + while ( ( className = classNames[ i++ ] ) ) { |
| 126 | + |
| 127 | + // Check each className given, space separated list |
| 128 | + if ( self.hasClass( className ) ) { |
| 129 | + self.removeClass( className ); |
| 130 | + } else { |
| 131 | + self.addClass( className ); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Toggle whole class name |
| 136 | + } else if ( value === undefined || type === "boolean" ) { |
| 137 | + className = getClass( this ); |
| 138 | + if ( className ) { |
| 139 | + |
| 140 | + // Store className if set |
| 141 | + dataPriv.set( this, "__className__", className ); |
| 142 | + } |
114 | 143 |
|
115 | | - if ( stateVal === false || stateVal !== true && self.hasClass( className ) ) { |
116 | | - self.removeClass( className ); |
117 | | - } else { |
118 | | - self.addClass( className ); |
| 144 | + // If the element has a class name or if we're passed `false`, |
| 145 | + // then remove the whole classname (if there was one, the above saved it). |
| 146 | + // Otherwise bring back whatever was previously saved (if anything), |
| 147 | + // falling back to the empty string if nothing was stored. |
| 148 | + if ( this.setAttribute ) { |
| 149 | + this.setAttribute( "class", |
| 150 | + className || value === false ? |
| 151 | + "" : |
| 152 | + dataPriv.get( this, "__className__" ) || "" |
| 153 | + ); |
119 | 154 | } |
120 | 155 | } |
121 | 156 | } ); |
|
0 commit comments