624e2327d0bec1b0ce7b460d735a89326c52fd116ab872f646315bdf790f80f33051cc8fb6f0606515551b679dd977cd74d1fcc815a8b446c676c4d359044e 4.4 KB

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