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