3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.dd.DragSource"></div>/**
\r
15 * @class Ext.dd.DragSource
\r
16 * @extends Ext.dd.DDProxy
\r
17 * A simple class that provides the basic implementation needed to make any element draggable.
\r
19 * @param {Mixed} el The container element
\r
20 * @param {Object} config
\r
22 Ext.dd.DragSource = function(el, config){
\r
23 this.el = Ext.get(el);
\r
28 Ext.apply(this, config);
\r
31 this.proxy = new Ext.dd.StatusProxy();
\r
33 Ext.dd.DragSource.superclass.constructor.call(this, this.el.dom, this.ddGroup || this.group,
\r
34 {dragElId : this.proxy.id, resizeFrame: false, isTarget: false, scroll: this.scroll === true});
\r
36 this.dragging = false;
\r
39 Ext.extend(Ext.dd.DragSource, Ext.dd.DDProxy, {
\r
40 <div id="cfg-Ext.dd.DragSource-ddGroup"></div>/**
\r
41 * @cfg {String} ddGroup
\r
42 * A named drag drop group to which this object belongs. If a group is specified, then this object will only
\r
43 * interact with other drag drop objects in the same group (defaults to undefined).
\r
45 <div id="cfg-Ext.dd.DragSource-dropAllowed"></div>/**
\r
46 * @cfg {String} dropAllowed
\r
47 * The CSS class returned to the drag source when drop is allowed (defaults to "x-dd-drop-ok").
\r
49 dropAllowed : "x-dd-drop-ok",
\r
50 <div id="cfg-Ext.dd.DragSource-dropNotAllowed"></div>/**
\r
51 * @cfg {String} dropNotAllowed
\r
52 * The CSS class returned to the drag source when drop is not allowed (defaults to "x-dd-drop-nodrop").
\r
54 dropNotAllowed : "x-dd-drop-nodrop",
\r
56 <div id="method-Ext.dd.DragSource-getDragData"></div>/**
\r
57 * Returns the data object associated with this drag source
\r
58 * @return {Object} data An object containing arbitrary data
\r
60 getDragData : function(e){
\r
61 return this.dragData;
\r
65 onDragEnter : function(e, id){
\r
66 var target = Ext.dd.DragDropMgr.getDDById(id);
\r
67 this.cachedTarget = target;
\r
68 if(this.beforeDragEnter(target, e, id) !== false){
\r
69 if(target.isNotifyTarget){
\r
70 var status = target.notifyEnter(this, e, this.dragData);
\r
71 this.proxy.setStatus(status);
\r
73 this.proxy.setStatus(this.dropAllowed);
\r
76 if(this.afterDragEnter){
\r
77 <div id="method-Ext.dd.DragSource-afterDragEnter"></div>/**
\r
78 * An empty function by default, but provided so that you can perform a custom action
\r
79 * when the dragged item enters the drop target by providing an implementation.
\r
80 * @param {Ext.dd.DragDrop} target The drop target
\r
81 * @param {Event} e The event object
\r
82 * @param {String} id The id of the dragged element
\r
83 * @method afterDragEnter
\r
85 this.afterDragEnter(target, e, id);
\r
90 <div id="method-Ext.dd.DragSource-beforeDragEnter"></div>/**
\r
91 * An empty function by default, but provided so that you can perform a custom action
\r
92 * before the dragged item enters the drop target and optionally cancel the onDragEnter.
\r
93 * @param {Ext.dd.DragDrop} target The drop target
\r
94 * @param {Event} e The event object
\r
95 * @param {String} id The id of the dragged element
\r
96 * @return {Boolean} isValid True if the drag event is valid, else false to cancel
\r
98 beforeDragEnter : function(target, e, id){
\r
103 alignElWithMouse: function() {
\r
104 Ext.dd.DragSource.superclass.alignElWithMouse.apply(this, arguments);
\r
109 onDragOver : function(e, id){
\r
110 var target = this.cachedTarget || Ext.dd.DragDropMgr.getDDById(id);
\r
111 if(this.beforeDragOver(target, e, id) !== false){
\r
112 if(target.isNotifyTarget){
\r
113 var status = target.notifyOver(this, e, this.dragData);
\r
114 this.proxy.setStatus(status);
\r
117 if(this.afterDragOver){
\r
118 <div id="method-Ext.dd.DragSource-afterDragOver"></div>/**
\r
119 * An empty function by default, but provided so that you can perform a custom action
\r
120 * while the dragged item is over the drop target by providing an implementation.
\r
121 * @param {Ext.dd.DragDrop} target The drop target
\r
122 * @param {Event} e The event object
\r
123 * @param {String} id The id of the dragged element
\r
124 * @method afterDragOver
\r
126 this.afterDragOver(target, e, id);
\r
131 <div id="method-Ext.dd.DragSource-beforeDragOver"></div>/**
\r
132 * An empty function by default, but provided so that you can perform a custom action
\r
133 * while the dragged item is over the drop target and optionally cancel the onDragOver.
\r
134 * @param {Ext.dd.DragDrop} target The drop target
\r
135 * @param {Event} e The event object
\r
136 * @param {String} id The id of the dragged element
\r
137 * @return {Boolean} isValid True if the drag event is valid, else false to cancel
\r
139 beforeDragOver : function(target, e, id){
\r
144 onDragOut : function(e, id){
\r
145 var target = this.cachedTarget || Ext.dd.DragDropMgr.getDDById(id);
\r
146 if(this.beforeDragOut(target, e, id) !== false){
\r
147 if(target.isNotifyTarget){
\r
148 target.notifyOut(this, e, this.dragData);
\r
150 this.proxy.reset();
\r
151 if(this.afterDragOut){
\r
152 <div id="method-Ext.dd.DragSource-afterDragOut"></div>/**
\r
153 * An empty function by default, but provided so that you can perform a custom action
\r
154 * after the dragged item is dragged out of the target without dropping.
\r
155 * @param {Ext.dd.DragDrop} target The drop target
\r
156 * @param {Event} e The event object
\r
157 * @param {String} id The id of the dragged element
\r
158 * @method afterDragOut
\r
160 this.afterDragOut(target, e, id);
\r
163 this.cachedTarget = null;
\r
166 <div id="method-Ext.dd.DragSource-beforeDragOut"></div>/**
\r
167 * An empty function by default, but provided so that you can perform a custom action before the dragged
\r
168 * item is dragged out of the target without dropping, and optionally cancel the onDragOut.
\r
169 * @param {Ext.dd.DragDrop} target The drop target
\r
170 * @param {Event} e The event object
\r
171 * @param {String} id The id of the dragged element
\r
172 * @return {Boolean} isValid True if the drag event is valid, else false to cancel
\r
174 beforeDragOut : function(target, e, id){
\r
179 onDragDrop : function(e, id){
\r
180 var target = this.cachedTarget || Ext.dd.DragDropMgr.getDDById(id);
\r
181 if(this.beforeDragDrop(target, e, id) !== false){
\r
182 if(target.isNotifyTarget){
\r
183 if(target.notifyDrop(this, e, this.dragData)){ // valid drop?
\r
184 this.onValidDrop(target, e, id);
\r
186 this.onInvalidDrop(target, e, id);
\r
189 this.onValidDrop(target, e, id);
\r
192 if(this.afterDragDrop){
\r
193 <div id="method-Ext.dd.DragSource-afterDragDrop"></div>/**
\r
194 * An empty function by default, but provided so that you can perform a custom action
\r
195 * after a valid drag drop has occurred by providing an implementation.
\r
196 * @param {Ext.dd.DragDrop} target The drop target
\r
197 * @param {Event} e The event object
\r
198 * @param {String} id The id of the dropped element
\r
199 * @method afterDragDrop
\r
201 this.afterDragDrop(target, e, id);
\r
204 delete this.cachedTarget;
\r
207 <div id="method-Ext.dd.DragSource-beforeDragDrop"></div>/**
\r
208 * An empty function by default, but provided so that you can perform a custom action before the dragged
\r
209 * item is dropped onto the target and optionally cancel the onDragDrop.
\r
210 * @param {Ext.dd.DragDrop} target The drop target
\r
211 * @param {Event} e The event object
\r
212 * @param {String} id The id of the dragged element
\r
213 * @return {Boolean} isValid True if the drag drop event is valid, else false to cancel
\r
215 beforeDragDrop : function(target, e, id){
\r
220 onValidDrop : function(target, e, id){
\r
222 if(this.afterValidDrop){
\r
224 * An empty function by default, but provided so that you can perform a custom action
\r
225 * after a valid drop has occurred by providing an implementation.
\r
226 * @param {Object} target The target DD
\r
227 * @param {Event} e The event object
\r
228 * @param {String} id The id of the dropped element
\r
229 * @method afterInvalidDrop
\r
231 this.afterValidDrop(target, e, id);
\r
236 getRepairXY : function(e, data){
\r
237 return this.el.getXY();
\r
241 onInvalidDrop : function(target, e, id){
\r
242 this.beforeInvalidDrop(target, e, id);
\r
243 if(this.cachedTarget){
\r
244 if(this.cachedTarget.isNotifyTarget){
\r
245 this.cachedTarget.notifyOut(this, e, this.dragData);
\r
247 this.cacheTarget = null;
\r
249 this.proxy.repair(this.getRepairXY(e, this.dragData), this.afterRepair, this);
\r
251 if(this.afterInvalidDrop){
\r
252 <div id="method-Ext.dd.DragSource-afterInvalidDrop"></div>/**
\r
253 * An empty function by default, but provided so that you can perform a custom action
\r
254 * after an invalid drop has occurred by providing an implementation.
\r
255 * @param {Event} e The event object
\r
256 * @param {String} id The id of the dropped element
\r
257 * @method afterInvalidDrop
\r
259 this.afterInvalidDrop(e, id);
\r
264 afterRepair : function(){
\r
266 this.el.highlight(this.hlColor || "c3daf9");
\r
268 this.dragging = false;
\r
271 <div id="method-Ext.dd.DragSource-beforeInvalidDrop"></div>/**
\r
272 * An empty function by default, but provided so that you can perform a custom action after an invalid
\r
273 * drop has occurred.
\r
274 * @param {Ext.dd.DragDrop} target The drop target
\r
275 * @param {Event} e The event object
\r
276 * @param {String} id The id of the dragged element
\r
277 * @return {Boolean} isValid True if the invalid drop should proceed, else false to cancel
\r
279 beforeInvalidDrop : function(target, e, id){
\r
284 handleMouseDown : function(e){
\r
285 if(this.dragging) {
\r
288 var data = this.getDragData(e);
\r
289 if(data && this.onBeforeDrag(data, e) !== false){
\r
290 this.dragData = data;
\r
292 Ext.dd.DragSource.superclass.handleMouseDown.apply(this, arguments);
\r
296 <div id="method-Ext.dd.DragSource-onBeforeDrag"></div>/**
\r
297 * An empty function by default, but provided so that you can perform a custom action before the initial
\r
298 * drag event begins and optionally cancel it.
\r
299 * @param {Object} data An object containing arbitrary data to be shared with drop targets
\r
300 * @param {Event} e The event object
\r
301 * @return {Boolean} isValid True if the drag event is valid, else false to cancel
\r
303 onBeforeDrag : function(data, e){
\r
307 <div id="method-Ext.dd.DragSource-onStartDrag"></div>/**
\r
308 * An empty function by default, but provided so that you can perform a custom action once the initial
\r
309 * drag event has begun. The drag cannot be canceled from this function.
\r
310 * @param {Number} x The x position of the click on the dragged object
\r
311 * @param {Number} y The y position of the click on the dragged object
\r
313 onStartDrag : Ext.emptyFn,
\r
315 // private override
\r
316 startDrag : function(x, y){
\r
317 this.proxy.reset();
\r
318 this.dragging = true;
\r
319 this.proxy.update("");
\r
320 this.onInitDrag(x, y);
\r
325 onInitDrag : function(x, y){
\r
326 var clone = this.el.dom.cloneNode(true);
\r
327 clone.id = Ext.id(); // prevent duplicate ids
\r
328 this.proxy.update(clone);
\r
329 this.onStartDrag(x, y);
\r
333 <div id="method-Ext.dd.DragSource-getProxy"></div>/**
\r
334 * Returns the drag source's underlying {@link Ext.dd.StatusProxy}
\r
335 * @return {Ext.dd.StatusProxy} proxy The StatusProxy
\r
337 getProxy : function(){
\r
338 return this.proxy;
\r
341 <div id="method-Ext.dd.DragSource-hideProxy"></div>/**
\r
342 * Hides the drag source's {@link Ext.dd.StatusProxy}
\r
344 hideProxy : function(){
\r
345 this.proxy.hide();
\r
346 this.proxy.reset(true);
\r
347 this.dragging = false;
\r
351 triggerCacheRefresh : function(){
\r
352 Ext.dd.DDM.refreshCache(this.groups);
\r
355 // private - override to prevent hiding
\r
356 b4EndDrag: function(e) {
\r
359 // private - override to prevent moving
\r
360 endDrag : function(e){
\r
361 this.onEndDrag(this.dragData, e);
\r
365 onEndDrag : function(data, e){
\r
368 // private - pin to cursor
\r
369 autoOffset : function(x, y) {
\r
370 this.setDelta(-12, -20);
\r