index now works in reverse when passed a selector or undefined. fixes #3971
[jquery.git] / src / data.js
1 var expando = "jQuery" + now(), uuid = 0, windowData = {};\r
2 \r
3 jQuery.extend({\r
4         cache: {},\r
5 \r
6         data: function( elem, name, data ) {\r
7                 elem = elem == window ?\r
8                         windowData :\r
9                         elem;\r
10 \r
11                 var id = elem[ expando ];\r
12 \r
13                 // Compute a unique ID for the element\r
14                 if ( !id )\r
15                         id = elem[ expando ] = ++uuid;\r
16 \r
17                 // Only generate the data cache if we're\r
18                 // trying to access or manipulate it\r
19                 if ( name && !jQuery.cache[ id ] )\r
20                         jQuery.cache[ id ] = {};\r
21 \r
22                 // Prevent overriding the named cache with undefined values\r
23                 if ( data !== undefined )\r
24                         jQuery.cache[ id ][ name ] = data;\r
25 \r
26                 // Return the named cache data, or the ID for the element\r
27                 return name ?\r
28                         jQuery.cache[ id ][ name ] :\r
29                         id;\r
30         },\r
31 \r
32         removeData: function( elem, name ) {\r
33                 elem = elem == window ?\r
34                         windowData :\r
35                         elem;\r
36 \r
37                 var id = elem[ expando ];\r
38 \r
39                 // If we want to remove a specific section of the element's data\r
40                 if ( name ) {\r
41                         if ( jQuery.cache[ id ] ) {\r
42                                 // Remove the section of cache data\r
43                                 delete jQuery.cache[ id ][ name ];\r
44 \r
45                                 // If we've removed all the data, remove the element's cache\r
46                                 name = "";\r
47 \r
48                                 for ( name in jQuery.cache[ id ] )\r
49                                         break;\r
50 \r
51                                 if ( !name )\r
52                                         jQuery.removeData( elem );\r
53                         }\r
54 \r
55                 // Otherwise, we want to remove all of the element's data\r
56                 } else {\r
57                         // Clean up the element expando\r
58                         try {\r
59                                 delete elem[ expando ];\r
60                         } catch(e){\r
61                                 // IE has trouble directly removing the expando\r
62                                 // but it's ok with using removeAttribute\r
63                                 if ( elem.removeAttribute )\r
64                                         elem.removeAttribute( expando );\r
65                         }\r
66 \r
67                         // Completely remove the data cache\r
68                         delete jQuery.cache[ id ];\r
69                 }\r
70         },\r
71         queue: function( elem, type, data ) {\r
72                 if ( elem ){\r
73 \r
74                         type = (type || "fx") + "queue";\r
75 \r
76                         var q = jQuery.data( elem, type );\r
77 \r
78                         if ( !q || jQuery.isArray(data) )\r
79                                 q = jQuery.data( elem, type, jQuery.makeArray(data) );\r
80                         else if( data )\r
81                                 q.push( data );\r
82 \r
83                 }\r
84                 return q;\r
85         },\r
86 \r
87         dequeue: function( elem, type ){\r
88                 var queue = jQuery.queue( elem, type ),\r
89                         fn = queue.shift();\r
90 \r
91                 if( !type || type === "fx" )\r
92                         fn = queue[0];\r
93 \r
94                 if( fn !== undefined )\r
95                         fn.call(elem);\r
96         }\r
97 });\r
98 \r
99 jQuery.fn.extend({\r
100         data: function( key, value ){\r
101                 var parts = key.split(".");\r
102                 parts[1] = parts[1] ? "." + parts[1] : "";\r
103 \r
104                 if ( value === undefined ) {\r
105                         var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);\r
106 \r
107                         if ( data === undefined && this.length )\r
108                                 data = jQuery.data( this[0], key );\r
109 \r
110                         return data === undefined && parts[1] ?\r
111                                 this.data( parts[0] ) :\r
112                                 data;\r
113                 } else\r
114                         return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function(){\r
115                                 jQuery.data( this, key, value );\r
116                         });\r
117         },\r
118 \r
119         removeData: function( key ){\r
120                 return this.each(function(){\r
121                         jQuery.removeData( this, key );\r
122                 });\r
123         },\r
124         queue: function(type, data){\r
125                 if ( typeof type !== "string" ) {\r
126                         data = type;\r
127                         type = "fx";\r
128                 }\r
129 \r
130                 if ( data === undefined )\r
131                         return jQuery.queue( this[0], type );\r
132 \r
133                 return this.each(function(){\r
134                         var queue = jQuery.queue( this, type, data );\r
135 \r
136                          if( type == "fx" && queue.length == 1 )\r
137                                 queue[0].call(this);\r
138                 });\r
139         },\r
140         dequeue: function(type){\r
141                 return this.each(function(){\r
142                         jQuery.dequeue( this, type );\r
143                 });\r
144         }\r
145 });