Mover.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. define("dojox/gfx/Mover", ["dojo/_base/lang","dojo/_base/array", "dojo/_base/declare", "dojo/_base/connect", "dojo/_base/event"],
  2. function(lang,arr,declare,connect,evt){
  3. return declare("dojox.gfx.Mover", null, {
  4. constructor: function(shape, e, host){
  5. // summary: an object, which makes a shape follow the mouse,
  6. // used as a default mover, and as a base class for custom movers
  7. // shape: dojox.gfx.Shape: a shape object to be moved
  8. // e: Event: a mouse event, which started the move;
  9. // only clientX and clientY properties are used
  10. // host: Object?: object which implements the functionality of the move,
  11. // and defines proper events (onMoveStart and onMoveStop)
  12. this.shape = shape;
  13. this.lastX = e.clientX
  14. this.lastY = e.clientY;
  15. var h = this.host = host, d = document,
  16. firstEvent = connect.connect(d, "onmousemove", this, "onFirstMove");
  17. this.events = [
  18. connect.connect(d, "onmousemove", this, "onMouseMove"),
  19. connect.connect(d, "onmouseup", this, "destroy"),
  20. // cancel text selection and text dragging
  21. connect.connect(d, "ondragstart", evt, "stop"),
  22. connect.connect(d, "onselectstart", evt, "stop"),
  23. firstEvent
  24. ];
  25. // notify that the move has started
  26. if(h && h.onMoveStart){
  27. h.onMoveStart(this);
  28. }
  29. },
  30. // mouse event processors
  31. onMouseMove: function(e){
  32. // summary: event processor for onmousemove
  33. // e: Event: mouse event
  34. var x = e.clientX;
  35. var y = e.clientY;
  36. this.host.onMove(this, {dx: x - this.lastX, dy: y - this.lastY});
  37. this.lastX = x;
  38. this.lastY = y;
  39. evt.stop(e);
  40. },
  41. // utilities
  42. onFirstMove: function(){
  43. // summary: it is meant to be called only once
  44. this.host.onFirstMove(this);
  45. connect.disconnect(this.events.pop());
  46. },
  47. destroy: function(){
  48. // summary: stops the move, deletes all references, so the object can be garbage-collected
  49. arr.forEach(this.events, connect.disconnect);
  50. // undo global settings
  51. var h = this.host;
  52. if(h && h.onMoveStop){
  53. h.onMoveStop(this);
  54. }
  55. // destroy objects
  56. this.events = this.shape = null;
  57. }
  58. });
  59. });