No Description

data.js 4.2KB

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