Blame view

bower_components/jquery/src/data/Data.js 4.77 KB
c5169e0e   Renato De Donato   a new hope
1
  define([
74249687   Luigi Serra   Cross browser con...
2
3
  	"../core",
  	"../var/rnotwhite",
c5169e0e   Renato De Donato   a new hope
4
5
  	"./accepts"
  ], function( jQuery, rnotwhite ) {
74249687   Luigi Serra   Cross browser con...
6
7
  
  function Data() {
c5169e0e   Renato De Donato   a new hope
8
9
10
11
12
13
14
15
16
  	// Support: Android<4,
  	// Old WebKit does not have Object.preventExtensions/freeze method,
  	// return new empty object instead with no [[set]] accessor
  	Object.defineProperty( this.cache = {}, 0, {
  		get: function() {
  			return {};
  		}
  	});
  
74249687   Luigi Serra   Cross browser con...
17
18
19
20
  	this.expando = jQuery.expando + Data.uid++;
  }
  
  Data.uid = 1;
c5169e0e   Renato De Donato   a new hope
21
  Data.accepts = jQuery.acceptData;
74249687   Luigi Serra   Cross browser con...
22
23
  
  Data.prototype = {
c5169e0e   Renato De Donato   a new hope
24
  	key: function( owner ) {
74249687   Luigi Serra   Cross browser con...
25
26
  		// We can accept data for non-element nodes in modern browsers,
  		// but we should not, see #8335.
c5169e0e   Renato De Donato   a new hope
27
28
29
  		// Always return the key for a frozen object.
  		if ( !Data.accepts( owner ) ) {
  			return 0;
74249687   Luigi Serra   Cross browser con...
30
31
  		}
  
c5169e0e   Renato De Donato   a new hope
32
33
34
  		var descriptor = {},
  			// Check if the owner object already has a cache key
  			unlock = owner[ this.expando ];
74249687   Luigi Serra   Cross browser con...
35
36
  
  		// If not, create one
c5169e0e   Renato De Donato   a new hope
37
38
39
40
41
42
43
44
45
46
47
48
49
  		if ( !unlock ) {
  			unlock = Data.uid++;
  
  			// Secure it in a non-enumerable, non-writable property
  			try {
  				descriptor[ this.expando ] = { value: unlock };
  				Object.defineProperties( owner, descriptor );
  
  			// Support: Android<4
  			// Fallback to a less secure definition
  			} catch ( e ) {
  				descriptor[ this.expando ] = unlock;
  				jQuery.extend( owner, descriptor );
74249687   Luigi Serra   Cross browser con...
50
51
52
  			}
  		}
  
c5169e0e   Renato De Donato   a new hope
53
54
55
56
57
58
  		// Ensure the cache object
  		if ( !this.cache[ unlock ] ) {
  			this.cache[ unlock ] = {};
  		}
  
  		return unlock;
74249687   Luigi Serra   Cross browser con...
59
60
61
  	},
  	set: function( owner, data, value ) {
  		var prop,
c5169e0e   Renato De Donato   a new hope
62
63
64
65
66
  			// There may be an unlock assigned to this node,
  			// if there is no entry for this "owner", create one inline
  			// and set the unlock as though an owner entry had always existed
  			unlock = this.key( owner ),
  			cache = this.cache[ unlock ];
74249687   Luigi Serra   Cross browser con...
67
68
69
70
71
72
73
  
  		// Handle: [ owner, key, value ] args
  		if ( typeof data === "string" ) {
  			cache[ data ] = value;
  
  		// Handle: [ owner, { properties } ] args
  		} else {
c5169e0e   Renato De Donato   a new hope
74
75
76
77
78
79
80
81
  			// Fresh assignments by object are shallow copied
  			if ( jQuery.isEmptyObject( cache ) ) {
  				jQuery.extend( this.cache[ unlock ], data );
  			// Otherwise, copy the properties one-by-one to the cache object
  			} else {
  				for ( prop in data ) {
  					cache[ prop ] = data[ prop ];
  				}
74249687   Luigi Serra   Cross browser con...
82
83
84
85
86
  			}
  		}
  		return cache;
  	},
  	get: function( owner, key ) {
c5169e0e   Renato De Donato   a new hope
87
88
89
90
91
92
  		// Either a valid cache is found, or will be created.
  		// New caches will be created and the unlock returned,
  		// allowing direct access to the newly created
  		// empty data object. A valid owner object must be provided.
  		var cache = this.cache[ this.key( owner ) ];
  
74249687   Luigi Serra   Cross browser con...
93
  		return key === undefined ?
c5169e0e   Renato De Donato   a new hope
94
  			cache : cache[ key ];
74249687   Luigi Serra   Cross browser con...
95
96
97
  	},
  	access: function( owner, key, value ) {
  		var stored;
74249687   Luigi Serra   Cross browser con...
98
99
100
101
102
103
104
105
106
107
108
109
  		// In cases where either:
  		//
  		//   1. No key was specified
  		//   2. A string key was specified, but no value provided
  		//
  		// Take the "read" path and allow the get method to determine
  		// which value to return, respectively either:
  		//
  		//   1. The entire cache object
  		//   2. The data stored at the key
  		//
  		if ( key === undefined ||
c5169e0e   Renato De Donato   a new hope
110
  				((key && typeof key === "string") && value === undefined) ) {
74249687   Luigi Serra   Cross browser con...
111
112
113
114
  
  			stored = this.get( owner, key );
  
  			return stored !== undefined ?
c5169e0e   Renato De Donato   a new hope
115
  				stored : this.get( owner, jQuery.camelCase(key) );
74249687   Luigi Serra   Cross browser con...
116
117
  		}
  
c5169e0e   Renato De Donato   a new hope
118
  		// [*]When the key is not a string, or both a key and value
74249687   Luigi Serra   Cross browser con...
119
120
121
122
123
124
125
126
127
128
129
130
131
  		// are specified, set or extend (existing objects) with either:
  		//
  		//   1. An object of properties
  		//   2. A key and value
  		//
  		this.set( owner, key, value );
  
  		// Since the "set" path can have two possible entry points
  		// return the expected data based on which path was taken[*]
  		return value !== undefined ? value : key;
  	},
  	remove: function( owner, key ) {
  		var i, name, camel,
c5169e0e   Renato De Donato   a new hope
132
133
  			unlock = this.key( owner ),
  			cache = this.cache[ unlock ];
74249687   Luigi Serra   Cross browser con...
134
135
  
  		if ( key === undefined ) {
c5169e0e   Renato De Donato   a new hope
136
  			this.cache[ unlock ] = {};
74249687   Luigi Serra   Cross browser con...
137
138
  
  		} else {
74249687   Luigi Serra   Cross browser con...
139
140
  			// Support array or space separated string of keys
  			if ( jQuery.isArray( key ) ) {
74249687   Luigi Serra   Cross browser con...
141
142
143
144
145
146
147
148
149
  				// If "name" is an array of keys...
  				// When data is initially created, via ("key", "val") signature,
  				// keys will be converted to camelCase.
  				// Since there is no way to tell _how_ a key was added, remove
  				// both plain key and camelCase key. #12786
  				// This will only penalize the array argument path.
  				name = key.concat( key.map( jQuery.camelCase ) );
  			} else {
  				camel = jQuery.camelCase( key );
74249687   Luigi Serra   Cross browser con...
150
151
152
153
  				// Try the string as a key before any manipulation
  				if ( key in cache ) {
  					name = [ key, camel ];
  				} else {
74249687   Luigi Serra   Cross browser con...
154
155
156
157
158
159
160
161
162
  					// If a key with the spaces exists, use it.
  					// Otherwise, create an array by matching non-whitespace
  					name = camel;
  					name = name in cache ?
  						[ name ] : ( name.match( rnotwhite ) || [] );
  				}
  			}
  
  			i = name.length;
74249687   Luigi Serra   Cross browser con...
163
164
165
166
  			while ( i-- ) {
  				delete cache[ name[ i ] ];
  			}
  		}
74249687   Luigi Serra   Cross browser con...
167
168
  	},
  	hasData: function( owner ) {
c5169e0e   Renato De Donato   a new hope
169
170
171
172
173
174
175
176
  		return !jQuery.isEmptyObject(
  			this.cache[ owner[ this.expando ] ] || {}
  		);
  	},
  	discard: function( owner ) {
  		if ( owner[ this.expando ] ) {
  			delete this.cache[ owner[ this.expando ] ];
  		}
74249687   Luigi Serra   Cross browser con...
177
178
179
180
  	}
  };
  
  return Data;
c5169e0e   Renato De Donato   a new hope
181
  });