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