Ticket #6808. Changes data() so on plain objects, it uses a function to contain the...
[jquery.git] / src / data.js
1 var windowData = {};
2
3 jQuery.extend({
4         cache: {},
5
6         // Please use with caution
7         uuid: 0,
8
9         // Unique for each copy of jQuery on the page   
10         expando: "jQuery" + jQuery.now(),
11
12         // The following elements throw uncatchable exceptions if you
13         // attempt to add expando properties to them.
14         noData: {
15                 "embed": true,
16                 "object": true,
17                 "applet": true
18         },
19
20         data: function( elem, name, data ) {
21                 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
22                         return;
23                 }
24
25                 elem = elem == window ?
26                         windowData :
27                         elem;
28
29                 var id = elem[ jQuery.expando ], cache = jQuery.cache, thisCache,
30                         isNode = elem.nodeType;
31
32                 if ( !id && typeof name === "string" && data === undefined ) {
33                         return;
34                 }
35
36                 // Get the data from the object directly
37                 if ( !isNode ) {
38                         cache = elem;
39                         id = jQuery.expando;
40
41                 // Compute a unique ID for the element
42                 } else if ( !id ) {
43                         elem[ jQuery.expando ] = id = ++jQuery.uuid;
44                 }
45
46                 // Avoid generating a new cache unless none exists and we
47                 // want to manipulate it.
48                 if ( typeof name === "object" ) {
49                         if ( isNode ) {
50                                 cache[ id ] = jQuery.extend(true, {}, name);
51                         } else {
52                                 cache[ id ] = function() {
53                                         return jQuery.extend(true, {}, name);
54                                 }
55                         }
56
57                 } else if ( !cache[ id ] ) {
58                         if ( isNode ) {
59                                 cache[ id ] = {};
60                         } else {
61                                 var store = {};
62                                 cache[ id ] = function() {
63                                         return store;
64                                 }
65                         }
66                         
67                 }
68
69                 thisCache = cache[ id ];
70                 if ( !isNode ) {
71                         thisCache = thisCache();
72                 }
73
74                 // Prevent overriding the named cache with undefined values
75                 if ( data !== undefined ) {
76                         thisCache[ name ] = data;
77                 }
78
79                 return typeof name === "string" ? thisCache[ name ] : thisCache;
80         },
81
82         removeData: function( elem, name ) {
83                 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
84                         return;
85                 }
86
87                 elem = elem == window ?
88                         windowData :
89                         elem;
90
91                 var isNode = elem.nodeType,
92                         id = elem[ jQuery.expando ], cache = jQuery.cache;
93                 if ( id && !isNode ) {
94                         id = id();
95                 }
96                 var thisCache = cache[ id ];
97
98                 // If we want to remove a specific section of the element's data
99                 if ( name ) {
100                         if ( thisCache ) {
101                                 // Remove the section of cache data
102                                 delete thisCache[ name ];
103
104                                 // If we've removed all the data, remove the element's cache
105                                 if ( jQuery.isEmptyObject(thisCache) ) {
106                                         jQuery.removeData( elem );
107                                 }
108                         }
109
110                 // Otherwise, we want to remove all of the element's data
111                 } else {
112                         if ( jQuery.support.deleteExpando || !isNode ) {
113                                 delete elem[ jQuery.expando ];
114
115                         } else if ( elem.removeAttribute ) {
116                                 elem.removeAttribute( jQuery.expando );
117                         }
118
119                         // Completely remove the data cache
120                         if ( isNode ) {
121                                 delete cache[ id ];
122                         }
123                 }
124         }
125 });
126
127 jQuery.fn.extend({
128         data: function( key, value ) {
129                 if ( typeof key === "undefined" && this.length ) {
130                         return jQuery.data( this[0] );
131
132                 } else if ( typeof key === "object" ) {
133                         return this.each(function() {
134                                 jQuery.data( this, key );
135                         });
136                 }
137
138                 var parts = key.split(".");
139                 parts[1] = parts[1] ? "." + parts[1] : "";
140
141                 if ( value === undefined ) {
142                         var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
143
144                         if ( data === undefined && this.length ) {
145                                 data = jQuery.data( this[0], key );
146                         }
147                         return data === undefined && parts[1] ?
148                                 this.data( parts[0] ) :
149                                 data;
150                 } else {
151                         return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function() {
152                                 jQuery.data( this, key, value );
153                                 jQuery.event.trigger( "changeData" + parts[1] + "!", [parts[0], value], this );
154                         });
155                 }
156         },
157
158         removeData: function( key ) {
159                 return this.each(function() {
160                         jQuery.removeData( this, key );
161                 });
162         }
163 });