Blame view
app/bower_components/jquery/src/attributes/classes.js
4.17 KB
|
87c93a029
|
1 |
define( [ |
|
f986e111b
|
2 |
"../core", |
|
87c93a029
|
3 4 5 |
"../core/stripAndCollapse", "../var/rnothtmlwhite", "../data/var/dataPriv", |
|
f986e111b
|
6 |
"../core/init" |
|
87c93a029
|
7 |
], function( jQuery, stripAndCollapse, rnothtmlwhite, dataPriv ) {
|
|
f986e111b
|
8 |
|
|
87c93a029
|
9 |
"use strict"; |
|
f986e111b
|
10 |
|
|
87c93a029
|
11 12 13 14 15 |
function getClass( elem ) {
return elem.getAttribute && elem.getAttribute( "class" ) || "";
}
jQuery.fn.extend( {
|
|
f986e111b
|
16 |
addClass: function( value ) {
|
|
87c93a029
|
17 18 |
var classes, elem, cur, curValue, clazz, j, finalValue, i = 0; |
|
f986e111b
|
19 20 |
if ( jQuery.isFunction( value ) ) {
|
|
87c93a029
|
21 22 23 |
return this.each( function( j ) {
jQuery( this ).addClass( value.call( this, j, getClass( this ) ) );
} );
|
|
f986e111b
|
24 |
} |
|
87c93a029
|
25 26 |
if ( typeof value === "string" && value ) {
classes = value.match( rnothtmlwhite ) || [];
|
|
f986e111b
|
27 |
|
|
87c93a029
|
28 29 30 |
while ( ( elem = this[ i++ ] ) ) {
curValue = getClass( elem );
cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
|
|
f986e111b
|
31 32 33 |
if ( cur ) {
j = 0;
|
|
87c93a029
|
34 |
while ( ( clazz = classes[ j++ ] ) ) {
|
|
f986e111b
|
35 36 37 38 |
if ( cur.indexOf( " " + clazz + " " ) < 0 ) {
cur += clazz + " ";
}
}
|
|
87c93a029
|
39 40 41 42 |
// Only assign if different to avoid unneeded rendering.
finalValue = stripAndCollapse( cur );
if ( curValue !== finalValue ) {
elem.setAttribute( "class", finalValue );
|
|
f986e111b
|
43 44 45 46 47 48 49 50 51 |
}
}
}
}
return this;
},
removeClass: function( value ) {
|
|
87c93a029
|
52 53 |
var classes, elem, cur, curValue, clazz, j, finalValue, i = 0; |
|
f986e111b
|
54 55 |
if ( jQuery.isFunction( value ) ) {
|
|
87c93a029
|
56 57 58 59 60 61 62 |
return this.each( function( j ) {
jQuery( this ).removeClass( value.call( this, j, getClass( this ) ) );
} );
}
if ( !arguments.length ) {
return this.attr( "class", "" );
|
|
f986e111b
|
63 |
} |
|
f986e111b
|
64 |
|
|
87c93a029
|
65 66 67 68 69 |
if ( typeof value === "string" && value ) {
classes = value.match( rnothtmlwhite ) || [];
while ( ( elem = this[ i++ ] ) ) {
curValue = getClass( elem );
|
|
f986e111b
|
70 |
// This expression is here for better compressibility (see addClass) |
|
87c93a029
|
71 |
cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " ); |
|
f986e111b
|
72 73 74 |
if ( cur ) {
j = 0;
|
|
87c93a029
|
75 |
while ( ( clazz = classes[ j++ ] ) ) {
|
|
f986e111b
|
76 |
// Remove *all* instances |
|
87c93a029
|
77 |
while ( cur.indexOf( " " + clazz + " " ) > -1 ) {
|
|
f986e111b
|
78 79 80 |
cur = cur.replace( " " + clazz + " ", " " ); } } |
|
87c93a029
|
81 82 83 84 |
// Only assign if different to avoid unneeded rendering.
finalValue = stripAndCollapse( cur );
if ( curValue !== finalValue ) {
elem.setAttribute( "class", finalValue );
|
|
f986e111b
|
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
}
}
}
}
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 ) ) {
|
|
87c93a029
|
101 102 103 104 105 106 |
return this.each( function( i ) {
jQuery( this ).toggleClass(
value.call( this, i, getClass( this ), stateVal ),
stateVal
);
} );
|
|
f986e111b
|
107 |
} |
|
87c93a029
|
108 109 |
return this.each( function() {
var className, i, self, classNames;
|
|
f986e111b
|
110 |
if ( type === "string" ) {
|
|
87c93a029
|
111 112 113 114 115 116 117 118 119 |
// Toggle individual class names
i = 0;
self = jQuery( this );
classNames = value.match( rnothtmlwhite ) || [];
while ( ( className = classNames[ i++ ] ) ) {
// Check each className given, space separated list
|
|
f986e111b
|
120 121 122 123 124 125 126 127 |
if ( self.hasClass( className ) ) {
self.removeClass( className );
} else {
self.addClass( className );
}
}
// Toggle whole class name
|
|
87c93a029
|
128 129 130 131 132 133 |
} else if ( value === undefined || type === "boolean" ) {
className = getClass( this );
if ( className ) {
// Store className if set
dataPriv.set( this, "__className__", className );
|
|
f986e111b
|
134 |
} |
|
87c93a029
|
135 |
// If the element has a class name or if we're passed `false`, |
|
f986e111b
|
136 137 138 |
// 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. |
|
87c93a029
|
139 140 141 142 143 144 145 |
if ( this.setAttribute ) {
this.setAttribute( "class",
className || value === false ?
"" :
dataPriv.get( this, "__className__" ) || ""
);
}
|
|
f986e111b
|
146 |
} |
|
87c93a029
|
147 |
} ); |
|
f986e111b
|
148 149 150 |
},
hasClass: function( selector ) {
|
|
87c93a029
|
151 152 153 154 155 156 157 158 |
var className, elem,
i = 0;
className = " " + selector + " ";
while ( ( elem = this[ i++ ] ) ) {
if ( elem.nodeType === 1 &&
( " " + stripAndCollapse( getClass( elem ) ) + " " ).indexOf( className ) > -1 ) {
return true;
|
|
f986e111b
|
159 160 161 162 163 |
} } return false; } |
|
87c93a029
|
164 |
} ); |
|
f986e111b
|
165 |
|
|
87c93a029
|
166 |
} ); |