Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / dd / Registry.js
1 /**
2  * @class Ext.dd.Registry
3  * Provides easy access to all drag drop components that are registered on a page.  Items can be retrieved either
4  * directly by DOM node id, or by passing in the drag drop event that occurred and looking up the event target.
5  * @singleton
6  */
7 Ext.define('Ext.dd.Registry', {
8     singleton: true,
9     constructor: function() {
10         this.elements = {}; 
11         this.handles = {}; 
12         this.autoIdSeed = 0;
13     },
14     
15     getId: function(el, autogen){
16         if(typeof el == "string"){
17             return el;
18         }
19         var id = el.id;
20         if(!id && autogen !== false){
21             id = "extdd-" + (++this.autoIdSeed);
22             el.id = id;
23         }
24         return id;
25     },
26     
27     /**
28      * Resgister a drag drop element
29      * @param {String/HTMLElement} element The id or DOM node to register
30      * @param {Object} data (optional) An custom data object that will be passed between the elements that are involved
31      * in drag drop operations.  You can populate this object with any arbitrary properties that your own code
32      * knows how to interpret, plus there are some specific properties known to the Registry that should be
33      * populated in the data object (if applicable):
34      * <pre>
35 Value      Description<br />
36 ---------  ------------------------------------------<br />
37 handles    Array of DOM nodes that trigger dragging<br />
38            for the element being registered<br />
39 isHandle   True if the element passed in triggers<br />
40            dragging itself, else false
41 </pre>
42      */
43     register : function(el, data){
44         data = data || {};
45         if (typeof el == "string") {
46             el = document.getElementById(el);
47         }
48         data.ddel = el;
49         this.elements[this.getId(el)] = data;
50         if (data.isHandle !== false) {
51             this.handles[data.ddel.id] = data;
52         }
53         if (data.handles) {
54             var hs = data.handles;
55             for (var i = 0, len = hs.length; i < len; i++) {
56                 this.handles[this.getId(hs[i])] = data;
57             }
58         }
59     },
60
61     /**
62      * Unregister a drag drop element
63      * @param {String/HTMLElement} element The id or DOM node to unregister
64      */
65     unregister : function(el){
66         var id = this.getId(el, false);
67         var data = this.elements[id];
68         if(data){
69             delete this.elements[id];
70             if(data.handles){
71                 var hs = data.handles;
72                 for (var i = 0, len = hs.length; i < len; i++) {
73                     delete this.handles[this.getId(hs[i], false)];
74                 }
75             }
76         }
77     },
78
79     /**
80      * Returns the handle registered for a DOM Node by id
81      * @param {String/HTMLElement} id The DOM node or id to look up
82      * @return {Object} handle The custom handle data
83      */
84     getHandle : function(id){
85         if(typeof id != "string"){ // must be element?
86             id = id.id;
87         }
88         return this.handles[id];
89     },
90
91     /**
92      * Returns the handle that is registered for the DOM node that is the target of the event
93      * @param {Event} e The event
94      * @return {Object} handle The custom handle data
95      */
96     getHandleFromEvent : function(e){
97         var t = e.getTarget();
98         return t ? this.handles[t.id] : null;
99     },
100
101     /**
102      * Returns a custom data object that is registered for a DOM node by id
103      * @param {String/HTMLElement} id The DOM node or id to look up
104      * @return {Object} data The custom data
105      */
106     getTarget : function(id){
107         if(typeof id != "string"){ // must be element?
108             id = id.id;
109         }
110         return this.elements[id];
111     },
112
113     /**
114      * Returns a custom data object that is registered for the DOM node that is the target of the event
115      * @param {Event} e The event
116      * @return {Object} data The custom data
117      */
118     getTargetFromEvent : function(e){
119         var t = e.getTarget();
120         return t ? this.elements[t.id] || this.handles[t.id] : null;
121     }
122 });