Upgrade to ExtJS 3.1.1 - Released 02/08/2010
[extjs.git] / src / dd / DropZone.js
1 /*!
2  * Ext JS Library 3.1.1
3  * Copyright(c) 2006-2010 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**\r
8  * @class Ext.dd.DropZone\r
9  * @extends Ext.dd.DropTarget\r
10  * <p>This class provides a container DD instance that allows dropping on multiple child target nodes.</p>\r
11  * <p>By default, this class requires that child nodes accepting drop are registered with {@link Ext.dd.Registry}.\r
12  * However a simpler way to allow a DropZone to manage any number of target elements is to configure the\r
13  * DropZone with an implementation of {@link #getTargetFromEvent} which interrogates the passed\r
14  * mouse event to see if it has taken place within an element, or class of elements. This is easily done\r
15  * by using the event's {@link Ext.EventObject#getTarget getTarget} method to identify a node based on a\r
16  * {@link Ext.DomQuery} selector.</p>\r
17  * <p>Once the DropZone has detected through calling getTargetFromEvent, that the mouse is over\r
18  * a drop target, that target is passed as the first parameter to {@link #onNodeEnter}, {@link #onNodeOver},\r
19  * {@link #onNodeOut}, {@link #onNodeDrop}. You may configure the instance of DropZone with implementations\r
20  * of these methods to provide application-specific behaviour for these events to update both\r
21  * application state, and UI state.</p>\r
22  * <p>For example to make a GridPanel a cooperating target with the example illustrated in\r
23  * {@link Ext.dd.DragZone DragZone}, the following technique might be used:</p><pre><code>\r
24 myGridPanel.on('render', function() {\r
25     myGridPanel.dropZone = new Ext.dd.DropZone(myGridPanel.getView().scroller, {\r
26 \r
27 //      If the mouse is over a grid row, return that node. This is\r
28 //      provided as the "target" parameter in all "onNodeXXXX" node event handling functions\r
29         getTargetFromEvent: function(e) {\r
30             return e.getTarget(myGridPanel.getView().rowSelector);\r
31         },\r
32 \r
33 //      On entry into a target node, highlight that node.\r
34         onNodeEnter : function(target, dd, e, data){ \r
35             Ext.fly(target).addClass('my-row-highlight-class');\r
36         },\r
37 \r
38 //      On exit from a target node, unhighlight that node.\r
39         onNodeOut : function(target, dd, e, data){ \r
40             Ext.fly(target).removeClass('my-row-highlight-class');\r
41         },\r
42 \r
43 //      While over a target node, return the default drop allowed class which\r
44 //      places a "tick" icon into the drag proxy.\r
45         onNodeOver : function(target, dd, e, data){ \r
46             return Ext.dd.DropZone.prototype.dropAllowed;\r
47         },\r
48 \r
49 //      On node drop we can interrogate the target to find the underlying\r
50 //      application object that is the real target of the dragged data.\r
51 //      In this case, it is a Record in the GridPanel's Store.\r
52 //      We can use the data set up by the DragZone's getDragData method to read\r
53 //      any data we decided to attach in the DragZone's getDragData method.\r
54         onNodeDrop : function(target, dd, e, data){\r
55             var rowIndex = myGridPanel.getView().findRowIndex(target);\r
56             var r = myGridPanel.getStore().getAt(rowIndex);\r
57             Ext.Msg.alert('Drop gesture', 'Dropped Record id ' + data.draggedRecord.id +\r
58                 ' on Record id ' + r.id);\r
59             return true;\r
60         }\r
61     });\r
62 }\r
63 </code></pre>\r
64  * See the {@link Ext.dd.DragZone DragZone} documentation for details about building a DragZone which\r
65  * cooperates with this DropZone.\r
66  * @constructor\r
67  * @param {Mixed} el The container element\r
68  * @param {Object} config\r
69  */\r
70 Ext.dd.DropZone = function(el, config){\r
71     Ext.dd.DropZone.superclass.constructor.call(this, el, config);\r
72 };\r
73 \r
74 Ext.extend(Ext.dd.DropZone, Ext.dd.DropTarget, {\r
75     /**\r
76      * Returns a custom data object associated with the DOM node that is the target of the event.  By default\r
77      * this looks up the event target in the {@link Ext.dd.Registry}, although you can override this method to\r
78      * provide your own custom lookup.\r
79      * @param {Event} e The event\r
80      * @return {Object} data The custom data\r
81      */\r
82     getTargetFromEvent : function(e){\r
83         return Ext.dd.Registry.getTargetFromEvent(e);\r
84     },\r
85 \r
86     /**\r
87      * Called when the DropZone determines that a {@link Ext.dd.DragSource} has entered a drop node\r
88      * that has either been registered or detected by a configured implementation of {@link #getTargetFromEvent}.\r
89      * This method has no default implementation and should be overridden to provide\r
90      * node-specific processing if necessary.\r
91      * @param {Object} nodeData The custom data associated with the drop node (this is the same value returned from \r
92      * {@link #getTargetFromEvent} for this node)\r
93      * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone\r
94      * @param {Event} e The event\r
95      * @param {Object} data An object containing arbitrary data supplied by the drag source\r
96      */\r
97     onNodeEnter : function(n, dd, e, data){\r
98         \r
99     },\r
100 \r
101     /**\r
102      * Called while the DropZone determines that a {@link Ext.dd.DragSource} is over a drop node\r
103      * that has either been registered or detected by a configured implementation of {@link #getTargetFromEvent}.\r
104      * The default implementation returns this.dropNotAllowed, so it should be\r
105      * overridden to provide the proper feedback.\r
106      * @param {Object} nodeData The custom data associated with the drop node (this is the same value returned from\r
107      * {@link #getTargetFromEvent} for this node)\r
108      * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone\r
109      * @param {Event} e The event\r
110      * @param {Object} data An object containing arbitrary data supplied by the drag source\r
111      * @return {String} status The CSS class that communicates the drop status back to the source so that the\r
112      * underlying {@link Ext.dd.StatusProxy} can be updated\r
113      */\r
114     onNodeOver : function(n, dd, e, data){\r
115         return this.dropAllowed;\r
116     },\r
117 \r
118     /**\r
119      * Called when the DropZone determines that a {@link Ext.dd.DragSource} has been dragged out of\r
120      * the drop node without dropping.  This method has no default implementation and should be overridden to provide\r
121      * node-specific processing if necessary.\r
122      * @param {Object} nodeData The custom data associated with the drop node (this is the same value returned from\r
123      * {@link #getTargetFromEvent} for this node)\r
124      * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone\r
125      * @param {Event} e The event\r
126      * @param {Object} data An object containing arbitrary data supplied by the drag source\r
127      */\r
128     onNodeOut : function(n, dd, e, data){\r
129         \r
130     },\r
131 \r
132     /**\r
133      * Called when the DropZone determines that a {@link Ext.dd.DragSource} has been dropped onto\r
134      * the drop node.  The default implementation returns false, so it should be overridden to provide the\r
135      * appropriate processing of the drop event and return true so that the drag source's repair action does not run.\r
136      * @param {Object} nodeData The custom data associated with the drop node (this is the same value returned from\r
137      * {@link #getTargetFromEvent} for this node)\r
138      * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone\r
139      * @param {Event} e The event\r
140      * @param {Object} data An object containing arbitrary data supplied by the drag source\r
141      * @return {Boolean} True if the drop was valid, else false\r
142      */\r
143     onNodeDrop : function(n, dd, e, data){\r
144         return false;\r
145     },\r
146 \r
147     /**\r
148      * Called while the DropZone determines that a {@link Ext.dd.DragSource} is being dragged over it,\r
149      * but not over any of its registered drop nodes.  The default implementation returns this.dropNotAllowed, so\r
150      * it should be overridden to provide the proper feedback if necessary.\r
151      * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone\r
152      * @param {Event} e The event\r
153      * @param {Object} data An object containing arbitrary data supplied by the drag source\r
154      * @return {String} status The CSS class that communicates the drop status back to the source so that the\r
155      * underlying {@link Ext.dd.StatusProxy} can be updated\r
156      */\r
157     onContainerOver : function(dd, e, data){\r
158         return this.dropNotAllowed;\r
159     },\r
160 \r
161     /**\r
162      * Called when the DropZone determines that a {@link Ext.dd.DragSource} has been dropped on it,\r
163      * but not on any of its registered drop nodes.  The default implementation returns false, so it should be\r
164      * overridden to provide the appropriate processing of the drop event if you need the drop zone itself to\r
165      * be able to accept drops.  It should return true when valid so that the drag source's repair action does not run.\r
166      * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone\r
167      * @param {Event} e The event\r
168      * @param {Object} data An object containing arbitrary data supplied by the drag source\r
169      * @return {Boolean} True if the drop was valid, else false\r
170      */\r
171     onContainerDrop : function(dd, e, data){\r
172         return false;\r
173     },\r
174 \r
175     /**\r
176      * The function a {@link Ext.dd.DragSource} calls once to notify this drop zone that the source is now over\r
177      * the zone.  The default implementation returns this.dropNotAllowed and expects that only registered drop\r
178      * nodes can process drag drop operations, so if you need the drop zone itself to be able to process drops\r
179      * you should override this method and provide a custom implementation.\r
180      * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone\r
181      * @param {Event} e The event\r
182      * @param {Object} data An object containing arbitrary data supplied by the drag source\r
183      * @return {String} status The CSS class that communicates the drop status back to the source so that the\r
184      * underlying {@link Ext.dd.StatusProxy} can be updated\r
185      */\r
186     notifyEnter : function(dd, e, data){\r
187         return this.dropNotAllowed;\r
188     },\r
189 \r
190     /**\r
191      * The function a {@link Ext.dd.DragSource} calls continuously while it is being dragged over the drop zone.\r
192      * This method will be called on every mouse movement while the drag source is over the drop zone.\r
193      * It will call {@link #onNodeOver} while the drag source is over a registered node, and will also automatically\r
194      * delegate to the appropriate node-specific methods as necessary when the drag source enters and exits\r
195      * registered nodes ({@link #onNodeEnter}, {@link #onNodeOut}). If the drag source is not currently over a\r
196      * registered node, it will call {@link #onContainerOver}.\r
197      * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone\r
198      * @param {Event} e The event\r
199      * @param {Object} data An object containing arbitrary data supplied by the drag source\r
200      * @return {String} status The CSS class that communicates the drop status back to the source so that the\r
201      * underlying {@link Ext.dd.StatusProxy} can be updated\r
202      */\r
203     notifyOver : function(dd, e, data){\r
204         var n = this.getTargetFromEvent(e);\r
205         if(!n){ // not over valid drop target\r
206             if(this.lastOverNode){\r
207                 this.onNodeOut(this.lastOverNode, dd, e, data);\r
208                 this.lastOverNode = null;\r
209             }\r
210             return this.onContainerOver(dd, e, data);\r
211         }\r
212         if(this.lastOverNode != n){\r
213             if(this.lastOverNode){\r
214                 this.onNodeOut(this.lastOverNode, dd, e, data);\r
215             }\r
216             this.onNodeEnter(n, dd, e, data);\r
217             this.lastOverNode = n;\r
218         }\r
219         return this.onNodeOver(n, dd, e, data);\r
220     },\r
221 \r
222     /**\r
223      * The function a {@link Ext.dd.DragSource} calls once to notify this drop zone that the source has been dragged\r
224      * out of the zone without dropping.  If the drag source is currently over a registered node, the notification\r
225      * will be delegated to {@link #onNodeOut} for node-specific handling, otherwise it will be ignored.\r
226      * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop target\r
227      * @param {Event} e The event\r
228      * @param {Object} data An object containing arbitrary data supplied by the drag zone\r
229      */\r
230     notifyOut : function(dd, e, data){\r
231         if(this.lastOverNode){\r
232             this.onNodeOut(this.lastOverNode, dd, e, data);\r
233             this.lastOverNode = null;\r
234         }\r
235     },\r
236 \r
237     /**\r
238      * The function a {@link Ext.dd.DragSource} calls once to notify this drop zone that the dragged item has\r
239      * been dropped on it.  The drag zone will look up the target node based on the event passed in, and if there\r
240      * is a node registered for that event, it will delegate to {@link #onNodeDrop} for node-specific handling,\r
241      * otherwise it will call {@link #onContainerDrop}.\r
242      * @param {Ext.dd.DragSource} source The drag source that was dragged over this drop zone\r
243      * @param {Event} e The event\r
244      * @param {Object} data An object containing arbitrary data supplied by the drag source\r
245      * @return {Boolean} True if the drop was valid, else false\r
246      */\r
247     notifyDrop : function(dd, e, data){\r
248         if(this.lastOverNode){\r
249             this.onNodeOut(this.lastOverNode, dd, e, data);\r
250             this.lastOverNode = null;\r
251         }\r
252         var n = this.getTargetFromEvent(e);\r
253         return n ?\r
254             this.onNodeDrop(n, dd, e, data) :\r
255             this.onContainerDrop(dd, e, data);\r
256     },\r
257 \r
258     // private\r
259     triggerCacheRefresh : function(){\r
260         Ext.dd.DDM.refreshCache(this.groups);\r
261     }  \r
262 });