1 | define([
|
2 | "../core",
|
3 | "../var/rnotwhite",
|
4 | "./accepts"
|
5 | ], function( jQuery, rnotwhite ) {
|
6 |
|
7 | function Data() {
|
8 |
|
9 |
|
10 |
|
11 | Object.defineProperty( this.cache = {}, 0, {
|
12 | get: function() {
|
13 | return {};
|
14 | }
|
15 | });
|
16 |
|
17 | this.expando = jQuery.expando + Math.random();
|
18 | }
|
19 |
|
20 | Data.uid = 1;
|
21 | Data.accepts = jQuery.acceptData;
|
22 |
|
23 | Data.prototype = {
|
24 | key: function( owner ) {
|
25 |
|
26 |
|
27 |
|
28 | if ( !Data.accepts( owner ) ) {
|
29 | return 0;
|
30 | }
|
31 |
|
32 | var descriptor = {},
|
33 |
|
34 | unlock = owner[ this.expando ];
|
35 |
|
36 |
|
37 | if ( !unlock ) {
|
38 | unlock = Data.uid++;
|
39 |
|
40 |
|
41 | try {
|
42 | descriptor[ this.expando ] = { value: unlock };
|
43 | Object.defineProperties( owner, descriptor );
|
44 |
|
45 |
|
46 |
|
47 | } catch ( e ) {
|
48 | descriptor[ this.expando ] = unlock;
|
49 | jQuery.extend( owner, descriptor );
|
50 | }
|
51 | }
|
52 |
|
53 |
|
54 | if ( !this.cache[ unlock ] ) {
|
55 | this.cache[ unlock ] = {};
|
56 | }
|
57 |
|
58 | return unlock;
|
59 | },
|
60 | set: function( owner, data, value ) {
|
61 | var prop,
|
62 |
|
63 |
|
64 |
|
65 | unlock = this.key( owner ),
|
66 | cache = this.cache[ unlock ];
|
67 |
|
68 |
|
69 | if ( typeof data === "string" ) {
|
70 | cache[ data ] = value;
|
71 |
|
72 |
|
73 | } else {
|
74 |
|
75 | if ( jQuery.isEmptyObject( cache ) ) {
|
76 | jQuery.extend( this.cache[ unlock ], data );
|
77 |
|
78 | } else {
|
79 | for ( prop in data ) {
|
80 | cache[ prop ] = data[ prop ];
|
81 | }
|
82 | }
|
83 | }
|
84 | return cache;
|
85 | },
|
86 | get: function( owner, key ) {
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | var cache = this.cache[ this.key( owner ) ];
|
92 |
|
93 | return key === undefined ?
|
94 | cache : cache[ key ];
|
95 | },
|
96 | access: function( owner, key, value ) {
|
97 | var stored;
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | if ( key === undefined ||
|
110 | ((key && typeof key === "string") && value === undefined) ) {
|
111 |
|
112 | stored = this.get( owner, key );
|
113 |
|
114 | return stored !== undefined ?
|
115 | stored : this.get( owner, jQuery.camelCase(key) );
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | this.set( owner, key, value );
|
125 |
|
126 |
|
127 |
|
128 | return value !== undefined ? value : key;
|
129 | },
|
130 | remove: function( owner, key ) {
|
131 | var i, name, camel,
|
132 | unlock = this.key( owner ),
|
133 | cache = this.cache[ unlock ];
|
134 |
|
135 | if ( key === undefined ) {
|
136 | this.cache[ unlock ] = {};
|
137 |
|
138 | } else {
|
139 |
|
140 | if ( jQuery.isArray( key ) ) {
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | name = key.concat( key.map( jQuery.camelCase ) );
|
148 | } else {
|
149 | camel = jQuery.camelCase( key );
|
150 |
|
151 | if ( key in cache ) {
|
152 | name = [ key, camel ];
|
153 | } else {
|
154 |
|
155 |
|
156 | name = camel;
|
157 | name = name in cache ?
|
158 | [ name ] : ( name.match( rnotwhite ) || [] );
|
159 | }
|
160 | }
|
161 |
|
162 | i = name.length;
|
163 | while ( i-- ) {
|
164 | delete cache[ name[ i ] ];
|
165 | }
|
166 | }
|
167 | },
|
168 | hasData: function( owner ) {
|
169 | return !jQuery.isEmptyObject(
|
170 | this.cache[ owner[ this.expando ] ] || {}
|
171 | );
|
172 | },
|
173 | discard: function( owner ) {
|
174 | if ( owner[ this.expando ] ) {
|
175 | delete this.cache[ owner[ this.expando ] ];
|
176 | }
|
177 | }
|
178 | };
|
179 |
|
180 | return Data;
|
181 | });
|