DragTracker.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  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; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-dd-DragTracker'>/**
  19. </span> * A DragTracker listens for drag events on an Element and fires events at the start and end of the drag,
  20. * as well as during the drag. This is useful for components such as {@link Ext.slider.Multi}, where there is
  21. * an element that can be dragged around to change the Slider's value.
  22. *
  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.
  26. */
  27. Ext.define('Ext.dd.DragTracker', {
  28. uses: ['Ext.util.Region'],
  29. mixins: {
  30. observable: 'Ext.util.Observable'
  31. },
  32. <span id='Ext-dd-DragTracker-property-active'> /**
  33. </span> * @property {Boolean} active
  34. * Indicates whether the user is currently dragging this tracker.
  35. * @readonly
  36. */
  37. active: false,
  38. <span id='Ext-dd-DragTracker-property-dragTarget'> /**
  39. </span> * @property {HTMLElement} dragTarget
  40. * The element being dragged.
  41. *
  42. * Only valid during drag operations.
  43. *
  44. * If the {@link #delegate} option is used, this will be the delegate element which was mousedowned.
  45. * @readonly
  46. */
  47. <span id='Ext-dd-DragTracker-cfg-trackOver'> /**
  48. </span> * @cfg {Boolean} trackOver
  49. * Set to true to fire mouseover and mouseout events when the mouse enters or leaves the target element.
  50. *
  51. * This is implicitly set when an {@link #overCls} is specified.
  52. *
  53. * If the {@link #delegate} option is used, these events fire only when a delegate element is entered of left.
  54. */
  55. trackOver: false,
  56. <span id='Ext-dd-DragTracker-cfg-overCls'> /**
  57. </span> * @cfg {String} overCls
  58. * A CSS class to add to the DragTracker's target element when the element (or, if the {@link #delegate}
  59. * option is used, when a delegate element) is mouseovered.
  60. *
  61. * If the {@link #delegate} option is used, these events fire only when a delegate element is entered of left.
  62. */
  63. <span id='Ext-dd-DragTracker-cfg-constrainTo'> /**
  64. </span> * @cfg {Ext.util.Region/Ext.Element} constrainTo
  65. * A {@link Ext.util.Region Region} (Or an element from which a Region measurement will be read)
  66. * which is used to constrain the result of the {@link #getOffset} call.
  67. *
  68. * This may be set any time during the DragTracker's lifecycle to set a dynamic constraining region.
  69. */
  70. <span id='Ext-dd-DragTracker-cfg-tolerance'> /**
  71. </span> * @cfg {Number} tolerance
  72. * Number of pixels the drag target must be moved before dragging is
  73. * considered to have started.
  74. */
  75. tolerance: 5,
  76. <span id='Ext-dd-DragTracker-cfg-autoStart'> /**
  77. </span> * @cfg {Boolean/Number} autoStart
  78. * Specify `true` to defer trigger start by 1000 ms.
  79. * Specify a Number for the number of milliseconds to defer trigger start.
  80. */
  81. autoStart: false,
  82. <span id='Ext-dd-DragTracker-cfg-delegate'> /**
  83. </span> * @cfg {String} delegate
  84. * A {@link Ext.DomQuery DomQuery} selector which identifies child elements within the DragTracker's encapsulating
  85. * Element which are the tracked elements. This limits tracking to only begin when the matching elements are mousedowned.
  86. *
  87. * This may also be a specific child element within the DragTracker's encapsulating element to use as the tracked element.
  88. */
  89. <span id='Ext-dd-DragTracker-cfg-preventDefault'> /**
  90. </span> * @cfg {Boolean} [preventDefault=true]
  91. * Specify `false` to enable default actions on onMouseDown events.
  92. */
  93. <span id='Ext-dd-DragTracker-cfg-stopEvent'> /**
  94. </span> * @cfg {Boolean} [stopEvent=false]
  95. * Specify `true` to stop the `mousedown` event from bubbling to outer listeners from the target element (or its delegates).
  96. */
  97. constructor : function(config){
  98. var me = this;
  99. Ext.apply(me, config);
  100. me.addEvents(
  101. <span id='Ext-dd-DragTracker-event-mouseover'> /**
  102. </span> * @event mouseover
  103. * Fires when the mouse enters the DragTracker's target element (or if {@link #delegate} is
  104. * used, when the mouse enters a delegate element).
  105. *
  106. * **Only available when {@link #trackOver} is `true`**
  107. *
  108. * @param {Object} this
  109. * @param {Object} e event object
  110. * @param {HTMLElement} target The element mouseovered.
  111. */
  112. 'mouseover',
  113. <span id='Ext-dd-DragTracker-event-mouseout'> /**
  114. </span> * @event mouseout
  115. * Fires when the mouse exits the DragTracker's target element (or if {@link #delegate} is
  116. * used, when the mouse exits a delegate element).
  117. *
  118. * **Only available when {@link #trackOver} is `true`**
  119. *
  120. * @param {Object} this
  121. * @param {Object} e event object
  122. */
  123. 'mouseout',
  124. <span id='Ext-dd-DragTracker-event-mousedown'> /**
  125. </span> * @event mousedown
  126. * Fires when the mouse button is pressed down, but before a drag operation begins. The
  127. * drag operation begins after either the mouse has been moved by {@link #tolerance} pixels,
  128. * or after the {@link #autoStart} timer fires.
  129. *
  130. * Return false to veto the drag operation.
  131. *
  132. * @param {Object} this
  133. * @param {Object} e event object
  134. */
  135. 'mousedown',
  136. <span id='Ext-dd-DragTracker-event-mouseup'> /**
  137. </span> * @event mouseup
  138. * @param {Object} this
  139. * @param {Object} e event object
  140. */
  141. 'mouseup',
  142. <span id='Ext-dd-DragTracker-event-mousemove'> /**
  143. </span> * @event mousemove
  144. * Fired when the mouse is moved. Returning false cancels the drag operation.
  145. * @param {Object} this
  146. * @param {Object} e event object
  147. */
  148. 'mousemove',
  149. <span id='Ext-dd-DragTracker-event-beforestart'> /**
  150. </span> * @event beforestart
  151. * @param {Object} this
  152. * @param {Object} e event object
  153. */
  154. 'beforedragstart',
  155. <span id='Ext-dd-DragTracker-event-dragstart'> /**
  156. </span> * @event dragstart
  157. * @param {Object} this
  158. * @param {Object} e event object
  159. */
  160. 'dragstart',
  161. <span id='Ext-dd-DragTracker-event-dragend'> /**
  162. </span> * @event dragend
  163. * @param {Object} this
  164. * @param {Object} e event object
  165. */
  166. 'dragend',
  167. <span id='Ext-dd-DragTracker-event-drag'> /**
  168. </span> * @event drag
  169. * @param {Object} this
  170. * @param {Object} e event object
  171. */
  172. 'drag'
  173. );
  174. me.dragRegion = new Ext.util.Region(0,0,0,0);
  175. if (me.el) {
  176. me.initEl(me.el);
  177. }
  178. // Dont pass the config so that it is not applied to 'this' again
  179. me.mixins.observable.constructor.call(me);
  180. if (me.disabled) {
  181. me.disable();
  182. }
  183. },
  184. <span id='Ext-dd-DragTracker-method-initEl'> /**
  185. </span> * Initializes the DragTracker on a given element.
  186. * @param {Ext.Element/HTMLElement} el The element
  187. */
  188. initEl: function(el) {
  189. var me = this;
  190. me.el = Ext.get(el);
  191. // The delegate option may also be an element on which to listen
  192. me.handle = Ext.get(me.delegate);
  193. // If delegate specified an actual element to listen on, we do not use the delegate listener option
  194. me.delegate = me.handle ? undefined : me.delegate;
  195. if (!me.handle) {
  196. me.handle = me.el;
  197. }
  198. // Add a mousedown listener which reacts only on the elements targeted by the delegate config.
  199. // We process mousedown to begin tracking.
  200. me.mon(me.handle, {
  201. mousedown: me.onMouseDown,
  202. delegate: me.delegate,
  203. scope: me
  204. });
  205. // If configured to do so, track mouse entry and exit into the target (or delegate).
  206. // The mouseover and mouseout CANNOT be replaced with mouseenter and mouseleave
  207. // because delegate cannot work with those pseudoevents. Entry/exit checking is done in the handler.
  208. if (me.trackOver || me.overCls) {
  209. me.mon(me.handle, {
  210. mouseover: me.onMouseOver,
  211. mouseout: me.onMouseOut,
  212. delegate: me.delegate,
  213. scope: me
  214. });
  215. }
  216. },
  217. disable: function() {
  218. this.disabled = true;
  219. },
  220. enable: function() {
  221. this.disabled = false;
  222. },
  223. destroy : function() {
  224. this.clearListeners();
  225. delete this.el;
  226. },
  227. // When the pointer enters a tracking element, fire a mouseover if the mouse entered from outside.
  228. // This is mouseenter functionality, but we cannot use mouseenter because we are using &quot;delegate&quot; to filter mouse targets
  229. onMouseOver: function(e, target) {
  230. var me = this;
  231. if (!me.disabled) {
  232. if (Ext.EventManager.contains(e) || me.delegate) {
  233. me.mouseIsOut = false;
  234. if (me.overCls) {
  235. me.el.addCls(me.overCls);
  236. }
  237. me.fireEvent('mouseover', me, e, me.delegate ? e.getTarget(me.delegate, target) : me.handle);
  238. }
  239. }
  240. },
  241. // When the pointer exits a tracking element, fire a mouseout.
  242. // This is mouseleave functionality, but we cannot use mouseleave because we are using &quot;delegate&quot; to filter mouse targets
  243. onMouseOut: function(e) {
  244. var me = this;
  245. if (me.mouseIsDown) {
  246. me.mouseIsOut = true;
  247. } else {
  248. if (me.overCls) {
  249. me.el.removeCls(me.overCls);
  250. }
  251. me.fireEvent('mouseout', me, e);
  252. }
  253. },
  254. onMouseDown: function(e, target){
  255. var me = this,
  256. el;
  257. // If this is disabled, or the mousedown has been processed by an upstream DragTracker, return
  258. if (me.disabled ||e.dragTracked) {
  259. return;
  260. }
  261. // This information should be available in mousedown listener and onBeforeStart implementations
  262. me.dragTarget = me.delegate ? target : me.handle.dom;
  263. me.startXY = me.lastXY = e.getXY();
  264. me.startRegion = Ext.fly(me.dragTarget).getRegion();
  265. if (me.fireEvent('mousedown', me, e) === false ||
  266. me.fireEvent('beforedragstart', me, e) === false ||
  267. me.onBeforeStart(e) === false) {
  268. return;
  269. }
  270. // Track when the mouse is down so that mouseouts while the mouse is down are not processed.
  271. // The onMouseOut method will only ever be called after mouseup.
  272. me.mouseIsDown = true;
  273. // Flag for downstream DragTracker instances that the mouse is being tracked.
  274. e.dragTracked = true;
  275. // See Ext.dd.DragDropManager::handleMouseDown
  276. el = me.el.dom;
  277. if (Ext.isIE &amp;&amp; el.setCapture) {
  278. el.setCapture();
  279. }
  280. if (me.preventDefault !== false) {
  281. e.preventDefault();
  282. }
  283. Ext.getDoc().on({
  284. scope: me,
  285. mouseup: me.onMouseUp,
  286. mousemove: me.onMouseMove,
  287. selectstart: me.stopSelect
  288. });
  289. if (me.autoStart) {
  290. me.timer = Ext.defer(me.triggerStart, me.autoStart === true ? 1000 : me.autoStart, me, [e]);
  291. }
  292. },
  293. onMouseMove: function(e, target){
  294. var me = this,
  295. xy = e.getXY(),
  296. s = me.startXY;
  297. e.preventDefault();
  298. me.lastXY = xy;
  299. if (!me.active) {
  300. if (Math.max(Math.abs(s[0]-xy[0]), Math.abs(s[1]-xy[1])) &gt; me.tolerance) {
  301. me.triggerStart(e);
  302. } else {
  303. return;
  304. }
  305. }
  306. // Returning false from a mousemove listener deactivates
  307. if (me.fireEvent('mousemove', me, e) === false) {
  308. me.onMouseUp(e);
  309. } else {
  310. me.onDrag(e);
  311. me.fireEvent('drag', me, e);
  312. }
  313. },
  314. onMouseUp: function(e) {
  315. var me = this;
  316. // Clear the flag which ensures onMouseOut fires only after the mouse button
  317. // is lifted if the mouseout happens *during* a drag.
  318. me.mouseIsDown = false;
  319. // If we mouseouted the el *during* the drag, the onMouseOut method will not have fired. Ensure that it gets processed.
  320. if (me.mouseIsOut) {
  321. me.mouseIsOut = false;
  322. me.onMouseOut(e);
  323. }
  324. e.preventDefault();
  325. // See Ext.dd.DragDropManager::handleMouseDown
  326. if (Ext.isIE &amp;&amp; document.releaseCapture) {
  327. document.releaseCapture();
  328. }
  329. me.fireEvent('mouseup', me, e);
  330. me.endDrag(e);
  331. },
  332. <span id='Ext-dd-DragTracker-method-endDrag'> /**
  333. </span> * @private
  334. * Stop the drag operation, and remove active mouse listeners.
  335. */
  336. endDrag: function(e) {
  337. var me = this,
  338. doc = Ext.getDoc(),
  339. wasActive = me.active;
  340. doc.un('mousemove', me.onMouseMove, me);
  341. doc.un('mouseup', me.onMouseUp, me);
  342. doc.un('selectstart', me.stopSelect, me);
  343. me.clearStart();
  344. me.active = false;
  345. if (wasActive) {
  346. me.onEnd(e);
  347. me.fireEvent('dragend', me, e);
  348. }
  349. // Private property calculated when first required and only cached during a drag
  350. delete me._constrainRegion;
  351. // Remove flag from event singleton. Using &quot;Ext.EventObject&quot; here since &quot;endDrag&quot; is called directly in some cases without an &quot;e&quot; param
  352. delete Ext.EventObject.dragTracked;
  353. },
  354. triggerStart: function(e) {
  355. var me = this;
  356. me.clearStart();
  357. me.active = true;
  358. me.onStart(e);
  359. me.fireEvent('dragstart', me, e);
  360. },
  361. clearStart : function() {
  362. var timer = this.timer;
  363. if (timer) {
  364. clearTimeout(timer);
  365. delete this.timer;
  366. }
  367. },
  368. stopSelect : function(e) {
  369. e.stopEvent();
  370. return false;
  371. },
  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
  376. * @template
  377. */
  378. onBeforeStart : function(e) {
  379. },
  380. <span id='Ext-dd-DragTracker-method-onStart'> /**
  381. </span> * Template method which should be overridden by each DragTracker instance. Called when a drag operation starts
  382. * (e.g. the user has moved the tracked element beyond the specified tolerance)
  383. * @param {Ext.EventObject} e The event object
  384. * @template
  385. */
  386. onStart : function(xy) {
  387. },
  388. <span id='Ext-dd-DragTracker-method-onDrag'> /**
  389. </span> * Template method which should be overridden by each DragTracker instance. Called whenever a drag has been detected.
  390. * @param {Ext.EventObject} e The event object
  391. * @template
  392. */
  393. onDrag : function(e) {
  394. },
  395. <span id='Ext-dd-DragTracker-method-onEnd'> /**
  396. </span> * Template method which should be overridden by each DragTracker instance. Called when a drag operation has been completed
  397. * (e.g. the user clicked and held the mouse down, dragged the element and then released the mouse button)
  398. * @param {Ext.EventObject} e The event object
  399. * @template
  400. */
  401. onEnd : function(e) {
  402. },
  403. <span id='Ext-dd-DragTracker-method-getDragTarget'> /**
  404. </span> * Returns the drag target. This is usually the DragTracker's encapsulating element.
  405. *
  406. * If the {@link #delegate} option is being used, this may be a child element which matches the
  407. * {@link #delegate} selector.
  408. *
  409. * @return {Ext.Element} The element currently being tracked.
  410. */
  411. getDragTarget : function(){
  412. return this.dragTarget;
  413. },
  414. <span id='Ext-dd-DragTracker-method-getDragCt'> /**
  415. </span> * @private
  416. * @returns {Ext.Element} The DragTracker's encapsulating element.
  417. */
  418. getDragCt : function(){
  419. return this.el;
  420. },
  421. <span id='Ext-dd-DragTracker-method-getConstrainRegion'> /**
  422. </span> * @private
  423. * Return the Region into which the drag operation is constrained.
  424. * Either the XY pointer itself can be constrained, or the dragTarget element
  425. * The private property _constrainRegion is cached until onMouseUp
  426. */
  427. getConstrainRegion: function() {
  428. var me = this;
  429. if (me.constrainTo) {
  430. if (me.constrainTo instanceof Ext.util.Region) {
  431. return me.constrainTo;
  432. }
  433. if (!me._constrainRegion) {
  434. me._constrainRegion = Ext.fly(me.constrainTo).getViewRegion();
  435. }
  436. } else {
  437. if (!me._constrainRegion) {
  438. me._constrainRegion = me.getDragCt().getViewRegion();
  439. }
  440. }
  441. return me._constrainRegion;
  442. },
  443. getXY : function(constrain){
  444. return constrain ? this.constrainModes[constrain](this, this.lastXY) : this.lastXY;
  445. },
  446. <span id='Ext-dd-DragTracker-method-getOffset'> /**
  447. </span> * Returns the X, Y offset of the current mouse position from the mousedown point.
  448. *
  449. * This method may optionally constrain the real offset values, and returns a point coerced in one
  450. * of two modes:
  451. *
  452. * - `point`
  453. * The current mouse position is coerced into the constrainRegion and the resulting position is returned.
  454. * - `dragTarget`
  455. * The new {@link Ext.util.Region Region} of the {@link #getDragTarget dragTarget} is calculated
  456. * based upon the current mouse position, and then coerced into the constrainRegion. The returned
  457. * mouse position is then adjusted by the same delta as was used to coerce the region.\
  458. *
  459. * @param constrainMode {String} (Optional) If omitted the true mouse position is returned. May be passed
  460. * as `point` or `dragTarget`. See above.
  461. * @returns {Number[]} The `X, Y` offset from the mousedown point, optionally constrained.
  462. */
  463. getOffset : function(constrain){
  464. var xy = this.getXY(constrain),
  465. s = this.startXY;
  466. return [xy[0]-s[0], xy[1]-s[1]];
  467. },
  468. constrainModes: {
  469. // Constrain the passed point to within the constrain region
  470. point: function(me, xy) {
  471. var dr = me.dragRegion,
  472. constrainTo = me.getConstrainRegion();
  473. // No constraint
  474. if (!constrainTo) {
  475. return xy;
  476. }
  477. dr.x = dr.left = dr[0] = dr.right = xy[0];
  478. dr.y = dr.top = dr[1] = dr.bottom = xy[1];
  479. dr.constrainTo(constrainTo);
  480. return [dr.left, dr.top];
  481. },
  482. // Constrain the dragTarget to within the constrain region. Return the passed xy adjusted by the same delta.
  483. dragTarget: function(me, xy) {
  484. var s = me.startXY,
  485. dr = me.startRegion.copy(),
  486. constrainTo = me.getConstrainRegion(),
  487. adjust;
  488. // No constraint
  489. if (!constrainTo) {
  490. return xy;
  491. }
  492. // See where the passed XY would put the dragTarget if translated by the unconstrained offset.
  493. // If it overflows, we constrain the passed XY to bring the potential
  494. // region back within the boundary.
  495. dr.translateBy(xy[0]-s[0], xy[1]-s[1]);
  496. // Constrain the X coordinate by however much the dragTarget overflows
  497. if (dr.right &gt; constrainTo.right) {
  498. xy[0] += adjust = (constrainTo.right - dr.right); // overflowed the right
  499. dr.left += adjust;
  500. }
  501. if (dr.left &lt; constrainTo.left) {
  502. xy[0] += (constrainTo.left - dr.left); // overflowed the left
  503. }
  504. // Constrain the Y coordinate by however much the dragTarget overflows
  505. if (dr.bottom &gt; constrainTo.bottom) {
  506. xy[1] += adjust = (constrainTo.bottom - dr.bottom); // overflowed the bottom
  507. dr.top += adjust;
  508. }
  509. if (dr.top &lt; constrainTo.top) {
  510. xy[1] += (constrainTo.top - dr.top); // overflowed the top
  511. }
  512. return xy;
  513. }
  514. }
  515. });</pre>
  516. </body>
  517. </html>