Debian Codegraph @

dijit/layout/AccordionContainer.js

raw symbols SCIP
1 define([
2 "require",
3 "dojo/_base/array", // array.forEach array.map
4 "dojo/_base/declare", // declare
5 "dojo/_base/fx", // fx.Animation
6 "dojo/dom", // dom.setSelectable
7 "dojo/dom-attr", // domAttr.attr
8 "dojo/dom-class", // domClass.remove
9 "dojo/dom-construct", // domConstruct.place
10 "dojo/dom-geometry",
11 "dojo/keys", // keys
12 "dojo/_base/lang", // lang.getObject lang.hitch
13 "dojo/sniff", // has("ie") has("dijit-legacy-requires")
14 "dojo/topic", // publish
15 "../focus", // focus.focus()
16 "../_base/manager", // manager.defaultDuration
17 "dojo/ready",
18 "../_Widget",
19 "../_Container",
20 "../_TemplatedMixin",
21 "../_CssStateMixin",
22 "./StackContainer",
23 "./ContentPane",
24 "dojo/text!./templates/AccordionButton.html",
25 "../a11yclick" // AccordionButton template uses ondijitclick; not for keyboard, but for responsive touch.
26 ], function(require, array, declare, fx, dom, domAttr, domClass, domConstruct, domGeometry, keys, lang, has, topic,
27 focus, manager, ready, _Widget, _Container, _TemplatedMixin, _CssStateMixin, StackContainer, ContentPane, template){
28
29 // module:
30 // dijit/layout/AccordionContainer
31
32
33 // Design notes:
34 //
35 // An AccordionContainer is a StackContainer, but each child (typically ContentPane)
36 // is wrapped in a _AccordionInnerContainer. This is hidden from the caller.
37 //
38 // The resulting markup will look like:
39 //
40 // <div class=dijitAccordionContainer>
41 // <div class=dijitAccordionInnerContainer> (one pane)
42 // <div class=dijitAccordionTitle> (title bar) ... </div>
43 // <div class=dijtAccordionChildWrapper> (content pane) </div>
44 // </div>
45 // </div>
46 //
47 // Normally the dijtAccordionChildWrapper is hidden for all but one child (the shown
48 // child), so the space for the content pane is all the title bars + the one dijtAccordionChildWrapper,
49 // which on claro has a 1px border plus a 2px bottom margin.
50 //
51 // During animation there are two dijtAccordionChildWrapper's shown, so we need
52 // to compensate for that.
53
54 function size(widget, dim){
55 widget.resize ? widget.resize(dim) : domGeometry.setMarginBox(widget.domNode, dim);
56 }
57
58 var AccordionButton = declare("dijit.layout._AccordionButton", [_Widget, _TemplatedMixin, _CssStateMixin], {
59 // summary:
60 // The title bar to click to open up an accordion pane.
61 // Internal widget used by AccordionContainer.
62 // tags:
63 // private
64
65 templateString: template,
66
67 // label: String
68 // Title of the pane
69 label: "",
70 _setLabelAttr: {node: "titleTextNode", type: "innerHTML" },
71
72 // title: String
73 // Tooltip that appears on hover
74 title: "",
75 _setTitleAttr: {node: "titleTextNode", type: "attribute", attribute: "title"},
76
77 // iconClassAttr: String
78 // CSS class for icon to left of label
79 iconClassAttr: "",
80 _setIconClassAttr: { node: "iconNode", type: "class" },
81
82 baseClass: "dijitAccordionTitle",
83
84 getParent: function(){
85 // summary:
86 // Returns the AccordionContainer parent.
87 // tags:
88 // private
89 return this.parent;
90 },
91
92 buildRendering: function(){
93 this.inherited(arguments);
94 var titleTextNodeId = this.id.replace(' ', '_');
95 domAttr.set(this.titleTextNode, "id", titleTextNodeId + "_title");
96 this.focusNode.setAttribute("aria-labelledby", domAttr.get(this.titleTextNode, "id"));
97 dom.setSelectable(this.domNode, false);
98 },
99
100 getTitleHeight: function(){
101 // summary:
102 // Returns the height of the title dom node.
103 return domGeometry.getMarginSize(this.domNode).h; // Integer
104 },
105
106 // TODO: maybe the parent should set these methods directly rather than forcing the code
107 // into the button widget?
108 _onTitleClick: function(){
109 // summary:
110 // Callback when someone clicks my title.
111 var parent = this.getParent();
112 parent.selectChild(this.contentWidget, true);
113 focus.focus(this.focusNode);
114 },
115
116 _onTitleKeyDown: function(/*Event*/ evt){
117 return this.getParent()._onKeyDown(evt, this.contentWidget);
118 },
119
120 _setSelectedAttr: function(/*Boolean*/ isSelected){
121 this._set("selected", isSelected);
122 this.focusNode.setAttribute("aria-expanded", isSelected ? "true" : "false");
123 this.focusNode.setAttribute("aria-selected", isSelected ? "true" : "false");
124 this.focusNode.setAttribute("tabIndex", isSelected ? "0" : "-1");
125 }
126 });
127
128 if(has("dojo-bidi")){
129 AccordionButton.extend({
130 _setLabelAttr: function(label){
131 this._set("label", label);
132 domAttr.set(this.titleTextNode, "innerHTML", label);
133 this.applyTextDir(this.titleTextNode);
134 },
135
136 _setTitleAttr: function(title){
137 this._set("title", title);
138 domAttr.set(this.titleTextNode, "title", title);
139 this.applyTextDir(this.titleTextNode);
140 }
141 });
142 }
143
144 var AccordionInnerContainer = declare("dijit.layout._AccordionInnerContainer" + (has("dojo-bidi") ? "_NoBidi" : ""), [_Widget, _CssStateMixin], {
145 // summary:
146 // Internal widget placed as direct child of AccordionContainer.containerNode.
147 // When other widgets are added as children to an AccordionContainer they are wrapped in
148 // this widget.
149
150 /*=====
151 // buttonWidget: Function|String
152 // Class to use to instantiate title
153 // (Wish we didn't have a separate widget for just the title but maintaining it
154 // for backwards compatibility, is it worth it?)
155 buttonWidget: null,
156 =====*/
157
158 /*=====
159 // contentWidget: dijit/_WidgetBase
160 // Pointer to the real child widget
161 contentWidget: null,
162 =====*/
163
164 baseClass: "dijitAccordionInnerContainer",
165
166 // tell nested layout widget that we will take care of sizing
167 isLayoutContainer: true,
168
169 buildRendering: function(){
170 // Builds a template like:
171 // <div class=dijitAccordionInnerContainer>
172 // Button
173 // <div class=dijitAccordionChildWrapper>
174 // ContentPane
175 // </div>
176 // </div>
177
178 // Create wrapper div, placed where the child is now
179 this.domNode = domConstruct.place("<div class='" + this.baseClass +
180 "' role='presentation'>", this.contentWidget.domNode, "after");
181
182 // wrapper div's first child is the button widget (ie, the title bar)
183 var child = this.contentWidget,
184 cls = lang.isString(this.buttonWidget) ? lang.getObject(this.buttonWidget) : this.buttonWidget;
185 this.button = child._buttonWidget = (new cls({
186 contentWidget: child,
187 label: child.title,
188 title: child.tooltip,
189 dir: child.dir,
190 lang: child.lang,
191 textDir: child.textDir || this.textDir,
192 iconClass: child.iconClass,
193 id: child.id + "_button",
194 parent: this.parent
195 })).placeAt(this.domNode);
196
197 // and then the actual content widget (changing it from prior-sibling to last-child),
198 // wrapped by a <div class=dijitAccordionChildWrapper>
199 this.containerNode = domConstruct.place("<div class='dijitAccordionChildWrapper' role='tabpanel' style='display:none'>", this.domNode);
200 this.containerNode.setAttribute("aria-labelledby", this.button.id);
201
202 domConstruct.place(this.contentWidget.domNode, this.containerNode);
203 },
204
205 postCreate: function(){
206 this.inherited(arguments);
207
208 // Map changes in content widget's title etc. to changes in the button
209 var button = this.button,
210 cw = this.contentWidget;
211 this._contentWidgetWatches = [
212 cw.watch('title', lang.hitch(this, function(name, oldValue, newValue){
213 button.set("label", newValue);
214 })),
215 cw.watch('tooltip', lang.hitch(this, function(name, oldValue, newValue){
216 button.set("title", newValue);
217 })),
218 cw.watch('iconClass', lang.hitch(this, function(name, oldValue, newValue){
219 button.set("iconClass", newValue);
220 }))
221 ];
222 },
223
224 _setSelectedAttr: function(/*Boolean*/ isSelected){
225 this._set("selected", isSelected);
226 this.button.set("selected", isSelected);
227 if(isSelected){
228 var cw = this.contentWidget;
229 if(cw.onSelected){
230 cw.onSelected();
231 }
232 }
233 },
234
235 startup: function(){
236 // Called by _Container.addChild()
237 this.contentWidget.startup();
238 },
239
240 destroy: function(){
241 this.button.destroyRecursive();
242
243 array.forEach(this._contentWidgetWatches || [], function(w){
244 w.unwatch();
245 });
246
247 delete this.contentWidget._buttonWidget;
248 delete this.contentWidget._wrapperWidget;
249
250 this.inherited(arguments);
251 },
252
253 destroyDescendants: function(/*Boolean*/ preserveDom){
254 // since getChildren isn't working for me, have to code this manually
255 this.contentWidget.destroyRecursive(preserveDom);
256 }
257 });
258
259 if(has("dojo-bidi")){
260 AccordionInnerContainer = declare("dijit.layout._AccordionInnerContainer", AccordionInnerContainer, {
261 postCreate: function(){
262 this.inherited(arguments);
263
264 // Map changes in content widget's textdir to changes in the button
265 var button = this.button;
266 this._contentWidgetWatches.push(
267 this.contentWidget.watch("textDir", function(name, oldValue, newValue){
268 button.set("textDir", newValue);
269 })
270 );
271 }
272 });
273 }
274
275 var AccordionContainer = declare("dijit.layout.AccordionContainer", StackContainer, {
276 // summary:
277 // Holds a set of panes where every pane's title is visible, but only one pane's content is visible at a time,
278 // and switching between panes is visualized by sliding the other panes up/down.
279 // example:
280 // | <div data-dojo-type="dijit/layout/AccordionContainer">
281 // | <div data-dojo-type="dijit/layout/ContentPane" title="pane 1">
282 // | </div>
283 // | <div data-dojo-type="dijit/layout/ContentPane" title="pane 2">
284 // | <p>This is some text</p>
285 // | </div>
286 // | </div>
287
288 // duration: Integer
289 // Amount of time (in ms) it takes to slide panes
290 duration: manager.defaultDuration,
291
292 // buttonWidget: [const] String
293 // The name of the widget used to display the title of each pane
294 buttonWidget: AccordionButton,
295
296 /*=====
297 // _verticalSpace: Number
298 // Pixels of space available for the open pane
299 // (my content box size minus the cumulative size of all the title bars)
300 _verticalSpace: 0,
301 =====*/
302 baseClass: "dijitAccordionContainer",
303
304 buildRendering: function(){
305 this.inherited(arguments);
306 this.domNode.style.overflow = "hidden"; // TODO: put this in dijit.css
307 this.domNode.setAttribute("role", "tablist");
308 },
309
310 startup: function(){
311 if(this._started){
312 return;
313 }
314 this.inherited(arguments);
315 if(this.selectedChildWidget){
316 this.selectedChildWidget._wrapperWidget.set("selected", true);
317 }
318 },
319
320 layout: function(){
321 // Implement _LayoutWidget.layout() virtual method.
322 // Set the height of the open pane based on what room remains.
323
324 var openPane = this.selectedChildWidget;
325
326 if(!openPane){
327 return;
328 }
329
330 // space taken up by title, plus wrapper div (with border/margin) for open pane
331 var wrapperDomNode = openPane._wrapperWidget.domNode,
332 wrapperDomNodeMargin = domGeometry.getMarginExtents(wrapperDomNode),
333 wrapperDomNodePadBorder = domGeometry.getPadBorderExtents(wrapperDomNode),
334 wrapperContainerNode = openPane._wrapperWidget.containerNode,
335 wrapperContainerNodeMargin = domGeometry.getMarginExtents(wrapperContainerNode),
336 wrapperContainerNodePadBorder = domGeometry.getPadBorderExtents(wrapperContainerNode),
337 mySize = this._contentBox;
338
339 // get cumulative height of all the unselected title bars
340 var totalCollapsedHeight = 0;
341 array.forEach(this.getChildren(), function(child){
342 if(child != openPane){
343 // Using domGeometry.getMarginSize() rather than domGeometry.position() since claro has 1px bottom margin
344 // to separate accordion panes. Not sure that works perfectly, it's probably putting a 1px
345 // margin below the bottom pane (even though we don't want one).
346 totalCollapsedHeight += domGeometry.getMarginSize(child._wrapperWidget.domNode).h;
347 }
348 });
349 this._verticalSpace = mySize.h - totalCollapsedHeight - wrapperDomNodeMargin.h
350 - wrapperDomNodePadBorder.h - wrapperContainerNodeMargin.h - wrapperContainerNodePadBorder.h
351 - openPane._buttonWidget.getTitleHeight();
352
353 // Memo size to make displayed child
354 this._containerContentBox = {
355 h: this._verticalSpace,
356 w: this._contentBox.w - wrapperDomNodeMargin.w - wrapperDomNodePadBorder.w
357 - wrapperContainerNodeMargin.w - wrapperContainerNodePadBorder.w
358 };
359
360 if(openPane){
361 size(openPane, this._containerContentBox);
362 }
363 },
364
365 _setupChild: function(child){
366 // Overrides _LayoutWidget._setupChild().
367 // Put wrapper widget around the child widget, showing title
368
369 child._wrapperWidget = AccordionInnerContainer({
370 contentWidget: child,
371 buttonWidget: this.buttonWidget,
372 id: child.id + "_wrapper",
373 dir: child.dir,
374 lang: child.lang,
375 textDir: child.textDir || this.textDir,
376 parent: this
377 });
378
379 this.inherited(arguments);
380
381 // Since we are wrapping children in AccordionInnerContainer, replace the default
382 // wrapper that we created in StackContainer.
383 domConstruct.place(child.domNode, child._wrapper, "replace");
384 },
385
386 removeChild: function(child){
387 // Overrides _LayoutWidget.removeChild().
388
389 // Destroy wrapper widget first, before StackContainer.getChildren() call.
390 // Replace wrapper widget with true child widget (ContentPane etc.).
391 // This step only happens if the AccordionContainer has been started; otherwise there's no wrapper.
392 // (TODO: since StackContainer destroys child._wrapper, maybe it can do this step too?)
393 if(child._wrapperWidget){
394 domConstruct.place(child.domNode, child._wrapperWidget.domNode, "after");
395 child._wrapperWidget.destroy();
396 delete child._wrapperWidget;
397 }
398
399 domClass.remove(child.domNode, "dijitHidden");
400
401 this.inherited(arguments);
402 },
403
404 getChildren: function(){
405 // Overrides _Container.getChildren() to return content panes rather than internal AccordionInnerContainer panes
406 return array.map(this.inherited(arguments), function(child){
407 return child.declaredClass == "dijit.layout._AccordionInnerContainer" ? child.contentWidget : child;
408 }, this);
409 },
410
411 destroy: function(){
412 if(this._animation){
413 this._animation.stop();
414 }
415 array.forEach(this.getChildren(), function(child){
416 // If AccordionContainer has been started, then each child has a wrapper widget which
417 // also needs to be destroyed.
418 if(child._wrapperWidget){
419 child._wrapperWidget.destroy();
420 }else{
421 child.destroyRecursive();
422 }
423 });
424 this.inherited(arguments);
425 },
426
427 _showChild: function(child){
428 // Override StackContainer._showChild() to set visibility of _wrapperWidget.containerNode
429 child._wrapperWidget.containerNode.style.display = "block";
430 return this.inherited(arguments);
431 },
432
433 _hideChild: function(child){
434 // Override StackContainer._showChild() to set visibility of _wrapperWidget.containerNode
435 child._wrapperWidget.containerNode.style.display = "none";
436 this.inherited(arguments);
437 },
438
439 _transition: function(/*dijit/_WidgetBase?*/ newWidget, /*dijit/_WidgetBase?*/ oldWidget, /*Boolean*/ animate){
440 // Overrides StackContainer._transition() to provide sliding of title bars etc.
441
442 if(has("ie") < 8){
443 // workaround animation bugs by not animating; not worth supporting animation for IE6 & 7
444 animate = false;
445 }
446
447 if(this._animation){
448 // there's an in-progress animation. speedily end it so we can do the newly requested one
449 this._animation.stop(true);
450 delete this._animation;
451 }
452
453 var self = this;
454
455 if(newWidget){
456 newWidget._wrapperWidget.set("selected", true);
457
458 var d = this._showChild(newWidget); // prepare widget to be slid in
459
460 // Size the new widget, in case this is the first time it's being shown,
461 // or I have been resized since the last time it was shown.
462 // Note that page must be visible for resizing to work.
463 if(this.doLayout){
464 size(newWidget, this._containerContentBox);
465 }
466 }
467
468 if(oldWidget){
469 oldWidget._wrapperWidget.set("selected", false);
470 if(!animate){
471 this._hideChild(oldWidget);
472 }
473 }
474
475 if(animate){
476 var newContents = newWidget._wrapperWidget.containerNode,
477 oldContents = oldWidget._wrapperWidget.containerNode;
478
479 // During the animation we will be showing two dijitAccordionChildWrapper nodes at once,
480 // which on claro takes up 4px extra space (compared to stable AccordionContainer).
481 // Have to compensate for that by immediately shrinking the pane being closed.
482 var wrapperContainerNode = newWidget._wrapperWidget.containerNode,
483 wrapperContainerNodeMargin = domGeometry.getMarginExtents(wrapperContainerNode),
484 wrapperContainerNodePadBorder = domGeometry.getPadBorderExtents(wrapperContainerNode),
485 animationHeightOverhead = wrapperContainerNodeMargin.h + wrapperContainerNodePadBorder.h;
486
487 oldContents.style.height = (self._verticalSpace - animationHeightOverhead) + "px";
488
489 this._animation = new fx.Animation({
490 node: newContents,
491 duration: this.duration,
492 curve: [1, this._verticalSpace - animationHeightOverhead - 1],
493 onAnimate: function(value){
494 value = Math.floor(value); // avoid fractional values
495 newContents.style.height = value + "px";
496 oldContents.style.height = (self._verticalSpace - animationHeightOverhead - value) + "px";
497 },
498 onEnd: function(){
499 delete self._animation;
500 newContents.style.height = "auto";
501 oldWidget._wrapperWidget.containerNode.style.display = "none";
502 oldContents.style.height = "auto";
503 self._hideChild(oldWidget);
504 }
505 });
506 this._animation.onStop = this._animation.onEnd;
507 this._animation.play();
508 }
509
510 return d; // If child has an href, promise that fires when the widget has finished loading
511 },
512
513 // note: we are treating the container as controller here
514 _onKeyDown: function(/*Event*/ e, /*dijit/_WidgetBase*/ fromTitle){
515 // summary:
516 // Handle keydown events
517 // description:
518 // This is called from a handler on AccordionContainer.domNode
519 // (setup in StackContainer), and is also called directly from
520 // the click handler for accordion labels
521 if(this.disabled || e.altKey || !(fromTitle || e.ctrlKey)){
522 return;
523 }
524 var c = e.keyCode;
525 if((fromTitle && (c == keys.LEFT_ARROW || c == keys.UP_ARROW)) ||
526 (e.ctrlKey && c == keys.PAGE_UP)){
527 this._adjacent(false)._buttonWidget._onTitleClick();
528 e.stopPropagation();
529 e.preventDefault();
530 }else if((fromTitle && (c == keys.RIGHT_ARROW || c == keys.DOWN_ARROW)) ||
531 (e.ctrlKey && (c == keys.PAGE_DOWN || c == keys.TAB))){
532 this._adjacent(true)._buttonWidget._onTitleClick();
533 e.stopPropagation();
534 e.preventDefault();
535 }
536 }
537 });
538
539 // Back compat w/1.6, remove for 2.0
540 if(has("dijit-legacy-requires")){
541 ready(0, function(){
542 var requires = ["dijit/layout/AccordionPane"];
543 require(requires); // use indirection so modules not rolled into a build
544 });
545 }
546
547 // For monkey patching
548 AccordionContainer._InnerContainer = AccordionInnerContainer;
549 AccordionContainer._Button = AccordionButton;
550
551 return AccordionContainer;
552 });