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