Drag.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Handler.js
  7. */
  8. /**
  9. * Class: OpenLayers.Handler.Drag
  10. * The drag handler is used to deal with sequences of browser events related
  11. * to dragging. The handler is used by controls that want to know when
  12. * a drag sequence begins, when a drag is happening, and when it has
  13. * finished.
  14. *
  15. * Controls that use the drag handler typically construct it with callbacks
  16. * for 'down', 'move', and 'done'. Callbacks for these keys are called
  17. * when the drag begins, with each move, and when the drag is done. In
  18. * addition, controls can have callbacks keyed to 'up' and 'out' if they
  19. * care to differentiate between the types of events that correspond with
  20. * the end of a drag sequence. If no drag actually occurs (no mouse move)
  21. * the 'down' and 'up' callbacks will be called, but not the 'done'
  22. * callback.
  23. *
  24. * Create a new drag handler with the <OpenLayers.Handler.Drag> constructor.
  25. *
  26. * Inherits from:
  27. * - <OpenLayers.Handler>
  28. */
  29. OpenLayers.Handler.Drag = OpenLayers.Class(OpenLayers.Handler, {
  30. /**
  31. * Property: started
  32. * {Boolean} When a mousedown or touchstart event is received, we want to
  33. * record it, but not set 'dragging' until the mouse moves after starting.
  34. */
  35. started: false,
  36. /**
  37. * Property: stopDown
  38. * {Boolean} Stop propagation of mousedown events from getting to listeners
  39. * on the same element. Default is true.
  40. */
  41. stopDown: true,
  42. /**
  43. * Property: dragging
  44. * {Boolean}
  45. */
  46. dragging: false,
  47. /**
  48. * Property: last
  49. * {<OpenLayers.Pixel>} The last pixel location of the drag.
  50. */
  51. last: null,
  52. /**
  53. * Property: start
  54. * {<OpenLayers.Pixel>} The first pixel location of the drag.
  55. */
  56. start: null,
  57. /**
  58. * Property: lastMoveEvt
  59. * {Object} The last mousemove event that occurred. Used to
  60. * position the map correctly when our "delay drag"
  61. * timeout expired.
  62. */
  63. lastMoveEvt: null,
  64. /**
  65. * Property: oldOnselectstart
  66. * {Function}
  67. */
  68. oldOnselectstart: null,
  69. /**
  70. * Property: interval
  71. * {Integer} In order to increase performance, an interval (in
  72. * milliseconds) can be set to reduce the number of drag events
  73. * called. If set, a new drag event will not be set until the
  74. * interval has passed.
  75. * Defaults to 0, meaning no interval.
  76. */
  77. interval: 0,
  78. /**
  79. * Property: timeoutId
  80. * {String} The id of the timeout used for the mousedown interval.
  81. * This is "private", and should be left alone.
  82. */
  83. timeoutId: null,
  84. /**
  85. * APIProperty: documentDrag
  86. * {Boolean} If set to true, the handler will also handle mouse moves when
  87. * the cursor has moved out of the map viewport. Default is false.
  88. */
  89. documentDrag: false,
  90. /**
  91. * Property: documentEvents
  92. * {Boolean} Are we currently observing document events?
  93. */
  94. documentEvents: null,
  95. /**
  96. * Constructor: OpenLayers.Handler.Drag
  97. * Returns OpenLayers.Handler.Drag
  98. *
  99. * Parameters:
  100. * control - {<OpenLayers.Control>} The control that is making use of
  101. * this handler. If a handler is being used without a control, the
  102. * handlers setMap method must be overridden to deal properly with
  103. * the map.
  104. * callbacks - {Object} An object containing a single function to be
  105. * called when the drag operation is finished. The callback should
  106. * expect to recieve a single argument, the pixel location of the event.
  107. * Callbacks for 'move' and 'done' are supported. You can also speficy
  108. * callbacks for 'down', 'up', and 'out' to respond to those events.
  109. * options - {Object}
  110. */
  111. initialize: function(control, callbacks, options) {
  112. OpenLayers.Handler.prototype.initialize.apply(this, arguments);
  113. if (this.documentDrag === true) {
  114. var me = this;
  115. this._docMove = function(evt) {
  116. me.mousemove({
  117. xy: {x: evt.clientX, y: evt.clientY},
  118. element: document
  119. });
  120. };
  121. this._docUp = function(evt) {
  122. me.mouseup({xy: {x: evt.clientX, y: evt.clientY}});
  123. };
  124. }
  125. },
  126. /**
  127. * Method: dragstart
  128. * This private method is factorized from mousedown and touchstart methods
  129. *
  130. * Parameters:
  131. * evt - {Event} The event
  132. *
  133. * Returns:
  134. * {Boolean} Let the event propagate.
  135. */
  136. dragstart: function (evt) {
  137. var propagate = true;
  138. this.dragging = false;
  139. if (this.checkModifiers(evt) &&
  140. (OpenLayers.Event.isLeftClick(evt) ||
  141. OpenLayers.Event.isSingleTouch(evt))) {
  142. this.started = true;
  143. this.start = evt.xy;
  144. this.last = evt.xy;
  145. OpenLayers.Element.addClass(
  146. this.map.viewPortDiv, "olDragDown"
  147. );
  148. this.down(evt);
  149. this.callback("down", [evt.xy]);
  150. // prevent document dragging
  151. OpenLayers.Event.preventDefault(evt);
  152. if(!this.oldOnselectstart) {
  153. this.oldOnselectstart = document.onselectstart ?
  154. document.onselectstart : OpenLayers.Function.True;
  155. }
  156. document.onselectstart = OpenLayers.Function.False;
  157. propagate = !this.stopDown;
  158. } else {
  159. this.started = false;
  160. this.start = null;
  161. this.last = null;
  162. }
  163. return propagate;
  164. },
  165. /**
  166. * Method: dragmove
  167. * This private method is factorized from mousemove and touchmove methods
  168. *
  169. * Parameters:
  170. * evt - {Event} The event
  171. *
  172. * Returns:
  173. * {Boolean} Let the event propagate.
  174. */
  175. dragmove: function (evt) {
  176. this.lastMoveEvt = evt;
  177. if (this.started && !this.timeoutId && (evt.xy.x != this.last.x ||
  178. evt.xy.y != this.last.y)) {
  179. if(this.documentDrag === true && this.documentEvents) {
  180. if(evt.element === document) {
  181. this.adjustXY(evt);
  182. // do setEvent manually because the documentEvents are not
  183. // registered with the map
  184. this.setEvent(evt);
  185. } else {
  186. this.removeDocumentEvents();
  187. }
  188. }
  189. if (this.interval > 0) {
  190. this.timeoutId = setTimeout(
  191. OpenLayers.Function.bind(this.removeTimeout, this),
  192. this.interval);
  193. }
  194. this.dragging = true;
  195. this.move(evt);
  196. this.callback("move", [evt.xy]);
  197. if(!this.oldOnselectstart) {
  198. this.oldOnselectstart = document.onselectstart;
  199. document.onselectstart = OpenLayers.Function.False;
  200. }
  201. this.last = evt.xy;
  202. }
  203. return true;
  204. },
  205. /**
  206. * Method: dragend
  207. * This private method is factorized from mouseup and touchend methods
  208. *
  209. * Parameters:
  210. * evt - {Event} The event
  211. *
  212. * Returns:
  213. * {Boolean} Let the event propagate.
  214. */
  215. dragend: function (evt) {
  216. if (this.started) {
  217. if(this.documentDrag === true && this.documentEvents) {
  218. this.adjustXY(evt);
  219. this.removeDocumentEvents();
  220. }
  221. var dragged = (this.start != this.last);
  222. this.started = false;
  223. this.dragging = false;
  224. OpenLayers.Element.removeClass(
  225. this.map.viewPortDiv, "olDragDown"
  226. );
  227. this.up(evt);
  228. this.callback("up", [evt.xy]);
  229. if(dragged) {
  230. this.callback("done", [evt.xy]);
  231. }
  232. document.onselectstart = this.oldOnselectstart;
  233. }
  234. return true;
  235. },
  236. /**
  237. * The four methods below (down, move, up, and out) are used by subclasses
  238. * to do their own processing related to these mouse events.
  239. */
  240. /**
  241. * Method: down
  242. * This method is called during the handling of the mouse down event.
  243. * Subclasses can do their own processing here.
  244. *
  245. * Parameters:
  246. * evt - {Event} The mouse down event
  247. */
  248. down: function(evt) {
  249. },
  250. /**
  251. * Method: move
  252. * This method is called during the handling of the mouse move event.
  253. * Subclasses can do their own processing here.
  254. *
  255. * Parameters:
  256. * evt - {Event} The mouse move event
  257. *
  258. */
  259. move: function(evt) {
  260. },
  261. /**
  262. * Method: up
  263. * This method is called during the handling of the mouse up event.
  264. * Subclasses can do their own processing here.
  265. *
  266. * Parameters:
  267. * evt - {Event} The mouse up event
  268. */
  269. up: function(evt) {
  270. },
  271. /**
  272. * Method: out
  273. * This method is called during the handling of the mouse out event.
  274. * Subclasses can do their own processing here.
  275. *
  276. * Parameters:
  277. * evt - {Event} The mouse out event
  278. */
  279. out: function(evt) {
  280. },
  281. /**
  282. * The methods below are part of the magic of event handling. Because
  283. * they are named like browser events, they are registered as listeners
  284. * for the events they represent.
  285. */
  286. /**
  287. * Method: mousedown
  288. * Handle mousedown events
  289. *
  290. * Parameters:
  291. * evt - {Event}
  292. *
  293. * Returns:
  294. * {Boolean} Let the event propagate.
  295. */
  296. mousedown: function(evt) {
  297. return this.dragstart(evt);
  298. },
  299. /**
  300. * Method: touchstart
  301. * Handle touchstart events
  302. *
  303. * Parameters:
  304. * evt - {Event}
  305. *
  306. * Returns:
  307. * {Boolean} Let the event propagate.
  308. */
  309. touchstart: function(evt) {
  310. this.startTouch();
  311. return this.dragstart(evt);
  312. },
  313. /**
  314. * Method: mousemove
  315. * Handle mousemove events
  316. *
  317. * Parameters:
  318. * evt - {Event}
  319. *
  320. * Returns:
  321. * {Boolean} Let the event propagate.
  322. */
  323. mousemove: function(evt) {
  324. return this.dragmove(evt);
  325. },
  326. /**
  327. * Method: touchmove
  328. * Handle touchmove events
  329. *
  330. * Parameters:
  331. * evt - {Event}
  332. *
  333. * Returns:
  334. * {Boolean} Let the event propagate.
  335. */
  336. touchmove: function(evt) {
  337. return this.dragmove(evt);
  338. },
  339. /**
  340. * Method: removeTimeout
  341. * Private. Called by mousemove() to remove the drag timeout.
  342. */
  343. removeTimeout: function() {
  344. this.timeoutId = null;
  345. // if timeout expires while we're still dragging (mouseup
  346. // hasn't occurred) then call mousemove to move to the
  347. // correct position
  348. if(this.dragging) {
  349. this.mousemove(this.lastMoveEvt);
  350. }
  351. },
  352. /**
  353. * Method: mouseup
  354. * Handle mouseup events
  355. *
  356. * Parameters:
  357. * evt - {Event}
  358. *
  359. * Returns:
  360. * {Boolean} Let the event propagate.
  361. */
  362. mouseup: function(evt) {
  363. return this.dragend(evt);
  364. },
  365. /**
  366. * Method: touchend
  367. * Handle touchend events
  368. *
  369. * Parameters:
  370. * evt - {Event}
  371. *
  372. * Returns:
  373. * {Boolean} Let the event propagate.
  374. */
  375. touchend: function(evt) {
  376. // override evt.xy with last position since touchend does not have
  377. // any touch position
  378. evt.xy = this.last;
  379. return this.dragend(evt);
  380. },
  381. /**
  382. * Method: mouseout
  383. * Handle mouseout events
  384. *
  385. * Parameters:
  386. * evt - {Event}
  387. *
  388. * Returns:
  389. * {Boolean} Let the event propagate.
  390. */
  391. mouseout: function (evt) {
  392. if (this.started && OpenLayers.Util.mouseLeft(evt, this.map.viewPortDiv)) {
  393. if(this.documentDrag === true) {
  394. this.addDocumentEvents();
  395. } else {
  396. var dragged = (this.start != this.last);
  397. this.started = false;
  398. this.dragging = false;
  399. OpenLayers.Element.removeClass(
  400. this.map.viewPortDiv, "olDragDown"
  401. );
  402. this.out(evt);
  403. this.callback("out", []);
  404. if(dragged) {
  405. this.callback("done", [evt.xy]);
  406. }
  407. if(document.onselectstart) {
  408. document.onselectstart = this.oldOnselectstart;
  409. }
  410. }
  411. }
  412. return true;
  413. },
  414. /**
  415. * Method: click
  416. * The drag handler captures the click event. If something else registers
  417. * for clicks on the same element, its listener will not be called
  418. * after a drag.
  419. *
  420. * Parameters:
  421. * evt - {Event}
  422. *
  423. * Returns:
  424. * {Boolean} Let the event propagate.
  425. */
  426. click: function (evt) {
  427. // let the click event propagate only if the mouse moved
  428. return (this.start == this.last);
  429. },
  430. /**
  431. * Method: activate
  432. * Activate the handler.
  433. *
  434. * Returns:
  435. * {Boolean} The handler was successfully activated.
  436. */
  437. activate: function() {
  438. var activated = false;
  439. if(OpenLayers.Handler.prototype.activate.apply(this, arguments)) {
  440. this.dragging = false;
  441. activated = true;
  442. }
  443. return activated;
  444. },
  445. /**
  446. * Method: deactivate
  447. * Deactivate the handler.
  448. *
  449. * Returns:
  450. * {Boolean} The handler was successfully deactivated.
  451. */
  452. deactivate: function() {
  453. var deactivated = false;
  454. if(OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) {
  455. this.started = false;
  456. this.dragging = false;
  457. this.start = null;
  458. this.last = null;
  459. deactivated = true;
  460. OpenLayers.Element.removeClass(
  461. this.map.viewPortDiv, "olDragDown"
  462. );
  463. }
  464. return deactivated;
  465. },
  466. /**
  467. * Method: adjustXY
  468. * Converts event coordinates that are relative to the document body to
  469. * ones that are relative to the map viewport. The latter is the default in
  470. * OpenLayers.
  471. *
  472. * Parameters:
  473. * evt - {Object}
  474. */
  475. adjustXY: function(evt) {
  476. var pos = OpenLayers.Util.pagePosition(this.map.viewPortDiv);
  477. evt.xy.x -= pos[0];
  478. evt.xy.y -= pos[1];
  479. },
  480. /**
  481. * Method: addDocumentEvents
  482. * Start observing document events when documentDrag is true and the mouse
  483. * cursor leaves the map viewport while dragging.
  484. */
  485. addDocumentEvents: function() {
  486. OpenLayers.Element.addClass(document.body, "olDragDown");
  487. this.documentEvents = true;
  488. OpenLayers.Event.observe(document, "mousemove", this._docMove);
  489. OpenLayers.Event.observe(document, "mouseup", this._docUp);
  490. },
  491. /**
  492. * Method: removeDocumentEvents
  493. * Stops observing document events when documentDrag is true and the mouse
  494. * cursor re-enters the map viewport while dragging.
  495. */
  496. removeDocumentEvents: function() {
  497. OpenLayers.Element.removeClass(document.body, "olDragDown");
  498. this.documentEvents = false;
  499. OpenLayers.Event.stopObserving(document, "mousemove", this._docMove);
  500. OpenLayers.Event.stopObserving(document, "mouseup", this._docUp);
  501. },
  502. CLASS_NAME: "OpenLayers.Handler.Drag"
  503. });