1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-dd.DragTracker'>/**
2 </span> * @class Ext.dd.DragTracker
3 * A DragTracker listens for drag events on an Element and fires events at the start and end of the drag,
4 * as well as during the drag. This is useful for components such as {@link Ext.slider.Multi}, where there is
5 * an element that can be dragged around to change the Slider's value.
6 * DragTracker provides a series of template methods that should be overridden to provide functionality
7 * in response to detected drag operations. These are onBeforeStart, onStart, onDrag and onEnd.
8 * See {@link Ext.slider.Multi}'s initEvents function for an example implementation.
10 Ext.define('Ext.dd.DragTracker', {
12 uses: ['Ext.util.Region'],
15 observable: 'Ext.util.Observable'
18 <span id='Ext-dd.DragTracker-property-active'> /**
19 </span> * @property active
21 * Read-only property indicated whether the user is currently dragging this
26 <span id='Ext-dd.DragTracker-property-dragTarget'> /**
27 </span> * @property dragTarget
29 * <p><b>Only valid during drag operations. Read-only.</b></p>
30 * <p>The element being dragged.</p>
31 * <p>If the {@link #delegate} option is used, this will be the delegate element which was mousedowned.</p>
34 <span id='Ext-dd.DragTracker-cfg-trackOver'> /**
35 </span> * @cfg {Boolean} trackOver
36 * <p>Defaults to <code>false</code>. Set to true to fire mouseover and mouseout events when the mouse enters or leaves the target element.</p>
37 * <p>This is implicitly set when an {@link #overCls} is specified.</p>
38 * <b>If the {@link #delegate} option is used, these events fire only when a delegate element is entered of left.</b>.
42 <span id='Ext-dd.DragTracker-cfg-overCls'> /**
43 </span> * @cfg {String} overCls
44 * <p>A CSS class to add to the DragTracker's target element when the element (or, if the {@link #delegate} option is used,
45 * when a delegate element) is mouseovered.</p>
46 * <b>If the {@link #delegate} option is used, these events fire only when a delegate element is entered of left.</b>.
49 <span id='Ext-dd.DragTracker-cfg-constrainTo'> /**
50 </span> * @cfg {Ext.util.Region/Element} constrainTo
51 * <p>A {@link Ext.util.Region Region} (Or an element from which a Region measurement will be read) which is used to constrain
52 * the result of the {@link #getOffset} call.</p>
53 * <p>This may be set any time during the DragTracker's lifecycle to set a dynamic constraining region.</p>
56 <span id='Ext-dd.DragTracker-cfg-tolerance'> /**
57 </span> * @cfg {Number} tolerance
58 * Number of pixels the drag target must be moved before dragging is
59 * considered to have started. Defaults to <code>5</code>.
63 <span id='Ext-dd.DragTracker-cfg-autoStart'> /**
64 </span> * @cfg {Boolean/Number} autoStart
65 * Defaults to <code>false</code>. Specify <code>true</code> to defer trigger start by 1000 ms.
66 * Specify a Number for the number of milliseconds to defer trigger start.
70 <span id='Ext-dd.DragTracker-cfg-delegate'> /**
71 </span> * @cfg {String} delegate
72 * Optional. <p>A {@link Ext.DomQuery DomQuery} selector which identifies child elements within the DragTracker's encapsulating
73 * Element which are the tracked elements. This limits tracking to only begin when the matching elements are mousedowned.</p>
74 * <p>This may also be a specific child element within the DragTracker's encapsulating element to use as the tracked element.</p>
77 <span id='Ext-dd.DragTracker-cfg-preventDefault'> /**
78 </span> * @cfg {Boolean} preventDefault
79 * Specify <code>false</code> to enable default actions on onMouseDown events. Defaults to <code>true</code>.
82 <span id='Ext-dd.DragTracker-cfg-stopEvent'> /**
83 </span> * @cfg {Boolean} stopEvent
84 * Specify <code>true</code> to stop the <code>mousedown</code> event from bubbling to outer listeners from the target element (or its delegates). Defaults to <code>false</code>.
87 constructor : function(config){
88 Ext.apply(this, config);
90 <span id='Ext-dd.DragTracker-event-mouseover'> /**
91 </span> * @event mouseover <p><b>Only available when {@link #trackOver} is <code>true</code></b></p>
92 * <p>Fires when the mouse enters the DragTracker's target element (or if {@link #delegate} is
93 * used, when the mouse enters a delegate element).</p>
94 * @param {Object} this
95 * @param {Object} e event object
96 * @param {HtmlElement} target The element mouseovered.
100 <span id='Ext-dd.DragTracker-event-mouseout'> /**
101 </span> * @event mouseout <p><b>Only available when {@link #trackOver} is <code>true</code></b></p>
102 * <p>Fires when the mouse exits the DragTracker's target element (or if {@link #delegate} is
103 * used, when the mouse exits a delegate element).</p>
104 * @param {Object} this
105 * @param {Object} e event object
109 <span id='Ext-dd.DragTracker-event-mousedown'> /**
110 </span> * @event mousedown <p>Fires when the mouse button is pressed down, but before a drag operation begins. The
111 * drag operation begins after either the mouse has been moved by {@link #tolerance} pixels, or after
112 * the {@link #autoStart} timer fires.</p>
113 * <p>Return false to veto the drag operation.</p>
114 * @param {Object} this
115 * @param {Object} e event object
119 <span id='Ext-dd.DragTracker-event-mouseup'> /**
120 </span> * @event mouseup
121 * @param {Object} this
122 * @param {Object} e event object
126 <span id='Ext-dd.DragTracker-event-mousemove'> /**
127 </span> * @event mousemove Fired when the mouse is moved. Returning false cancels the drag operation.
128 * @param {Object} this
129 * @param {Object} e event object
133 <span id='Ext-dd.DragTracker-event-dragstart'> /**
134 </span> * @event dragstart
135 * @param {Object} this
136 * @param {Object} e event object
140 <span id='Ext-dd.DragTracker-event-dragend'> /**
141 </span> * @event dragend
142 * @param {Object} this
143 * @param {Object} e event object
147 <span id='Ext-dd.DragTracker-event-drag'> /**
148 </span> * @event drag
149 * @param {Object} this
150 * @param {Object} e event object
155 this.dragRegion = Ext.create('Ext.util.Region', 0,0,0,0);
158 this.initEl(this.el);
161 // Dont pass the config so that it is not applied to 'this' again
162 this.mixins.observable.constructor.call(this);
169 <span id='Ext-dd.DragTracker-method-initEl'> /**
170 </span> * Initializes the DragTracker on a given element.
171 * @param {Ext.core.Element/HTMLElement} el The element
173 initEl: function(el) {
174 this.el = Ext.get(el);
176 // The delegate option may also be an element on which to listen
177 this.handle = Ext.get(this.delegate);
179 // If delegate specified an actual element to listen on, we do not use the delegate listener option
180 this.delegate = this.handle ? undefined : this.delegate;
183 this.handle = this.el;
186 // Add a mousedown listener which reacts only on the elements targeted by the delegate config.
187 // We process mousedown to begin tracking.
188 this.mon(this.handle, {
189 mousedown: this.onMouseDown,
190 delegate: this.delegate,
194 // If configured to do so, track mouse entry and exit into the target (or delegate).
195 // The mouseover and mouseout CANNOT be replaced with mouseenter and mouseleave
196 // because delegate cannot work with those pseudoevents. Entry/exit checking is done in the handler.
197 if (this.trackOver || this.overCls) {
198 this.mon(this.handle, {
199 mouseover: this.onMouseOver,
200 mouseout: this.onMouseOut,
201 delegate: this.delegate,
207 disable: function() {
208 this.disabled = true;
212 this.disabled = false;
215 destroy : function() {
216 this.clearListeners();
220 // When the pointer enters a tracking element, fire a mouseover if the mouse entered from outside.
221 // This is mouseenter functionality, but we cannot use mouseenter because we are using "delegate" to filter mouse targets
222 onMouseOver: function(e, target) {
225 if (Ext.EventManager.contains(e) || me.delegate) {
226 me.mouseIsOut = false;
228 me.el.addCls(me.overCls);
230 me.fireEvent('mouseover', me, e, me.delegate ? e.getTarget(me.delegate, target) : me.handle);
235 // When the pointer exits a tracking element, fire a mouseout.
236 // This is mouseleave functionality, but we cannot use mouseleave because we are using "delegate" to filter mouse targets
237 onMouseOut: function(e) {
238 if (this.mouseIsDown) {
239 this.mouseIsOut = true;
242 this.el.removeCls(this.overCls);
244 this.fireEvent('mouseout', this, e);
248 onMouseDown: function(e, target){
249 // If this is disabled, or the mousedown has been processed by an upstream DragTracker, return
250 if (this.disabled ||e.dragTracked) {
254 // This information should be available in mousedown listener and onBeforeStart implementations
255 this.dragTarget = this.delegate ? target : this.handle.dom;
256 this.startXY = this.lastXY = e.getXY();
257 this.startRegion = Ext.fly(this.dragTarget).getRegion();
259 if (this.fireEvent('mousedown', this, e) !== false && this.onBeforeStart(e) !== false) {
261 // Track when the mouse is down so that mouseouts while the mouse is down are not processed.
262 // The onMouseOut method will only ever be called after mouseup.
263 this.mouseIsDown = true;
265 // Flag for downstream DragTracker instances that the mouse is being tracked.
266 e.dragTracked = true;
268 if (this.preventDefault !== false) {
273 mouseup: this.onMouseUp,
274 mousemove: this.onMouseMove,
275 selectstart: this.stopSelect
277 if (this.autoStart) {
278 this.timer = Ext.defer(this.triggerStart, this.autoStart === true ? 1000 : this.autoStart, this, [e]);
283 onMouseMove: function(e, target){
284 // BrowserBug: IE hack to see if button was released outside of window.
285 // Needed in IE6-9 in quirks and strictmode
286 if (this.active && Ext.isIE && !e.browserEvent.button) {
298 if (Math.max(Math.abs(s[0]-xy[0]), Math.abs(s[1]-xy[1])) > this.tolerance) {
299 this.triggerStart(e);
305 // Returning false from a mousemove listener deactivates
306 if (this.fireEvent('mousemove', this, e) === false) {
310 this.fireEvent('drag', this, e);
314 onMouseUp: function(e) {
315 // Clear the flag which ensures onMouseOut fires only after the mouse button
316 // is lifted if the mouseout happens *during* a drag.
317 this.mouseIsDown = false;
319 // Remove flag from event singleton
320 delete e.dragTracked;
322 // If we mouseouted the el *during* the drag, the onMouseOut method will not have fired. Ensure that it gets processed.
323 if (this.mouseIsOut) {
324 this.mouseIsOut = false;
328 this.fireEvent('mouseup', this, e);
332 <span id='Ext-dd.DragTracker-method-endDrag'> /**
334 * Stop the drag operation, and remove active mouse listeners.
336 endDrag: function(e) {
337 var doc = Ext.getDoc(),
338 wasActive = this.active;
340 doc.un('mousemove', this.onMouseMove, this);
341 doc.un('mouseup', this.onMouseUp, this);
342 doc.un('selectstart', this.stopSelect, this);
347 this.fireEvent('dragend', this, e);
349 // Private property calculated when first required and only cached during a drag
350 delete this._constrainRegion;
353 triggerStart: function(e) {
357 this.fireEvent('dragstart', this, e);
360 clearStart : function() {
362 clearTimeout(this.timer);
367 stopSelect : function(e) {
372 <span id='Ext-dd.DragTracker-method-onBeforeStart'> /**
373 </span> * Template method which should be overridden by each DragTracker instance. Called when the user first clicks and
374 * holds the mouse button down. Return false to disallow the drag
375 * @param {Ext.EventObject} e The event object
377 onBeforeStart : function(e) {
381 <span id='Ext-dd.DragTracker-method-onStart'> /**
382 </span> * Template method which should be overridden by each DragTracker instance. Called when a drag operation starts
383 * (e.g. the user has moved the tracked element beyond the specified tolerance)
384 * @param {Ext.EventObject} e The event object
386 onStart : function(xy) {
390 <span id='Ext-dd.DragTracker-method-onDrag'> /**
391 </span> * Template method which should be overridden by each DragTracker instance. Called whenever a drag has been detected.
392 * @param {Ext.EventObject} e The event object
394 onDrag : function(e) {
398 <span id='Ext-dd.DragTracker-method-onEnd'> /**
399 </span> * Template method which should be overridden by each DragTracker instance. Called when a drag operation has been completed
400 * (e.g. the user clicked and held the mouse down, dragged the element and then released the mouse button)
401 * @param {Ext.EventObject} e The event object
403 onEnd : function(e) {
407 <span id='Ext-dd.DragTracker-method-getDragTarget'> /**
408 </span> * </p>Returns the drag target. This is usually the DragTracker's encapsulating element.</p>
409 * <p>If the {@link #delegate} option is being used, this may be a child element which matches the
410 * {@link #delegate} selector.</p>
411 * @return {Ext.core.Element} The element currently being tracked.
413 getDragTarget : function(){
414 return this.dragTarget;
417 <span id='Ext-dd.DragTracker-method-getDragCt'> /**
419 * @returns {Element} The DragTracker's encapsulating element.
421 getDragCt : function(){
425 <span id='Ext-dd.DragTracker-method-getConstrainRegion'> /**
427 * Return the Region into which the drag operation is constrained.
428 * Either the XY pointer itself can be constrained, or the dragTarget element
429 * The private property _constrainRegion is cached until onMouseUp
431 getConstrainRegion: function() {
432 if (this.constrainTo) {
433 if (this.constrainTo instanceof Ext.util.Region) {
434 return this.constrainTo;
436 if (!this._constrainRegion) {
437 this._constrainRegion = Ext.fly(this.constrainTo).getViewRegion();
440 if (!this._constrainRegion) {
441 this._constrainRegion = this.getDragCt().getViewRegion();
444 return this._constrainRegion;
447 getXY : function(constrain){
448 return constrain ? this.constrainModes[constrain].call(this, this.lastXY) : this.lastXY;
451 <span id='Ext-dd.DragTracker-method-getOffset'> /**
452 </span> * <p>Returns the X, Y offset of the current mouse position from the mousedown point.</p>
453 * <p>This method may optionally constrain the real offset values, and returns a point coerced in one
454 * of two modes:</p><ul>
455 * <li><code>point</code><div class="sub-desc">The current mouse position is coerced into the
456 * {@link #constrainRegion}, and the resulting position is returned.</div></li>
457 * <li><code>dragTarget</code><div class="sub-desc">The new {@link Ext.util.Region Region} of the
458 * {@link #getDragTarget dragTarget} is calculated based upon the current mouse position, and then
459 * coerced into the {@link #constrainRegion}. The returned mouse position is then adjusted by the
460 * same delta as was used to coerce the region.</div></li>
462 * @param constrainMode {String} Optional. If omitted the true mouse position is returned. May be passed
463 * as <code>'point'</code> or <code>'dragTarget'. See above.</code>.
464 * @returns {Array} The <code>X, Y</code> offset from the mousedown point, optionally constrained.
466 getOffset : function(constrain){
467 var xy = this.getXY(constrain),
470 return [xy[0]-s[0], xy[1]-s[1]];
474 // Constrain the passed point to within the constrain region
475 point: function(xy) {
476 var dr = this.dragRegion,
477 constrainTo = this.getConstrainRegion();
484 dr.x = dr.left = dr[0] = dr.right = xy[0];
485 dr.y = dr.top = dr[1] = dr.bottom = xy[1];
486 dr.constrainTo(constrainTo);
488 return [dr.left, dr.top];
491 // Constrain the dragTarget to within the constrain region. Return the passed xy adjusted by the same delta.
492 dragTarget: function(xy) {
493 var s = this.startXY,
494 dr = this.startRegion.copy(),
495 constrainTo = this.getConstrainRegion(),
503 // See where the passed XY would put the dragTarget if translated by the unconstrained offset.
504 // If it overflows, we constrain the passed XY to bring the potential
505 // region back within the boundary.
506 dr.translateBy.apply(dr, [xy[0]-s[0], xy[1]-s[1]]);
508 // Constrain the X coordinate by however much the dragTarget overflows
509 if (dr.right > constrainTo.right) {
510 xy[0] += adjust = (constrainTo.right - dr.right); // overflowed the right
513 if (dr.left < constrainTo.left) {
514 xy[0] += (constrainTo.left - dr.left); // overflowed the left
517 // Constrain the X coordinate by however much the dragTarget overflows
518 if (dr.bottom > constrainTo.bottom) {
519 xy[1] += adjust = (constrainTo.bottom - dr.bottom); // overflowed the bottom
522 if (dr.top < constrainTo.top) {
523 xy[1] += (constrainTo.top - dr.top); // overflowed the top
528 });</pre></pre></body></html>