4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-dd-DragTracker'>/**
19 </span> * @class Ext.dd.DragTracker
20 * A DragTracker listens for drag events on an Element and fires events at the start and end of the drag,
21 * as well as during the drag. This is useful for components such as {@link Ext.slider.Multi}, where there is
22 * an element that can be dragged around to change the Slider's value.
23 * DragTracker provides a series of template methods that should be overridden to provide functionality
24 * in response to detected drag operations. These are onBeforeStart, onStart, onDrag and onEnd.
25 * See {@link Ext.slider.Multi}'s initEvents function for an example implementation.
27 Ext.define('Ext.dd.DragTracker', {
29 uses: ['Ext.util.Region'],
32 observable: 'Ext.util.Observable'
35 <span id='Ext-dd-DragTracker-property-active'> /**
36 </span> * @property {Boolean} active
37 * Read-only property indicated whether the user is currently dragging this
42 <span id='Ext-dd-DragTracker-property-dragTarget'> /**
43 </span> * @property {HTMLElement} dragTarget
44 * <p><b>Only valid during drag operations. Read-only.</b></p>
45 * <p>The element being dragged.</p>
46 * <p>If the {@link #delegate} option is used, this will be the delegate element which was mousedowned.</p>
49 <span id='Ext-dd-DragTracker-cfg-trackOver'> /**
50 </span> * @cfg {Boolean} trackOver
51 * <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>
52 * <p>This is implicitly set when an {@link #overCls} is specified.</p>
53 * <b>If the {@link #delegate} option is used, these events fire only when a delegate element is entered of left.</b>.
57 <span id='Ext-dd-DragTracker-cfg-overCls'> /**
58 </span> * @cfg {String} overCls
59 * <p>A CSS class to add to the DragTracker's target element when the element (or, if the {@link #delegate} option is used,
60 * when a delegate element) is mouseovered.</p>
61 * <b>If the {@link #delegate} option is used, these events fire only when a delegate element is entered of left.</b>.
64 <span id='Ext-dd-DragTracker-cfg-constrainTo'> /**
65 </span> * @cfg {Ext.util.Region/Ext.Element} constrainTo
66 * <p>A {@link Ext.util.Region Region} (Or an element from which a Region measurement will be read) which is used to constrain
67 * the result of the {@link #getOffset} call.</p>
68 * <p>This may be set any time during the DragTracker's lifecycle to set a dynamic constraining region.</p>
71 <span id='Ext-dd-DragTracker-cfg-tolerance'> /**
72 </span> * @cfg {Number} tolerance
73 * Number of pixels the drag target must be moved before dragging is
74 * considered to have started. Defaults to <code>5</code>.
78 <span id='Ext-dd-DragTracker-cfg-autoStart'> /**
79 </span> * @cfg {Boolean/Number} autoStart
80 * Defaults to <code>false</code>. Specify <code>true</code> to defer trigger start by 1000 ms.
81 * Specify a Number for the number of milliseconds to defer trigger start.
85 <span id='Ext-dd-DragTracker-cfg-delegate'> /**
86 </span> * @cfg {String} delegate
87 * Optional. <p>A {@link Ext.DomQuery DomQuery} selector which identifies child elements within the DragTracker's encapsulating
88 * Element which are the tracked elements. This limits tracking to only begin when the matching elements are mousedowned.</p>
89 * <p>This may also be a specific child element within the DragTracker's encapsulating element to use as the tracked element.</p>
92 <span id='Ext-dd-DragTracker-cfg-preventDefault'> /**
93 </span> * @cfg {Boolean} preventDefault
94 * Specify <code>false</code> to enable default actions on onMouseDown events. Defaults to <code>true</code>.
97 <span id='Ext-dd-DragTracker-cfg-stopEvent'> /**
98 </span> * @cfg {Boolean} stopEvent
99 * 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>.
102 constructor : function(config){
103 Ext.apply(this, config);
105 <span id='Ext-dd-DragTracker-event-mouseover'> /**
106 </span> * @event mouseover <p><b>Only available when {@link #trackOver} is <code>true</code></b></p>
107 * <p>Fires when the mouse enters the DragTracker's target element (or if {@link #delegate} is
108 * used, when the mouse enters a delegate element).</p>
109 * @param {Object} this
110 * @param {Object} e event object
111 * @param {HTMLElement} target The element mouseovered.
115 <span id='Ext-dd-DragTracker-event-mouseout'> /**
116 </span> * @event mouseout <p><b>Only available when {@link #trackOver} is <code>true</code></b></p>
117 * <p>Fires when the mouse exits the DragTracker's target element (or if {@link #delegate} is
118 * used, when the mouse exits a delegate element).</p>
119 * @param {Object} this
120 * @param {Object} e event object
124 <span id='Ext-dd-DragTracker-event-mousedown'> /**
125 </span> * @event mousedown <p>Fires when the mouse button is pressed down, but before a drag operation begins. The
126 * drag operation begins after either the mouse has been moved by {@link #tolerance} pixels, or after
127 * the {@link #autoStart} timer fires.</p>
128 * <p>Return false to veto the drag operation.</p>
129 * @param {Object} this
130 * @param {Object} e event object
134 <span id='Ext-dd-DragTracker-event-mouseup'> /**
135 </span> * @event mouseup
136 * @param {Object} this
137 * @param {Object} e event object
141 <span id='Ext-dd-DragTracker-event-mousemove'> /**
142 </span> * @event mousemove Fired when the mouse is moved. Returning false cancels the drag operation.
143 * @param {Object} this
144 * @param {Object} e event object
148 <span id='Ext-dd-DragTracker-event-beforestart'> /**
149 </span> * @event beforestart
150 * @param {Object} this
151 * @param {Object} e event object
155 <span id='Ext-dd-DragTracker-event-dragstart'> /**
156 </span> * @event dragstart
157 * @param {Object} this
158 * @param {Object} e event object
162 <span id='Ext-dd-DragTracker-event-dragend'> /**
163 </span> * @event dragend
164 * @param {Object} this
165 * @param {Object} e event object
169 <span id='Ext-dd-DragTracker-event-drag'> /**
170 </span> * @event drag
171 * @param {Object} this
172 * @param {Object} e event object
177 this.dragRegion = Ext.create('Ext.util.Region', 0,0,0,0);
180 this.initEl(this.el);
183 // Dont pass the config so that it is not applied to 'this' again
184 this.mixins.observable.constructor.call(this);
191 <span id='Ext-dd-DragTracker-method-initEl'> /**
192 </span> * Initializes the DragTracker on a given element.
193 * @param {Ext.Element/HTMLElement} el The element
195 initEl: function(el) {
196 this.el = Ext.get(el);
198 // The delegate option may also be an element on which to listen
199 this.handle = Ext.get(this.delegate);
201 // If delegate specified an actual element to listen on, we do not use the delegate listener option
202 this.delegate = this.handle ? undefined : this.delegate;
205 this.handle = this.el;
208 // Add a mousedown listener which reacts only on the elements targeted by the delegate config.
209 // We process mousedown to begin tracking.
210 this.mon(this.handle, {
211 mousedown: this.onMouseDown,
212 delegate: this.delegate,
216 // If configured to do so, track mouse entry and exit into the target (or delegate).
217 // The mouseover and mouseout CANNOT be replaced with mouseenter and mouseleave
218 // because delegate cannot work with those pseudoevents. Entry/exit checking is done in the handler.
219 if (this.trackOver || this.overCls) {
220 this.mon(this.handle, {
221 mouseover: this.onMouseOver,
222 mouseout: this.onMouseOut,
223 delegate: this.delegate,
229 disable: function() {
230 this.disabled = true;
234 this.disabled = false;
237 destroy : function() {
238 this.clearListeners();
242 // When the pointer enters a tracking element, fire a mouseover if the mouse entered from outside.
243 // This is mouseenter functionality, but we cannot use mouseenter because we are using "delegate" to filter mouse targets
244 onMouseOver: function(e, target) {
247 if (Ext.EventManager.contains(e) || me.delegate) {
248 me.mouseIsOut = false;
250 me.el.addCls(me.overCls);
252 me.fireEvent('mouseover', me, e, me.delegate ? e.getTarget(me.delegate, target) : me.handle);
257 // When the pointer exits a tracking element, fire a mouseout.
258 // This is mouseleave functionality, but we cannot use mouseleave because we are using "delegate" to filter mouse targets
259 onMouseOut: function(e) {
260 if (this.mouseIsDown) {
261 this.mouseIsOut = true;
264 this.el.removeCls(this.overCls);
266 this.fireEvent('mouseout', this, e);
270 onMouseDown: function(e, target){
271 // If this is disabled, or the mousedown has been processed by an upstream DragTracker, return
272 if (this.disabled ||e.dragTracked) {
276 // This information should be available in mousedown listener and onBeforeStart implementations
277 this.dragTarget = this.delegate ? target : this.handle.dom;
278 this.startXY = this.lastXY = e.getXY();
279 this.startRegion = Ext.fly(this.dragTarget).getRegion();
281 if (this.fireEvent('mousedown', this, e) === false ||
282 this.fireEvent('beforedragstart', this, e) === false ||
283 this.onBeforeStart(e) === false) {
287 // Track when the mouse is down so that mouseouts while the mouse is down are not processed.
288 // The onMouseOut method will only ever be called after mouseup.
289 this.mouseIsDown = true;
291 // Flag for downstream DragTracker instances that the mouse is being tracked.
292 e.dragTracked = true;
294 if (this.preventDefault !== false) {
299 mouseup: this.onMouseUp,
300 mousemove: this.onMouseMove,
301 selectstart: this.stopSelect
303 if (this.autoStart) {
304 this.timer = Ext.defer(this.triggerStart, this.autoStart === true ? 1000 : this.autoStart, this, [e]);
308 onMouseMove: function(e, target){
309 // BrowserBug: IE hack to see if button was released outside of window.
310 // Needed in IE6-9 in quirks and strictmode
311 if (this.active && Ext.isIE && !e.browserEvent.button) {
323 if (Math.max(Math.abs(s[0]-xy[0]), Math.abs(s[1]-xy[1])) > this.tolerance) {
324 this.triggerStart(e);
330 // Returning false from a mousemove listener deactivates
331 if (this.fireEvent('mousemove', this, e) === false) {
335 this.fireEvent('drag', this, e);
339 onMouseUp: function(e) {
340 // Clear the flag which ensures onMouseOut fires only after the mouse button
341 // is lifted if the mouseout happens *during* a drag.
342 this.mouseIsDown = false;
344 // If we mouseouted the el *during* the drag, the onMouseOut method will not have fired. Ensure that it gets processed.
345 if (this.mouseIsOut) {
346 this.mouseIsOut = false;
350 this.fireEvent('mouseup', this, e);
354 <span id='Ext-dd-DragTracker-method-endDrag'> /**
356 * Stop the drag operation, and remove active mouse listeners.
358 endDrag: function(e) {
359 var doc = Ext.getDoc(),
360 wasActive = this.active;
362 doc.un('mousemove', this.onMouseMove, this);
363 doc.un('mouseup', this.onMouseUp, this);
364 doc.un('selectstart', this.stopSelect, this);
369 this.fireEvent('dragend', this, e);
371 // Private property calculated when first required and only cached during a drag
372 delete this._constrainRegion;
374 // Remove flag from event singleton. Using "Ext.EventObject" here since "endDrag" is called directly in some cases without an "e" param
375 delete Ext.EventObject.dragTracked;
378 triggerStart: function(e) {
382 this.fireEvent('dragstart', this, e);
385 clearStart : function() {
387 clearTimeout(this.timer);
392 stopSelect : function(e) {
397 <span id='Ext-dd-DragTracker-method-onBeforeStart'> /**
398 </span> * Template method which should be overridden by each DragTracker instance. Called when the user first clicks and
399 * holds the mouse button down. Return false to disallow the drag
400 * @param {Ext.EventObject} e The event object
403 onBeforeStart : function(e) {
407 <span id='Ext-dd-DragTracker-method-onStart'> /**
408 </span> * Template method which should be overridden by each DragTracker instance. Called when a drag operation starts
409 * (e.g. the user has moved the tracked element beyond the specified tolerance)
410 * @param {Ext.EventObject} e The event object
413 onStart : function(xy) {
417 <span id='Ext-dd-DragTracker-method-onDrag'> /**
418 </span> * Template method which should be overridden by each DragTracker instance. Called whenever a drag has been detected.
419 * @param {Ext.EventObject} e The event object
422 onDrag : function(e) {
426 <span id='Ext-dd-DragTracker-method-onEnd'> /**
427 </span> * Template method which should be overridden by each DragTracker instance. Called when a drag operation has been completed
428 * (e.g. the user clicked and held the mouse down, dragged the element and then released the mouse button)
429 * @param {Ext.EventObject} e The event object
432 onEnd : function(e) {
436 <span id='Ext-dd-DragTracker-method-getDragTarget'> /**
437 </span> * </p>Returns the drag target. This is usually the DragTracker's encapsulating element.</p>
438 * <p>If the {@link #delegate} option is being used, this may be a child element which matches the
439 * {@link #delegate} selector.</p>
440 * @return {Ext.Element} The element currently being tracked.
442 getDragTarget : function(){
443 return this.dragTarget;
446 <span id='Ext-dd-DragTracker-method-getDragCt'> /**
448 * @returns {Ext.Element} The DragTracker's encapsulating element.
450 getDragCt : function(){
454 <span id='Ext-dd-DragTracker-method-getConstrainRegion'> /**
456 * Return the Region into which the drag operation is constrained.
457 * Either the XY pointer itself can be constrained, or the dragTarget element
458 * The private property _constrainRegion is cached until onMouseUp
460 getConstrainRegion: function() {
461 if (this.constrainTo) {
462 if (this.constrainTo instanceof Ext.util.Region) {
463 return this.constrainTo;
465 if (!this._constrainRegion) {
466 this._constrainRegion = Ext.fly(this.constrainTo).getViewRegion();
469 if (!this._constrainRegion) {
470 this._constrainRegion = this.getDragCt().getViewRegion();
473 return this._constrainRegion;
476 getXY : function(constrain){
477 return constrain ? this.constrainModes[constrain](this, this.lastXY) : this.lastXY;
480 <span id='Ext-dd-DragTracker-method-getOffset'> /**
481 </span> * Returns the X, Y offset of the current mouse position from the mousedown point.
483 * This method may optionally constrain the real offset values, and returns a point coerced in one
487 * The current mouse position is coerced into the constrainRegion and the resulting position is returned.
489 * The new {@link Ext.util.Region Region} of the {@link #getDragTarget dragTarget} is calculated
490 * based upon the current mouse position, and then coerced into the constrainRegion. The returned
491 * mouse position is then adjusted by the same delta as was used to coerce the region.\
493 * @param constrainMode {String} (Optional) If omitted the true mouse position is returned. May be passed
494 * as `point` or `dragTarget`. See above.
495 * @returns {Number[]} The `X, Y` offset from the mousedown point, optionally constrained.
497 getOffset : function(constrain){
498 var xy = this.getXY(constrain),
501 return [xy[0]-s[0], xy[1]-s[1]];
505 // Constrain the passed point to within the constrain region
506 point: function(me, xy) {
507 var dr = me.dragRegion,
508 constrainTo = me.getConstrainRegion();
515 dr.x = dr.left = dr[0] = dr.right = xy[0];
516 dr.y = dr.top = dr[1] = dr.bottom = xy[1];
517 dr.constrainTo(constrainTo);
519 return [dr.left, dr.top];
522 // Constrain the dragTarget to within the constrain region. Return the passed xy adjusted by the same delta.
523 dragTarget: function(me, xy) {
525 dr = me.startRegion.copy(),
526 constrainTo = me.getConstrainRegion(),
534 // See where the passed XY would put the dragTarget if translated by the unconstrained offset.
535 // If it overflows, we constrain the passed XY to bring the potential
536 // region back within the boundary.
537 dr.translateBy(xy[0]-s[0], xy[1]-s[1]);
539 // Constrain the X coordinate by however much the dragTarget overflows
540 if (dr.right > constrainTo.right) {
541 xy[0] += adjust = (constrainTo.right - dr.right); // overflowed the right
544 if (dr.left < constrainTo.left) {
545 xy[0] += (constrainTo.left - dr.left); // overflowed the left
548 // Constrain the Y coordinate by however much the dragTarget overflows
549 if (dr.bottom > constrainTo.bottom) {
550 xy[1] += adjust = (constrainTo.bottom - dr.bottom); // overflowed the bottom
553 if (dr.top < constrainTo.top) {
554 xy[1] += (constrainTo.top - dr.top); // overflowed the top