Debian Codegraph @

dijit/layout/BorderContainer.js

raw symbols SCIP
1 define([
2 "dojo/_base/array", // array.filter array.forEach array.map
3 "dojo/cookie", // cookie
4 "dojo/_base/declare", // declare
5 "dojo/dom-class", // domClass.add domClass.remove domClass.toggle
6 "dojo/dom-construct", // domConstruct.destroy domConstruct.place
7 "dojo/dom-geometry", // domGeometry.marginBox
8 "dojo/dom-style", // domStyle.style
9 "dojo/keys",
10 "dojo/_base/lang", // getObject() hitch() delegate()
11 "dojo/on",
12 "dojo/touch",
13 "../_WidgetBase",
14 "../_Widget",
15 "../_TemplatedMixin",
16 "./LayoutContainer",
17 "./utils" // layoutUtils.layoutChildren
18 ], function(array, cookie, declare, domClass, domConstruct, domGeometry, domStyle, keys, lang, on, touch,
19 _WidgetBase, _Widget, _TemplatedMixin, LayoutContainer, layoutUtils){
20
21 // module:
22 // dijit/layout/BorderContainer
23
24 var _Splitter = declare("dijit.layout._Splitter", [_Widget, _TemplatedMixin ], {
25 // summary:
26 // A draggable spacer between two items in a `dijit/layout/BorderContainer`.
27 // description:
28 // This is instantiated by `dijit/layout/BorderContainer`. Users should not
29 // create it directly.
30 // tags:
31 // private
32
33 /*=====
34 // container: [const] dijit/layout/BorderContainer
35 // Pointer to the parent BorderContainer
36 container: null,
37
38 // child: [const] dijit/layout/_LayoutWidget
39 // Pointer to the pane associated with this splitter
40 child: null,
41
42 // region: [const] String
43 // Region of pane associated with this splitter.
44 // "top", "bottom", "left", "right".
45 region: null,
46 =====*/
47
48 // live: [const] Boolean
49 // If true, the child's size changes and the child widget is redrawn as you drag the splitter;
50 // otherwise, the size doesn't change until you drop the splitter (by mouse-up)
51 live: true,
52
53 templateString: '<div class="dijitSplitter" data-dojo-attach-event="onkeydown:_onKeyDown,press:_startDrag,onmouseenter:_onMouse,onmouseleave:_onMouse" tabIndex="0" role="separator"><div class="dijitSplitterThumb"></div></div>',
54
55 constructor: function(){
56 this._handlers = [];
57 },
58
59 postMixInProperties: function(){
60 this.inherited(arguments);
61
62 this.horizontal = /top|bottom/.test(this.region);
63 this._factor = /top|left/.test(this.region) ? 1 : -1;
64 this._cookieName = this.container.id + "_" + this.region;
65 },
66
67 buildRendering: function(){
68 this.inherited(arguments);
69
70 domClass.add(this.domNode, "dijitSplitter" + (this.horizontal ? "H" : "V"));
71
72 if(this.container.persist){
73 // restore old size
74 var persistSize = this._getPersistentSplit();
75 if(persistSize){
76 this.child.domNode.style[this.horizontal ? "height" : "width"] = persistSize;
77 }
78 }
79 },
80
81 _computeMaxSize: function(){
82 // summary:
83 // Return the maximum size that my corresponding pane can be set to
84
85 var dim = this.horizontal ? 'h' : 'w',
86 childSize = domGeometry.getMarginBox(this.child.domNode)[dim],
87 center = array.filter(this.container.getChildren(), function(child){
88 return child.region == "center";
89 })[0];
90
91 // Can expand until center is crushed. But always leave room for center's padding + border,
92 // otherwise on the next call domGeometry methods start to lie about size.
93 var spaceAvailable = domGeometry.getContentBox(center.domNode)[dim] - 10;
94
95 return Math.min(this.child.maxSize, childSize + spaceAvailable);
96 },
97
98 _startDrag: function(e){
99 if(!this.cover){
100 this.cover = domConstruct.place("<div class=dijitSplitterCover></div>", this.child.domNode, "after");
101 }
102 domClass.add(this.cover, "dijitSplitterCoverActive");
103
104 // Safeguard in case the stop event was missed. Shouldn't be necessary if we always get the mouse up.
105 if(this.fake){
106 domConstruct.destroy(this.fake);
107 }
108 if(!(this._resize = this.live)){ //TODO: disable live for IE6?
109 // create fake splitter to display at old position while we drag
110 (this.fake = this.domNode.cloneNode(true)).removeAttribute("id");
111 domClass.add(this.domNode, "dijitSplitterShadow");
112 domConstruct.place(this.fake, this.domNode, "after");
113 }
114 domClass.add(this.domNode, "dijitSplitterActive dijitSplitter" + (this.horizontal ? "H" : "V") + "Active");
115 if(this.fake){
116 domClass.remove(this.fake, "dijitSplitterHover dijitSplitter" + (this.horizontal ? "H" : "V") + "Hover");
117 }
118
119 //Performance: load data info local vars for onmousevent function closure
120 var factor = this._factor,
121 isHorizontal = this.horizontal,
122 axis = isHorizontal ? "pageY" : "pageX",
123 pageStart = e[axis],
124 splitterStyle = this.domNode.style,
125 dim = isHorizontal ? 'h' : 'w',
126 childCS = domStyle.getComputedStyle(this.child.domNode),
127 childStart = domGeometry.getMarginBox(this.child.domNode, childCS)[dim],
128 max = this._computeMaxSize(),
129 min = Math.max(this.child.minSize, domGeometry.getPadBorderExtents(this.child.domNode, childCS)[dim] + 10),
130 region = this.region,
131 splitterAttr = region == "top" || region == "bottom" ? "top" : "left", // style attribute of splitter to adjust
132 splitterStart = parseInt(splitterStyle[splitterAttr], 10),
133 resize = this._resize,
134 layoutFunc = lang.hitch(this.container, "_layoutChildren", this.child.id),
135 de = this.ownerDocument;
136
137 this._handlers = this._handlers.concat([
138 on(de, touch.move, this._drag = function(e, forceResize){
139 var delta = e[axis] - pageStart,
140 childSize = factor * delta + childStart,
141 boundChildSize = Math.max(Math.min(childSize, max), min);
142
143 if(resize || forceResize){
144 layoutFunc(boundChildSize);
145 }
146 // TODO: setting style directly (usually) sets content box size, need to set margin box size
147 splitterStyle[splitterAttr] = delta + splitterStart + factor * (boundChildSize - childSize) + "px";
148 }),
149 on(de, "dragstart", function(e){
150 e.stopPropagation();
151 e.preventDefault();
152 }),
153 on(this.ownerDocumentBody, "selectstart", function(e){
154 e.stopPropagation();
155 e.preventDefault();
156 }),
157 on(de, touch.release, lang.hitch(this, "_stopDrag"))
158 ]);
159 e.stopPropagation();
160 e.preventDefault();
161 },
162
163 _onMouse: function(e){
164 // summary:
165 // Handler for onmouseenter / onmouseleave events
166 var o = (e.type == "mouseover" || e.type == "mouseenter");
167 domClass.toggle(this.domNode, "dijitSplitterHover", o);
168 domClass.toggle(this.domNode, "dijitSplitter" + (this.horizontal ? "H" : "V") + "Hover", o);
169 },
170
171 _getPersistentSplit: function() {
172 return cookie(this._cookieName);
173 },
174
175 _setPersistentSplit: function(value) {
176 cookie(this._cookieName, value, {expires: 365});
177 },
178
179 _stopDrag: function(e){
180 try{
181 if(this.cover){
182 domClass.remove(this.cover, "dijitSplitterCoverActive");
183 }
184 if(this.fake){
185 domConstruct.destroy(this.fake);
186 }
187 domClass.remove(this.domNode, "dijitSplitterActive dijitSplitter"
188 + (this.horizontal ? "H" : "V") + "Active dijitSplitterShadow");
189 this._drag(e); //TODO: redundant with onmousemove?
190 this._drag(e, true);
191 }finally{
192 this._cleanupHandlers();
193 delete this._drag;
194 }
195
196 if(this.container.persist){
197 this._setPersistentSplit(this.child.domNode.style[this.horizontal ? "height" : "width"]);
198 }
199 },
200
201 _cleanupHandlers: function(){
202 var h;
203 while(h = this._handlers.pop()){
204 h.remove();
205 }
206 },
207
208 _onKeyDown: function(/*Event*/ e){
209 // should we apply typematic to this?
210 this._resize = true;
211 var horizontal = this.horizontal;
212 var tick = 1;
213 switch(e.keyCode){
214 case horizontal ? keys.UP_ARROW : keys.LEFT_ARROW:
215 tick *= -1;
216 // break;
217 case horizontal ? keys.DOWN_ARROW : keys.RIGHT_ARROW:
218 break;
219 default:
220 // this.inherited(arguments);
221 return;
222 }
223 var childSize = domGeometry.getMarginSize(this.child.domNode)[ horizontal ? 'h' : 'w' ] + this._factor * tick;
224 this.container._layoutChildren(this.child.id, Math.max(Math.min(childSize, this._computeMaxSize()), this.child.minSize));
225 e.stopPropagation();
226 e.preventDefault();
227 },
228
229 destroy: function(){
230 this._cleanupHandlers();
231 delete this.child;
232 delete this.container;
233 delete this.cover;
234 delete this.fake;
235 this.inherited(arguments);
236 }
237 });
238
239 var _Gutter = declare("dijit.layout._Gutter", [_Widget, _TemplatedMixin], {
240 // summary:
241 // Just a spacer div to separate side pane from center pane.
242 // Basically a trick to lookup the gutter/splitter width from the theme.
243 // description:
244 // Instantiated by `dijit/layout/BorderContainer`. Users should not
245 // create directly.
246 // tags:
247 // private
248
249 templateString: '<div class="dijitGutter" role="presentation"></div>',
250
251 postMixInProperties: function(){
252 this.inherited(arguments);
253 this.horizontal = /top|bottom/.test(this.region);
254 },
255
256 buildRendering: function(){
257 this.inherited(arguments);
258 domClass.add(this.domNode, "dijitGutter" + (this.horizontal ? "H" : "V"));
259 }
260 });
261
262 var BorderContainer = declare("dijit.layout.BorderContainer", LayoutContainer, {
263 // summary:
264 // A BorderContainer is a `dijit/LayoutContainer` that can have draggable splitters between the children,
265 // in order to adjust their sizes.
266 //
267 // In addition, it automatically adds some space between the children even
268 // if they don't have a draggable splitter between them, and space between the edge of the BorderContainer
269 // and the children that are adjacent to the edge. Note that the intended style is that all the children
270 // have borders, but (despite the name) the BorderContainer itself does not.
271 //
272 // See `BorderContainer.ChildWidgetProperties` for details on the properties that can be set on
273 // children of a `BorderContainer`.
274
275 // gutters: [const] Boolean
276 // Give each pane a border and margin.
277 // Margin determined by domNode.paddingLeft.
278 // When false, only resizable panes have a gutter (i.e. draggable splitter) for resizing.
279 gutters: true,
280
281 // liveSplitters: [const] Boolean
282 // Specifies whether splitters resize as you drag (true) or only upon mouseup (false)
283 liveSplitters: true,
284
285 // persist: Boolean
286 // Save splitter positions in a cookie.
287 persist: false,
288
289 baseClass: "dijitBorderContainer",
290
291 // _splitterClass: Function||String
292 // Optional hook to override the default Splitter widget used by BorderContainer
293 _splitterClass: _Splitter,
294
295 postMixInProperties: function(){
296 // change class name to indicate that BorderContainer is being used purely for
297 // layout (like LayoutContainer) rather than for pretty formatting.
298 if(!this.gutters){
299 this.baseClass += "NoGutter";
300 }
301 this.inherited(arguments);
302 },
303
304 _setupChild: function(/*dijit/_WidgetBase*/ child){
305 // Override LayoutContainer._setupChild().
306
307 this.inherited(arguments);
308
309 var region = child.region, ltr = child.isLeftToRight();
310 if(region == "leading"){
311 region = ltr ? "left" : "right";
312 }
313 if(region == "trailing"){
314 region = ltr ? "right" : "left";
315 }
316
317 if(region){
318 // Create draggable splitter for resizing pane,
319 // or alternately if splitter=false but BorderContainer.gutters=true then
320 // insert dummy div just for spacing
321 if(region != "center" && (child.splitter || this.gutters) && !child._splitterWidget){
322 var _Splitter = child.splitter ? this._splitterClass : _Gutter;
323 if(lang.isString(_Splitter)){
324 _Splitter = lang.getObject(_Splitter); // for back-compat, remove in 2.0
325 }
326 var splitter = new _Splitter({
327 id: child.id + "_splitter",
328 container: this,
329 child: child,
330 region: region,
331 live: this.liveSplitters
332 });
333 splitter.isSplitter = true;
334 child._splitterWidget = splitter;
335
336 // Make the tab order match the visual layout by placing the splitter before or after the pane,
337 // depending on where the splitter is visually compared to the pane.
338 var before = region == "bottom" || region == (this.isLeftToRight() ? "right" : "left");
339 domConstruct.place(splitter.domNode, child.domNode, before ? "before" : "after");
340
341 // Splitters aren't added as Contained children, so we need to call startup explicitly
342 splitter.startup();
343 }
344 }
345 },
346
347 layout: function(){
348 // Implement _LayoutWidget.layout() virtual method.
349 this._layoutChildren();
350 },
351
352 removeChild: function(/*dijit/_WidgetBase*/ child){
353 // Override _LayoutWidget.removeChild().
354
355 var splitter = child._splitterWidget;
356 if(splitter){
357 splitter.destroy();
358 delete child._splitterWidget;
359 }
360
361 this.inherited(arguments);
362 },
363
364 getChildren: function(){
365 // Override _LayoutWidget.getChildren() to only return real children, not the splitters.
366 return array.filter(this.inherited(arguments), function(widget){
367 return !widget.isSplitter;
368 });
369 },
370
371 // TODO: remove in 2.0
372 getSplitter: function(/*String*/region){
373 // summary:
374 // Returns the widget responsible for rendering the splitter associated with region
375 // tags:
376 // deprecated
377 return array.filter(this.getChildren(), function(child){
378 return child.region == region;
379 })[0]._splitterWidget;
380 },
381
382 resize: function(newSize, currentSize){
383 // Overrides _LayoutWidget.resize().
384
385 // resetting potential padding to 0px to provide support for 100% width/height + padding
386 // TODO: this hack doesn't respect the box model and is a temporary fix
387 if(!this.cs || !this.pe){
388 var node = this.domNode;
389 this.cs = domStyle.getComputedStyle(node);
390 this.pe = domGeometry.getPadExtents(node, this.cs);
391 this.pe.r = domStyle.toPixelValue(node, this.cs.paddingRight);
392 this.pe.b = domStyle.toPixelValue(node, this.cs.paddingBottom);
393
394 domStyle.set(node, "padding", "0px");
395 }
396
397 this.inherited(arguments);
398 },
399
400 _layoutChildren: function(/*String?*/ changedChildId, /*Number?*/ changedChildSize){
401 // summary:
402 // This is the main routine for setting size/position of each child.
403 // description:
404 // With no arguments, measures the height of top/bottom panes, the width
405 // of left/right panes, and then sizes all panes accordingly.
406 //
407 // With changedRegion specified (as "left", "top", "bottom", or "right"),
408 // it changes that region's width/height to changedRegionSize and
409 // then resizes other regions that were affected.
410 // changedChildId:
411 // Id of the child which should be resized because splitter was dragged.
412 // changedChildSize:
413 // The new width/height (in pixels) to make specified child
414
415 if(!this._borderBox || !this._borderBox.h){
416 // We are currently hidden, or we haven't been sized by our parent yet.
417 // Abort. Someone will resize us later.
418 return;
419 }
420
421 // Combining the externally specified children with splitters and gutters
422 var childrenAndSplitters = [];
423 array.forEach(this._getOrderedChildren(), function(pane){
424 childrenAndSplitters.push(pane);
425 if(pane._splitterWidget){
426 childrenAndSplitters.push(pane._splitterWidget);
427 }
428 });
429
430 // Compute the box in which to lay out my children
431 var dim = {
432 l: this.pe.l,
433 t: this.pe.t,
434 w: this._borderBox.w - this.pe.w,
435 h: this._borderBox.h - this.pe.h
436 };
437
438 // Layout the children, possibly changing size due to a splitter drag
439 layoutUtils.layoutChildren(this.domNode, dim, childrenAndSplitters,
440 changedChildId, changedChildSize);
441 },
442
443 destroyRecursive: function(){
444 // Destroy splitters first, while getChildren() still works
445 array.forEach(this.getChildren(), function(child){
446 var splitter = child._splitterWidget;
447 if(splitter){
448 splitter.destroy();
449 }
450 delete child._splitterWidget;
451 });
452
453 // Then destroy the real children, and myself
454 this.inherited(arguments);
455 }
456 });
457
458 BorderContainer.ChildWidgetProperties = {
459 // summary:
460 // These properties can be specified for the children of a BorderContainer.
461
462 // splitter: [const] Boolean
463 // Parameter for children where region != "center".
464 // If true, enables user to resize the widget by putting a draggable splitter between
465 // this widget and the region=center widget.
466 splitter: false,
467
468 // minSize: [const] Number
469 // Specifies a minimum size (in pixels) for this widget when resized by a splitter.
470 minSize: 0,
471
472 // maxSize: [const] Number
473 // Specifies a maximum size (in pixels) for this widget when resized by a splitter.
474 maxSize: Infinity
475 };
476 lang.mixin(BorderContainer.ChildWidgetProperties, LayoutContainer.ChildWidgetProperties);
477
478 // Since any widget can be specified as a BorderContainer child, mix it
479 // into the base widget class. (This is a hack, but it's effective.)
480 // This is for the benefit of the parser. Remove for 2.0. Also, hide from doc viewer.
481 lang.extend(_WidgetBase, /*===== {} || =====*/ BorderContainer.ChildWidgetProperties);
482
483 // For monkey patching
484 BorderContainer._Splitter = _Splitter;
485 BorderContainer._Gutter = _Gutter;
486
487 return BorderContainer;
488 });