corrade-nucleus-nucleons – Blame information for rev 20
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
20 | office | 1 | define([ |
2 | "./core", |
||
3 | "./var/rnotwhite", |
||
4 | "./core/access", |
||
5 | "./data/var/data_priv", |
||
6 | "./data/var/data_user" |
||
7 | ], function( jQuery, rnotwhite, access, data_priv, data_user ) { |
||
8 | |||
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 dataAttr( elem, key, data ) { |
||
24 | var name; |
||
25 | |||
26 | // If nothing was found internally, try to fetch any |
||
27 | // data from the HTML5 data-* attribute |
||
28 | if ( data === undefined && elem.nodeType === 1 ) { |
||
29 | name = "data-" + key.replace( rmultiDash, "-$1" ).toLowerCase(); |
||
30 | data = elem.getAttribute( name ); |
||
31 | |||
32 | if ( typeof data === "string" ) { |
||
33 | try { |
||
34 | data = data === "true" ? true : |
||
35 | data === "false" ? false : |
||
36 | data === "null" ? null : |
||
37 | // Only convert to a number if it doesn't change the string |
||
38 | +data + "" === data ? +data : |
||
39 | rbrace.test( data ) ? jQuery.parseJSON( data ) : |
||
40 | data; |
||
41 | } catch( e ) {} |
||
42 | |||
43 | // Make sure we set the data so it isn't changed later |
||
44 | data_user.set( elem, key, data ); |
||
45 | } else { |
||
46 | data = undefined; |
||
47 | } |
||
48 | } |
||
49 | return data; |
||
50 | } |
||
51 | |||
52 | jQuery.extend({ |
||
53 | hasData: function( elem ) { |
||
54 | return data_user.hasData( elem ) || data_priv.hasData( elem ); |
||
55 | }, |
||
56 | |||
57 | data: function( elem, name, data ) { |
||
58 | return data_user.access( elem, name, data ); |
||
59 | }, |
||
60 | |||
61 | removeData: function( elem, name ) { |
||
62 | data_user.remove( elem, name ); |
||
63 | }, |
||
64 | |||
65 | // TODO: Now that all calls to _data and _removeData have been replaced |
||
66 | // with direct calls to data_priv methods, these can be deprecated. |
||
67 | _data: function( elem, name, data ) { |
||
68 | return data_priv.access( elem, name, data ); |
||
69 | }, |
||
70 | |||
71 | _removeData: function( elem, name ) { |
||
72 | data_priv.remove( elem, name ); |
||
73 | } |
||
74 | }); |
||
75 | |||
76 | jQuery.fn.extend({ |
||
77 | data: function( key, value ) { |
||
78 | var i, name, data, |
||
79 | elem = this[ 0 ], |
||
80 | attrs = elem && elem.attributes; |
||
81 | |||
82 | // Gets all values |
||
83 | if ( key === undefined ) { |
||
84 | if ( this.length ) { |
||
85 | data = data_user.get( elem ); |
||
86 | |||
87 | if ( elem.nodeType === 1 && !data_priv.get( elem, "hasDataAttrs" ) ) { |
||
88 | i = attrs.length; |
||
89 | while ( i-- ) { |
||
90 | name = attrs[ i ].name; |
||
91 | |||
92 | if ( name.indexOf( "data-" ) === 0 ) { |
||
93 | name = jQuery.camelCase( name.slice(5) ); |
||
94 | dataAttr( elem, name, data[ name ] ); |
||
95 | } |
||
96 | } |
||
97 | data_priv.set( elem, "hasDataAttrs", true ); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | return data; |
||
102 | } |
||
103 | |||
104 | // Sets multiple values |
||
105 | if ( typeof key === "object" ) { |
||
106 | return this.each(function() { |
||
107 | data_user.set( this, key ); |
||
108 | }); |
||
109 | } |
||
110 | |||
111 | return access( this, function( value ) { |
||
112 | var data, |
||
113 | camelKey = jQuery.camelCase( key ); |
||
114 | |||
115 | // The calling jQuery object (element matches) is not empty |
||
116 | // (and therefore has an element appears at this[ 0 ]) and the |
||
117 | // `value` parameter was not undefined. An empty jQuery object |
||
118 | // will result in `undefined` for elem = this[ 0 ] which will |
||
119 | // throw an exception if an attempt to read a data cache is made. |
||
120 | if ( elem && value === undefined ) { |
||
121 | // Attempt to get data from the cache |
||
122 | // with the key as-is |
||
123 | data = data_user.get( elem, key ); |
||
124 | if ( data !== undefined ) { |
||
125 | return data; |
||
126 | } |
||
127 | |||
128 | // Attempt to get data from the cache |
||
129 | // with the key camelized |
||
130 | data = data_user.get( elem, camelKey ); |
||
131 | if ( data !== undefined ) { |
||
132 | return data; |
||
133 | } |
||
134 | |||
135 | // Attempt to "discover" the data in |
||
136 | // HTML5 custom data-* attrs |
||
137 | data = dataAttr( elem, camelKey, undefined ); |
||
138 | if ( data !== undefined ) { |
||
139 | return data; |
||
140 | } |
||
141 | |||
142 | // We tried really hard, but the data doesn't exist. |
||
143 | return; |
||
144 | } |
||
145 | |||
146 | // Set the data... |
||
147 | this.each(function() { |
||
148 | // First, attempt to store a copy or reference of any |
||
149 | // data that might've been store with a camelCased key. |
||
150 | var data = data_user.get( this, camelKey ); |
||
151 | |||
152 | // For HTML5 data-* attribute interop, we have to |
||
153 | // store property names with dashes in a camelCase form. |
||
154 | // This might not apply to all properties...* |
||
155 | data_user.set( this, camelKey, value ); |
||
156 | |||
157 | // *... In the case of properties that might _actually_ |
||
158 | // have dashes, we need to also store a copy of that |
||
159 | // unchanged property. |
||
160 | if ( key.indexOf("-") !== -1 && data !== undefined ) { |
||
161 | data_user.set( this, key, value ); |
||
162 | } |
||
163 | }); |
||
164 | }, null, value, arguments.length > 1, null, true ); |
||
165 | }, |
||
166 | |||
167 | removeData: function( key ) { |
||
168 | return this.each(function() { |
||
169 | data_user.remove( this, key ); |
||
170 | }); |
||
171 | } |
||
172 | }); |
||
173 | |||
174 | return jQuery; |
||
175 | }); |