Debian Codegraph @

dojox/layout/ResizeHandle.js

raw symbols SCIP
1 define(["dojo/_base/kernel","dojo/_base/lang","dojo/_base/connect","dojo/_base/array","dojo/_base/event",
2 "dojo/_base/fx","dojo/_base/window","dojo/fx","dojo/dom","dojo/dom-class",
3 "dojo/dom-geometry","dojo/dom-style","dojo/_base/declare", "dojo/touch",
4 "dijit/_base/manager","dijit/_Widget","dijit/_TemplatedMixin"
5 ], function (
6 kernel, lang, connect, arrayUtil, eventUtil, fxBase, windowBase, fxUtil,
7 domUtil, domClass, domGeometry, domStyle, declare, touch, manager, Widget, TemplatedMixin) {
8
9 kernel.experimental("dojox.layout.ResizeHandle");
10
11 var _ResizeHelper = declare("dojox.layout._ResizeHelper", Widget, {
12 // summary:
13 // A global private resize helper shared between any
14 // `dojox.layout.ResizeHandle` with activeSizing off.
15
16 show: function(){
17 // summary:
18 // show helper to start resizing
19 domStyle.set(this.domNode, "display", "");
20 },
21
22 hide: function(){
23 // summary:
24 // hide helper after resizing is complete
25 domStyle.set(this.domNode, "display", "none");
26 },
27
28 resize: function(/* Object */dim){
29 // summary:
30 // size the widget and place accordingly
31 domGeometry.setMarginBox(this.domNode, dim);
32 }
33
34 });
35
36 var ResizeHandle = declare("dojox.layout.ResizeHandle",[Widget, TemplatedMixin],
37 {
38 // summary:
39 // A draggable handle used to resize an attached node.
40 //
41 // description:
42 // The handle on the bottom-right corner of FloatingPane or other widgets that allows
43 // the widget to be resized.
44 // Typically not used directly.
45
46 // targetId: String
47 // id of the Widget OR DomNode that I will size
48 targetId: "",
49
50 // targetContainer: DomNode
51 // over-ride targetId and attch this handle directly to a reference of a DomNode
52 targetContainer: null,
53
54 // resizeAxis: String
55 // one of: x|y|xy limit resizing to a single axis, default to xy ...
56 resizeAxis: "xy",
57
58 // activeResize: Boolean
59 // if true, node will size realtime with mouse movement,
60 // if false, node will create virtual node, and only resize target on mouseUp
61 activeResize: false,
62
63 // activeResizeClass: String
64 // css class applied to virtual resize node.
65 activeResizeClass: "dojoxResizeHandleClone",
66
67 // animateSizing: Boolean
68 // only applicable if activeResize = false. onMouseup, animate the node to the
69 // new size
70 animateSizing: true,
71
72 // animateMethod: String
73 // one of "chain" or "combine" ... visual effect only. combine will "scale"
74 // node to size, "chain" will alter width, then height
75 animateMethod: "chain",
76
77 // animateDuration: Integer
78 // time in MS to run sizing animation. if animateMethod="chain", total animation
79 // playtime is 2*animateDuration
80 animateDuration: 225,
81
82 // minHeight: Integer
83 // smallest height in px resized node can be
84 minHeight: 100,
85
86 // minWidth: Integer
87 // smallest width in px resize node can be
88 minWidth: 100,
89
90 // constrainMax: Boolean
91 // Toggle if this widget cares about the maxHeight and maxWidth
92 // parameters.
93 constrainMax: false,
94
95 // maxHeight: Integer
96 // Largest height size in px the resize node can become.
97 maxHeight:0,
98
99 // maxWidth: Integer
100 // Largest width size in px the resize node can become.
101 maxWidth:0,
102
103 // fixedAspect: Boolean
104 // Toggle to enable this widget to maintain the aspect
105 // ratio of the attached node.
106 fixedAspect: false,
107
108 // intermediateChanges: Boolean
109 // Toggle to enable/disable this widget from firing onResize
110 // events at every step of a resize. If `activeResize` is true,
111 // and this is false, onResize only fires _after_ the drop
112 // operation. Animated resizing is not affected by this setting.
113 intermediateChanges: false,
114
115 // startTopic: String
116 // The name of the topic this resizehandle publishes when resize is starting
117 startTopic: "/dojo/resize/start",
118
119 // endTopic: String
120 // The name of the topic this resizehandle publishes when resize is complete
121 endTopic:"/dojo/resize/stop",
122
123 templateString: '<div dojoAttachPoint="resizeHandle" class="dojoxResizeHandle"><div></div></div>',
124
125 postCreate: function(){
126 // summary:
127 // setup our one major listener upon creation
128 this.connect(this.resizeHandle, touch.press, "_beginSizing");
129 if(!this.activeResize){
130 // there shall be only a single resize rubberbox that at the top
131 // level so that we can overlay it on anything whenever the user
132 // resizes something. Since there is only one mouse pointer he
133 // can't at once resize multiple things interactively.
134 this._resizeHelper = manager.byId('dojoxGlobalResizeHelper');
135 if(!this._resizeHelper){
136 this._resizeHelper = new _ResizeHelper({
137 id: 'dojoxGlobalResizeHelper'
138 }).placeAt(windowBase.body());
139 domClass.add(this._resizeHelper.domNode, this.activeResizeClass);
140 }
141 }else{ this.animateSizing = false; }
142
143 if(!this.minSize){
144 this.minSize = { w: this.minWidth, h: this.minHeight };
145 }
146
147 if(this.constrainMax){
148 this.maxSize = { w: this.maxWidth, h: this.maxHeight };
149 }
150
151 // should we modify the css for the cursor hover to n-resize nw-resize and w-resize?
152 this._resizeX = this._resizeY = false;
153 var addClass = lang.partial(domClass.add, this.resizeHandle);
154 switch(this.resizeAxis.toLowerCase()){
155 case "xy" :
156 this._resizeX = this._resizeY = true;
157 // FIXME: need logic to determine NW or NE class to see
158 // based on which [todo] corner is clicked
159 addClass("dojoxResizeNW");
160 break;
161 case "x" :
162 this._resizeX = true;
163 addClass("dojoxResizeW");
164 break;
165 case "y" :
166 this._resizeY = true;
167 addClass("dojoxResizeN");
168 break;
169 }
170 },
171
172 _beginSizing: function(/*Event*/ e){
173 // summary:
174 // setup movement listeners and calculate initial size
175
176 if(this._isSizing){ return; }
177
178 connect.publish(this.startTopic, [ this ]);
179 this.targetWidget = manager.byId(this.targetId);
180
181 this.targetDomNode = this.targetWidget ? this.targetWidget.domNode : domUtil.byId(this.targetId);
182 if(this.targetContainer){ this.targetDomNode = this.targetContainer; }
183 if(!this.targetDomNode){ return; }
184
185 if(!this.activeResize){
186 var c = domGeometry.position(this.targetDomNode, true);
187 this._resizeHelper.resize({l: c.x, t: c.y, w: c.w, h: c.h});
188 this._resizeHelper.show();
189 if(!this.isLeftToRight()){
190 this._resizeHelper.startPosition = {l: c.x, t: c.y};
191 }
192 }
193
194 this._isSizing = true;
195 this.startPoint = { x:e.clientX, y:e.clientY };
196
197 // widget.resize() or setting style.width/height expects native box model dimension
198 // (in most cases content-box, but it may be border-box if in backcompact mode)
199 var style = domStyle.getComputedStyle(this.targetDomNode),
200 borderModel = domGeometry.boxModel==='border-model',
201 padborder = borderModel?{w:0,h:0}:domGeometry.getPadBorderExtents(this.targetDomNode, style),
202 margin = domGeometry.getMarginExtents(this.targetDomNode, style);
203 this.startSize = {
204 w: domStyle.get(this.targetDomNode, 'width', style),
205 h: domStyle.get(this.targetDomNode, 'height', style),
206 //ResizeHelper.resize expects a bounding box of the
207 //border box, so let's keep track of padding/border
208 //width/height as well
209 pbw: padborder.w, pbh: padborder.h,
210 mw: margin.w, mh: margin.h};
211 if(!this.isLeftToRight() && domStyle.get(this.targetDomNode, "position") == "absolute"){
212 var p = domGeometry.position(this.targetDomNode, true);
213 this.startPosition = {l: p.x, t: p.y};
214 }
215
216 this._pconnects = [
217 connect.connect(windowBase.doc, touch.move, this,"_updateSizing"),
218 connect.connect(windowBase.doc, touch.release, this, "_endSizing")
219 ];
220
221 eventUtil.stop(e);
222 },
223
224 _updateSizing: function(/*Event*/ e){
225 // summary:
226 // called when moving the ResizeHandle ... determines
227 // new size based on settings/position and sets styles.
228
229 if(this.activeResize){
230 this._changeSizing(e);
231 }else{
232 var tmp = this._getNewCoords(e, 'border', this._resizeHelper.startPosition);
233 if(tmp === false){ return; }
234 this._resizeHelper.resize(tmp);
235 }
236 e.preventDefault();
237 },
238
239 _getNewCoords: function(/* Event */ e, /* String */ box, /* Object */startPosition){
240
241 // On IE, if you move the mouse above/to the left of the object being resized,
242 // sometimes clientX/Y aren't set, apparently. Just ignore the event.
243 try{
244 if(!e.clientX || !e.clientY){ return false; }
245 }catch(err){
246 // sometimes you get an exception accessing above fields...
247 return false;
248 }
249 this._activeResizeLastEvent = e;
250
251 var dx = (this.isLeftToRight()?1:-1) * (this.startPoint.x - e.clientX),
252 dy = this.startPoint.y - e.clientY,
253 newW = this.startSize.w - (this._resizeX ? dx : 0),
254 newH = this.startSize.h - (this._resizeY ? dy : 0),
255 r = this._checkConstraints(newW, newH)
256 ;
257
258 startPosition = (startPosition || this.startPosition);
259 if(startPosition && this._resizeX){
260 // adjust x position for RtoL
261 r.l = startPosition.l + dx;
262 if(r.w != newW){
263 r.l += (newW - r.w);
264 }
265 r.t = startPosition.t;
266 }
267
268 switch(box){
269 case 'margin':
270 r.w += this.startSize.mw;
271 r.h += this.startSize.mh;
272 //pass through
273 case "border":
274 r.w += this.startSize.pbw;
275 r.h += this.startSize.pbh;
276 break;
277 //default: //native, do nothing
278 }
279
280 return r; // Object
281 },
282
283 _checkConstraints: function(newW, newH){
284 // summary:
285 // filter through the various possible constaint possibilities.
286
287 // minimum size check
288 if(this.minSize){
289 var tm = this.minSize;
290 if(newW < tm.w){
291 newW = tm.w;
292 }
293 if(newH < tm.h){
294 newH = tm.h;
295 }
296 }
297
298 // maximum size check:
299 if(this.constrainMax && this.maxSize){
300 var ms = this.maxSize;
301 if(newW > ms.w){
302 newW = ms.w;
303 }
304 if(newH > ms.h){
305 newH = ms.h;
306 }
307 }
308
309 if(this.fixedAspect){
310 var w = this.startSize.w, h = this.startSize.h,
311 delta = w * newH - h * newW;
312 if(delta<0){
313 newW = newH * w / h;
314 }else if(delta>0){
315 newH = newW * h / w;
316 }
317 }
318
319 return { w: newW, h: newH }; // Object
320 },
321
322 _changeSizing: function(/*Event*/ e){
323 // summary:
324 // apply sizing information based on information in (e) to attached node
325
326 var isWidget = this.targetWidget && lang.isFunction(this.targetWidget.resize),
327 tmp = this._getNewCoords(e, isWidget && 'margin');
328 if(tmp === false){ return; }
329
330 if(isWidget){
331 this.targetWidget.resize(tmp);
332 }else{
333 if(this.animateSizing){
334 var anim = fxUtil[this.animateMethod]([
335 fxBase.animateProperty({
336 node: this.targetDomNode,
337 properties: {
338 width: { start: this.startSize.w, end: tmp.w }
339 },
340 duration: this.animateDuration
341 }),
342 fxBase.animateProperty({
343 node: this.targetDomNode,
344 properties: {
345 height: { start: this.startSize.h, end: tmp.h }
346 },
347 duration: this.animateDuration
348 })
349 ]);
350 anim.play();
351 }else{
352 domStyle.set(this.targetDomNode,{
353 width: tmp.w + "px",
354 height: tmp.h + "px"
355 });
356 }
357 }
358 if(this.intermediateChanges){
359 this.onResize(e);
360 }
361 },
362
363 _endSizing: function(/*Event*/ e){
364 // summary:
365 // disconnect listenrs and cleanup sizing
366 arrayUtil.forEach(this._pconnects, connect.disconnect);
367 var pub = lang.partial(connect.publish, this.endTopic, [ this ]);
368 if(!this.activeResize){
369 this._resizeHelper.hide();
370 this._changeSizing(e);
371 setTimeout(pub, this.animateDuration + 15);
372 }else{
373 pub();
374 }
375 this._isSizing = false;
376 this.onResize(e);
377 },
378
379 onResize: function(e){
380 // summary:
381 // Stub fired when sizing is done. Fired once
382 // after resize, or often when `intermediateChanges` is
383 // set to true.
384 }
385
386 });
387
388 return ResizeHandle;
389 });