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