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