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