Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

188 Zeilen
5.0KB

  1. define( [
  2. "./core",
  3. "./core/access",
  4. "./data/var/dataPriv",
  5. "./data/var/dataUser"
  6. ], function( jQuery, access, dataPriv, dataUser ) {
  7. // Implementation Summary
  8. //
  9. // 1. Enforce API surface and semantic compatibility with 1.9.x branch
  10. // 2. Improve the module's maintainability by reducing the storage
  11. // paths to a single mechanism.
  12. // 3. Use the same single mechanism to support "private" and "user" data.
  13. // 4. _Never_ expose "private" data to user code (TODO: Drop _data, _removeData)
  14. // 5. Avoid exposing implementation details on user objects (eg. expando properties)
  15. // 6. Provide a clear path for implementation upgrade to WeakMap in 2014
  16. var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/,
  17. rmultiDash = /[A-Z]/g;
  18. function dataAttr( elem, key, data ) {
  19. var name;
  20. // If nothing was found internally, try to fetch any
  21. // data from the HTML5 data-* attribute
  22. if ( data === undefined && elem.nodeType === 1 ) {
  23. name = "data-" + key.replace( rmultiDash, "-$&" ).toLowerCase();
  24. data = elem.getAttribute( name );
  25. if ( typeof data === "string" ) {
  26. try {
  27. data = data === "true" ? true :
  28. data === "false" ? false :
  29. data === "null" ? null :
  30. // Only convert to a number if it doesn't change the string
  31. +data + "" === data ? +data :
  32. rbrace.test( data ) ? jQuery.parseJSON( data ) :
  33. data;
  34. } catch ( e ) {}
  35. // Make sure we set the data so it isn't changed later
  36. dataUser.set( elem, key, data );
  37. } else {
  38. data = undefined;
  39. }
  40. }
  41. return data;
  42. }
  43. jQuery.extend( {
  44. hasData: function( elem ) {
  45. return dataUser.hasData( elem ) || dataPriv.hasData( elem );
  46. },
  47. data: function( elem, name, data ) {
  48. return dataUser.access( elem, name, data );
  49. },
  50. removeData: function( elem, name ) {
  51. dataUser.remove( elem, name );
  52. },
  53. // TODO: Now that all calls to _data and _removeData have been replaced
  54. // with direct calls to dataPriv methods, these can be deprecated.
  55. _data: function( elem, name, data ) {
  56. return dataPriv.access( elem, name, data );
  57. },
  58. _removeData: function( elem, name ) {
  59. dataPriv.remove( elem, name );
  60. }
  61. } );
  62. jQuery.fn.extend( {
  63. data: function( key, value ) {
  64. var i, name, data,
  65. elem = this[ 0 ],
  66. attrs = elem && elem.attributes;
  67. // Gets all values
  68. if ( key === undefined ) {
  69. if ( this.length ) {
  70. data = dataUser.get( elem );
  71. if ( elem.nodeType === 1 && !dataPriv.get( elem, "hasDataAttrs" ) ) {
  72. i = attrs.length;
  73. while ( i-- ) {
  74. // Support: IE11+
  75. // The attrs elements can be null (#14894)
  76. if ( attrs[ i ] ) {
  77. name = attrs[ i ].name;
  78. if ( name.indexOf( "data-" ) === 0 ) {
  79. name = jQuery.camelCase( name.slice( 5 ) );
  80. dataAttr( elem, name, data[ name ] );
  81. }
  82. }
  83. }
  84. dataPriv.set( elem, "hasDataAttrs", true );
  85. }
  86. }
  87. return data;
  88. }
  89. // Sets multiple values
  90. if ( typeof key === "object" ) {
  91. return this.each( function() {
  92. dataUser.set( this, key );
  93. } );
  94. }
  95. return access( this, function( value ) {
  96. var data, camelKey;
  97. // The calling jQuery object (element matches) is not empty
  98. // (and therefore has an element appears at this[ 0 ]) and the
  99. // `value` parameter was not undefined. An empty jQuery object
  100. // will result in `undefined` for elem = this[ 0 ] which will
  101. // throw an exception if an attempt to read a data cache is made.
  102. if ( elem && value === undefined ) {
  103. // Attempt to get data from the cache
  104. // with the key as-is
  105. data = dataUser.get( elem, key ) ||
  106. // Try to find dashed key if it exists (gh-2779)
  107. // This is for 2.2.x only
  108. dataUser.get( elem, key.replace( rmultiDash, "-$&" ).toLowerCase() );
  109. if ( data !== undefined ) {
  110. return data;
  111. }
  112. camelKey = jQuery.camelCase( key );
  113. // Attempt to get data from the cache
  114. // with the key camelized
  115. data = dataUser.get( elem, camelKey );
  116. if ( data !== undefined ) {
  117. return data;
  118. }
  119. // Attempt to "discover" the data in
  120. // HTML5 custom data-* attrs
  121. data = dataAttr( elem, camelKey, undefined );
  122. if ( data !== undefined ) {
  123. return data;
  124. }
  125. // We tried really hard, but the data doesn't exist.
  126. return;
  127. }
  128. // Set the data...
  129. camelKey = jQuery.camelCase( key );
  130. this.each( function() {
  131. // First, attempt to store a copy or reference of any
  132. // data that might've been store with a camelCased key.
  133. var data = dataUser.get( this, camelKey );
  134. // For HTML5 data-* attribute interop, we have to
  135. // store property names with dashes in a camelCase form.
  136. // This might not apply to all properties...*
  137. dataUser.set( this, camelKey, value );
  138. // *... In the case of properties that might _actually_
  139. // have dashes, we need to also store a copy of that
  140. // unchanged property.
  141. if ( key.indexOf( "-" ) > -1 && data !== undefined ) {
  142. dataUser.set( this, key, value );
  143. }
  144. } );
  145. }, null, value, arguments.length > 1, null, true );
  146. },
  147. removeData: function( key ) {
  148. return this.each( function() {
  149. dataUser.remove( this, key );
  150. } );
  151. }
  152. } );
  153. return jQuery;
  154. } );