move.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. define("dojo/dnd/move", ["../main", "./Mover", "./Moveable"], function(dojo) {
  2. // module:
  3. // dojo/dnd/move
  4. // summary:
  5. // TODOC
  6. /*=====
  7. dojo.declare("dojo.dnd.move.__constrainedMoveableArgs", [dojo.dnd.__MoveableArgs], {
  8. // constraints: Function
  9. // Calculates a constraint box.
  10. // It is called in a context of the moveable object.
  11. constraints: function(){},
  12. // within: Boolean
  13. // restrict move within boundaries.
  14. within: false
  15. });
  16. =====*/
  17. dojo.declare("dojo.dnd.move.constrainedMoveable", dojo.dnd.Moveable, {
  18. // object attributes (for markup)
  19. constraints: function(){},
  20. within: false,
  21. constructor: function(node, params){
  22. // summary:
  23. // an object that makes a node moveable
  24. // node: Node
  25. // a node (or node's id) to be moved
  26. // params: dojo.dnd.move.__constrainedMoveableArgs?
  27. // an optional object with additional parameters;
  28. // the rest is passed to the base class
  29. if(!params){ params = {}; }
  30. this.constraints = params.constraints;
  31. this.within = params.within;
  32. },
  33. onFirstMove: function(/* dojo.dnd.Mover */ mover){
  34. // summary:
  35. // called during the very first move notification;
  36. // can be used to initialize coordinates, can be overwritten.
  37. var c = this.constraintBox = this.constraints.call(this, mover);
  38. c.r = c.l + c.w;
  39. c.b = c.t + c.h;
  40. if(this.within){
  41. var mb = dojo._getMarginSize(mover.node);
  42. c.r -= mb.w;
  43. c.b -= mb.h;
  44. }
  45. },
  46. onMove: function(/* dojo.dnd.Mover */ mover, /* Object */ leftTop){
  47. // summary:
  48. // called during every move notification;
  49. // should actually move the node; can be overwritten.
  50. var c = this.constraintBox, s = mover.node.style;
  51. this.onMoving(mover, leftTop);
  52. leftTop.l = leftTop.l < c.l ? c.l : c.r < leftTop.l ? c.r : leftTop.l;
  53. leftTop.t = leftTop.t < c.t ? c.t : c.b < leftTop.t ? c.b : leftTop.t;
  54. s.left = leftTop.l + "px";
  55. s.top = leftTop.t + "px";
  56. this.onMoved(mover, leftTop);
  57. }
  58. });
  59. /*=====
  60. dojo.declare("dojo.dnd.move.__boxConstrainedMoveableArgs", [dojo.dnd.move.__constrainedMoveableArgs], {
  61. // box: Object
  62. // a constraint box
  63. box: {}
  64. });
  65. =====*/
  66. dojo.declare("dojo.dnd.move.boxConstrainedMoveable", dojo.dnd.move.constrainedMoveable, {
  67. // box:
  68. // object attributes (for markup)
  69. box: {},
  70. constructor: function(node, params){
  71. // summary:
  72. // an object, which makes a node moveable
  73. // node: Node
  74. // a node (or node's id) to be moved
  75. // params: dojo.dnd.move.__boxConstrainedMoveableArgs?
  76. // an optional object with parameters
  77. var box = params && params.box;
  78. this.constraints = function(){ return box; };
  79. }
  80. });
  81. /*=====
  82. dojo.declare("dojo.dnd.move.__parentConstrainedMoveableArgs", [dojo.dnd.move.__constrainedMoveableArgs], {
  83. // area: String
  84. // A parent's area to restrict the move.
  85. // Can be "margin", "border", "padding", or "content".
  86. area: ""
  87. });
  88. =====*/
  89. dojo.declare("dojo.dnd.move.parentConstrainedMoveable", dojo.dnd.move.constrainedMoveable, {
  90. // area:
  91. // object attributes (for markup)
  92. area: "content",
  93. constructor: function(node, params){
  94. // summary:
  95. // an object, which makes a node moveable
  96. // node: Node
  97. // a node (or node's id) to be moved
  98. // params: dojo.dnd.move.__parentConstrainedMoveableArgs?
  99. // an optional object with parameters
  100. var area = params && params.area;
  101. this.constraints = function(){
  102. var n = this.node.parentNode,
  103. s = dojo.getComputedStyle(n),
  104. mb = dojo._getMarginBox(n, s);
  105. if(area == "margin"){
  106. return mb; // Object
  107. }
  108. var t = dojo._getMarginExtents(n, s);
  109. mb.l += t.l, mb.t += t.t, mb.w -= t.w, mb.h -= t.h;
  110. if(area == "border"){
  111. return mb; // Object
  112. }
  113. t = dojo._getBorderExtents(n, s);
  114. mb.l += t.l, mb.t += t.t, mb.w -= t.w, mb.h -= t.h;
  115. if(area == "padding"){
  116. return mb; // Object
  117. }
  118. t = dojo._getPadExtents(n, s);
  119. mb.l += t.l, mb.t += t.t, mb.w -= t.w, mb.h -= t.h;
  120. return mb; // Object
  121. };
  122. }
  123. });
  124. // patching functions one level up for compatibility
  125. dojo.dnd.constrainedMover = dojo.dnd.move.constrainedMover;
  126. dojo.dnd.boxConstrainedMover = dojo.dnd.move.boxConstrainedMover;
  127. dojo.dnd.parentConstrainedMover = dojo.dnd.move.parentConstrainedMover;
  128. return dojo.dnd.move;
  129. });