added curly braces around all if/else statements
[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         queue: function( elem, type, data ) {
83                 if ( !elem ) { return; }
84
85                 type = (type || "fx") + "queue";
86                 var q = jQuery.data( elem, type );
87
88                 // Speed up dequeue by getting out quickly if this is just a lookup
89                 if ( !data ) { return q || []; }
90
91                 if ( !q || jQuery.isArray(data) ) {
92                         q = jQuery.data( elem, type, jQuery.makeArray(data) );
93                 } else {
94                         q.push( data );
95                 }
96                 return q;
97         },
98
99         dequeue: function( elem, type ){
100                 type = type || "fx";
101
102                 var queue = jQuery.queue( elem, type ), fn = queue.shift();
103
104                 // If the fx queue is dequeued, always remove the progress sentinel
105                 if ( fn === "inprogress" ) { fn = queue.shift(); }
106
107                 if ( fn ) {
108                         // Add a progress sentinel to prevent the fx queue from being
109                         // automatically dequeued
110                         if ( type == "fx" ) { queue.unshift("inprogress"); }
111
112                         fn.call(elem, function() { jQuery.dequeue(elem, type); });
113                 }
114         }
115 });
116
117 jQuery.fn.extend({
118         data: function( key, value ){
119                 if ( typeof key === "undefined" && this.length ) {
120                         return jQuery.data( this[0] );
121                 }
122
123                 var parts = key.split(".");
124                 parts[1] = parts[1] ? "." + parts[1] : "";
125
126                 if ( value === undefined ) {
127                         var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
128
129                         if ( data === undefined && this.length ) {
130                                 data = jQuery.data( this[0], key );
131                         }
132                         return data === undefined && parts[1] ?
133                                 this.data( parts[0] ) :
134                                 data;
135                 } else {
136                         return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function(){
137                                 jQuery.data( this, key, value );
138                         });
139                 }
140         },
141
142         removeData: function( key ){
143                 return this.each(function(){
144                         jQuery.removeData( this, key );
145                 });
146         },
147         queue: function(type, data){
148                 if ( typeof type !== "string" ) {
149                         data = type;
150                         type = "fx";
151                 }
152
153                 if ( data === undefined ) {
154                         return jQuery.queue( this[0], type );
155                 }
156                 return this.each(function(i, elem){
157                         var queue = jQuery.queue( this, type, data );
158
159                         if ( type == "fx" && queue[0] !== "inprogress" ) {
160                                 jQuery.dequeue( this, type );
161                         }
162                 });
163         },
164         dequeue: function(type){
165                 return this.each(function(){
166                         jQuery.dequeue( this, type )
167                 });
168         },
169         clearQueue: function(type){
170                 return this.queue( type || "fx", [] );
171         }
172 });