Upgrade to ExtJS 3.3.0 - Released 10/06/2010
[extjs.git] / docs / source / DragTracker.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.3.0
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.DragTracker"></div>/**
16  * @class Ext.dd.DragTracker
17  * @extends Ext.util.Observable
18  * A DragTracker listens for drag events on an Element and fires events at the start and end of the drag,
19  * as well as during the drag. This is useful for components such as {@link Ext.slider.MultiSlider}, where there is
20  * an element that can be dragged around to change the Slider's value.
21  * DragTracker provides a series of template methods that should be overridden to provide functionality
22  * in response to detected drag operations. These are onBeforeStart, onStart, onDrag and onEnd.
23  * See {@link Ext.slider.MultiSlider}'s initEvents function for an example implementation.
24  */
25 Ext.dd.DragTracker = Ext.extend(Ext.util.Observable,  {    
26     <div id="cfg-Ext.dd.DragTracker-active"></div>/**
27      * @cfg {Boolean} active
28          * Defaults to <tt>false</tt>.
29          */     
30     active: false,
31     <div id="cfg-Ext.dd.DragTracker-tolerance"></div>/**
32      * @cfg {Number} tolerance
33          * Number of pixels the drag target must be moved before dragging is considered to have started. Defaults to <tt>5</tt>.
34          */     
35     tolerance: 5,
36     <div id="cfg-Ext.dd.DragTracker-autoStart"></div>/**
37      * @cfg {Boolean/Number} autoStart
38          * Defaults to <tt>false</tt>. Specify <tt>true</tt> to defer trigger start by 1000 ms.
39          * Specify a Number for the number of milliseconds to defer trigger start.
40          */     
41     autoStart: false,
42     
43     constructor : function(config){
44         Ext.apply(this, config);
45             this.addEvents(
46                 <div id="event-Ext.dd.DragTracker-mousedown"></div>/**
47                  * @event mousedown
48                  * @param {Object} this
49                  * @param {Object} e event object
50                  */
51                 'mousedown',
52                 <div id="event-Ext.dd.DragTracker-mouseup"></div>/**
53                  * @event mouseup
54                  * @param {Object} this
55                  * @param {Object} e event object
56                  */
57                 'mouseup',
58                 <div id="event-Ext.dd.DragTracker-mousemove"></div>/**
59                  * @event mousemove
60                  * @param {Object} this
61                  * @param {Object} e event object
62                  */
63                 'mousemove',
64                 <div id="event-Ext.dd.DragTracker-dragstart"></div>/**
65                  * @event dragstart
66                  * @param {Object} this
67                  * @param {Object} e event object
68                  */
69                 'dragstart',
70                 <div id="event-Ext.dd.DragTracker-dragend"></div>/**
71                  * @event dragend
72                  * @param {Object} this
73                  * @param {Object} e event object
74                  */
75                 'dragend',
76                 <div id="event-Ext.dd.DragTracker-drag"></div>/**
77                  * @event drag
78                  * @param {Object} this
79                  * @param {Object} e event object
80                  */
81                 'drag'
82             );
83         
84             this.dragRegion = new Ext.lib.Region(0,0,0,0);
85         
86             if(this.el){
87                 this.initEl(this.el);
88             }
89         Ext.dd.DragTracker.superclass.constructor.call(this, config);
90     },
91
92     initEl: function(el){
93         this.el = Ext.get(el);
94         el.on('mousedown', this.onMouseDown, this,
95                 this.delegate ? {delegate: this.delegate} : undefined);
96     },
97
98     destroy : function(){
99         this.el.un('mousedown', this.onMouseDown, this);
100         delete this.el;
101     },
102
103     onMouseDown: function(e, target){
104         if(this.fireEvent('mousedown', this, e) !== false && this.onBeforeStart(e) !== false){
105             this.startXY = this.lastXY = e.getXY();
106             this.dragTarget = this.delegate ? target : this.el.dom;
107             if(this.preventDefault !== false){
108                 e.preventDefault();
109             }
110             Ext.getDoc().on({
111                 scope: this,
112                 mouseup: this.onMouseUp,
113                 mousemove: this.onMouseMove,
114                 selectstart: this.stopSelect
115             });
116             if(this.autoStart){
117                 this.timer = this.triggerStart.defer(this.autoStart === true ? 1000 : this.autoStart, this, [e]);
118             }
119         }
120     },
121
122     onMouseMove: function(e, target){
123         // HACK: IE hack to see if button was released outside of window. */
124         if(this.active && Ext.isIE && !e.browserEvent.button){
125             e.preventDefault();
126             this.onMouseUp(e);
127             return;
128         }
129
130         e.preventDefault();
131         var xy = e.getXY(), s = this.startXY;
132         this.lastXY = xy;
133         if(!this.active){
134             if(Math.abs(s[0]-xy[0]) > this.tolerance || Math.abs(s[1]-xy[1]) > this.tolerance){
135                 this.triggerStart(e);
136             }else{
137                 return;
138             }
139         }
140         this.fireEvent('mousemove', this, e);
141         this.onDrag(e);
142         this.fireEvent('drag', this, e);
143     },
144
145     onMouseUp: function(e) {
146         var doc = Ext.getDoc(),
147             wasActive = this.active;
148             
149         doc.un('mousemove', this.onMouseMove, this);
150         doc.un('mouseup', this.onMouseUp, this);
151         doc.un('selectstart', this.stopSelect, this);
152         e.preventDefault();
153         this.clearStart();
154         this.active = false;
155         delete this.elRegion;
156         this.fireEvent('mouseup', this, e);
157         if(wasActive){
158             this.onEnd(e);
159             this.fireEvent('dragend', this, e);
160         }
161     },
162
163     triggerStart: function(e) {
164         this.clearStart();
165         this.active = true;
166         this.onStart(e);
167         this.fireEvent('dragstart', this, e);
168     },
169
170     clearStart : function() {
171         if(this.timer){
172             clearTimeout(this.timer);
173             delete this.timer;
174         }
175     },
176
177     stopSelect : function(e) {
178         e.stopEvent();
179         return false;
180     },
181     
182     <div id="method-Ext.dd.DragTracker-onBeforeStart"></div>/**
183      * Template method which should be overridden by each DragTracker instance. Called when the user first clicks and
184      * holds the mouse button down. Return false to disallow the drag
185      * @param {Ext.EventObject} e The event object
186      */
187     onBeforeStart : function(e) {
188
189     },
190
191     <div id="method-Ext.dd.DragTracker-onStart"></div>/**
192      * Template method which should be overridden by each DragTracker instance. Called when a drag operation starts
193      * (e.g. the user has moved the tracked element beyond the specified tolerance)
194      * @param {Ext.EventObject} e The event object
195      */
196     onStart : function(xy) {
197
198     },
199
200     <div id="method-Ext.dd.DragTracker-onDrag"></div>/**
201      * Template method which should be overridden by each DragTracker instance. Called whenever a drag has been detected.
202      * @param {Ext.EventObject} e The event object
203      */
204     onDrag : function(e) {
205
206     },
207
208     <div id="method-Ext.dd.DragTracker-onEnd"></div>/**
209      * Template method which should be overridden by each DragTracker instance. Called when a drag operation has been completed
210      * (e.g. the user clicked and held the mouse down, dragged the element and then released the mouse button)
211      * @param {Ext.EventObject} e The event object
212      */
213     onEnd : function(e) {
214
215     },
216
217     <div id="method-Ext.dd.DragTracker-getDragTarget"></div>/**
218      * Returns the drag target
219      * @return {Ext.Element} The element currently being tracked
220      */
221     getDragTarget : function(){
222         return this.dragTarget;
223     },
224
225     getDragCt : function(){
226         return this.el;
227     },
228
229     getXY : function(constrain){
230         return constrain ?
231                this.constrainModes[constrain].call(this, this.lastXY) : this.lastXY;
232     },
233
234     getOffset : function(constrain){
235         var xy = this.getXY(constrain),
236             s = this.startXY;
237         return [s[0]-xy[0], s[1]-xy[1]];
238     },
239
240     constrainModes: {
241         'point' : function(xy){
242
243             if(!this.elRegion){
244                 this.elRegion = this.getDragCt().getRegion();
245             }
246
247             var dr = this.dragRegion;
248
249             dr.left = xy[0];
250             dr.top = xy[1];
251             dr.right = xy[0];
252             dr.bottom = xy[1];
253
254             dr.constrainTo(this.elRegion);
255
256             return [dr.left, dr.top];
257         }
258     }
259 });</pre>    
260 </body>
261 </html>