data.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 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, "-$&" ).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 ) ? JSON.parse( data ) :
  34. data;
  35. } catch ( e ) {}
  36. // Make sure we set the data so it isn't changed later
  37. dataUser.set( elem, key, data );
  38. } else {
  39. data = undefined;
  40. }
  41. }
  42. return data;
  43. }
  44. jQuery.extend( {
  45. hasData: function( elem ) {
  46. return dataUser.hasData( elem ) || dataPriv.hasData( elem );
  47. },
  48. data: function( elem, name, data ) {
  49. return dataUser.access( elem, name, data );
  50. },
  51. removeData: function( elem, name ) {
  52. dataUser.remove( elem, name );
  53. },
  54. // TODO: Now that all calls to _data and _removeData have been replaced
  55. // with direct calls to dataPriv methods, these can be deprecated.
  56. _data: function( elem, name, data ) {
  57. return dataPriv.access( elem, name, data );
  58. },
  59. _removeData: function( elem, name ) {
  60. dataPriv.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 = dataUser.get( elem );
  72. if ( elem.nodeType === 1 && !dataPriv.get( elem, "hasDataAttrs" ) ) {
  73. i = attrs.length;
  74. while ( i-- ) {
  75. // Support: IE 11 only
  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. dataPriv.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. dataUser.set( this, key );
  94. } );
  95. }
  96. return access( this, function( value ) {
  97. var data;
  98. // The calling jQuery object (element matches) is not empty
  99. // (and therefore has an element appears at this[ 0 ]) and the
  100. // `value` parameter was not undefined. An empty jQuery object
  101. // will result in `undefined` for elem = this[ 0 ] which will
  102. // throw an exception if an attempt to read a data cache is made.
  103. if ( elem && value === undefined ) {
  104. // Attempt to get data from the cache
  105. // The key will always be camelCased in Data
  106. data = dataUser.get( elem, key );
  107. if ( data !== undefined ) {
  108. return data;
  109. }
  110. // Attempt to "discover" the data in
  111. // HTML5 custom data-* attrs
  112. data = dataAttr( elem, key );
  113. if ( data !== undefined ) {
  114. return data;
  115. }
  116. // We tried really hard, but the data doesn't exist.
  117. return;
  118. }
  119. // Set the data...
  120. this.each( function() {
  121. // We always store the camelCased key
  122. dataUser.set( this, key, value );
  123. } );
  124. }, null, value, arguments.length > 1, null, true );
  125. },
  126. removeData: function( key ) {
  127. return this.each( function() {
  128. dataUser.remove( this, key );
  129. } );
  130. }
  131. } );
  132. return jQuery;
  133. } );