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