3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
8 * @class Ext.dd.DragTracker
9 * @extends Ext.util.Observable
10 * A DragTracker listens for drag events on an Element and fires events at the start and end of the drag,
11 * as well as during the drag. This is useful for components such as {@link Ext.slider.MultiSlider}, where there is
12 * an element that can be dragged around to change the Slider's value.
13 * DragTracker provides a series of template methods that should be overridden to provide functionality
14 * in response to detected drag operations. These are onBeforeStart, onStart, onDrag and onEnd.
15 * See {@link Ext.slider.MultiSlider}'s initEvents function for an example implementation.
17 Ext.dd.DragTracker = Ext.extend(Ext.util.Observable, {
19 * @cfg {Boolean} active
20 * Defaults to <tt>false</tt>.
24 * @cfg {Number} tolerance
25 * Number of pixels the drag target must be moved before dragging is considered to have started. Defaults to <tt>5</tt>.
29 * @cfg {Boolean/Number} autoStart
30 * Defaults to <tt>false</tt>. Specify <tt>true</tt> to defer trigger start by 1000 ms.
31 * Specify a Number for the number of milliseconds to defer trigger start.
35 constructor : function(config){
36 Ext.apply(this, config);
40 * @param {Object} this
41 * @param {Object} e event object
46 * @param {Object} this
47 * @param {Object} e event object
52 * @param {Object} this
53 * @param {Object} e event object
58 * @param {Object} this
59 * @param {Object} e event object
64 * @param {Object} this
65 * @param {Object} e event object
70 * @param {Object} this
71 * @param {Object} e event object
76 this.dragRegion = new Ext.lib.Region(0,0,0,0);
81 Ext.dd.DragTracker.superclass.constructor.call(this, config);
85 this.el = Ext.get(el);
86 el.on('mousedown', this.onMouseDown, this,
87 this.delegate ? {delegate: this.delegate} : undefined);
91 this.el.un('mousedown', this.onMouseDown, this);
95 onMouseDown: function(e, target){
96 if(this.fireEvent('mousedown', this, e) !== false && this.onBeforeStart(e) !== false){
97 this.startXY = this.lastXY = e.getXY();
98 this.dragTarget = this.delegate ? target : this.el.dom;
99 if(this.preventDefault !== false){
104 mouseup: this.onMouseUp,
105 mousemove: this.onMouseMove,
106 selectstart: this.stopSelect
109 this.timer = this.triggerStart.defer(this.autoStart === true ? 1000 : this.autoStart, this, [e]);
114 onMouseMove: function(e, target){
115 // HACK: IE hack to see if button was released outside of window. */
116 if(this.active && Ext.isIE && !e.browserEvent.button){
123 var xy = e.getXY(), s = this.startXY;
126 if(Math.abs(s[0]-xy[0]) > this.tolerance || Math.abs(s[1]-xy[1]) > this.tolerance){
127 this.triggerStart(e);
132 this.fireEvent('mousemove', this, e);
134 this.fireEvent('drag', this, e);
137 onMouseUp: function(e) {
138 var doc = Ext.getDoc(),
139 wasActive = this.active;
141 doc.un('mousemove', this.onMouseMove, this);
142 doc.un('mouseup', this.onMouseUp, this);
143 doc.un('selectstart', this.stopSelect, this);
147 delete this.elRegion;
148 this.fireEvent('mouseup', this, e);
151 this.fireEvent('dragend', this, e);
155 triggerStart: function(e) {
159 this.fireEvent('dragstart', this, e);
162 clearStart : function() {
164 clearTimeout(this.timer);
169 stopSelect : function(e) {
175 * Template method which should be overridden by each DragTracker instance. Called when the user first clicks and
176 * holds the mouse button down. Return false to disallow the drag
177 * @param {Ext.EventObject} e The event object
179 onBeforeStart : function(e) {
184 * Template method which should be overridden by each DragTracker instance. Called when a drag operation starts
185 * (e.g. the user has moved the tracked element beyond the specified tolerance)
186 * @param {Ext.EventObject} e The event object
188 onStart : function(xy) {
193 * Template method which should be overridden by each DragTracker instance. Called whenever a drag has been detected.
194 * @param {Ext.EventObject} e The event object
196 onDrag : function(e) {
201 * Template method which should be overridden by each DragTracker instance. Called when a drag operation has been completed
202 * (e.g. the user clicked and held the mouse down, dragged the element and then released the mouse button)
203 * @param {Ext.EventObject} e The event object
205 onEnd : function(e) {
210 * Returns the drag target
211 * @return {Ext.Element} The element currently being tracked
213 getDragTarget : function(){
214 return this.dragTarget;
217 getDragCt : function(){
221 getXY : function(constrain){
223 this.constrainModes[constrain].call(this, this.lastXY) : this.lastXY;
226 getOffset : function(constrain){
227 var xy = this.getXY(constrain),
229 return [s[0]-xy[0], s[1]-xy[1]];
233 'point' : function(xy){
236 this.elRegion = this.getDragCt().getRegion();
239 var dr = this.dragRegion;
246 dr.constrainTo(this.elRegion);
248 return [dr.left, dr.top];