Blame view

bower_components/jquery/src/data.js 5 KB
a1a3bc73   Luigi Serra   graphs updates
1
  define( [
74249687   Luigi Serra   Cross browser con...
2
  	"./core",
74249687   Luigi Serra   Cross browser con...
3
  	"./core/access",
a1a3bc73   Luigi Serra   graphs updates
4
5
6
  	"./data/var/dataPriv",
  	"./data/var/dataUser"
  ], function( jQuery, access, dataPriv, dataUser ) {
74249687   Luigi Serra   Cross browser con...
7
8
9
10
11
12
13
14
15
16
17
18
  
  //	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]*\])$/,
a1a3bc73   Luigi Serra   graphs updates
19
  	rmultiDash = /[A-Z]/g;
74249687   Luigi Serra   Cross browser con...
20
21
22
23
24
25
26
  
  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 ) {
a1a3bc73   Luigi Serra   graphs updates
27
  		name = "data-" + key.replace( rmultiDash, "-$&" ).toLowerCase();
74249687   Luigi Serra   Cross browser con...
28
29
30
31
32
33
34
  		data = elem.getAttribute( name );
  
  		if ( typeof data === "string" ) {
  			try {
  				data = data === "true" ? true :
  					data === "false" ? false :
  					data === "null" ? null :
a1a3bc73   Luigi Serra   graphs updates
35
  
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;
a1a3bc73   Luigi Serra   graphs updates
40
  			} catch ( e ) {}
74249687   Luigi Serra   Cross browser con...
41
42
  
  			// Make sure we set the data so it isn't changed later
a1a3bc73   Luigi Serra   graphs updates
43
  			dataUser.set( elem, key, data );
74249687   Luigi Serra   Cross browser con...
44
45
46
47
48
49
50
  		} else {
  			data = undefined;
  		}
  	}
  	return data;
  }
  
a1a3bc73   Luigi Serra   graphs updates
51
  jQuery.extend( {
74249687   Luigi Serra   Cross browser con...
52
  	hasData: function( elem ) {
a1a3bc73   Luigi Serra   graphs updates
53
  		return dataUser.hasData( elem ) || dataPriv.hasData( elem );
74249687   Luigi Serra   Cross browser con...
54
55
56
  	},
  
  	data: function( elem, name, data ) {
a1a3bc73   Luigi Serra   graphs updates
57
  		return dataUser.access( elem, name, data );
74249687   Luigi Serra   Cross browser con...
58
59
60
  	},
  
  	removeData: function( elem, name ) {
a1a3bc73   Luigi Serra   graphs updates
61
  		dataUser.remove( elem, name );
74249687   Luigi Serra   Cross browser con...
62
63
64
  	},
  
  	// TODO: Now that all calls to _data and _removeData have been replaced
a1a3bc73   Luigi Serra   graphs updates
65
  	// with direct calls to dataPriv methods, these can be deprecated.
74249687   Luigi Serra   Cross browser con...
66
  	_data: function( elem, name, data ) {
a1a3bc73   Luigi Serra   graphs updates
67
  		return dataPriv.access( elem, name, data );
74249687   Luigi Serra   Cross browser con...
68
69
70
  	},
  
  	_removeData: function( elem, name ) {
a1a3bc73   Luigi Serra   graphs updates
71
  		dataPriv.remove( elem, name );
74249687   Luigi Serra   Cross browser con...
72
  	}
a1a3bc73   Luigi Serra   graphs updates
73
  } );
74249687   Luigi Serra   Cross browser con...
74
  
a1a3bc73   Luigi Serra   graphs updates
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 ) {
a1a3bc73   Luigi Serra   graphs updates
84
  				data = dataUser.get( elem );
74249687   Luigi Serra   Cross browser con...
85
  
a1a3bc73   Luigi Serra   graphs updates
86
  				if ( elem.nodeType === 1 && !dataPriv.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 ) {
a1a3bc73   Luigi Serra   graphs updates
95
  								name = jQuery.camelCase( name.slice( 5 ) );
74249687   Luigi Serra   Cross browser con...
96
97
98
99
  								dataAttr( elem, name, data[ name ] );
  							}
  						}
  					}
a1a3bc73   Luigi Serra   graphs updates
100
  					dataPriv.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" ) {
a1a3bc73   Luigi Serra   graphs updates
109
110
111
  			return this.each( function() {
  				dataUser.set( this, key );
  			} );
74249687   Luigi Serra   Cross browser con...
112
113
114
  		}
  
  		return access( this, function( value ) {
a1a3bc73   Luigi Serra   graphs updates
115
  			var data, camelKey;
74249687   Luigi Serra   Cross browser con...
116
117
118
119
120
121
122
  
  			// 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 ) {
a1a3bc73   Luigi Serra   graphs updates
123
  
74249687   Luigi Serra   Cross browser con...
124
125
  				// Attempt to get data from the cache
  				// with the key as-is
a1a3bc73   Luigi Serra   graphs updates
126
127
128
129
130
131
  				data = dataUser.get( elem, key ) ||
  
  					// Try to find dashed key if it exists (gh-2779)
  					// This is for 2.2.x only
  					dataUser.get( elem, key.replace( rmultiDash, "-$&" ).toLowerCase() );
  
74249687   Luigi Serra   Cross browser con...
132
133
134
135
  				if ( data !== undefined ) {
  					return data;
  				}
  
a1a3bc73   Luigi Serra   graphs updates
136
137
  				camelKey = jQuery.camelCase( key );
  
74249687   Luigi Serra   Cross browser con...
138
139
  				// Attempt to get data from the cache
  				// with the key camelized
a1a3bc73   Luigi Serra   graphs updates
140
  				data = dataUser.get( elem, camelKey );
74249687   Luigi Serra   Cross browser con...
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  				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...
a1a3bc73   Luigi Serra   graphs updates
157
158
159
  			camelKey = jQuery.camelCase( key );
  			this.each( function() {
  
74249687   Luigi Serra   Cross browser con...
160
161
  				// First, attempt to store a copy or reference of any
  				// data that might've been store with a camelCased key.
a1a3bc73   Luigi Serra   graphs updates
162
  				var data = dataUser.get( this, camelKey );
74249687   Luigi Serra   Cross browser con...
163
164
165
166
  
  				// For HTML5 data-* attribute interop, we have to
  				// store property names with dashes in a camelCase form.
  				// This might not apply to all properties...*
a1a3bc73   Luigi Serra   graphs updates
167
  				dataUser.set( this, camelKey, value );
74249687   Luigi Serra   Cross browser con...
168
169
170
171
  
  				// *... In the case of properties that might _actually_
  				// have dashes, we need to also store a copy of that
  				// unchanged property.
a1a3bc73   Luigi Serra   graphs updates
172
173
  				if ( key.indexOf( "-" ) > -1 && data !== undefined ) {
  					dataUser.set( this, key, value );
74249687   Luigi Serra   Cross browser con...
174
  				}
a1a3bc73   Luigi Serra   graphs updates
175
  			} );
74249687   Luigi Serra   Cross browser con...
176
177
178
179
  		}, null, value, arguments.length > 1, null, true );
  	},
  
  	removeData: function( key ) {
a1a3bc73   Luigi Serra   graphs updates
180
181
182
  		return this.each( function() {
  			dataUser.remove( this, key );
  		} );
74249687   Luigi Serra   Cross browser con...
183
  	}
a1a3bc73   Luigi Serra   graphs updates
184
  } );
74249687   Luigi Serra   Cross browser con...
185
186
  
  return jQuery;
a1a3bc73   Luigi Serra   graphs updates
187
  } );