Projet

Général

Profil

Paste
Télécharger (8,31 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / libraries / flexslider-2.5.0 / bower_components / jquery / src / data.js @ 0aee3c58

1
define([
2
        "./core",
3
        "./var/deletedIds",
4
        "./data/support",
5
        "./data/accepts"
6
], function( jQuery, deletedIds, support ) {
7

    
8
var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/,
9
        rmultiDash = /([A-Z])/g;
10

    
11
function dataAttr( elem, key, data ) {
12
        // If nothing was found internally, try to fetch any
13
        // data from the HTML5 data-* attribute
14
        if ( data === undefined && elem.nodeType === 1 ) {
15

    
16
                var name = "data-" + key.replace( rmultiDash, "-$1" ).toLowerCase();
17

    
18
                data = elem.getAttribute( name );
19

    
20
                if ( typeof data === "string" ) {
21
                        try {
22
                                data = data === "true" ? true :
23
                                        data === "false" ? false :
24
                                        data === "null" ? null :
25
                                        // Only convert to a number if it doesn't change the string
26
                                        +data + "" === data ? +data :
27
                                        rbrace.test( data ) ? jQuery.parseJSON( data ) :
28
                                        data;
29
                        } catch( e ) {}
30

    
31
                        // Make sure we set the data so it isn't changed later
32
                        jQuery.data( elem, key, data );
33

    
34
                } else {
35
                        data = undefined;
36
                }
37
        }
38

    
39
        return data;
40
}
41

    
42
// checks a cache object for emptiness
43
function isEmptyDataObject( obj ) {
44
        var name;
45
        for ( name in obj ) {
46

    
47
                // if the public data object is empty, the private is still empty
48
                if ( name === "data" && jQuery.isEmptyObject( obj[name] ) ) {
49
                        continue;
50
                }
51
                if ( name !== "toJSON" ) {
52
                        return false;
53
                }
54
        }
55

    
56
        return true;
57
}
58

    
59
function internalData( elem, name, data, pvt /* Internal Use Only */ ) {
60
        if ( !jQuery.acceptData( elem ) ) {
61
                return;
62
        }
63

    
64
        var ret, thisCache,
65
                internalKey = jQuery.expando,
66

    
67
                // We have to handle DOM nodes and JS objects differently because IE6-7
68
                // can't GC object references properly across the DOM-JS boundary
69
                isNode = elem.nodeType,
70

    
71
                // Only DOM nodes need the global jQuery cache; JS object data is
72
                // attached directly to the object so GC can occur automatically
73
                cache = isNode ? jQuery.cache : elem,
74

    
75
                // Only defining an ID for JS objects if its cache already exists allows
76
                // the code to shortcut on the same path as a DOM node with no cache
77
                id = isNode ? elem[ internalKey ] : elem[ internalKey ] && internalKey;
78

    
79
        // Avoid doing any more work than we need to when trying to get data on an
80
        // object that has no data at all
81
        if ( (!id || !cache[id] || (!pvt && !cache[id].data)) && data === undefined && typeof name === "string" ) {
82
                return;
83
        }
84

    
85
        if ( !id ) {
86
                // Only DOM nodes need a new unique ID for each element since their data
87
                // ends up in the global cache
88
                if ( isNode ) {
89
                        id = elem[ internalKey ] = deletedIds.pop() || jQuery.guid++;
90
                } else {
91
                        id = internalKey;
92
                }
93
        }
94

    
95
        if ( !cache[ id ] ) {
96
                // Avoid exposing jQuery metadata on plain JS objects when the object
97
                // is serialized using JSON.stringify
98
                cache[ id ] = isNode ? {} : { toJSON: jQuery.noop };
99
        }
100

    
101
        // An object can be passed to jQuery.data instead of a key/value pair; this gets
102
        // shallow copied over onto the existing cache
103
        if ( typeof name === "object" || typeof name === "function" ) {
104
                if ( pvt ) {
105
                        cache[ id ] = jQuery.extend( cache[ id ], name );
106
                } else {
107
                        cache[ id ].data = jQuery.extend( cache[ id ].data, name );
108
                }
109
        }
110

    
111
        thisCache = cache[ id ];
112

    
113
        // jQuery data() is stored in a separate object inside the object's internal data
114
        // cache in order to avoid key collisions between internal data and user-defined
115
        // data.
116
        if ( !pvt ) {
117
                if ( !thisCache.data ) {
118
                        thisCache.data = {};
119
                }
120

    
121
                thisCache = thisCache.data;
122
        }
123

    
124
        if ( data !== undefined ) {
125
                thisCache[ jQuery.camelCase( name ) ] = data;
126
        }
127

    
128
        // Check for both converted-to-camel and non-converted data property names
129
        // If a data property was specified
130
        if ( typeof name === "string" ) {
131

    
132
                // First Try to find as-is property data
133
                ret = thisCache[ name ];
134

    
135
                // Test for null|undefined property data
136
                if ( ret == null ) {
137

    
138
                        // Try to find the camelCased property
139
                        ret = thisCache[ jQuery.camelCase( name ) ];
140
                }
141
        } else {
142
                ret = thisCache;
143
        }
144

    
145
        return ret;
146
}
147

    
148
function internalRemoveData( elem, name, pvt ) {
149
        if ( !jQuery.acceptData( elem ) ) {
150
                return;
151
        }
152

    
153
        var thisCache, i,
154
                isNode = elem.nodeType,
155

    
156
                // See jQuery.data for more information
157
                cache = isNode ? jQuery.cache : elem,
158
                id = isNode ? elem[ jQuery.expando ] : jQuery.expando;
159

    
160
        // If there is already no cache entry for this object, there is no
161
        // purpose in continuing
162
        if ( !cache[ id ] ) {
163
                return;
164
        }
165

    
166
        if ( name ) {
167

    
168
                thisCache = pvt ? cache[ id ] : cache[ id ].data;
169

    
170
                if ( thisCache ) {
171

    
172
                        // Support array or space separated string names for data keys
173
                        if ( !jQuery.isArray( name ) ) {
174

    
175
                                // try the string as a key before any manipulation
176
                                if ( name in thisCache ) {
177
                                        name = [ name ];
178
                                } else {
179

    
180
                                        // split the camel cased version by spaces unless a key with the spaces exists
181
                                        name = jQuery.camelCase( name );
182
                                        if ( name in thisCache ) {
183
                                                name = [ name ];
184
                                        } else {
185
                                                name = name.split(" ");
186
                                        }
187
                                }
188
                        } else {
189
                                // If "name" is an array of keys...
190
                                // When data is initially created, via ("key", "val") signature,
191
                                // keys will be converted to camelCase.
192
                                // Since there is no way to tell _how_ a key was added, remove
193
                                // both plain key and camelCase key. #12786
194
                                // This will only penalize the array argument path.
195
                                name = name.concat( jQuery.map( name, jQuery.camelCase ) );
196
                        }
197

    
198
                        i = name.length;
199
                        while ( i-- ) {
200
                                delete thisCache[ name[i] ];
201
                        }
202

    
203
                        // If there is no data left in the cache, we want to continue
204
                        // and let the cache object itself get destroyed
205
                        if ( pvt ? !isEmptyDataObject(thisCache) : !jQuery.isEmptyObject(thisCache) ) {
206
                                return;
207
                        }
208
                }
209
        }
210

    
211
        // See jQuery.data for more information
212
        if ( !pvt ) {
213
                delete cache[ id ].data;
214

    
215
                // Don't destroy the parent cache unless the internal data object
216
                // had been the only thing left in it
217
                if ( !isEmptyDataObject( cache[ id ] ) ) {
218
                        return;
219
                }
220
        }
221

    
222
        // Destroy the cache
223
        if ( isNode ) {
224
                jQuery.cleanData( [ elem ], true );
225

    
226
        // Use delete when supported for expandos or `cache` is not a window per isWindow (#10080)
227
        /* jshint eqeqeq: false */
228
        } else if ( support.deleteExpando || cache != cache.window ) {
229
                /* jshint eqeqeq: true */
230
                delete cache[ id ];
231

    
232
        // When all else fails, null
233
        } else {
234
                cache[ id ] = null;
235
        }
236
}
237

    
238
jQuery.extend({
239
        cache: {},
240

    
241
        // The following elements (space-suffixed to avoid Object.prototype collisions)
242
        // throw uncatchable exceptions if you attempt to set expando properties
243
        noData: {
244
                "applet ": true,
245
                "embed ": true,
246
                // ...but Flash objects (which have this classid) *can* handle expandos
247
                "object ": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
248
        },
249

    
250
        hasData: function( elem ) {
251
                elem = elem.nodeType ? jQuery.cache[ elem[jQuery.expando] ] : elem[ jQuery.expando ];
252
                return !!elem && !isEmptyDataObject( elem );
253
        },
254

    
255
        data: function( elem, name, data ) {
256
                return internalData( elem, name, data );
257
        },
258

    
259
        removeData: function( elem, name ) {
260
                return internalRemoveData( elem, name );
261
        },
262

    
263
        // For internal use only.
264
        _data: function( elem, name, data ) {
265
                return internalData( elem, name, data, true );
266
        },
267

    
268
        _removeData: function( elem, name ) {
269
                return internalRemoveData( elem, name, true );
270
        }
271
});
272

    
273
jQuery.fn.extend({
274
        data: function( key, value ) {
275
                var i, name, data,
276
                        elem = this[0],
277
                        attrs = elem && elem.attributes;
278

    
279
                // Special expections of .data basically thwart jQuery.access,
280
                // so implement the relevant behavior ourselves
281

    
282
                // Gets all values
283
                if ( key === undefined ) {
284
                        if ( this.length ) {
285
                                data = jQuery.data( elem );
286

    
287
                                if ( elem.nodeType === 1 && !jQuery._data( elem, "parsedAttrs" ) ) {
288
                                        i = attrs.length;
289
                                        while ( i-- ) {
290

    
291
                                                // Support: IE11+
292
                                                // The attrs elements can be null (#14894)
293
                                                if ( attrs[ i ] ) {
294
                                                        name = attrs[ i ].name;
295
                                                        if ( name.indexOf( "data-" ) === 0 ) {
296
                                                                name = jQuery.camelCase( name.slice(5) );
297
                                                                dataAttr( elem, name, data[ name ] );
298
                                                        }
299
                                                }
300
                                        }
301
                                        jQuery._data( elem, "parsedAttrs", true );
302
                                }
303
                        }
304

    
305
                        return data;
306
                }
307

    
308
                // Sets multiple values
309
                if ( typeof key === "object" ) {
310
                        return this.each(function() {
311
                                jQuery.data( this, key );
312
                        });
313
                }
314

    
315
                return arguments.length > 1 ?
316

    
317
                        // Sets one value
318
                        this.each(function() {
319
                                jQuery.data( this, key, value );
320
                        }) :
321

    
322
                        // Gets one value
323
                        // Try to fetch any internally stored data first
324
                        elem ? dataAttr( elem, key, jQuery.data( elem, key ) ) : undefined;
325
        },
326

    
327
        removeData: function( key ) {
328
                return this.each(function() {
329
                        jQuery.removeData( this, key );
330
                });
331
        }
332
});
333

    
334
return jQuery;
335
});