Blame view

bower_components/jquery/src/attributes/classes.js 4.06 KB
c5169e0e   Renato De Donato   a new hope
1
  define([
74249687   Luigi Serra   Cross browser con...
2
3
  	"../core",
  	"../var/rnotwhite",
c5169e0e   Renato De Donato   a new hope
4
5
  	"../var/strundefined",
  	"../data/var/data_priv",
74249687   Luigi Serra   Cross browser con...
6
  	"../core/init"
c5169e0e   Renato De Donato   a new hope
7
  ], function( jQuery, rnotwhite, strundefined, data_priv ) {
74249687   Luigi Serra   Cross browser con...
8
9
10
  
  var rclass = /[\t\r\n\f]/g;
  
c5169e0e   Renato De Donato   a new hope
11
  jQuery.fn.extend({
74249687   Luigi Serra   Cross browser con...
12
  	addClass: function( value ) {
c5169e0e   Renato De Donato   a new hope
13
14
15
16
  		var classes, elem, cur, clazz, j, finalValue,
  			proceed = typeof value === "string" && value,
  			i = 0,
  			len = this.length;
74249687   Luigi Serra   Cross browser con...
17
18
  
  		if ( jQuery.isFunction( value ) ) {
c5169e0e   Renato De Donato   a new hope
19
20
21
  			return this.each(function( j ) {
  				jQuery( this ).addClass( value.call( this, j, this.className ) );
  			});
74249687   Luigi Serra   Cross browser con...
22
23
  		}
  
c5169e0e   Renato De Donato   a new hope
24
25
26
  		if ( proceed ) {
  			// The disjunction here is for better compressibility (see removeClass)
  			classes = ( value || "" ).match( rnotwhite ) || [];
74249687   Luigi Serra   Cross browser con...
27
  
c5169e0e   Renato De Donato   a new hope
28
29
30
31
32
33
  			for ( ; i < len; i++ ) {
  				elem = this[ i ];
  				cur = elem.nodeType === 1 && ( elem.className ?
  					( " " + elem.className + " " ).replace( rclass, " " ) :
  					" "
  				);
74249687   Luigi Serra   Cross browser con...
34
35
36
  
  				if ( cur ) {
  					j = 0;
c5169e0e   Renato De Donato   a new hope
37
  					while ( (clazz = classes[j++]) ) {
74249687   Luigi Serra   Cross browser con...
38
39
40
41
42
  						if ( cur.indexOf( " " + clazz + " " ) < 0 ) {
  							cur += clazz + " ";
  						}
  					}
  
c5169e0e   Renato De Donato   a new hope
43
  					// only assign if different to avoid unneeded rendering.
74249687   Luigi Serra   Cross browser con...
44
  					finalValue = jQuery.trim( cur );
c5169e0e   Renato De Donato   a new hope
45
46
  					if ( elem.className !== finalValue ) {
  						elem.className = finalValue;
74249687   Luigi Serra   Cross browser con...
47
48
49
50
51
52
53
54
55
  					}
  				}
  			}
  		}
  
  		return this;
  	},
  
  	removeClass: function( value ) {
c5169e0e   Renato De Donato   a new hope
56
57
58
59
  		var classes, elem, cur, clazz, j, finalValue,
  			proceed = arguments.length === 0 || typeof value === "string" && value,
  			i = 0,
  			len = this.length;
74249687   Luigi Serra   Cross browser con...
60
61
  
  		if ( jQuery.isFunction( value ) ) {
c5169e0e   Renato De Donato   a new hope
62
63
64
  			return this.each(function( j ) {
  				jQuery( this ).removeClass( value.call( this, j, this.className ) );
  			});
74249687   Luigi Serra   Cross browser con...
65
  		}
c5169e0e   Renato De Donato   a new hope
66
67
  		if ( proceed ) {
  			classes = ( value || "" ).match( rnotwhite ) || [];
74249687   Luigi Serra   Cross browser con...
68
  
c5169e0e   Renato De Donato   a new hope
69
70
  			for ( ; i < len; i++ ) {
  				elem = this[ i ];
74249687   Luigi Serra   Cross browser con...
71
  				// This expression is here for better compressibility (see addClass)
c5169e0e   Renato De Donato   a new hope
72
73
74
75
  				cur = elem.nodeType === 1 && ( elem.className ?
  					( " " + elem.className + " " ).replace( rclass, " " ) :
  					""
  				);
74249687   Luigi Serra   Cross browser con...
76
77
78
  
  				if ( cur ) {
  					j = 0;
c5169e0e   Renato De Donato   a new hope
79
  					while ( (clazz = classes[j++]) ) {
74249687   Luigi Serra   Cross browser con...
80
  						// Remove *all* instances
c5169e0e   Renato De Donato   a new hope
81
  						while ( cur.indexOf( " " + clazz + " " ) >= 0 ) {
74249687   Luigi Serra   Cross browser con...
82
83
84
85
86
  							cur = cur.replace( " " + clazz + " ", " " );
  						}
  					}
  
  					// Only assign if different to avoid unneeded rendering.
c5169e0e   Renato De Donato   a new hope
87
88
89
  					finalValue = value ? jQuery.trim( cur ) : "";
  					if ( elem.className !== finalValue ) {
  						elem.className = finalValue;
74249687   Luigi Serra   Cross browser con...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  					}
  				}
  			}
  		}
  
  		return this;
  	},
  
  	toggleClass: function( value, stateVal ) {
  		var type = typeof value;
  
  		if ( typeof stateVal === "boolean" && type === "string" ) {
  			return stateVal ? this.addClass( value ) : this.removeClass( value );
  		}
  
  		if ( jQuery.isFunction( value ) ) {
c5169e0e   Renato De Donato   a new hope
106
107
108
  			return this.each(function( i ) {
  				jQuery( this ).toggleClass( value.call(this, i, this.className, stateVal), stateVal );
  			});
74249687   Luigi Serra   Cross browser con...
109
110
  		}
  
c5169e0e   Renato De Donato   a new hope
111
  		return this.each(function() {
74249687   Luigi Serra   Cross browser con...
112
  			if ( type === "string" ) {
74249687   Luigi Serra   Cross browser con...
113
  				// Toggle individual class names
c5169e0e   Renato De Donato   a new hope
114
115
116
117
  				var className,
  					i = 0,
  					self = jQuery( this ),
  					classNames = value.match( rnotwhite ) || [];
74249687   Luigi Serra   Cross browser con...
118
  
c5169e0e   Renato De Donato   a new hope
119
  				while ( (className = classNames[ i++ ]) ) {
74249687   Luigi Serra   Cross browser con...
120
121
122
123
124
125
126
127
128
  					// Check each className given, space separated list
  					if ( self.hasClass( className ) ) {
  						self.removeClass( className );
  					} else {
  						self.addClass( className );
  					}
  				}
  
  			// Toggle whole class name
c5169e0e   Renato De Donato   a new hope
129
130
131
132
  			} else if ( type === strundefined || type === "boolean" ) {
  				if ( this.className ) {
  					// store className if set
  					data_priv.set( this, "__className__", this.className );
74249687   Luigi Serra   Cross browser con...
133
134
135
136
137
138
  				}
  
  				// If the element has a class name or if we're passed `false`,
  				// then remove the whole classname (if there was one, the above saved it).
  				// Otherwise bring back whatever was previously saved (if anything),
  				// falling back to the empty string if nothing was stored.
c5169e0e   Renato De Donato   a new hope
139
  				this.className = this.className || value === false ? "" : data_priv.get( this, "__className__" ) || "";
74249687   Luigi Serra   Cross browser con...
140
  			}
c5169e0e   Renato De Donato   a new hope
141
  		});
74249687   Luigi Serra   Cross browser con...
142
143
144
  	},
  
  	hasClass: function( selector ) {
c5169e0e   Renato De Donato   a new hope
145
146
147
148
149
  		var className = " " + selector + " ",
  			i = 0,
  			l = this.length;
  		for ( ; i < l; i++ ) {
  			if ( this[i].nodeType === 1 && (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) >= 0 ) {
74249687   Luigi Serra   Cross browser con...
150
151
152
153
154
155
  				return true;
  			}
  		}
  
  		return false;
  	}
c5169e0e   Renato De Donato   a new hope
156
  });
74249687   Luigi Serra   Cross browser con...
157
  
c5169e0e   Renato De Donato   a new hope
158
  });