You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

178 lines
4.1 KiB

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