Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / dd / Registry.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.dd.Registry
17  * Provides easy access to all drag drop components that are registered on a page.  Items can be retrieved either
18  * directly by DOM node id, or by passing in the drag drop event that occurred and looking up the event target.
19  * @singleton
20  */
21 Ext.define('Ext.dd.Registry', {
22     singleton: true,
23     constructor: function() {
24         this.elements = {}; 
25         this.handles = {}; 
26         this.autoIdSeed = 0;
27     },
28     
29     getId: function(el, autogen){
30         if(typeof el == "string"){
31             return el;
32         }
33         var id = el.id;
34         if(!id && autogen !== false){
35             id = "extdd-" + (++this.autoIdSeed);
36             el.id = id;
37         }
38         return id;
39     },
40     
41     /**
42      * Resgister a drag drop element
43      * @param {String/HTMLElement} element The id or DOM node to register
44      * @param {Object} data (optional) An custom data object that will be passed between the elements that are involved
45      * in drag drop operations.  You can populate this object with any arbitrary properties that your own code
46      * knows how to interpret, plus there are some specific properties known to the Registry that should be
47      * populated in the data object (if applicable):
48      * <pre>
49 Value      Description<br />
50 ---------  ------------------------------------------<br />
51 handles    Array of DOM nodes that trigger dragging<br />
52            for the element being registered<br />
53 isHandle   True if the element passed in triggers<br />
54            dragging itself, else false
55 </pre>
56      */
57     register : function(el, data){
58         data = data || {};
59         if (typeof el == "string") {
60             el = document.getElementById(el);
61         }
62         data.ddel = el;
63         this.elements[this.getId(el)] = data;
64         if (data.isHandle !== false) {
65             this.handles[data.ddel.id] = data;
66         }
67         if (data.handles) {
68             var hs = data.handles;
69             for (var i = 0, len = hs.length; i < len; i++) {
70                 this.handles[this.getId(hs[i])] = data;
71             }
72         }
73     },
74
75     /**
76      * Unregister a drag drop element
77      * @param {String/HTMLElement} element The id or DOM node to unregister
78      */
79     unregister : function(el){
80         var id = this.getId(el, false);
81         var data = this.elements[id];
82         if(data){
83             delete this.elements[id];
84             if(data.handles){
85                 var hs = data.handles;
86                 for (var i = 0, len = hs.length; i < len; i++) {
87                     delete this.handles[this.getId(hs[i], false)];
88                 }
89             }
90         }
91     },
92
93     /**
94      * Returns the handle registered for a DOM Node by id
95      * @param {String/HTMLElement} id The DOM node or id to look up
96      * @return {Object} handle The custom handle data
97      */
98     getHandle : function(id){
99         if(typeof id != "string"){ // must be element?
100             id = id.id;
101         }
102         return this.handles[id];
103     },
104
105     /**
106      * Returns the handle that is registered for the DOM node that is the target of the event
107      * @param {Event} e The event
108      * @return {Object} handle The custom handle data
109      */
110     getHandleFromEvent : function(e){
111         var t = e.getTarget();
112         return t ? this.handles[t.id] : null;
113     },
114
115     /**
116      * Returns a custom data object that is registered for a DOM node by id
117      * @param {String/HTMLElement} id The DOM node or id to look up
118      * @return {Object} data The custom data
119      */
120     getTarget : function(id){
121         if(typeof id != "string"){ // must be element?
122             id = id.id;
123         }
124         return this.elements[id];
125     },
126
127     /**
128      * Returns a custom data object that is registered for the DOM node that is the target of the event
129      * @param {Event} e The event
130      * @return {Object} data The custom data
131      */
132     getTargetFromEvent : function(e){
133         var t = e.getTarget();
134         return t ? this.elements[t.id] || this.handles[t.id] : null;
135     }
136 });