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