move.js 4.8 KB

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