data.js 4.7 KB

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