Split the queue code out from data.js into a dedicated queue.js file (also split...
[jquery.git] / src / data.js
1 var expando = "jQuery" + now(), uuid = 0, windowData = {};
2 var emptyObject = {};
3
4 jQuery.extend({
5         cache: {},
6         
7         expando:expando,
8
9         data: function( elem, name, data ) {
10                 elem = elem == window ?
11                         windowData :
12                         elem;
13
14                 var id = elem[ expando ], cache = jQuery.cache, thisCache;
15
16                 // Handle the case where there's no name immediately
17                 if ( !name && !id ) {
18                         return null;
19                 }
20
21                 // Compute a unique ID for the element
22                 if ( !id ) { 
23                         id = ++uuid;
24                 }
25
26                 // Avoid generating a new cache unless none exists and we
27                 // want to manipulate it.
28                 if ( cache[ id ] ) {
29                         thisCache = cache[ id ];
30                 } else if ( typeof data === "undefined" ) {
31                         thisCache = emptyObject;
32                 } else {
33                         thisCache = cache[ id ] = {};
34                 }
35                 
36                 // Prevent overriding the named cache with undefined values
37                 if ( data !== undefined ) {
38                         elem[ expando ] = id;
39                         thisCache[ name ] = data;
40                 }
41                 
42                 return name ? thisCache[ name ] : thisCache;
43         },
44
45         removeData: function( elem, name ) {
46                 elem = elem == window ?
47                         windowData :
48                         elem;
49
50                 var id = elem[ expando ], cache = jQuery.cache, thisCache = cache[ id ];
51
52                 // If we want to remove a specific section of the element's data
53                 if ( name ) {
54                         if ( thisCache ) {
55                                 // Remove the section of cache data
56                                 delete thisCache[ name ];
57
58                                 // If we've removed all the data, remove the element's cache
59                                 if ( jQuery.isEmptyObject(thisCache) ) {
60                                         jQuery.removeData( elem );
61                                 }
62                         }
63
64                 // Otherwise, we want to remove all of the element's data
65                 } else {
66                         // Clean up the element expando
67                         try {
68                                 delete elem[ expando ];
69                         } catch( e ) {
70                                 // IE has trouble directly removing the expando
71                                 // but it's ok with using removeAttribute
72                                 if ( elem.removeAttribute ) {
73                                         elem.removeAttribute( expando );
74                                 }
75                         }
76
77                         // Completely remove the data cache
78                         delete cache[ id ];
79                 }
80         }
81 });
82
83 jQuery.fn.extend({
84         data: function( key, value ){
85                 if ( typeof key === "undefined" && this.length ) {
86                         return jQuery.data( this[0] );
87                 }
88
89                 var parts = key.split(".");
90                 parts[1] = parts[1] ? "." + parts[1] : "";
91
92                 if ( value === undefined ) {
93                         var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
94
95                         if ( data === undefined && this.length ) {
96                                 data = jQuery.data( this[0], key );
97                         }
98                         return data === undefined && parts[1] ?
99                                 this.data( parts[0] ) :
100                                 data;
101                 } else {
102                         return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function(){
103                                 jQuery.data( this, key, value );
104                         });
105                 }
106         },
107
108         removeData: function( key ){
109                 return this.each(function(){
110                         jQuery.removeData( this, key );
111                 });
112         }
113 });