Moveable.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. define("dojox/gfx/Moveable", ["dojo/_base/lang","dojo/_base/declare","dojo/_base/array","dojo/_base/event","dojo/_base/connect",
  2. "dojo/dom-class","dojo/_base/window","./Mover"],
  3. function(lang,declare,arr,event,connect,domClass,win,Mover){
  4. return declare("dojox.gfx.Moveable", null, {
  5. constructor: function(shape, params){
  6. // summary: an object, which makes a shape moveable
  7. // shape: dojox.gfx.Shape: a shape object to be moved
  8. // params: Object: an optional object with additional parameters;
  9. // following parameters are recognized:
  10. // delay: Number: delay move by this number of pixels
  11. // mover: Object: a constructor of custom Mover
  12. this.shape = shape;
  13. this.delay = (params && params.delay > 0) ? params.delay : 0;
  14. this.mover = (params && params.mover) ? params.mover : Mover;
  15. this.events = [
  16. this.shape.connect("onmousedown", this, "onMouseDown")
  17. // cancel text selection and text dragging
  18. //, dojo.connect(this.handle, "ondragstart", dojo, "stopEvent")
  19. //, dojo.connect(this.handle, "onselectstart", dojo, "stopEvent")
  20. ];
  21. },
  22. // methods
  23. destroy: function(){
  24. // summary: stops watching for possible move, deletes all references, so the object can be garbage-collected
  25. arr.forEach(this.events, this.shape.disconnect, this.shape);
  26. this.events = this.shape = null;
  27. },
  28. // mouse event processors
  29. onMouseDown: function(e){
  30. // summary: event processor for onmousedown, creates a Mover for the shape
  31. // e: Event: mouse event
  32. if(this.delay){
  33. this.events.push(
  34. this.shape.connect("onmousemove", this, "onMouseMove"),
  35. this.shape.connect("onmouseup", this, "onMouseUp"));
  36. this._lastX = e.clientX;
  37. this._lastY = e.clientY;
  38. }else{
  39. new this.mover(this.shape, e, this);
  40. }
  41. event.stop(e);
  42. },
  43. onMouseMove: function(e){
  44. // summary: event processor for onmousemove, used only for delayed drags
  45. // e: Event: mouse event
  46. if(Math.abs(e.clientX - this._lastX) > this.delay || Math.abs(e.clientY - this._lastY) > this.delay){
  47. this.onMouseUp(e);
  48. new this.mover(this.shape, e, this);
  49. }
  50. event.stop(e);
  51. },
  52. onMouseUp: function(e){
  53. // summary: event processor for onmouseup, used only for delayed delayed drags
  54. // e: Event: mouse event
  55. this.shape.disconnect(this.events.pop());
  56. this.shape.disconnect(this.events.pop());
  57. },
  58. // local events
  59. onMoveStart: function(/* dojox.gfx.Mover */ mover){
  60. // summary: called before every move operation
  61. connect.publish("/gfx/move/start", [mover]);
  62. domClass.add(win.body(), "dojoMove");
  63. },
  64. onMoveStop: function(/* dojox.gfx.Mover */ mover){
  65. // summary: called after every move operation
  66. connect.publish("/gfx/move/stop", [mover]);
  67. domClass.remove(win.body(), "dojoMove");
  68. },
  69. onFirstMove: function(/* dojox.gfx.Mover */ mover){
  70. // summary: called during the very first move notification,
  71. // can be used to initialize coordinates, can be overwritten.
  72. // default implementation does nothing
  73. },
  74. onMove: function(/* dojox.gfx.Mover */ mover, /* Object */ shift){
  75. // summary: called during every move notification,
  76. // should actually move the node, can be overwritten.
  77. this.onMoving(mover, shift);
  78. this.shape.applyLeftTransform(shift);
  79. this.onMoved(mover, shift);
  80. },
  81. onMoving: function(/* dojox.gfx.Mover */ mover, /* Object */ shift){
  82. // summary: called before every incremental move,
  83. // can be overwritten.
  84. // default implementation does nothing
  85. },
  86. onMoved: function(/* dojox.gfx.Mover */ mover, /* Object */ shift){
  87. // summary: called after every incremental move,
  88. // can be overwritten.
  89. // default implementation does nothing
  90. }
  91. });
  92. });