dragger.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Drag Utilities.
  16. *
  17. * Provides extensible functionality for drag & drop behaviour.
  18. *
  19. * @see ../demos/drag.html
  20. * @see ../demos/dragger.html
  21. */
  22. goog.provide('goog.fx.DragEvent');
  23. goog.provide('goog.fx.Dragger');
  24. goog.provide('goog.fx.Dragger.EventType');
  25. goog.require('goog.dom');
  26. goog.require('goog.dom.TagName');
  27. goog.require('goog.events');
  28. goog.require('goog.events.Event');
  29. goog.require('goog.events.EventHandler');
  30. goog.require('goog.events.EventTarget');
  31. goog.require('goog.events.EventType');
  32. goog.require('goog.math.Coordinate');
  33. goog.require('goog.math.Rect');
  34. goog.require('goog.style');
  35. goog.require('goog.style.bidi');
  36. goog.require('goog.userAgent');
  37. /**
  38. * A class that allows mouse or touch-based dragging (moving) of an element
  39. *
  40. * @param {Element} target The element that will be dragged.
  41. * @param {Element=} opt_handle An optional handle to control the drag, if null
  42. * the target is used.
  43. * @param {goog.math.Rect=} opt_limits Object containing left, top, width,
  44. * and height.
  45. *
  46. * @extends {goog.events.EventTarget}
  47. * @constructor
  48. * @struct
  49. */
  50. goog.fx.Dragger = function(target, opt_handle, opt_limits) {
  51. goog.fx.Dragger.base(this, 'constructor');
  52. /**
  53. * Reference to drag target element.
  54. * @type {?Element}
  55. */
  56. this.target = target;
  57. /**
  58. * Reference to the handler that initiates the drag.
  59. * @type {?Element}
  60. */
  61. this.handle = opt_handle || target;
  62. /**
  63. * Object representing the limits of the drag region.
  64. * @type {goog.math.Rect}
  65. */
  66. this.limits = opt_limits || new goog.math.Rect(NaN, NaN, NaN, NaN);
  67. /**
  68. * Reference to a document object to use for the events.
  69. * @private {Document}
  70. */
  71. this.document_ = goog.dom.getOwnerDocument(target);
  72. /** @private {!goog.events.EventHandler} */
  73. this.eventHandler_ = new goog.events.EventHandler(this);
  74. this.registerDisposable(this.eventHandler_);
  75. /**
  76. * Whether the element is rendered right-to-left. We initialize this lazily.
  77. * @private {boolean|undefined}}
  78. */
  79. this.rightToLeft_;
  80. /**
  81. * Current x position of mouse or touch relative to viewport.
  82. * @type {number}
  83. */
  84. this.clientX = 0;
  85. /**
  86. * Current y position of mouse or touch relative to viewport.
  87. * @type {number}
  88. */
  89. this.clientY = 0;
  90. /**
  91. * Current x position of mouse or touch relative to screen. Deprecated because
  92. * it doesn't take into affect zoom level or pixel density.
  93. * @type {number}
  94. * @deprecated Consider switching to clientX instead.
  95. */
  96. this.screenX = 0;
  97. /**
  98. * Current y position of mouse or touch relative to screen. Deprecated because
  99. * it doesn't take into affect zoom level or pixel density.
  100. * @type {number}
  101. * @deprecated Consider switching to clientY instead.
  102. */
  103. this.screenY = 0;
  104. /**
  105. * The x position where the first mousedown or touchstart occurred.
  106. * @type {number}
  107. */
  108. this.startX = 0;
  109. /**
  110. * The y position where the first mousedown or touchstart occurred.
  111. * @type {number}
  112. */
  113. this.startY = 0;
  114. /**
  115. * Current x position of drag relative to target's parent.
  116. * @type {number}
  117. */
  118. this.deltaX = 0;
  119. /**
  120. * Current y position of drag relative to target's parent.
  121. * @type {number}
  122. */
  123. this.deltaY = 0;
  124. /**
  125. * The current page scroll value.
  126. * @type {?goog.math.Coordinate}
  127. */
  128. this.pageScroll;
  129. /**
  130. * Whether dragging is currently enabled.
  131. * @private {boolean}
  132. */
  133. this.enabled_ = true;
  134. /**
  135. * Whether object is currently being dragged.
  136. * @private {boolean}
  137. */
  138. this.dragging_ = false;
  139. /**
  140. * Whether mousedown should be default prevented.
  141. * @private {boolean}
  142. **/
  143. this.preventMouseDown_ = true;
  144. /**
  145. * The amount of distance, in pixels, after which a mousedown or touchstart is
  146. * considered a drag.
  147. * @private {number}
  148. */
  149. this.hysteresisDistanceSquared_ = 0;
  150. /**
  151. * The SCROLL event target used to make drag element follow scrolling.
  152. * @private {?EventTarget}
  153. */
  154. this.scrollTarget_;
  155. /**
  156. * Whether IE drag events cancelling is on.
  157. * @private {boolean}
  158. */
  159. this.ieDragStartCancellingOn_ = false;
  160. /**
  161. * Whether the dragger implements the changes described in http://b/6324964,
  162. * making it truly RTL. This is a temporary flag to allow clients to
  163. * transition to the new behavior at their convenience. At some point it will
  164. * be the default.
  165. * @private {boolean}
  166. */
  167. this.useRightPositioningForRtl_ = false;
  168. // Add listener. Do not use the event handler here since the event handler is
  169. // used for listeners added and removed during the drag operation.
  170. goog.events.listen(
  171. this.handle,
  172. [goog.events.EventType.TOUCHSTART, goog.events.EventType.MOUSEDOWN],
  173. this.startDrag, false, this);
  174. };
  175. goog.inherits(goog.fx.Dragger, goog.events.EventTarget);
  176. // Dragger is meant to be extended, but defines most properties on its
  177. // prototype, thus making it unsuitable for sealing.
  178. goog.tagUnsealableClass(goog.fx.Dragger);
  179. /**
  180. * Whether setCapture is supported by the browser.
  181. * IE and Gecko after 1.9.3 have setCapture. MS Edge and WebKit
  182. * (https://bugs.webkit.org/show_bug.cgi?id=27330) don't.
  183. * @type {boolean}
  184. * @private
  185. */
  186. goog.fx.Dragger.HAS_SET_CAPTURE_ = goog.global.document &&
  187. goog.global.document.documentElement &&
  188. !!goog.global.document.documentElement.setCapture &&
  189. !!goog.global.document.releaseCapture;
  190. /**
  191. * Creates copy of node being dragged. This is a utility function to be used
  192. * wherever it is inappropriate for the original source to follow the mouse
  193. * cursor itself.
  194. *
  195. * @param {Element} sourceEl Element to copy.
  196. * @return {!Element} The clone of {@code sourceEl}.
  197. */
  198. goog.fx.Dragger.cloneNode = function(sourceEl) {
  199. var clonedEl = sourceEl.cloneNode(true),
  200. origTexts =
  201. goog.dom.getElementsByTagName(goog.dom.TagName.TEXTAREA, sourceEl),
  202. dragTexts =
  203. goog.dom.getElementsByTagName(goog.dom.TagName.TEXTAREA, clonedEl);
  204. // Cloning does not copy the current value of textarea elements, so correct
  205. // this manually.
  206. for (var i = 0; i < origTexts.length; i++) {
  207. dragTexts[i].value = origTexts[i].value;
  208. }
  209. switch (sourceEl.tagName) {
  210. case String(goog.dom.TagName.TR):
  211. return goog.dom.createDom(
  212. goog.dom.TagName.TABLE, null,
  213. goog.dom.createDom(goog.dom.TagName.TBODY, null, clonedEl));
  214. case String(goog.dom.TagName.TD):
  215. case String(goog.dom.TagName.TH):
  216. return goog.dom.createDom(
  217. goog.dom.TagName.TABLE, null,
  218. goog.dom.createDom(
  219. goog.dom.TagName.TBODY, null,
  220. goog.dom.createDom(goog.dom.TagName.TR, null, clonedEl)));
  221. case String(goog.dom.TagName.TEXTAREA):
  222. clonedEl.value = sourceEl.value;
  223. default:
  224. return clonedEl;
  225. }
  226. };
  227. /**
  228. * Constants for event names.
  229. * @enum {string}
  230. */
  231. goog.fx.Dragger.EventType = {
  232. // The drag action was canceled before the START event. Possible reasons:
  233. // disabled dragger, dragging with the right mouse button or releasing the
  234. // button before reaching the hysteresis distance.
  235. EARLY_CANCEL: 'earlycancel',
  236. START: 'start',
  237. BEFOREDRAG: 'beforedrag',
  238. DRAG: 'drag',
  239. END: 'end'
  240. };
  241. /**
  242. * Turns on/off true RTL behavior. This should be called immediately after
  243. * construction. This is a temporary flag to allow clients to transition
  244. * to the new component at their convenience. At some point true will be the
  245. * default.
  246. * @param {boolean} useRightPositioningForRtl True if "right" should be used for
  247. * positioning, false if "left" should be used for positioning.
  248. */
  249. goog.fx.Dragger.prototype.enableRightPositioningForRtl = function(
  250. useRightPositioningForRtl) {
  251. this.useRightPositioningForRtl_ = useRightPositioningForRtl;
  252. };
  253. /**
  254. * Returns the event handler, intended for subclass use.
  255. * @return {!goog.events.EventHandler<T>} The event handler.
  256. * @this {T}
  257. * @template T
  258. */
  259. goog.fx.Dragger.prototype.getHandler = function() {
  260. // TODO(user): templated "this" values currently result in "this" being
  261. // "unknown" in the body of the function.
  262. var self = /** @type {goog.fx.Dragger} */ (this);
  263. return self.eventHandler_;
  264. };
  265. /**
  266. * Sets (or reset) the Drag limits after a Dragger is created.
  267. * @param {goog.math.Rect?} limits Object containing left, top, width,
  268. * height for new Dragger limits. If target is right-to-left and
  269. * enableRightPositioningForRtl(true) is called, then rect is interpreted as
  270. * right, top, width, and height.
  271. */
  272. goog.fx.Dragger.prototype.setLimits = function(limits) {
  273. this.limits = limits || new goog.math.Rect(NaN, NaN, NaN, NaN);
  274. };
  275. /**
  276. * Sets the distance the user has to drag the element before a drag operation is
  277. * started.
  278. * @param {number} distance The number of pixels after which a mousedown and
  279. * move is considered a drag.
  280. */
  281. goog.fx.Dragger.prototype.setHysteresis = function(distance) {
  282. this.hysteresisDistanceSquared_ = Math.pow(distance, 2);
  283. };
  284. /**
  285. * Gets the distance the user has to drag the element before a drag operation is
  286. * started.
  287. * @return {number} distance The number of pixels after which a mousedown and
  288. * move is considered a drag.
  289. */
  290. goog.fx.Dragger.prototype.getHysteresis = function() {
  291. return Math.sqrt(this.hysteresisDistanceSquared_);
  292. };
  293. /**
  294. * Sets the SCROLL event target to make drag element follow scrolling.
  295. *
  296. * @param {EventTarget} scrollTarget The event target that dispatches SCROLL
  297. * events.
  298. */
  299. goog.fx.Dragger.prototype.setScrollTarget = function(scrollTarget) {
  300. this.scrollTarget_ = scrollTarget;
  301. };
  302. /**
  303. * Enables cancelling of built-in IE drag events.
  304. * @param {boolean} cancelIeDragStart Whether to enable cancelling of IE
  305. * dragstart event.
  306. */
  307. goog.fx.Dragger.prototype.setCancelIeDragStart = function(cancelIeDragStart) {
  308. this.ieDragStartCancellingOn_ = cancelIeDragStart;
  309. };
  310. /**
  311. * @return {boolean} Whether the dragger is enabled.
  312. */
  313. goog.fx.Dragger.prototype.getEnabled = function() {
  314. return this.enabled_;
  315. };
  316. /**
  317. * Set whether dragger is enabled
  318. * @param {boolean} enabled Whether dragger is enabled.
  319. */
  320. goog.fx.Dragger.prototype.setEnabled = function(enabled) {
  321. this.enabled_ = enabled;
  322. };
  323. /**
  324. * Set whether mousedown should be default prevented.
  325. * @param {boolean} preventMouseDown Whether mousedown should be default
  326. * prevented.
  327. */
  328. goog.fx.Dragger.prototype.setPreventMouseDown = function(preventMouseDown) {
  329. this.preventMouseDown_ = preventMouseDown;
  330. };
  331. /** @override */
  332. goog.fx.Dragger.prototype.disposeInternal = function() {
  333. goog.fx.Dragger.superClass_.disposeInternal.call(this);
  334. goog.events.unlisten(
  335. this.handle,
  336. [goog.events.EventType.TOUCHSTART, goog.events.EventType.MOUSEDOWN],
  337. this.startDrag, false, this);
  338. this.cleanUpAfterDragging_();
  339. this.target = null;
  340. this.handle = null;
  341. };
  342. /**
  343. * Whether the DOM element being manipulated is rendered right-to-left.
  344. * @return {boolean} True if the DOM element is rendered right-to-left, false
  345. * otherwise.
  346. * @private
  347. */
  348. goog.fx.Dragger.prototype.isRightToLeft_ = function() {
  349. if (!goog.isDef(this.rightToLeft_)) {
  350. this.rightToLeft_ = goog.style.isRightToLeft(this.target);
  351. }
  352. return this.rightToLeft_;
  353. };
  354. /**
  355. * Event handler that is used to start the drag
  356. * @param {goog.events.BrowserEvent} e Event object.
  357. */
  358. goog.fx.Dragger.prototype.startDrag = function(e) {
  359. var isMouseDown = e.type == goog.events.EventType.MOUSEDOWN;
  360. // Dragger.startDrag() can be called by AbstractDragDrop with a mousemove
  361. // event and IE does not report pressed mouse buttons on mousemove. Also,
  362. // it does not make sense to check for the button if the user is already
  363. // dragging.
  364. if (this.enabled_ && !this.dragging_ &&
  365. (!isMouseDown || e.isMouseActionButton())) {
  366. if (this.hysteresisDistanceSquared_ == 0) {
  367. if (this.fireDragStart_(e)) {
  368. this.dragging_ = true;
  369. if (this.preventMouseDown_ && isMouseDown) {
  370. e.preventDefault();
  371. }
  372. } else {
  373. // If the start drag is cancelled, don't setup for a drag.
  374. return;
  375. }
  376. } else if (this.preventMouseDown_ && isMouseDown) {
  377. // Need to preventDefault for hysteresis to prevent page getting selected.
  378. e.preventDefault();
  379. }
  380. this.setupDragHandlers();
  381. this.clientX = this.startX = e.clientX;
  382. this.clientY = this.startY = e.clientY;
  383. this.screenX = e.screenX;
  384. this.screenY = e.screenY;
  385. this.computeInitialPosition();
  386. this.pageScroll = goog.dom.getDomHelper(this.document_).getDocumentScroll();
  387. } else {
  388. this.dispatchEvent(goog.fx.Dragger.EventType.EARLY_CANCEL);
  389. }
  390. };
  391. /**
  392. * Sets up event handlers when dragging starts.
  393. * @protected
  394. */
  395. goog.fx.Dragger.prototype.setupDragHandlers = function() {
  396. var doc = this.document_;
  397. var docEl = doc.documentElement;
  398. // Use bubbling when we have setCapture since we got reports that IE has
  399. // problems with the capturing events in combination with setCapture.
  400. var useCapture = !goog.fx.Dragger.HAS_SET_CAPTURE_;
  401. this.eventHandler_.listen(
  402. doc, [goog.events.EventType.TOUCHMOVE, goog.events.EventType.MOUSEMOVE],
  403. this.handleMove_, {capture: useCapture, passive: false});
  404. this.eventHandler_.listen(
  405. doc, [goog.events.EventType.TOUCHEND, goog.events.EventType.MOUSEUP],
  406. this.endDrag, useCapture);
  407. if (goog.fx.Dragger.HAS_SET_CAPTURE_) {
  408. docEl.setCapture(false);
  409. this.eventHandler_.listen(
  410. docEl, goog.events.EventType.LOSECAPTURE, this.endDrag);
  411. } else {
  412. // Make sure we stop the dragging if the window loses focus.
  413. // Don't use capture in this listener because we only want to end the drag
  414. // if the actual window loses focus. Since blur events do not bubble we use
  415. // a bubbling listener on the window.
  416. this.eventHandler_.listen(
  417. goog.dom.getWindow(doc), goog.events.EventType.BLUR, this.endDrag);
  418. }
  419. if (goog.userAgent.IE && this.ieDragStartCancellingOn_) {
  420. // Cancel IE's 'ondragstart' event.
  421. this.eventHandler_.listen(
  422. doc, goog.events.EventType.DRAGSTART, goog.events.Event.preventDefault);
  423. }
  424. if (this.scrollTarget_) {
  425. this.eventHandler_.listen(
  426. this.scrollTarget_, goog.events.EventType.SCROLL, this.onScroll_,
  427. useCapture);
  428. }
  429. };
  430. /**
  431. * Fires a goog.fx.Dragger.EventType.START event.
  432. * @param {goog.events.BrowserEvent} e Browser event that triggered the drag.
  433. * @return {boolean} False iff preventDefault was called on the DragEvent.
  434. * @private
  435. */
  436. goog.fx.Dragger.prototype.fireDragStart_ = function(e) {
  437. return this.dispatchEvent(
  438. new goog.fx.DragEvent(
  439. goog.fx.Dragger.EventType.START, this, e.clientX, e.clientY, e));
  440. };
  441. /**
  442. * Unregisters the event handlers that are only active during dragging, and
  443. * releases mouse capture.
  444. * @private
  445. */
  446. goog.fx.Dragger.prototype.cleanUpAfterDragging_ = function() {
  447. this.eventHandler_.removeAll();
  448. if (goog.fx.Dragger.HAS_SET_CAPTURE_) {
  449. this.document_.releaseCapture();
  450. }
  451. };
  452. /**
  453. * Event handler that is used to end the drag.
  454. * @param {goog.events.BrowserEvent} e Event object.
  455. * @param {boolean=} opt_dragCanceled Whether the drag has been canceled.
  456. */
  457. goog.fx.Dragger.prototype.endDrag = function(e, opt_dragCanceled) {
  458. this.cleanUpAfterDragging_();
  459. if (this.dragging_) {
  460. this.dragging_ = false;
  461. var x = this.limitX(this.deltaX);
  462. var y = this.limitY(this.deltaY);
  463. var dragCanceled =
  464. opt_dragCanceled || e.type == goog.events.EventType.TOUCHCANCEL;
  465. this.dispatchEvent(
  466. new goog.fx.DragEvent(
  467. goog.fx.Dragger.EventType.END, this, e.clientX, e.clientY, e, x, y,
  468. dragCanceled));
  469. } else {
  470. this.dispatchEvent(goog.fx.Dragger.EventType.EARLY_CANCEL);
  471. }
  472. };
  473. /**
  474. * Event handler that is used to end the drag by cancelling it.
  475. * @param {goog.events.BrowserEvent} e Event object.
  476. */
  477. goog.fx.Dragger.prototype.endDragCancel = function(e) {
  478. this.endDrag(e, true);
  479. };
  480. /**
  481. * Event handler that is used on mouse / touch move to update the drag
  482. * @param {goog.events.BrowserEvent} e Event object.
  483. * @private
  484. */
  485. goog.fx.Dragger.prototype.handleMove_ = function(e) {
  486. if (this.enabled_) {
  487. // dx in right-to-left cases is relative to the right.
  488. var sign =
  489. this.useRightPositioningForRtl_ && this.isRightToLeft_() ? -1 : 1;
  490. var dx = sign * (e.clientX - this.clientX);
  491. var dy = e.clientY - this.clientY;
  492. this.clientX = e.clientX;
  493. this.clientY = e.clientY;
  494. this.screenX = e.screenX;
  495. this.screenY = e.screenY;
  496. if (!this.dragging_) {
  497. var diffX = this.startX - this.clientX;
  498. var diffY = this.startY - this.clientY;
  499. var distance = diffX * diffX + diffY * diffY;
  500. if (distance > this.hysteresisDistanceSquared_) {
  501. if (this.fireDragStart_(e)) {
  502. this.dragging_ = true;
  503. } else {
  504. // DragListGroup disposes of the dragger if BEFOREDRAGSTART is
  505. // canceled.
  506. if (!this.isDisposed()) {
  507. this.endDrag(e);
  508. }
  509. return;
  510. }
  511. }
  512. }
  513. var pos = this.calculatePosition_(dx, dy);
  514. var x = pos.x;
  515. var y = pos.y;
  516. if (this.dragging_) {
  517. var rv = this.dispatchEvent(
  518. new goog.fx.DragEvent(
  519. goog.fx.Dragger.EventType.BEFOREDRAG, this, e.clientX, e.clientY,
  520. e, x, y));
  521. // Only do the defaultAction and dispatch drag event if predrag didn't
  522. // prevent default
  523. if (rv) {
  524. this.doDrag(e, x, y, false);
  525. e.preventDefault();
  526. }
  527. }
  528. }
  529. };
  530. /**
  531. * Calculates the drag position.
  532. *
  533. * @param {number} dx The horizontal movement delta.
  534. * @param {number} dy The vertical movement delta.
  535. * @return {!goog.math.Coordinate} The newly calculated drag element position.
  536. * @private
  537. */
  538. goog.fx.Dragger.prototype.calculatePosition_ = function(dx, dy) {
  539. // Update the position for any change in body scrolling
  540. var pageScroll = goog.dom.getDomHelper(this.document_).getDocumentScroll();
  541. dx += pageScroll.x - this.pageScroll.x;
  542. dy += pageScroll.y - this.pageScroll.y;
  543. this.pageScroll = pageScroll;
  544. this.deltaX += dx;
  545. this.deltaY += dy;
  546. var x = this.limitX(this.deltaX);
  547. var y = this.limitY(this.deltaY);
  548. return new goog.math.Coordinate(x, y);
  549. };
  550. /**
  551. * Event handler for scroll target scrolling.
  552. * @param {goog.events.BrowserEvent} e The event.
  553. * @private
  554. */
  555. goog.fx.Dragger.prototype.onScroll_ = function(e) {
  556. var pos = this.calculatePosition_(0, 0);
  557. e.clientX = this.clientX;
  558. e.clientY = this.clientY;
  559. this.doDrag(e, pos.x, pos.y, true);
  560. };
  561. /**
  562. * @param {goog.events.BrowserEvent} e The closure object
  563. * representing the browser event that caused a drag event.
  564. * @param {number} x The new horizontal position for the drag element.
  565. * @param {number} y The new vertical position for the drag element.
  566. * @param {boolean} dragFromScroll Whether dragging was caused by scrolling
  567. * the associated scroll target.
  568. * @protected
  569. */
  570. goog.fx.Dragger.prototype.doDrag = function(e, x, y, dragFromScroll) {
  571. this.defaultAction(x, y);
  572. this.dispatchEvent(
  573. new goog.fx.DragEvent(
  574. goog.fx.Dragger.EventType.DRAG, this, e.clientX, e.clientY, e, x, y));
  575. };
  576. /**
  577. * Returns the 'real' x after limits are applied (allows for some
  578. * limits to be undefined).
  579. * @param {number} x X-coordinate to limit.
  580. * @return {number} The 'real' X-coordinate after limits are applied.
  581. */
  582. goog.fx.Dragger.prototype.limitX = function(x) {
  583. var rect = this.limits;
  584. var left = !isNaN(rect.left) ? rect.left : null;
  585. var width = !isNaN(rect.width) ? rect.width : 0;
  586. var maxX = left != null ? left + width : Infinity;
  587. var minX = left != null ? left : -Infinity;
  588. return Math.min(maxX, Math.max(minX, x));
  589. };
  590. /**
  591. * Returns the 'real' y after limits are applied (allows for some
  592. * limits to be undefined).
  593. * @param {number} y Y-coordinate to limit.
  594. * @return {number} The 'real' Y-coordinate after limits are applied.
  595. */
  596. goog.fx.Dragger.prototype.limitY = function(y) {
  597. var rect = this.limits;
  598. var top = !isNaN(rect.top) ? rect.top : null;
  599. var height = !isNaN(rect.height) ? rect.height : 0;
  600. var maxY = top != null ? top + height : Infinity;
  601. var minY = top != null ? top : -Infinity;
  602. return Math.min(maxY, Math.max(minY, y));
  603. };
  604. /**
  605. * Overridable function for computing the initial position of the target
  606. * before dragging begins.
  607. * @protected
  608. */
  609. goog.fx.Dragger.prototype.computeInitialPosition = function() {
  610. this.deltaX = this.useRightPositioningForRtl_ ?
  611. goog.style.bidi.getOffsetStart(this.target) :
  612. /** @type {!HTMLElement} */ (this.target).offsetLeft;
  613. this.deltaY = /** @type {!HTMLElement} */ (this.target).offsetTop;
  614. };
  615. /**
  616. * Overridable function for handling the default action of the drag behaviour.
  617. * Normally this is simply moving the element to x,y though in some cases it
  618. * might be used to resize the layer. This is basically a shortcut to
  619. * implementing a default ondrag event handler.
  620. * @param {number} x X-coordinate for target element. In right-to-left, x this
  621. * is the number of pixels the target should be moved to from the right.
  622. * @param {number} y Y-coordinate for target element.
  623. */
  624. goog.fx.Dragger.prototype.defaultAction = function(x, y) {
  625. if (this.useRightPositioningForRtl_ && this.isRightToLeft_()) {
  626. this.target.style.right = x + 'px';
  627. } else {
  628. this.target.style.left = x + 'px';
  629. }
  630. this.target.style.top = y + 'px';
  631. };
  632. /**
  633. * @return {boolean} Whether the dragger is currently in the midst of a drag.
  634. */
  635. goog.fx.Dragger.prototype.isDragging = function() {
  636. return this.dragging_;
  637. };
  638. /**
  639. * Object representing a drag event
  640. * @param {string} type Event type.
  641. * @param {goog.fx.Dragger} dragobj Drag object initiating event.
  642. * @param {number} clientX X-coordinate relative to the viewport.
  643. * @param {number} clientY Y-coordinate relative to the viewport.
  644. * @param {goog.events.BrowserEvent} browserEvent The closure object
  645. * representing the browser event that caused this drag event.
  646. * @param {number=} opt_actX Optional actual x for drag if it has been limited.
  647. * @param {number=} opt_actY Optional actual y for drag if it has been limited.
  648. * @param {boolean=} opt_dragCanceled Whether the drag has been canceled.
  649. * @constructor
  650. * @struct
  651. * @extends {goog.events.Event}
  652. */
  653. goog.fx.DragEvent = function(
  654. type, dragobj, clientX, clientY, browserEvent, opt_actX, opt_actY,
  655. opt_dragCanceled) {
  656. goog.events.Event.call(this, type);
  657. /**
  658. * X-coordinate relative to the viewport
  659. * @type {number}
  660. */
  661. this.clientX = clientX;
  662. /**
  663. * Y-coordinate relative to the viewport
  664. * @type {number}
  665. */
  666. this.clientY = clientY;
  667. /**
  668. * The closure object representing the browser event that caused this drag
  669. * event.
  670. * @type {goog.events.BrowserEvent}
  671. */
  672. this.browserEvent = browserEvent;
  673. /**
  674. * The real x-position of the drag if it has been limited
  675. * @type {number}
  676. */
  677. this.left = goog.isDef(opt_actX) ? opt_actX : dragobj.deltaX;
  678. /**
  679. * The real y-position of the drag if it has been limited
  680. * @type {number}
  681. */
  682. this.top = goog.isDef(opt_actY) ? opt_actY : dragobj.deltaY;
  683. /**
  684. * Reference to the drag object for this event
  685. * @type {goog.fx.Dragger}
  686. */
  687. this.dragger = dragobj;
  688. /**
  689. * Whether drag was canceled with this event. Used to differentiate between
  690. * a legitimate drag END that can result in an action and a drag END which is
  691. * a result of a drag cancelation. For now it can happen 1) with drag END
  692. * event on FireFox when user drags the mouse out of the window, 2) with
  693. * drag END event on IE7 which is generated on MOUSEMOVE event when user
  694. * moves the mouse into the document after the mouse button has been
  695. * released, 3) when TOUCHCANCEL is raised instead of TOUCHEND (on touch
  696. * events).
  697. * @type {boolean}
  698. */
  699. this.dragCanceled = !!opt_dragCanceled;
  700. };
  701. goog.inherits(goog.fx.DragEvent, goog.events.Event);