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