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