Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / tree / plugin / TreeViewDragDrop.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * This plugin provides drag and/or drop functionality for a TreeView.
17  *
18  * It creates a specialized instance of {@link Ext.dd.DragZone DragZone} which knows how to drag out of a
19  * {@link Ext.tree.View TreeView} and loads the data object which is passed to a cooperating
20  * {@link Ext.dd.DragZone DragZone}'s methods with the following properties:
21  *
22  *   - copy : Boolean
23  *
24  *     The value of the TreeView's `copy` property, or `true` if the TreeView was configured with `allowCopy: true` *and*
25  *     the control key was pressed when the drag operation was begun.
26  *
27  *   - view : TreeView
28  *
29  *     The source TreeView from which the drag originated.
30  *
31  *   - ddel : HtmlElement
32  *
33  *     The drag proxy element which moves with the mouse
34  *
35  *   - item : HtmlElement
36  *
37  *     The TreeView node upon which the mousedown event was registered.
38  *
39  *   - records : Array
40  *
41  *     An Array of {@link Ext.data.Model Models} representing the selected data being dragged from the source TreeView.
42  *
43  * It also creates a specialized instance of {@link Ext.dd.DropZone} which cooperates with other DropZones which are
44  * members of the same ddGroup which processes such data objects.
45  *
46  * Adding this plugin to a view means that two new events may be fired from the client TreeView, {@link #beforedrop} and
47  * {@link #drop}.
48  *
49  * Note that the plugin must be added to the tree view, not to the tree panel. For example using viewConfig:
50  *
51  *     viewConfig: {
52  *         plugins: { ptype: 'treeviewdragdrop' }
53  *     }
54  */
55 Ext.define('Ext.tree.plugin.TreeViewDragDrop', {
56     extend: 'Ext.AbstractPlugin',
57     alias: 'plugin.treeviewdragdrop',
58
59     uses: [
60         'Ext.tree.ViewDragZone',
61         'Ext.tree.ViewDropZone'
62     ],
63
64     /**
65      * @event beforedrop
66      *
67      * **This event is fired through the TreeView. Add listeners to the TreeView object**
68      *
69      * Fired when a drop gesture has been triggered by a mouseup event in a valid drop position in the TreeView.
70      *
71      * @param {HTMLElement} node The TreeView node **if any** over which the mouse was positioned.
72      *
73      * Returning `false` to this event signals that the drop gesture was invalid, and if the drag proxy will animate
74      * back to the point from which the drag began.
75      *
76      * Returning `0` To this event signals that the data transfer operation should not take place, but that the gesture
77      * was valid, and that the repair operation should not take place.
78      *
79      * Any other return value continues with the data transfer operation.
80      *
81      * @param {Object} data The data object gathered at mousedown time by the cooperating
82      * {@link Ext.dd.DragZone DragZone}'s {@link Ext.dd.DragZone#getDragData getDragData} method it contains the following
83      * properties:
84      * @param {Boolean} data.copy The value of the TreeView's `copy` property, or `true` if the TreeView was configured with
85      * `allowCopy: true` and the control key was pressed when the drag operation was begun
86      * @param {Ext.tree.View} data.view The source TreeView from which the drag originated.
87      * @param {HTMLElement} data.ddel The drag proxy element which moves with the mouse
88      * @param {HTMLElement} data.item The TreeView node upon which the mousedown event was registered.
89      * @param {Ext.data.Model[]} data.records An Array of {@link Ext.data.Model Model}s representing the selected data being
90      * dragged from the source TreeView.
91      *
92      * @param {Ext.data.Model} overModel The Model over which the drop gesture took place.
93      *
94      * @param {String} dropPosition `"before"`, `"after"` or `"append"` depending on whether the mouse is above or below
95      * the midline of the node, or the node is a branch node which accepts new child nodes.
96      *
97      * @param {Function} dropFunction A function to call to complete the data transfer operation and either move or copy
98      * Model instances from the source View's Store to the destination View's Store.
99      *
100      * This is useful when you want to perform some kind of asynchronous processing before confirming the drop, such as
101      * an {@link Ext.window.MessageBox#confirm confirm} call, or an Ajax request.
102      *
103      * Return `0` from this event handler, and call the `dropFunction` at any time to perform the data transfer.
104      */
105
106     /**
107      * @event drop
108      *
109      * **This event is fired through the TreeView. Add listeners to the TreeView object** Fired when a drop operation
110      * has been completed and the data has been moved or copied.
111      *
112      * @param {HTMLElement} node The TreeView node **if any** over which the mouse was positioned.
113      *
114      * @param {Object} data The data object gathered at mousedown time by the cooperating
115      * {@link Ext.dd.DragZone DragZone}'s {@link Ext.dd.DragZone#getDragData getDragData} method it contains the following
116      * properties:
117      * @param {Boolean} data.copy The value of the TreeView's `copy` property, or `true` if the TreeView was configured with
118      * `allowCopy: true` and the control key was pressed when the drag operation was begun
119      * @param {Ext.tree.View} data.view The source TreeView from which the drag originated.
120      * @param {HTMLElement} data.ddel The drag proxy element which moves with the mouse
121      * @param {HTMLElement} data.item The TreeView node upon which the mousedown event was registered.
122      * @param {Ext.data.Model[]} data.records An Array of {@link Ext.data.Model Model}s representing the selected data being
123      * dragged from the source TreeView.
124      *
125      * @param {Ext.data.Model} overModel The Model over which the drop gesture took place.
126      *
127      * @param {String} dropPosition `"before"`, `"after"` or `"append"` depending on whether the mouse is above or below
128      * the midline of the node, or the node is a branch node which accepts new child nodes.
129      */
130
131     dragText : '{0} selected node{1}',
132
133     /**
134      * @cfg {Boolean} allowParentInsert
135      * Allow inserting a dragged node between an expanded parent node and its first child that will become a sibling of
136      * the parent when dropped.
137      */
138     allowParentInserts: false,
139
140     /**
141      * @cfg {String} allowContainerDrop
142      * True if drops on the tree container (outside of a specific tree node) are allowed.
143      */
144     allowContainerDrops: false,
145
146     /**
147      * @cfg {String} appendOnly
148      * True if the tree should only allow append drops (use for trees which are sorted).
149      */
150     appendOnly: false,
151
152     /**
153      * @cfg {String} ddGroup
154      * A named drag drop group to which this object belongs. If a group is specified, then both the DragZones and
155      * DropZone used by this plugin will only interact with other drag drop objects in the same group.
156      */
157     ddGroup : "TreeDD",
158
159     /**
160      * @cfg {String} dragGroup
161      * The ddGroup to which the DragZone will belong.
162      *
163      * This defines which other DropZones the DragZone will interact with. Drag/DropZones only interact with other
164      * Drag/DropZones which are members of the same ddGroup.
165      */
166
167     /**
168      * @cfg {String} dropGroup
169      * The ddGroup to which the DropZone will belong.
170      *
171      * This defines which other DragZones the DropZone will interact with. Drag/DropZones only interact with other
172      * Drag/DropZones which are members of the same ddGroup.
173      */
174
175     /**
176      * @cfg {String} expandDelay
177      * The delay in milliseconds to wait before expanding a target tree node while dragging a droppable node over the
178      * target.
179      */
180     expandDelay : 1000,
181
182     /**
183      * @cfg {Boolean} enableDrop
184      * Set to `false` to disallow the View from accepting drop gestures.
185      */
186     enableDrop: true,
187
188     /**
189      * @cfg {Boolean} enableDrag
190      * Set to `false` to disallow dragging items from the View.
191      */
192     enableDrag: true,
193
194     /**
195      * @cfg {String} nodeHighlightColor
196      * The color to use when visually highlighting the dragged or dropped node (default value is light blue).
197      * The color must be a 6 digit hex value, without a preceding '#'. See also {@link #nodeHighlightOnDrop} and
198      * {@link #nodeHighlightOnRepair}.
199      */
200     nodeHighlightColor: 'c3daf9',
201
202     /**
203      * @cfg {Boolean} nodeHighlightOnDrop
204      * Whether or not to highlight any nodes after they are
205      * successfully dropped on their target. Defaults to the value of `Ext.enableFx`.
206      * See also {@link #nodeHighlightColor} and {@link #nodeHighlightOnRepair}.
207      */
208     nodeHighlightOnDrop: Ext.enableFx,
209
210     /**
211      * @cfg {Boolean} nodeHighlightOnRepair
212      * Whether or not to highlight any nodes after they are
213      * repaired from an unsuccessful drag/drop. Defaults to the value of `Ext.enableFx`.
214      * See also {@link #nodeHighlightColor} and {@link #nodeHighlightOnDrop}.
215      */
216     nodeHighlightOnRepair: Ext.enableFx,
217
218     init : function(view) {
219         view.on('render', this.onViewRender, this, {single: true});
220     },
221
222     /**
223      * @private
224      * AbstractComponent calls destroy on all its plugins at destroy time.
225      */
226     destroy: function() {
227         Ext.destroy(this.dragZone, this.dropZone);
228     },
229
230     onViewRender : function(view) {
231         var me = this;
232
233         if (me.enableDrag) {
234             me.dragZone = Ext.create('Ext.tree.ViewDragZone', {
235                 view: view,
236                 ddGroup: me.dragGroup || me.ddGroup,
237                 dragText: me.dragText,
238                 repairHighlightColor: me.nodeHighlightColor,
239                 repairHighlight: me.nodeHighlightOnRepair
240             });
241         }
242
243         if (me.enableDrop) {
244             me.dropZone = Ext.create('Ext.tree.ViewDropZone', {
245                 view: view,
246                 ddGroup: me.dropGroup || me.ddGroup,
247                 allowContainerDrops: me.allowContainerDrops,
248                 appendOnly: me.appendOnly,
249                 allowParentInserts: me.allowParentInserts,
250                 expandDelay: me.expandDelay,
251                 dropHighlightColor: me.nodeHighlightColor,
252                 dropHighlight: me.nodeHighlightOnDrop
253             });
254         }
255     }
256 });