Mover.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.gfx.Mover"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.gfx.Mover"] = true;
  8. dojo.provide("dojox.gfx.Mover");
  9. dojo.declare("dojox.gfx.Mover", null, {
  10. constructor: function(shape, e, host){
  11. // summary: an object, which makes a shape follow the mouse,
  12. // used as a default mover, and as a base class for custom movers
  13. // shape: dojox.gfx.Shape: a shape object to be moved
  14. // e: Event: a mouse event, which started the move;
  15. // only clientX and clientY properties are used
  16. // host: Object?: object which implements the functionality of the move,
  17. // and defines proper events (onMoveStart and onMoveStop)
  18. this.shape = shape;
  19. this.lastX = e.clientX
  20. this.lastY = e.clientY;
  21. var h = this.host = host, d = document,
  22. firstEvent = dojo.connect(d, "onmousemove", this, "onFirstMove");
  23. this.events = [
  24. dojo.connect(d, "onmousemove", this, "onMouseMove"),
  25. dojo.connect(d, "onmouseup", this, "destroy"),
  26. // cancel text selection and text dragging
  27. dojo.connect(d, "ondragstart", dojo, "stopEvent"),
  28. dojo.connect(d, "onselectstart", dojo, "stopEvent"),
  29. firstEvent
  30. ];
  31. // notify that the move has started
  32. if(h && h.onMoveStart){
  33. h.onMoveStart(this);
  34. }
  35. },
  36. // mouse event processors
  37. onMouseMove: function(e){
  38. // summary: event processor for onmousemove
  39. // e: Event: mouse event
  40. var x = e.clientX;
  41. var y = e.clientY;
  42. this.host.onMove(this, {dx: x - this.lastX, dy: y - this.lastY});
  43. this.lastX = x;
  44. this.lastY = y;
  45. dojo.stopEvent(e);
  46. },
  47. // utilities
  48. onFirstMove: function(){
  49. // summary: it is meant to be called only once
  50. this.host.onFirstMove(this);
  51. dojo.disconnect(this.events.pop());
  52. },
  53. destroy: function(){
  54. // summary: stops the move, deletes all references, so the object can be garbage-collected
  55. dojo.forEach(this.events, dojo.disconnect);
  56. // undo global settings
  57. var h = this.host;
  58. if(h && h.onMoveStop){
  59. h.onMoveStop(this);
  60. }
  61. // destroy objects
  62. this.events = this.shape = null;
  63. }
  64. });
  65. }