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     Roo.apply(this, config);
26     
27     if(this.containerScroll){
28         Roo.dd.ScrollManager.register(this.el);
29     }
30     this.events : {
31          /**
32          * @scope Roo.dd.DropTarget
33          */
34          
35          /**
36          * @event enter
37          * The function a {@link Roo.dd.DragSource} calls once to notify this drop target that the source is now over the
38          * target.  This default implementation adds the CSS class specified by overClass (if any) to the drop element
39          * and returns the dropAllowed config value.  This method should be overridden if drop validation is required.
40          * 
41          * IMPORTANT : it should set this.overClass and this.dropAllowed
42          * 
43          * @param {Roo.dd.DragSource} source The drag source that was dragged over this drop target
44          * @param {Event} e The event
45          * @param {Object} data An object containing arbitrary data supplied by the drag source
46          */
47         "enter" : true,
48         
49          /**
50          * @event over
51          * The function a {@link Roo.dd.DragSource} calls continuously while it is being dragged over the target.
52          * This method will be called on every mouse movement while the drag source is over the drop target.
53          * This default implementation simply returns the dropAllowed config value.
54          * 
55          * IMPORTANT : it should set this.dropAllowed
56          * 
57          * @param {Roo.dd.DragSource} source The drag source that was dragged over this drop target
58          * @param {Event} e The event
59          * @param {Object} data An object containing arbitrary data supplied by the drag source
60          
61          */
62         "over" : true,
63         /**
64          * @event out
65          * The function a {@link Roo.dd.DragSource} calls once to notify this drop target that the source has been dragged
66          * out of the target without dropping.  This default implementation simply removes the CSS class specified by
67          * overClass (if any) from the drop element.
68          * @param {Roo.dd.DragSource} source The drag source that was dragged over this drop target
69          * @param {Event} e The event
70          * @param {Object} data An object containing arbitrary data supplied by the drag source
71          */
72          "out" : true,
73          
74         /**
75          * @event drop
76          * The function a {@link Roo.dd.DragSource} calls once to notify this drop target that the dragged item has
77          * been dropped on it.  This method has no default implementation and returns false, so you must provide an
78          * implementation that does something to process the drop event and returns true so that the drag source's
79          * repair action does not run.
80          * 
81          * IMPORTANT : it should set this.success
82          * 
83          * @param {Roo.dd.DragSource} source The drag source that was dragged over this drop target
84          * @param {Event} e The event
85          * @param {Object} data An object containing arbitrary data supplied by the drag source
86         */
87          "drop" : true
88     }
89             
90      
91     Roo.dd.DropTarget.superclass.constructor.call(  this, 
92         this.el.dom, 
93         this.ddGroup || this.group,
94         {
95             isTarget: true,
96             listeners : config.listeners || {} ,
97            
98         
99         }
100     );
101
102 };
103
104 Roo.extend(Roo.dd.DropTarget, Roo.dd.DDTarget, {
105     /**
106      * @cfg {String} overClass
107      * The CSS class applied to the drop target element while the drag source is over it (defaults to "").
108      */
109      /**
110      * @cfg {String} ddGroup
111      * The drag drop group to handle drop events for
112      */
113      
114     /**
115      * @cfg {String} dropAllowed
116      * The CSS class returned to the drag source when drop is allowed (defaults to "x-dd-drop-ok").
117      */
118     dropAllowed : "x-dd-drop-ok",
119     /**
120      * @cfg {String} dropNotAllowed
121      * The CSS class returned to the drag source when drop is not allowed (defaults to "x-dd-drop-nodrop").
122      */
123     dropNotAllowed : "x-dd-drop-nodrop",
124     /**
125      * @cfg {boolean} success
126      * set this after drop listener.. 
127      */
128     success : false,
129     /**
130      * @cfg {boolean} valid
131      * if the drop point is valid for over/enter..
132      */
133     valid : false,
134     // private
135     isTarget : true,
136
137     // private
138     isNotifyTarget : true,
139     
140     /**
141      * @hide
142      */
143     notifyEnter : function(dd, e, data){
144         this.valid = true;
145         this.fireEvent('enter', this, dd, e, data);
146         if(this.overClass){
147             this.el.addClass(this.overClass);
148         }
149         return this.valid ? this.dropAllowed : this.dropNotAllowed;
150     },
151
152     /**
153      * @hide
154      */
155     notifyOver : function(dd, e, data){
156         this.valid = true;
157         this.fireEvent('over', this, dd, e, data);
158         return this.valid ? this.dropAllowed : this.dropNotAllowed;
159     },
160
161     /**
162      * @hide
163      */
164     notifyOut : function(dd, e, data){
165         this.fireEvent('out', this, dd, e, data);
166         if(this.overClass){
167             this.el.removeClass(this.overClass);
168         }
169     },
170
171     /**
172      * @hide
173      */
174     notifyDrop : function(dd, e, data){
175         this.success = false;
176         this.fireEvent('drop', this, dd, e, data);
177         return this.success;
178     }
179 });