Debian Codegraph @

dijit/layout/StackContainer.js

raw symbols SCIP
1 define([
2 "dojo/_base/array", // array.forEach array.indexOf array.some
3 "dojo/cookie", // cookie
4 "dojo/_base/declare", // declare
5 "dojo/dom-class", // domClass.add domClass.replace
6 "dojo/dom-construct",
7 "dojo/has", // has("dijit-legacy-requires")
8 "dojo/_base/lang", // lang.extend
9 "dojo/on",
10 "dojo/ready",
11 "dojo/topic", // publish
12 "dojo/when",
13 "../registry", // registry.byId
14 "../_WidgetBase",
15 "./_LayoutWidget"
16 ], function(array, cookie, declare, domClass, domConstruct, has, lang, on, ready, topic, when, registry, _WidgetBase, _LayoutWidget){
17
18 // module:
19 // dijit/layout/StackContainer
20
21 // Back compat w/1.6, remove for 2.0
22 if(has("dijit-legacy-requires")){
23 ready(0, function(){
24 var requires = ["dijit/layout/StackController"];
25 require(requires); // use indirection so modules not rolled into a build
26 });
27 }
28
29 var StackContainer = declare("dijit.layout.StackContainer", _LayoutWidget, {
30 // summary:
31 // A container that has multiple children, but shows only
32 // one child at a time
33 //
34 // description:
35 // A container for widgets (ContentPanes, for example) That displays
36 // only one Widget at a time.
37 //
38 // Publishes topics [widgetId]-addChild, [widgetId]-removeChild, and [widgetId]-selectChild
39 //
40 // Can be base class for container, Wizard, Show, etc.
41 //
42 // See `StackContainer.ChildWidgetProperties` for details on the properties that can be set on
43 // children of a `StackContainer`.
44
45 // doLayout: Boolean
46 // If true, change the size of my currently displayed child to match my size
47 doLayout: true,
48
49 // persist: Boolean
50 // Remembers the selected child across sessions
51 persist: false,
52
53 baseClass: "dijitStackContainer",
54
55 /*=====
56 // selectedChildWidget: [readonly] dijit._Widget
57 // References the currently selected child widget, if any.
58 // Adjust selected child with selectChild() method.
59 selectedChildWidget: null,
60 =====*/
61
62 buildRendering: function(){
63 this.inherited(arguments);
64 domClass.add(this.domNode, "dijitLayoutContainer");
65 },
66
67 postCreate: function(){
68 this.inherited(arguments);
69 this.own(
70 on(this.domNode, "keydown", lang.hitch(this, "_onKeyDown"))
71 );
72 },
73
74 startup: function(){
75 if(this._started){
76 return;
77 }
78
79 var children = this.getChildren();
80
81 // Setup each page panel to be initially hidden
82 array.forEach(children, this._setupChild, this);
83
84 // Figure out which child to initially display, defaulting to first one
85 if(this.persist){
86 this.selectedChildWidget = registry.byId(cookie(this.id + "_selectedChild"));
87 }else{
88 array.some(children, function(child){
89 if(child.selected){
90 this.selectedChildWidget = child;
91 }
92 return child.selected;
93 }, this);
94 }
95 var selected = this.selectedChildWidget;
96 if(!selected && children[0]){
97 selected = this.selectedChildWidget = children[0];
98 selected.selected = true;
99 }
100
101 // Publish information about myself so any StackControllers can initialize.
102 // This needs to happen before this.inherited(arguments) so that for
103 // TabContainer, this._contentBox doesn't include the space for the tab labels.
104 topic.publish(this.id + "-startup", {children: children, selected: selected, textDir: this.textDir});
105
106 // Startup each child widget, and do initial layout like setting this._contentBox,
107 // then calls this.resize() which does the initial sizing on the selected child.
108 this.inherited(arguments);
109 },
110
111 resize: function(){
112 // Overrides _LayoutWidget.resize()
113 // Resize is called when we are first made visible (it's called from startup()
114 // if we are initially visible). If this is the first time we've been made
115 // visible then show our first child.
116 if(!this._hasBeenShown){
117 this._hasBeenShown = true;
118 var selected = this.selectedChildWidget;
119 if(selected){
120 this._showChild(selected);
121 }
122 }
123 this.inherited(arguments);
124 },
125
126 _setupChild: function(/*dijit/_WidgetBase*/ child){
127 // Overrides _LayoutWidget._setupChild()
128
129 // For aria support, wrap child widget in a <div role="tabpanel">
130 var childNode = child.domNode,
131 wrapper = domConstruct.place(
132 "<div role='tabpanel' class='" + this.baseClass + "ChildWrapper dijitHidden'>",
133 child.domNode,
134 "replace"),
135 label = child["aria-label"] || child.title || child.label;
136 if(label){
137 // setAttribute() escapes special chars, and if() statement avoids setting aria-label="undefined"
138 wrapper.setAttribute("aria-label", label);
139 }
140 domConstruct.place(childNode, wrapper);
141 child._wrapper = wrapper; // to set the aria-labelledby in StackController
142
143 this.inherited(arguments);
144
145 // child may have style="display: none" (at least our test cases do), so remove that
146 if(childNode.style.display == "none"){
147 childNode.style.display = "block";
148 }
149
150 // remove the title attribute so it doesn't show up when i hover over a node
151 child.domNode.removeAttribute("title");
152 },
153
154 addChild: function(/*dijit/_WidgetBase*/ child, /*Integer?*/ insertIndex){
155 // Overrides _Container.addChild() to do layout and publish events
156
157 this.inherited(arguments);
158
159 if(this._started){
160 topic.publish(this.id + "-addChild", child, insertIndex); // publish
161
162 // in case the tab titles have overflowed from one line to two lines
163 // (or, if this if first child, from zero lines to one line)
164 // TODO: w/ScrollingTabController this is no longer necessary, although
165 // ScrollTabController.resize() does need to get called to show/hide
166 // the navigation buttons as appropriate, but that's handled in ScrollingTabController.onAddChild().
167 // If this is updated to not layout [except for initial child added / last child removed], update
168 // "childless startup" test in StackContainer.html to check for no resize event after second addChild()
169 this.layout();
170
171 // if this is the first child, then select it
172 if(!this.selectedChildWidget){
173 this.selectChild(child);
174 }
175 }
176 },
177
178 removeChild: function(/*dijit/_WidgetBase*/ page){
179 // Overrides _Container.removeChild() to do layout and publish events
180
181 var idx = array.indexOf(this.getChildren(), page);
182
183 this.inherited(arguments);
184
185 // Remove the child widget wrapper we use to set aria roles. This won't affect the page itself since it's
186 // already been detached from page._wrapper via the this.inherited(arguments) call above.
187 domConstruct.destroy(page._wrapper);
188 delete page._wrapper;
189
190 if(this._started){
191 // This will notify any tablists to remove a button; do this first because it may affect sizing.
192 topic.publish(this.id + "-removeChild", page);
193 }
194
195 // If all our children are being destroyed than don't run the code below (to select another page),
196 // because we are deleting every page one by one
197 if(this._descendantsBeingDestroyed){
198 return;
199 }
200
201 // Select new page to display, also updating TabController to show the respective tab.
202 // Do this before layout call because it can affect the height of the TabController.
203 if(this.selectedChildWidget === page){
204 this.selectedChildWidget = undefined;
205 if(this._started){
206 var children = this.getChildren();
207 if(children.length){
208 this.selectChild(children[Math.max(idx - 1, 0)]);
209 }
210 }
211 }
212
213 if(this._started){
214 // In case the tab titles now take up one line instead of two lines
215 // (note though that ScrollingTabController never overflows to multiple lines),
216 // or the height has changed slightly because of addition/removal of tab which close icon
217 this.layout();
218 }
219 },
220
221 selectChild: function(/*dijit/_WidgetBase|String*/ page, /*Boolean*/ animate){
222 // summary:
223 // Show the given widget (which must be one of my children)
224 // page:
225 // Reference to child widget or id of child widget
226
227 var d;
228
229 page = registry.byId(page);
230
231 if(this.selectedChildWidget != page){
232 // Deselect old page and select new one
233 d = this._transition(page, this.selectedChildWidget, animate);
234 this._set("selectedChildWidget", page);
235 topic.publish(this.id + "-selectChild", page, this._focused); // publish
236
237 if(this.persist){
238 cookie(this.id + "_selectedChild", this.selectedChildWidget.id);
239 }
240 }
241
242 // d may be null, or a scalar like true. Return a promise in all cases
243 return when(d || true); // Promise
244 },
245
246 _transition: function(newWidget, oldWidget /*===== , animate =====*/){
247 // summary:
248 // Hide the old widget and display the new widget.
249 // Subclasses should override this.
250 // newWidget: dijit/_WidgetBase
251 // The newly selected widget.
252 // oldWidget: dijit/_WidgetBase
253 // The previously selected widget.
254 // animate: Boolean
255 // Used by AccordionContainer to turn on/off slide effect.
256 // tags:
257 // protected extension
258 if(oldWidget){
259 this._hideChild(oldWidget);
260 }
261 var d = this._showChild(newWidget);
262
263 // Size the new widget, in case this is the first time it's being shown,
264 // or I have been resized since the last time it was shown.
265 // Note that page must be visible for resizing to work.
266 if(newWidget.resize){
267 if(this.doLayout){
268 newWidget.resize(this._containerContentBox || this._contentBox);
269 }else{
270 // the child should pick it's own size but we still need to call resize()
271 // (with no arguments) to let the widget lay itself out
272 newWidget.resize();
273 }
274 }
275
276 return d; // If child has an href, promise that fires when the child's href finishes loading
277 },
278
279 _adjacent: function(/*Boolean*/ forward){
280 // summary:
281 // Gets the next/previous child widget in this container from the current selection.
282
283 // TODO: remove for 2.0 if this isn't being used. Otherwise, fix to skip disabled tabs.
284
285 var children = this.getChildren();
286 var index = array.indexOf(children, this.selectedChildWidget);
287 index += forward ? 1 : children.length - 1;
288 return children[ index % children.length ]; // dijit/_WidgetBase
289 },
290
291 forward: function(){
292 // summary:
293 // Advance to next page.
294 return this.selectChild(this._adjacent(true), true);
295 },
296
297 back: function(){
298 // summary:
299 // Go back to previous page.
300 return this.selectChild(this._adjacent(false), true);
301 },
302
303 _onKeyDown: function(e){
304 topic.publish(this.id + "-containerKeyDown", { e: e, page: this}); // publish
305 },
306
307 layout: function(){
308 // Implement _LayoutWidget.layout() virtual method.
309 var child = this.selectedChildWidget;
310 if(child && child.resize){
311 if(this.doLayout){
312 child.resize(this._containerContentBox || this._contentBox);
313 }else{
314 child.resize();
315 }
316 }
317 },
318
319 _showChild: function(/*dijit/_WidgetBase*/ page){
320 // summary:
321 // Show the specified child by changing it's CSS, and call _onShow()/onShow() so
322 // it can do any updates it needs regarding loading href's etc.
323 // returns:
324 // Promise that fires when page has finished showing, or true if there's no href
325 var children = this.getChildren();
326 page.isFirstChild = (page == children[0]);
327 page.isLastChild = (page == children[children.length - 1]);
328 page._set("selected", true);
329
330 if(page._wrapper){ // false if not started yet
331 domClass.replace(page._wrapper, "dijitVisible", "dijitHidden");
332 }
333
334 return (page._onShow && page._onShow()) || true;
335 },
336
337 _hideChild: function(/*dijit/_WidgetBase*/ page){
338 // summary:
339 // Hide the specified child by changing it's CSS, and call _onHide() so
340 // it's notified.
341 page._set("selected", false);
342
343 if(page._wrapper){ // false if not started yet
344 domClass.replace(page._wrapper, "dijitHidden", "dijitVisible");
345 }
346
347 page.onHide && page.onHide();
348 },
349
350 closeChild: function(/*dijit/_WidgetBase*/ page){
351 // summary:
352 // Callback when user clicks the [X] to remove a page.
353 // If onClose() returns true then remove and destroy the child.
354 // tags:
355 // private
356 var remove = !page.onClose || page.onClose(this, page);
357 if(remove){
358 this.removeChild(page);
359 // makes sure we can clean up executeScripts in ContentPane onUnLoad
360 page.destroyRecursive();
361 }
362 },
363
364 destroyDescendants: function(/*Boolean*/ preserveDom){
365 this._descendantsBeingDestroyed = true;
366 this.selectedChildWidget = undefined;
367 array.forEach(this.getChildren(), function(child){
368 if(!preserveDom){
369 this.removeChild(child);
370 }
371 child.destroyRecursive(preserveDom);
372 }, this);
373 this._descendantsBeingDestroyed = false;
374 }
375 });
376
377 StackContainer.ChildWidgetProperties = {
378 // summary:
379 // These properties can be specified for the children of a StackContainer.
380
381 // selected: Boolean
382 // Specifies that this widget should be the initially displayed pane.
383 // Note: to change the selected child use `dijit/layout/StackContainer.selectChild`
384 selected: false,
385
386 // disabled: Boolean
387 // Specifies that the button to select this pane should be disabled.
388 // Doesn't affect programmatic selection of the pane, nor does it deselect the pane if it is currently selected.
389 disabled: false,
390
391 // closable: Boolean
392 // True if user can close (destroy) this child, such as (for example) clicking the X on the tab.
393 closable: false,
394
395 // iconClass: String
396 // CSS Class specifying icon to use in label associated with this pane.
397 iconClass: "dijitNoIcon",
398
399 // showTitle: Boolean
400 // When true, display title of this widget as tab label etc., rather than just using
401 // icon specified in iconClass
402 showTitle: true
403 };
404
405 // Since any widget can be specified as a StackContainer child, mix them
406 // into the base widget class. (This is a hack, but it's effective.)
407 // This is for the benefit of the parser. Remove for 2.0. Also, hide from doc viewer.
408 lang.extend(_WidgetBase, /*===== {} || =====*/ StackContainer.ChildWidgetProperties);
409
410 return StackContainer;
411 });