Moveable.js 3.7 KB

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