Make sure that live events bubble unless explicitly told not to, like a normal event...
[jquery.git] / src / data.js
1 var expando = "jQuery" + now(), uuid = 0, windowData = {};
2
3 jQuery.extend({
4         cache: {},
5         
6         expando:expando,
7
8         // The following elements throw uncatchable exceptions if you
9         // attempt to add expando properties to them.
10         noData: {
11                 "embed": true,
12                 "object": true,
13                 "applet": true
14         },
15
16         data: function( elem, name, data ) {
17                 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
18                         return;
19                 }
20
21                 elem = elem == window ?
22                         windowData :
23                         elem;
24
25                 var id = elem[ expando ], cache = jQuery.cache, thisCache;
26
27                 if ( !id && typeof name === "string" && data === undefined ) {
28                         return null;
29                 }
30
31                 // Compute a unique ID for the element
32                 if ( !id ) { 
33                         id = ++uuid;
34                 }
35
36                 // Avoid generating a new cache unless none exists and we
37                 // want to manipulate it.
38                 if ( typeof name === "object" ) {
39                         elem[ expando ] = id;
40                         thisCache = cache[ id ] = jQuery.extend(true, {}, name);
41
42                 } else if ( !cache[ id ] ) {
43                         elem[ expando ] = id;
44                         cache[ id ] = {};
45                 }
46
47                 thisCache = cache[ id ];
48
49                 // Prevent overriding the named cache with undefined values
50                 if ( data !== undefined ) {
51                         thisCache[ name ] = data;
52                 }
53
54                 return typeof name === "string" ? thisCache[ name ] : thisCache;
55         },
56
57         removeData: function( elem, name ) {
58                 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
59                         return;
60                 }
61
62                 elem = elem == window ?
63                         windowData :
64                         elem;
65
66                 var id = elem[ expando ], cache = jQuery.cache, thisCache = cache[ id ];
67
68                 // If we want to remove a specific section of the element's data
69                 if ( name ) {
70                         if ( thisCache ) {
71                                 // Remove the section of cache data
72                                 delete thisCache[ name ];
73
74                                 // If we've removed all the data, remove the element's cache
75                                 if ( jQuery.isEmptyObject(thisCache) ) {
76                                         jQuery.removeData( elem );
77                                 }
78                         }
79
80                 // Otherwise, we want to remove all of the element's data
81                 } else {
82                         if ( jQuery.support.deleteExpando ) {
83                                 delete elem[ jQuery.expando ];
84
85                         } else if ( elem.removeAttribute ) {
86                                 elem.removeAttribute( jQuery.expando );
87                         }
88
89                         // Completely remove the data cache
90                         delete cache[ id ];
91                 }
92         }
93 });
94
95 jQuery.fn.extend({
96         data: function( key, value ) {
97                 if ( typeof key === "undefined" && this.length ) {
98                         return jQuery.data( this[0] );
99
100                 } else if ( typeof key === "object" ) {
101                         return this.each(function() {
102                                 jQuery.data( this, key );
103                         });
104                 }
105
106                 var parts = key.split(".");
107                 parts[1] = parts[1] ? "." + parts[1] : "";
108
109                 if ( value === undefined ) {
110                         var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
111
112                         if ( data === undefined && this.length ) {
113                                 data = jQuery.data( this[0], key );
114                         }
115                         return data === undefined && parts[1] ?
116                                 this.data( parts[0] ) :
117                                 data;
118                 } else {
119                         return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function() {
120                                 jQuery.data( this, key, value );
121                         });
122                 }
123         },
124
125         removeData: function( key ) {
126                 return this.each(function() {
127                         jQuery.removeData( this, key );
128                 });
129         }
130 });