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