scratch – Blame information for rev 58

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