Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / docs / source / DragZone.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.DragZone"></div>/**\r
15  * @class Ext.dd.DragZone\r
16  * @extends Ext.dd.DragSource\r
17  * <p>This class provides a container DD instance that allows dragging of multiple child source nodes.</p>\r
18  * <p>This class does not move the drag target nodes, but a proxy element which may contain\r
19  * any DOM structure you wish. The DOM element to show in the proxy is provided by either a\r
20  * provided implementation of {@link #getDragData}, or by registered draggables registered with {@link Ext.dd.Registry}</p>\r
21  * <p>If you wish to provide draggability for an arbitrary number of DOM nodes, each of which represent some\r
22  * application object (For example nodes in a {@link Ext.DataView DataView}) then use of this class\r
23  * is the most efficient way to "activate" those nodes.</p>\r
24  * <p>By default, this class requires that draggable child nodes are registered with {@link Ext.dd.Registry}.\r
25  * However a simpler way to allow a DragZone to manage any number of draggable elements is to configure\r
26  * the DragZone with  an implementation of the {@link #getDragData} method which interrogates the passed\r
27  * mouse event to see if it has taken place within an element, or class of elements. This is easily done\r
28  * by using the event's {@link Ext.EventObject#getTarget getTarget} method to identify a node based on a\r
29  * {@link Ext.DomQuery} selector. For example, to make the nodes of a DataView draggable, use the following\r
30  * technique. Knowledge of the use of the DataView is required:</p><pre><code>\r
31 myDataView.on('render', function() {\r
32     myDataView.dragZone = new Ext.dd.DragZone(myDataView.getEl(), {\r
33 \r
34 //      On receipt of a mousedown event, see if it is within a DataView node.\r
35 //      Return a drag data object if so.\r
36         getDragData: function(e) {\r
37 \r
38 //          Use the DataView's own itemSelector (a mandatory property) to\r
39 //          test if the mousedown is within one of the DataView's nodes.\r
40             var sourceEl = e.getTarget(myDataView.itemSelector, 10);\r
41 \r
42 //          If the mousedown is within a DataView node, clone the node to produce\r
43 //          a ddel element for use by the drag proxy. Also add application data\r
44 //          to the returned data object.\r
45             if (sourceEl) {\r
46                 d = sourceEl.cloneNode(true);\r
47                 d.id = Ext.id();\r
48                 return {\r
49                     ddel: d,\r
50                     sourceEl: sourceEl,\r
51                     repairXY: Ext.fly(sourceEl).getXY(),\r
52                     sourceStore: myDataView.store,\r
53                     draggedRecord: v.getRecord(sourceEl)\r
54                 }\r
55             }\r
56         },\r
57 \r
58 //      Provide coordinates for the proxy to slide back to on failed drag.\r
59 //      This is the original XY coordinates of the draggable element captured\r
60 //      in the getDragData method.\r
61         getRepairXY: function() {\r
62             return this.dragData.repairXY;\r
63         }\r
64     });\r
65 });</code></pre>\r
66  * See the {@link Ext.dd.DropZone DropZone} documentation for details about building a DropZone which\r
67  * cooperates with this DragZone.\r
68  * @constructor\r
69  * @param {Mixed} el The container element\r
70  * @param {Object} config\r
71  */\r
72 Ext.dd.DragZone = function(el, config){\r
73     Ext.dd.DragZone.superclass.constructor.call(this, el, config);\r
74     if(this.containerScroll){\r
75         Ext.dd.ScrollManager.register(this.el);\r
76     }\r
77 };\r
78 \r
79 Ext.extend(Ext.dd.DragZone, Ext.dd.DragSource, {\r
80     <div id="prop-Ext.dd.DragZone-dragData"></div>/**\r
81      * This property contains the data representing the dragged object. This data is set up by the implementation\r
82      * of the {@link #getDragData} method. It must contain a <tt>ddel</tt> property, but can contain\r
83      * any other data according to the application's needs.\r
84      * @type Object\r
85      * @property dragData\r
86      */\r
87     <div id="cfg-Ext.dd.DragZone-containerScroll"></div>/**\r
88      * @cfg {Boolean} containerScroll True to register this container with the Scrollmanager\r
89      * for auto scrolling during drag operations.\r
90      */\r
91     <div id="cfg-Ext.dd.DragZone-hlColor"></div>/**\r
92      * @cfg {String} hlColor The color to use when visually highlighting the drag source in the afterRepair\r
93      * method after a failed drop (defaults to "c3daf9" - light blue)\r
94      */\r
95 \r
96     <div id="method-Ext.dd.DragZone-getDragData"></div>/**\r
97      * Called when a mousedown occurs in this container. Looks in {@link Ext.dd.Registry}\r
98      * for a valid target to drag based on the mouse down. Override this method\r
99      * to provide your own lookup logic (e.g. finding a child by class name). Make sure your returned\r
100      * object has a "ddel" attribute (with an HTML Element) for other functions to work.\r
101      * @param {EventObject} e The mouse down event\r
102      * @return {Object} The dragData\r
103      */\r
104     getDragData : function(e){\r
105         return Ext.dd.Registry.getHandleFromEvent(e);\r
106     },\r
107     \r
108     <div id="method-Ext.dd.DragZone-onInitDrag"></div>/**\r
109      * Called once drag threshold has been reached to initialize the proxy element. By default, it clones the\r
110      * this.dragData.ddel\r
111      * @param {Number} x The x position of the click on the dragged object\r
112      * @param {Number} y The y position of the click on the dragged object\r
113      * @return {Boolean} true to continue the drag, false to cancel\r
114      */\r
115     onInitDrag : function(x, y){\r
116         this.proxy.update(this.dragData.ddel.cloneNode(true));\r
117         this.onStartDrag(x, y);\r
118         return true;\r
119     },\r
120     \r
121     <div id="method-Ext.dd.DragZone-afterRepair"></div>/**\r
122      * Called after a repair of an invalid drop. By default, highlights this.dragData.ddel \r
123      */\r
124     afterRepair : function(){\r
125         if(Ext.enableFx){\r
126             Ext.Element.fly(this.dragData.ddel).highlight(this.hlColor || "c3daf9");\r
127         }\r
128         this.dragging = false;\r
129     },\r
130 \r
131     <div id="method-Ext.dd.DragZone-getRepairXY"></div>/**\r
132      * Called before a repair of an invalid drop to get the XY to animate to. By default returns\r
133      * the XY of this.dragData.ddel\r
134      * @param {EventObject} e The mouse up event\r
135      * @return {Array} The xy location (e.g. [100, 200])\r
136      */\r
137     getRepairXY : function(e){\r
138         return Ext.Element.fly(this.dragData.ddel).getXY();  \r
139     }\r
140 });</pre>
141 </body>
142 </html>