Project

General

Profile

1
define( [
2
	"../core",
3
	"../core/stripAndCollapse",
4
	"../var/rnothtmlwhite",
5
	"../data/var/dataPriv",
6
	"../core/init"
7
], function( jQuery, stripAndCollapse, rnothtmlwhite, dataPriv ) {
8

    
9
"use strict";
10

    
11
function getClass( elem ) {
12
	return elem.getAttribute && elem.getAttribute( "class" ) || "";
13
}
14

    
15
jQuery.fn.extend( {
16
	addClass: function( value ) {
17
		var classes, elem, cur, curValue, clazz, j, finalValue,
18
			i = 0;
19

    
20
		if ( jQuery.isFunction( value ) ) {
21
			return this.each( function( j ) {
22
				jQuery( this ).addClass( value.call( this, j, getClass( this ) ) );
23
			} );
24
		}
25

    
26
		if ( typeof value === "string" && value ) {
27
			classes = value.match( rnothtmlwhite ) || [];
28

    
29
			while ( ( elem = this[ i++ ] ) ) {
30
				curValue = getClass( elem );
31
				cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
32

    
33
				if ( cur ) {
34
					j = 0;
35
					while ( ( clazz = classes[ j++ ] ) ) {
36
						if ( cur.indexOf( " " + clazz + " " ) < 0 ) {
37
							cur += clazz + " ";
38
						}
39
					}
40

    
41
					// Only assign if different to avoid unneeded rendering.
42
					finalValue = stripAndCollapse( cur );
43
					if ( curValue !== finalValue ) {
44
						elem.setAttribute( "class", finalValue );
45
					}
46
				}
47
			}
48
		}
49

    
50
		return this;
51
	},
52

    
53
	removeClass: function( value ) {
54
		var classes, elem, cur, curValue, clazz, j, finalValue,
55
			i = 0;
56

    
57
		if ( jQuery.isFunction( value ) ) {
58
			return this.each( function( j ) {
59
				jQuery( this ).removeClass( value.call( this, j, getClass( this ) ) );
60
			} );
61
		}
62

    
63
		if ( !arguments.length ) {
64
			return this.attr( "class", "" );
65
		}
66

    
67
		if ( typeof value === "string" && value ) {
68
			classes = value.match( rnothtmlwhite ) || [];
69

    
70
			while ( ( elem = this[ i++ ] ) ) {
71
				curValue = getClass( elem );
72

    
73
				// This expression is here for better compressibility (see addClass)
74
				cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
75

    
76
				if ( cur ) {
77
					j = 0;
78
					while ( ( clazz = classes[ j++ ] ) ) {
79

    
80
						// Remove *all* instances
81
						while ( cur.indexOf( " " + clazz + " " ) > -1 ) {
82
							cur = cur.replace( " " + clazz + " ", " " );
83
						}
84
					}
85

    
86
					// Only assign if different to avoid unneeded rendering.
87
					finalValue = stripAndCollapse( cur );
88
					if ( curValue !== finalValue ) {
89
						elem.setAttribute( "class", finalValue );
90
					}
91
				}
92
			}
93
		}
94

    
95
		return this;
96
	},
97

    
98
	toggleClass: function( value, stateVal ) {
99
		var type = typeof value;
100

    
101
		if ( typeof stateVal === "boolean" && type === "string" ) {
102
			return stateVal ? this.addClass( value ) : this.removeClass( value );
103
		}
104

    
105
		if ( jQuery.isFunction( value ) ) {
106
			return this.each( function( i ) {
107
				jQuery( this ).toggleClass(
108
					value.call( this, i, getClass( this ), stateVal ),
109
					stateVal
110
				);
111
			} );
112
		}
113

    
114
		return this.each( function() {
115
			var className, i, self, classNames;
116

    
117
			if ( type === "string" ) {
118

    
119
				// Toggle individual class names
120
				i = 0;
121
				self = jQuery( this );
122
				classNames = value.match( rnothtmlwhite ) || [];
123

    
124
				while ( ( className = classNames[ i++ ] ) ) {
125

    
126
					// Check each className given, space separated list
127
					if ( self.hasClass( className ) ) {
128
						self.removeClass( className );
129
					} else {
130
						self.addClass( className );
131
					}
132
				}
133

    
134
			// Toggle whole class name
135
			} else if ( value === undefined || type === "boolean" ) {
136
				className = getClass( this );
137
				if ( className ) {
138

    
139
					// Store className if set
140
					dataPriv.set( this, "__className__", className );
141
				}
142

    
143
				// If the element has a class name or if we're passed `false`,
144
				// then remove the whole classname (if there was one, the above saved it).
145
				// Otherwise bring back whatever was previously saved (if anything),
146
				// falling back to the empty string if nothing was stored.
147
				if ( this.setAttribute ) {
148
					this.setAttribute( "class",
149
						className || value === false ?
150
						"" :
151
						dataPriv.get( this, "__className__" ) || ""
152
					);
153
				}
154
			}
155
		} );
156
	},
157

    
158
	hasClass: function( selector ) {
159
		var className, elem,
160
			i = 0;
161

    
162
		className = " " + selector + " ";
163
		while ( ( elem = this[ i++ ] ) ) {
164
			if ( elem.nodeType === 1 &&
165
				( " " + stripAndCollapse( getClass( elem ) ) + " " ).indexOf( className ) > -1 ) {
166
					return true;
167
			}
168
		}
169

    
170
		return false;
171
	}
172
} );
173

    
174
} );
(2-2/5)