move.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. if (!dojo._hasResource["dojox.gfx.move"]) { // _hasResource checks added by
  2. // build. Do not use _hasResource
  3. // directly in your code.
  4. dojo._hasResource["dojox.gfx.move"] = true;
  5. dojo.provide("dojox.gfx.move");
  6. dojo.experimental("dojox.gfx.move");
  7. dojox.gfx.Mover = function(shape, e) {
  8. // summary: an object, which makes a shape follow the mouse,
  9. // used as a default mover, and as a base class for custom movers
  10. // shape: dojox.gfx.Shape: a shape object to be moved
  11. // e: Event: a mouse event, which started the move;
  12. // only clientX and clientY properties are used
  13. this.shape = shape;
  14. this.lastX = e.clientX
  15. this.lastY = e.clientY;
  16. var d = document, firstEvent = dojo.connect(d, "onmousemove", this,
  17. "onFirstMove");
  18. this.events = [dojo.connect(d, "onmousemove", this, "onMouseMove"),
  19. dojo.connect(d, "onmouseup", this, "destroy"),
  20. // cancel text selection and text dragging
  21. dojo.connect(d, "ondragstart", dojo, "stopEvent"),
  22. dojo.connect(d, "onselectstart", dojo, "stopEvent"), firstEvent];
  23. // set globals to indicate that move has started
  24. dojo.publish("/gfx/move/start", [this]);
  25. dojo.addClass(dojo.body(), "dojoMove");
  26. };
  27. dojo.extend(dojox.gfx.Mover, {
  28. // mouse event processors
  29. onMouseMove : function(e) {
  30. // summary: event processor for onmousemove
  31. // e: Event: mouse event
  32. var x = e.clientX;
  33. var y = e.clientY;
  34. this.shape.applyLeftTransform({
  35. dx : x - this.lastX,
  36. dy : y - this.lastY
  37. });
  38. this.lastX = x;
  39. this.lastY = y;
  40. dojo.stopEvent(e);
  41. },
  42. // utilities
  43. onFirstMove : function() {
  44. // summary: it is meant to be called only once
  45. dojo.disconnect(this.events.pop());
  46. },
  47. destroy : function() {
  48. // summary: stops the move, deletes all references, so the
  49. // object can be garbage-collected
  50. dojo.forEach(this.events, dojo.disconnect);
  51. // undo global settings
  52. dojo.publish("/gfx/move/stop", [this]);
  53. dojo.removeClass(dojo.body(), "dojoMove");
  54. // destroy objects
  55. this.events = this.shape = null;
  56. }
  57. });
  58. dojox.gfx.Moveable = function(shape, params) {
  59. // summary: an object, which makes a shape moveable
  60. // shape: dojox.gfx.Shape: a shape object to be moved
  61. // params: Object: an optional object with additional parameters;
  62. // following parameters are recognized:
  63. // delay: Number: delay move by this number of pixels
  64. // mover: Object: a constructor of custom Mover
  65. this.shape = shape;
  66. this.delay = (params && params.delay > 0) ? params.delay : 0;
  67. this.mover = (params && params.mover) ? params.mover : dojox.gfx.Mover;
  68. this.events = [this.shape.connect("onmousedown", this, "onMouseDown"),
  69. // cancel text selection and text dragging
  70. // dojo.connect(this.handle, "ondragstart", dojo, "stopEvent"),
  71. // dojo.connect(this.handle, "onselectstart", dojo, "stopEvent")
  72. ];
  73. };
  74. dojo.extend(dojox.gfx.Moveable, {
  75. // methods
  76. destroy : function() {
  77. // summary: stops watching for possible move, deletes all
  78. // references, so the object can be garbage-collected
  79. dojo.forEach(this.events, "disconnect", this.shape);
  80. this.events = this.shape = null;
  81. },
  82. // mouse event processors
  83. onMouseDown : function(e) {
  84. // summary: event processor for onmousedown, creates a Mover
  85. // for the shape
  86. // e: Event: mouse event
  87. if (this.delay) {
  88. this.events.push(this.shape.connect("onmousemove",
  89. this, "onMouseMove"));
  90. this.events.push(this.shape.connect("onmouseup", this,
  91. "onMouseUp"));
  92. this._lastX = e.clientX;
  93. this._lastY = e.clientY;
  94. } else {
  95. new this.mover(this.shape, e);
  96. }
  97. dojo.stopEvent(e);
  98. },
  99. onMouseMove : function(e) {
  100. // summary: event processor for onmousemove, used only for
  101. // delayed drags
  102. // e: Event: mouse event
  103. if (Math.abs(e.clientX - this._lastX) > this.delay
  104. || Math.abs(e.clientY - this._lastY) > this.delay) {
  105. this.onMouseUp(e);
  106. new this.mover(this.shape, e);
  107. }
  108. dojo.stopEvent(e);
  109. },
  110. onMouseUp : function(e) {
  111. // summary: event processor for onmouseup, used only for
  112. // delayed delayed drags
  113. // e: Event: mouse event
  114. this.shape.disconnect(this.events.pop());
  115. this.shape.disconnect(this.events.pop());
  116. }
  117. });
  118. }