classes.js 4.1 KB

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