Roo/dd/DropTarget.js
[roojs1] / Roo / dd / DropTarget.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11
12
13 /**
14  * @class Roo.dd.DropTarget
15  * @extends Roo.dd.DDTarget
16  * A simple class that provides the basic implementation needed to make any element a drop target that can have
17  * draggable items dropped onto it.  The drop has no effect until an implementation of notifyDrop is provided.
18  * @constructor
19  * @param {String/HTMLElement/Element} el The container element
20  * @param {Object} config
21  */
22 Roo.dd.DropTarget = function(el, config){
23     this.el = Roo.get(el);
24     
25     var listeners = false; ;
26     if (config & config.listeners) {
27         delete config.listeners;
28     }
29     Roo.apply(this, config);
30     
31     if(this.containerScroll){
32         Roo.dd.ScrollManager.register(this.el);
33     }
34     this.addEvents( {
35          /**
36          * @scope Roo.dd.DropTarget
37          */
38          
39          /**
40          * @event enter
41          * The function a {@link Roo.dd.DragSource} calls once to notify this drop target that the source is now over the
42          * target.  This default implementation adds the CSS class specified by overClass (if any) to the drop element
43          * and returns the dropAllowed config value.  This method should be overridden if drop validation is required.
44          * 
45          * IMPORTANT : it should set this.overClass and this.dropAllowed
46          * 
47          * @param {Roo.dd.DragSource} source The drag source that was dragged over this drop target
48          * @param {Event} e The event
49          * @param {Object} data An object containing arbitrary data supplied by the drag source
50          */
51         "enter" : true,
52         
53          /**
54          * @event over
55          * The function a {@link Roo.dd.DragSource} calls continuously while it is being dragged over the target.
56          * This method will be called on every mouse movement while the drag source is over the drop target.
57          * This default implementation simply returns the dropAllowed config value.
58          * 
59          * IMPORTANT : it should set this.dropAllowed
60          * 
61          * @param {Roo.dd.DragSource} source The drag source that was dragged over this drop target
62          * @param {Event} e The event
63          * @param {Object} data An object containing arbitrary data supplied by the drag source
64          
65          */
66         "over" : true,
67         /**
68          * @event out
69          * The function a {@link Roo.dd.DragSource} calls once to notify this drop target that the source has been dragged
70          * out of the target without dropping.  This default implementation simply removes the CSS class specified by
71          * overClass (if any) from the drop element.
72          * @param {Roo.dd.DragSource} source The drag source that was dragged over this drop target
73          * @param {Event} e The event
74          * @param {Object} data An object containing arbitrary data supplied by the drag source
75          */
76          "out" : true,
77          
78         /**
79          * @event drop
80          * The function a {@link Roo.dd.DragSource} calls once to notify this drop target that the dragged item has
81          * been dropped on it.  This method has no default implementation and returns false, so you must provide an
82          * implementation that does something to process the drop event and returns true so that the drag source's
83          * repair action does not run.
84          * 
85          * IMPORTANT : it should set this.success
86          * 
87          * @param {Roo.dd.DragSource} source The drag source that was dragged over this drop target
88          * @param {Event} e The event
89          * @param {Object} data An object containing arbitrary data supplied by the drag source
90         */
91          "drop" : true
92     });
93             
94      
95     Roo.dd.DropTarget.superclass.constructor.call(  this, 
96         this.el.dom, 
97         this.ddGroup || this.group,
98         {
99             isTarget: true,
100             listeners : listeners || {} 
101            
102         
103         }
104     );
105
106 };
107
108 Roo.extend(Roo.dd.DropTarget, Roo.dd.DDTarget, {
109     /**
110      * @cfg {String} overClass
111      * The CSS class applied to the drop target element while the drag source is over it (defaults to "").
112      */
113      /**
114      * @cfg {String} ddGroup
115      * The drag drop group to handle drop events for
116      */
117      
118     /**
119      * @cfg {String} dropAllowed
120      * The CSS class returned to the drag source when drop is allowed (defaults to "x-dd-drop-ok").
121      */
122     dropAllowed : "x-dd-drop-ok",
123     /**
124      * @cfg {String} dropNotAllowed
125      * The CSS class returned to the drag source when drop is not allowed (defaults to "x-dd-drop-nodrop").
126      */
127     dropNotAllowed : "x-dd-drop-nodrop",
128     /**
129      * @cfg {boolean} success
130      * set this after drop listener.. 
131      */
132     success : false,
133     /**
134      * @cfg {boolean} valid
135      * if the drop point is valid for over/enter..
136      */
137     valid : false,
138     // private
139     isTarget : true,
140
141     // private
142     isNotifyTarget : true,
143     
144     /**
145      * @hide
146      */
147     notifyEnter : function(dd, e, data){
148         this.valid = true;
149         this.fireEvent('enter', this, dd, e, data);
150         if(this.overClass){
151             this.el.addClass(this.overClass);
152         }
153         return this.valid ? this.dropAllowed : this.dropNotAllowed;
154     },
155
156     /**
157      * @hide
158      */
159     notifyOver : function(dd, e, data){
160         this.valid = true;
161         this.fireEvent('over', this, dd, e, data);
162         return this.valid ? this.dropAllowed : this.dropNotAllowed;
163     },
164
165     /**
166      * @hide
167      */
168     notifyOut : function(dd, e, data){
169         this.fireEvent('out', this, dd, e, data);
170         if(this.overClass){
171             this.el.removeClass(this.overClass);
172         }
173     },
174
175     /**
176      * @hide
177      */
178     notifyDrop : function(dd, e, data){
179         this.success = false;
180         this.fireEvent('drop', this, dd, e, data);
181         return this.success;
182     }
183 });