Ticket 6808. Updated per code review comments by jeresig
[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 = isNode? cache[ id ] : cache[ id ]();
70
71                 // Prevent overriding the named cache with undefined values
72                 if ( data !== undefined ) {
73                         thisCache[ name ] = data;
74                 }
75
76                 return typeof name === "string" ? thisCache[ name ] : thisCache;
77         },
78
79         removeData: function( elem, name ) {
80                 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
81                         return;
82                 }
83
84                 elem = elem == window ?
85                         windowData :
86                         elem;
87
88                 var isNode = elem.nodeType,
89                         id = elem[ jQuery.expando ], cache = jQuery.cache;
90                 if ( id && !isNode ) {
91                         id = id();
92                 }
93                 var thisCache = cache[ id ];
94
95                 // If we want to remove a specific section of the element's data
96                 if ( name ) {
97                         if ( thisCache ) {
98                                 // Remove the section of cache data
99                                 delete thisCache[ name ];
100
101                                 // If we've removed all the data, remove the element's cache
102                                 if ( jQuery.isEmptyObject(thisCache) ) {
103                                         jQuery.removeData( elem );
104                                 }
105                         }
106
107                 // Otherwise, we want to remove all of the element's data
108                 } else {
109                         if ( jQuery.support.deleteExpando || !isNode ) {
110                                 delete elem[ jQuery.expando ];
111
112                         } else if ( elem.removeAttribute ) {
113                                 elem.removeAttribute( jQuery.expando );
114                         }
115
116                         // Completely remove the data cache
117                         if ( isNode ) {
118                                 delete cache[ id ];
119                         }
120                 }
121         }
122 });
123
124 jQuery.fn.extend({
125         data: function( key, value ) {
126                 if ( typeof key === "undefined" && this.length ) {
127                         return jQuery.data( this[0] );
128
129                 } else if ( typeof key === "object" ) {
130                         return this.each(function() {
131                                 jQuery.data( this, key );
132                         });
133                 }
134
135                 var parts = key.split(".");
136                 parts[1] = parts[1] ? "." + parts[1] : "";
137
138                 if ( value === undefined ) {
139                         var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
140
141                         if ( data === undefined && this.length ) {
142                                 data = jQuery.data( this[0], key );
143                         }
144                         return data === undefined && parts[1] ?
145                                 this.data( parts[0] ) :
146                                 data;
147                 } else {
148                         return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function() {
149                                 jQuery.data( this, key, value );
150                                 jQuery.event.trigger( "changeData" + parts[1] + "!", [parts[0], value], this );
151                         });
152                 }
153         },
154
155         removeData: function( key ) {
156                 return this.each(function() {
157                         jQuery.removeData( this, key );
158                 });
159         }
160 });