data.js 4.2 KB

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