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