Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / grid / plugin / DragDrop.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.grid.plugin.DragDrop
17  * <p>This plugin provides drag and/or drop functionality for a GridView.</p>
18  * <p>It creates a specialized instance of {@link Ext.dd.DragZone DragZone} which knows how to drag out of a {@link Ext.grid.View GridView}
19  * and loads the data object which is passed to a cooperating {@link Ext.dd.DragZone DragZone}'s methods with the following properties:<ul>
20  * <li>copy : Boolean
21  *  <div class="sub-desc">The value of the GridView's <code>copy</code> property, or <code>true</code> if the GridView was configured
22  *  with <code>allowCopy: true</code> <u>and</u> the control key was pressed when the drag operation was begun.</div></li>
23  * <li>view : GridView
24  *  <div class="sub-desc">The source GridView from which the drag originated.</div></li>
25  * <li>ddel : HtmlElement
26  *  <div class="sub-desc">The drag proxy element which moves with the mouse</div></li>
27  * <li>item : HtmlElement
28  *  <div class="sub-desc">The GridView node upon which the mousedown event was registered.</div></li>
29  * <li>records : Array
30  *  <div class="sub-desc">An Array of {@link Ext.data.Model Model}s representing the selected data being dragged from the source GridView.</div></li>
31  * </ul></p>
32  * <p>It also creates a specialized instance of {@link Ext.dd.DropZone} which cooperates with other DropZones which are members of the same
33  * ddGroup which processes such data objects.</p>
34  * <p>Adding this plugin to a view means that two new events may be fired from the client GridView, <code>{@link #event-beforedrop beforedrop}</code> and
35  * <code>{@link #event-drop drop}</code></p>
36  */
37 Ext.define('Ext.grid.plugin.DragDrop', {
38     extend: 'Ext.AbstractPlugin',
39     alias: 'plugin.gridviewdragdrop',
40
41     uses: [
42         'Ext.view.DragZone',
43         'Ext.grid.ViewDropZone'
44     ],
45
46     /**
47      * @event beforedrop
48      * <p><b>This event is fired through the GridView. Add listeners to the GridView object</b></p>
49      * <p>Fired when a drop gesture has been triggered by a mouseup event in a valid drop position in the GridView.
50      * @param {HtmlElement} node The GridView node <b>if any</b> over which the mouse was positioned.</p>
51      * <p>Returning <code>false</code> to this event signals that the drop gesture was invalid, and if the drag proxy
52      * will animate back to the point from which the drag began.</p>
53      * <p>Returning <code>0</code> To this event signals that the data transfer operation should not take place, but
54      * that the gesture was valid, and that the repair operation should not take place.</p>
55      * <p>Any other return value continues with the data transfer operation.</p>
56      * @param {Object} data The data object gathered at mousedown time by the cooperating {@link Ext.dd.DragZone DragZone}'s
57      * {@link Ext.dd.DragZone#getDragData getDragData} method it contains the following properties:<ul>
58      * <li>copy : Boolean
59      *  <div class="sub-desc">The value of the GridView's <code>copy</code> property, or <code>true</code> if the GridView was configured
60      *  with <code>allowCopy: true</code> and the control key was pressed when the drag operation was begun</div></li>
61      * <li>view : GridView
62      *  <div class="sub-desc">The source GridView from which the drag originated.</div></li>
63      * <li>ddel : HtmlElement
64      *  <div class="sub-desc">The drag proxy element which moves with the mouse</div></li>
65      * <li>item : HtmlElement
66      *  <div class="sub-desc">The GridView node upon which the mousedown event was registered.</div></li>
67      * <li>records : Array
68      *  <div class="sub-desc">An Array of {@link Ext.data.Model Model}s representing the selected data being dragged from the source GridView.</div></li>
69      * </ul>
70      * @param {Ext.data.Model} overModel The Model over which the drop gesture took place.
71      * @param {String} dropPosition <code>"before"</code> or <code>"after"</code> depending on whether the mouse is above or below the midline of the node.
72      * @param {Function} dropFunction <p>A function to call to complete the data transfer operation and either move or copy Model instances from the source
73      * View's Store to the destination View's Store.</p>
74      * <p>This is useful when you want to perform some kind of asynchronous processing before confirming
75      * the drop, such as an {@link Ext.window.MessageBox#confirm confirm} call, or an Ajax request.</p>
76      * <p>Return <code>0</code> from this event handler, and call the <code>dropFunction</code> at any time to perform the data transfer.</p>
77      */
78
79     /**
80      * @event drop
81      * <b>This event is fired through the GridView. Add listeners to the GridView object</b>
82      * Fired when a drop operation has been completed and the data has been moved or copied.
83      * @param {HtmlElement} node The GridView node <b>if any</b> over which the mouse was positioned.
84      * @param {Object} data The data object gathered at mousedown time by the cooperating {@link Ext.dd.DragZone DragZone}'s
85      * {@link Ext.dd.DragZone#getDragData getDragData} method it contains the following properties:<ul>
86      * <li>copy : Boolean
87      *  <div class="sub-desc">The value of the GridView's <code>copy</code> property, or <code>true</code> if the GridView was configured
88      *  with <code>allowCopy: true</code> and the control key was pressed when the drag operation was begun</div></li>
89      * <li>view : GridView
90      *  <div class="sub-desc">The source GridView from which the drag originated.</div></li>
91      * <li>ddel : HtmlElement
92      *  <div class="sub-desc">The drag proxy element which moves with the mouse</div></li>
93      * <li>item : HtmlElement
94      *  <div class="sub-desc">The GridView node upon which the mousedown event was registered.</div></li>
95      * <li>records : Array
96      *  <div class="sub-desc">An Array of {@link Ext.data.Model Model}s representing the selected data being dragged from the source GridView.</div></li>
97      * </ul>
98      * @param {Ext.data.Model} overModel The Model over which the drop gesture took place.
99      * @param {String} dropPosition <code>"before"</code> or <code>"after"</code> depending on whether the mouse is above or below the midline of the node.
100      */
101
102     dragText : '{0} selected row{1}',
103
104     /**
105      * @cfg {String} ddGroup
106      * A named drag drop group to which this object belongs.  If a group is specified, then both the DragZones and DropZone
107      * used by this plugin will only interact with other drag drop objects in the same group (defaults to 'TreeDD').
108      */
109     ddGroup : "GridDD",
110
111     /**
112      * @cfg {String} dragGroup
113      * <p>The ddGroup to which the DragZone will belong.</p>
114      * <p>This defines which other DropZones the DragZone will interact with. Drag/DropZones only interact with other Drag/DropZones
115      * which are members of the same ddGroup.</p>
116      */
117
118     /**
119      * @cfg {String} dropGroup
120      * <p>The ddGroup to which the DropZone will belong.</p>
121      * <p>This defines which other DragZones the DropZone will interact with. Drag/DropZones only interact with other Drag/DropZones
122      * which are members of the same ddGroup.</p>
123      */
124
125     /**
126      * @cfg {Boolean} enableDrop
127      * <p>Defaults to <code>true</code></p>
128      * <p>Set to <code>false</code> to disallow the View from accepting drop gestures</p>
129      */
130     enableDrop: true,
131
132     /**
133      * @cfg {Boolean} enableDrag
134      * <p>Defaults to <code>true</code></p>
135      * <p>Set to <code>false</code> to disallow dragging items from the View </p>
136      */
137     enableDrag: true,
138
139     init : function(view) {
140         view.on('render', this.onViewRender, this, {single: true});
141     },
142
143     /**
144      * @private
145      * AbstractComponent calls destroy on all its plugins at destroy time.
146      */
147     destroy: function() {
148         Ext.destroy(this.dragZone, this.dropZone);
149     },
150
151     onViewRender : function(view) {
152         var me = this;
153
154         if (me.enableDrag) {
155             me.dragZone = Ext.create('Ext.view.DragZone', {
156                 view: view,
157                 ddGroup: me.dragGroup || me.ddGroup,
158                 dragText: me.dragText
159             });
160         }
161
162         if (me.enableDrop) {
163             me.dropZone = Ext.create('Ext.grid.ViewDropZone', {
164                 view: view,
165                 ddGroup: me.dropGroup || me.ddGroup
166             });
167         }
168     }
169 });