c5169e0e
Renato De Donato
a new hope
|
1
|
define([
|
74249687
Luigi Serra
Cross browser con...
|
2
|
"./core",
|
c5169e0e
Renato De Donato
a new hope
|
3
|
"./var/rnotwhite",
|
74249687
Luigi Serra
Cross browser con...
|
4
|
"./core/access",
|
c5169e0e
Renato De Donato
a new hope
|
5
6
7
|
"./data/var/data_priv",
"./data/var/data_user"
], function( jQuery, rnotwhite, access, data_priv, data_user ) {
|
74249687
Luigi Serra
Cross browser con...
|
8
9
10
11
12
13
14
15
16
17
18
19
|
// 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
var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/,
|
c5169e0e
Renato De Donato
a new hope
|
20
|
rmultiDash = /([A-Z])/g;
|
74249687
Luigi Serra
Cross browser con...
|
21
22
23
24
25
26
27
|
function dataAttr( elem, key, data ) {
var name;
// If nothing was found internally, try to fetch any
// data from the HTML5 data-* attribute
if ( data === undefined && elem.nodeType === 1 ) {
|
c5169e0e
Renato De Donato
a new hope
|
28
|
name = "data-" + key.replace( rmultiDash, "-$1" ).toLowerCase();
|
74249687
Luigi Serra
Cross browser con...
|
29
30
31
32
33
34
35
|
data = elem.getAttribute( name );
if ( typeof data === "string" ) {
try {
data = data === "true" ? true :
data === "false" ? false :
data === "null" ? null :
|
74249687
Luigi Serra
Cross browser con...
|
36
37
38
39
|
// Only convert to a number if it doesn't change the string
+data + "" === data ? +data :
rbrace.test( data ) ? jQuery.parseJSON( data ) :
data;
|
c5169e0e
Renato De Donato
a new hope
|
40
|
} catch( e ) {}
|
74249687
Luigi Serra
Cross browser con...
|
41
42
|
// Make sure we set the data so it isn't changed later
|
c5169e0e
Renato De Donato
a new hope
|
43
|
data_user.set( elem, key, data );
|
74249687
Luigi Serra
Cross browser con...
|
44
45
46
47
48
49
50
|
} else {
data = undefined;
}
}
return data;
}
|
c5169e0e
Renato De Donato
a new hope
|
51
|
jQuery.extend({
|
74249687
Luigi Serra
Cross browser con...
|
52
|
hasData: function( elem ) {
|
c5169e0e
Renato De Donato
a new hope
|
53
|
return data_user.hasData( elem ) || data_priv.hasData( elem );
|
74249687
Luigi Serra
Cross browser con...
|
54
55
56
|
},
data: function( elem, name, data ) {
|
c5169e0e
Renato De Donato
a new hope
|
57
|
return data_user.access( elem, name, data );
|
74249687
Luigi Serra
Cross browser con...
|
58
59
60
|
},
removeData: function( elem, name ) {
|
c5169e0e
Renato De Donato
a new hope
|
61
|
data_user.remove( elem, name );
|
74249687
Luigi Serra
Cross browser con...
|
62
63
64
|
},
// TODO: Now that all calls to _data and _removeData have been replaced
|
c5169e0e
Renato De Donato
a new hope
|
65
|
// with direct calls to data_priv methods, these can be deprecated.
|
74249687
Luigi Serra
Cross browser con...
|
66
|
_data: function( elem, name, data ) {
|
c5169e0e
Renato De Donato
a new hope
|
67
|
return data_priv.access( elem, name, data );
|
74249687
Luigi Serra
Cross browser con...
|
68
69
70
|
},
_removeData: function( elem, name ) {
|
c5169e0e
Renato De Donato
a new hope
|
71
|
data_priv.remove( elem, name );
|
74249687
Luigi Serra
Cross browser con...
|
72
|
}
|
c5169e0e
Renato De Donato
a new hope
|
73
|
});
|
74249687
Luigi Serra
Cross browser con...
|
74
|
|
c5169e0e
Renato De Donato
a new hope
|
75
|
jQuery.fn.extend({
|
74249687
Luigi Serra
Cross browser con...
|
76
77
78
79
80
81
82
83
|
data: function( key, value ) {
var i, name, data,
elem = this[ 0 ],
attrs = elem && elem.attributes;
// Gets all values
if ( key === undefined ) {
if ( this.length ) {
|
c5169e0e
Renato De Donato
a new hope
|
84
|
data = data_user.get( elem );
|
74249687
Luigi Serra
Cross browser con...
|
85
|
|
c5169e0e
Renato De Donato
a new hope
|
86
|
if ( elem.nodeType === 1 && !data_priv.get( elem, "hasDataAttrs" ) ) {
|
74249687
Luigi Serra
Cross browser con...
|
87
88
89
90
91
92
93
94
|
i = attrs.length;
while ( i-- ) {
// Support: IE11+
// The attrs elements can be null (#14894)
if ( attrs[ i ] ) {
name = attrs[ i ].name;
if ( name.indexOf( "data-" ) === 0 ) {
|
c5169e0e
Renato De Donato
a new hope
|
95
|
name = jQuery.camelCase( name.slice(5) );
|
74249687
Luigi Serra
Cross browser con...
|
96
97
98
99
|
dataAttr( elem, name, data[ name ] );
}
}
}
|
c5169e0e
Renato De Donato
a new hope
|
100
|
data_priv.set( elem, "hasDataAttrs", true );
|
74249687
Luigi Serra
Cross browser con...
|
101
102
103
104
105
106
107
108
|
}
}
return data;
}
// Sets multiple values
if ( typeof key === "object" ) {
|
c5169e0e
Renato De Donato
a new hope
|
109
110
111
|
return this.each(function() {
data_user.set( this, key );
});
|
74249687
Luigi Serra
Cross browser con...
|
112
113
114
|
}
return access( this, function( value ) {
|
c5169e0e
Renato De Donato
a new hope
|
115
116
|
var data,
camelKey = jQuery.camelCase( key );
|
74249687
Luigi Serra
Cross browser con...
|
117
118
119
120
121
122
123
|
// 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 ) {
|
74249687
Luigi Serra
Cross browser con...
|
124
125
|
// Attempt to get data from the cache
// with the key as-is
|
c5169e0e
Renato De Donato
a new hope
|
126
|
data = data_user.get( elem, key );
|
74249687
Luigi Serra
Cross browser con...
|
127
128
129
130
|
if ( data !== undefined ) {
return data;
}
|
74249687
Luigi Serra
Cross browser con...
|
131
132
|
// Attempt to get data from the cache
// with the key camelized
|
c5169e0e
Renato De Donato
a new hope
|
133
|
data = data_user.get( elem, camelKey );
|
74249687
Luigi Serra
Cross browser con...
|
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
if ( data !== undefined ) {
return data;
}
// Attempt to "discover" the data in
// HTML5 custom data-* attrs
data = dataAttr( elem, camelKey, undefined );
if ( data !== undefined ) {
return data;
}
// We tried really hard, but the data doesn't exist.
return;
}
// Set the data...
|
c5169e0e
Renato De Donato
a new hope
|
150
|
this.each(function() {
|
74249687
Luigi Serra
Cross browser con...
|
151
152
|
// First, attempt to store a copy or reference of any
// data that might've been store with a camelCased key.
|
c5169e0e
Renato De Donato
a new hope
|
153
|
var data = data_user.get( this, camelKey );
|
74249687
Luigi Serra
Cross browser con...
|
154
155
156
157
|
// For HTML5 data-* attribute interop, we have to
// store property names with dashes in a camelCase form.
// This might not apply to all properties...*
|
c5169e0e
Renato De Donato
a new hope
|
158
|
data_user.set( this, camelKey, value );
|
74249687
Luigi Serra
Cross browser con...
|
159
160
161
162
|
// *... In the case of properties that might _actually_
// have dashes, we need to also store a copy of that
// unchanged property.
|
c5169e0e
Renato De Donato
a new hope
|
163
164
|
if ( key.indexOf("-") !== -1 && data !== undefined ) {
data_user.set( this, key, value );
|
74249687
Luigi Serra
Cross browser con...
|
165
|
}
|
c5169e0e
Renato De Donato
a new hope
|
166
|
});
|
74249687
Luigi Serra
Cross browser con...
|
167
168
169
170
|
}, null, value, arguments.length > 1, null, true );
},
removeData: function( key ) {
|
c5169e0e
Renato De Donato
a new hope
|
171
172
173
|
return this.each(function() {
data_user.remove( this, key );
});
|
74249687
Luigi Serra
Cross browser con...
|
174
|
}
|
c5169e0e
Renato De Donato
a new hope
|
175
|
});
|
74249687
Luigi Serra
Cross browser con...
|
176
177
|
return jQuery;
|
c5169e0e
Renato De Donato
a new hope
|
178
|
});
|