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