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