data.js 4.8 KB

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