PageRenderTime 24ms CodeModel.GetById 10ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/hippo/src/main/webapp/ext/src/dd/DragTracker.js

http://hdbc.googlecode.com/
JavaScript | 217 lines | 133 code | 30 blank | 54 comment | 14 complexity | 7d57cbf479fb97f1826830c425fb2d18 MD5 | raw file
  1/*!
  2 * Ext JS Library 3.0.0
  3 * Copyright(c) 2006-2009 Ext JS, LLC
  4 * licensing@extjs.com
  5 * http://www.extjs.com/license
  6 */
  7/**
  8 * @class Ext.dd.DragTracker
  9 * @extends Ext.util.Observable
 10 */
 11Ext.dd.DragTracker = function(config){
 12    Ext.apply(this, config);
 13    this.addEvents(
 14        /**
 15         * @event mousedown
 16         * @param {Object} this
 17         * @param {Object} e event object
 18         */
 19        'mousedown',
 20        /**
 21         * @event mouseup
 22         * @param {Object} this
 23         * @param {Object} e event object
 24         */
 25        'mouseup',
 26        /**
 27         * @event mousemove
 28         * @param {Object} this
 29         * @param {Object} e event object
 30         */
 31        'mousemove',
 32        /**
 33         * @event dragstart
 34         * @param {Object} this
 35         * @param {Object} startXY the page coordinates of the event
 36         */
 37        'dragstart',
 38        /**
 39         * @event dragend
 40         * @param {Object} this
 41         * @param {Object} e event object
 42         */
 43        'dragend',
 44        /**
 45         * @event drag
 46         * @param {Object} this
 47         * @param {Object} e event object
 48         */
 49        'drag'
 50    );
 51
 52    this.dragRegion = new Ext.lib.Region(0,0,0,0);
 53
 54    if(this.el){
 55        this.initEl(this.el);
 56    }
 57}
 58
 59Ext.extend(Ext.dd.DragTracker, Ext.util.Observable,  {
 60    /**
 61     * @cfg {Boolean} active
 62	 * Defaults to <tt>false</tt>.
 63	 */	
 64    active: false,
 65    /**
 66     * @cfg {Number} tolerance
 67	 * Defaults to <tt>5</tt>.
 68	 */	
 69    tolerance: 5,
 70    /**
 71     * @cfg {Boolean/Number} autoStart
 72	 * Defaults to <tt>false</tt>. Specify <tt>true</tt> to defer trigger start by 1000 ms.
 73	 * Specify a Number for the number of milliseconds to defer trigger start.
 74	 */	
 75    autoStart: false,
 76
 77    initEl: function(el){
 78        this.el = Ext.get(el);
 79        el.on('mousedown', this.onMouseDown, this,
 80                this.delegate ? {delegate: this.delegate} : undefined);
 81    },
 82
 83    destroy : function(){
 84        this.el.un('mousedown', this.onMouseDown, this);
 85    },
 86
 87    onMouseDown: function(e, target){
 88        if(this.fireEvent('mousedown', this, e) !== false && this.onBeforeStart(e) !== false){
 89            this.startXY = this.lastXY = e.getXY();
 90            this.dragTarget = this.delegate ? target : this.el.dom;
 91            if(this.preventDefault !== false){
 92                e.preventDefault();
 93            }
 94            var doc = Ext.getDoc();
 95            doc.on('mouseup', this.onMouseUp, this);
 96            doc.on('mousemove', this.onMouseMove, this);
 97            doc.on('selectstart', this.stopSelect, this);
 98            if(this.autoStart){
 99                this.timer = this.triggerStart.defer(this.autoStart === true ? 1000 : this.autoStart, this);
100            }
101        }
102    },
103
104    onMouseMove: function(e, target){
105        // HACK: IE hack to see if button was released outside of window. */
106        if(this.active && Ext.isIE && !e.browserEvent.button){
107            e.preventDefault();
108            this.onMouseUp(e);
109            return;
110        }
111
112        e.preventDefault();
113        var xy = e.getXY(), s = this.startXY;
114        this.lastXY = xy;
115        if(!this.active){
116            if(Math.abs(s[0]-xy[0]) > this.tolerance || Math.abs(s[1]-xy[1]) > this.tolerance){
117                this.triggerStart();
118            }else{
119                return;
120            }
121        }
122        this.fireEvent('mousemove', this, e);
123        this.onDrag(e);
124        this.fireEvent('drag', this, e);
125    },
126
127    onMouseUp: function(e){
128        var doc = Ext.getDoc();
129        doc.un('mousemove', this.onMouseMove, this);
130        doc.un('mouseup', this.onMouseUp, this);
131        doc.un('selectstart', this.stopSelect, this);
132        e.preventDefault();
133        this.clearStart();
134        var wasActive = this.active;
135        this.active = false;
136        delete this.elRegion;
137        this.fireEvent('mouseup', this, e);
138        if(wasActive){
139            this.onEnd(e);
140            this.fireEvent('dragend', this, e);
141        }
142    },
143
144    triggerStart: function(isTimer){
145        this.clearStart();
146        this.active = true;
147        this.onStart(this.startXY);
148        this.fireEvent('dragstart', this, this.startXY);
149    },
150
151    clearStart : function(){
152        if(this.timer){
153            clearTimeout(this.timer);
154            delete this.timer;
155        }
156    },
157
158    stopSelect : function(e){
159        e.stopEvent();
160        return false;
161    },
162
163    onBeforeStart : function(e){
164
165    },
166
167    onStart : function(xy){
168
169    },
170
171    onDrag : function(e){
172
173    },
174
175    onEnd : function(e){
176
177    },
178
179    getDragTarget : function(){
180        return this.dragTarget;
181    },
182
183    getDragCt : function(){
184        return this.el;
185    },
186
187    getXY : function(constrain){
188        return constrain ?
189               this.constrainModes[constrain].call(this, this.lastXY) : this.lastXY;
190    },
191
192    getOffset : function(constrain){
193        var xy = this.getXY(constrain);
194        var s = this.startXY;
195        return [s[0]-xy[0], s[1]-xy[1]];
196    },
197
198    constrainModes: {
199        'point' : function(xy){
200
201            if(!this.elRegion){
202                this.elRegion = this.getDragCt().getRegion();
203            }
204
205            var dr = this.dragRegion;
206
207            dr.left = xy[0];
208            dr.top = xy[1];
209            dr.right = xy[0];
210            dr.bottom = xy[1];
211
212            dr.constrainTo(this.elRegion);
213
214            return [dr.left, dr.top];
215        }
216    }
217});