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