Debian Codegraph @

dojox/mobile/Accordion.js

raw symbols SCIP
1 define([
2 "dojo/_base/array",
3 "dojo/_base/declare",
4 "dojo/_base/lang",
5 "dojo/sniff",
6 "dojo/dom-class",
7 "dojo/dom-construct",
8 "dojo/dom-attr",
9 "dijit/_Contained",
10 "dijit/_Container",
11 "dijit/_WidgetBase",
12 "./iconUtils",
13 "./lazyLoadUtils",
14 "./_css3",
15 "./common",
16 "require",
17 "dojo/has!dojo-bidi?dojox/mobile/bidi/Accordion"
18 ], function(array, declare, lang, has, domClass, domConstruct, domAttr, Contained, Container, WidgetBase, iconUtils, lazyLoadUtils, css3, common, require, BidiAccordion){
19
20 // module:
21 // dojox/mobile/Accordion
22
23 // inner class
24 var _AccordionTitle = declare([WidgetBase, Contained], {
25 // summary:
26 // A widget for the title of the accordion.
27
28 // label: String
29 // The title of the accordion.
30 label: "Label",
31
32 // icon1: String
33 // A path for the unselected (typically dark) icon. If icon is not
34 // specified, the iconBase parameter of the parent widget is used.
35 icon1: "",
36
37 // icon2: String
38 // A path for the selected (typically highlight) icon. If icon is
39 // not specified, the iconBase parameter of the parent widget or
40 // icon1 is used.
41 icon2: "",
42
43 // iconPos1: String
44 // The position of an aggregated unselected (typically dark)
45 // icon. IconPos1 is a comma-separated list of values like
46 // top,left,width,height (ex. "0,0,29,29"). If iconPos1 is not
47 // specified, the iconPos parameter of the parent widget is used.
48 iconPos1: "",
49
50 // iconPos2: String
51 // The position of an aggregated selected (typically highlight)
52 // icon. IconPos2 is a comma-separated list of values like
53 // top,left,width,height (ex. "0,0,29,29"). If iconPos2 is not
54 // specified, the iconPos parameter of the parent widget or
55 // iconPos1 is used.
56 iconPos2: "",
57
58 // selected: Boolean
59 // If true, the widget is in the selected state.
60 selected: false,
61
62 // baseClass: String
63 // The name of the CSS class of this widget.
64 baseClass: "mblAccordionTitle",
65
66 buildRendering: function(){
67 this.inherited(arguments);
68
69 var a = this.anchorNode = domConstruct.create("a", {
70 className: "mblAccordionTitleAnchor",
71 role: "presentation"
72 }, this.domNode);
73
74 // text box
75 this.textBoxNode = domConstruct.create("div", {className:"mblAccordionTitleTextBox"}, a);
76 this.labelNode = domConstruct.create("span", {
77 className: "mblAccordionTitleLabel",
78 innerHTML: this._cv ? this._cv(this.label) : this.label
79 }, this.textBoxNode);
80 this._isOnLine = this.inheritParams();
81
82 domAttr.set(this.textBoxNode, "role", "tab"); // A11Y
83 domAttr.set(this.textBoxNode, "tabindex", "0");
84 },
85
86 postCreate: function(){
87 this.connect(this.domNode, "onclick", "_onClick");
88 common.setSelectable(this.domNode, false);
89 },
90
91 inheritParams: function(){
92 var parent = this.getParent();
93 if(parent){
94 if(this.icon1 && parent.iconBase &&
95 parent.iconBase.charAt(parent.iconBase.length - 1) === '/'){
96 this.icon1 = parent.iconBase + this.icon1;
97 }
98 if(!this.icon1){ this.icon1 = parent.iconBase; }
99 if(!this.iconPos1){ this.iconPos1 = parent.iconPos; }
100 if(this.icon2 && parent.iconBase &&
101 parent.iconBase.charAt(parent.iconBase.length - 1) === '/'){
102 this.icon2 = parent.iconBase + this.icon2;
103 }
104 if(!this.icon2){ this.icon2 = parent.iconBase || this.icon1; }
105 if(!this.iconPos2){ this.iconPos2 = parent.iconPos || this.iconPos1; }
106 }
107 return !!parent;
108 },
109
110 _setIcon: function(icon, n){
111 // tags:
112 // private
113 if(!this.getParent()){ return; } // icon may be invalid because inheritParams is not called yet
114 this._set("icon" + n, icon);
115 if(!this["iconParentNode" + n]){
116 this["iconParentNode" + n] = domConstruct.create("div",
117 {className:"mblAccordionIconParent mblAccordionIconParent" + n}, this.anchorNode, "first");
118 }
119 this["iconNode" + n] = iconUtils.setIcon(icon, this["iconPos" + n],
120 this["iconNode" + n], this.alt, this["iconParentNode" + n]);
121 this["icon" + n] = icon;
122 domClass.toggle(this.domNode, "mblAccordionHasIcon", icon && icon !== "none");
123 if(has("dojo-bidi") && !this.getParent().isLeftToRight()){
124 this.getParent()._setIconDir(this["iconParentNode" + n]);
125 }
126 },
127
128 _setIcon1Attr: function(icon){
129 // tags:
130 // private
131 this._setIcon(icon, 1);
132 },
133
134 _setIcon2Attr: function(icon){
135 // tags:
136 // private
137 this._setIcon(icon, 2);
138 },
139
140 startup: function(){
141 if(this._started){ return; }
142 if(!this._isOnLine){
143 this.inheritParams();
144 }
145 if(!this._isOnLine){
146 this.set({ // retry applying the attribute
147 icon1: this.icon1,
148 icon2: this.icon2
149 });
150 }
151 this.inherited(arguments);
152 },
153
154 _onClick: function(e){
155 // summary:
156 // Internal handler for click events.
157 // tags:
158 // private
159 if(this.onClick(e) === false){ return; } // user's click action
160 var p = this.getParent();
161 if(!p.fixedHeight && this.contentWidget.domNode.style.display !== "none"){
162 p.collapse(this.contentWidget, !p.animation);
163 }else{
164 p.expand(this.contentWidget, !p.animation);
165 }
166 },
167
168 onClick: function(/*Event*/ /*===== e =====*/){
169 // summary:
170 // User-defined function to handle clicks
171 // tags:
172 // callback
173 },
174
175 _setSelectedAttr: function(/*Boolean*/selected){
176 // tags:
177 // private
178 domClass.toggle(this.domNode, "mblAccordionTitleSelected", selected);
179 this._set("selected", selected);
180 }
181 });
182
183 var Accordion = declare(has("dojo-bidi") ? "dojox.mobile.NonBidiAccordion" : "dojox.mobile.Accordion", [WidgetBase, Container, Contained], {
184 // summary:
185 // A container widget that can display a group of child panes in a stacked format.
186 // description:
187 // Typically, dojox/mobile/Pane, dojox/mobile/Container, or dojox/mobile/ContentPane are
188 // used as child widgets, but Accordion requires no specific child widget.
189 // Accordion supports three modes for opening child panes: multiselect, fixed-height,
190 // and single-select. Accordion can have rounded corners, and it can lazy-load the
191 // content modules.
192
193 // iconBase: String
194 // The default icon path for child widgets.
195 iconBase: "",
196
197 // iconPos: String
198 // The default icon position for child widgets.
199 iconPos: "",
200
201 // fixedHeight: Boolean
202 // If true, the entire accordion widget has fixed height regardless
203 // of the height of each pane; in this mode, there is always an open pane and
204 // collapsing a pane can only be done by opening a different pane.
205 fixedHeight: false,
206
207 // singleOpen: Boolean
208 // If true, only one pane is open at a time. The current open pane
209 // is collapsed, when another pane is opened.
210 singleOpen: false,
211
212 // animation: Boolean
213 // If true, animation is used when a pane is opened or
214 // collapsed. The animation works only on webkit browsers.
215 animation: true,
216
217 // roundRect: Boolean
218 // If true, the widget shows rounded corners.
219 // Adding the "mblAccordionRoundRect" class to domNode has the same effect.
220 roundRect: false,
221
222 /* internal properties */
223 duration: .3, // [seconds]
224
225 // baseClass: String
226 // The name of the CSS class of this widget.
227 baseClass: "mblAccordion",
228
229 // _openSpace: [private] Number|String
230 _openSpace: 1,
231
232 buildRendering: function(){
233 this.inherited(arguments);
234 domAttr.set(this.domNode, "role", "tablist"); // A11Y
235 domAttr.set(this.domNode, "aria-multiselectable", !this.singleOpen); // A11Y
236 },
237
238 startup: function(){
239 if(this._started){ return; }
240
241 if(domClass.contains(this.domNode, "mblAccordionRoundRect")){
242 this.roundRect = true;
243 }else if(this.roundRect){
244 domClass.add(this.domNode, "mblAccordionRoundRect");
245 }
246
247 if(this.fixedHeight){
248 this.singleOpen = true;
249 }
250 var children = this.getChildren();
251 array.forEach(children, this._setupChild, this);
252 var sel;
253 var posinset = 1;
254 array.forEach(children, function(child){
255 child.startup();
256 child._at.startup();
257 this.collapse(child, true);
258 domAttr.set(child._at.textBoxNode, "aria-setsize", children.length);
259 domAttr.set(child._at.textBoxNode, "aria-posinset", posinset++);
260 if(child.selected){
261 sel = child;
262 }
263 }, this);
264 if(!sel && this.fixedHeight){
265 sel = children[children.length - 1];
266 }
267 if(sel){
268 this.expand(sel, true);
269 }else{
270 this._updateLast();
271 }
272 this.defer(function(){ this.resize(); });
273
274 this._started = true;
275 },
276
277 _setupChild: function(/*Widget*/ child){
278 // tags:
279 // private
280 if(child.domNode.style.overflow != "hidden"){
281 child.domNode.style.overflow = this.fixedHeight ? "auto" : "hidden";
282 }
283 child._at = new _AccordionTitle({
284 label: child.label,
285 alt: child.alt,
286 icon1: child.icon1,
287 icon2: child.icon2,
288 iconPos1: child.iconPos1,
289 iconPos2: child.iconPos2,
290 contentWidget: child
291 });
292 domConstruct.place(child._at.domNode, child.domNode, "before");
293 domClass.add(child.domNode, "mblAccordionPane");
294 domAttr.set(child._at.textBoxNode, "aria-controls", child.domNode.id); // A11Y
295 domAttr.set(child.domNode, "role", "tabpanel"); // A11Y
296 domAttr.set(child.domNode, "aria-labelledby", child._at.id); // A11Y
297 },
298
299 addChild: function(/*Widget*/ widget, /*int?*/ insertIndex){
300 this.inherited(arguments);
301 if(this._started){
302 this._setupChild(widget);
303 widget._at.startup();
304 if(widget.selected){
305 this.expand(widget, true);
306 this.defer(function(){
307 widget.domNode.style.height = "";
308 });
309 }else{
310 this.collapse(widget);
311 }
312 this._addChildAriaAttrs();
313 }
314 },
315
316 removeChild: function(/*Widget|int*/ widget){
317 if(typeof widget == "number"){
318 widget = this.getChildren()[widget];
319 }
320 if(widget){
321 widget._at.destroy();
322 }
323 this.inherited(arguments);
324 this._addChildAriaAttrs();
325 },
326
327 _addChildAriaAttrs: function(){
328 var posinset = 1;
329 var children = this.getChildren();
330 array.forEach(children, function(child){
331 domAttr.set(child._at.textBoxNode, "aria-posinset", posinset++);
332 domAttr.set(child._at.textBoxNode, "aria-setsize", children.length);
333 });
334 },
335
336 getChildren: function(){
337 return array.filter(this.inherited(arguments), function(child){
338 return !(child instanceof _AccordionTitle);
339 });
340 },
341
342 getSelectedPanes: function(){
343 return array.filter(this.getChildren(), function(pane){
344 return pane.domNode.style.display != "none";
345 });
346 },
347
348 resize: function(){
349 if(this.fixedHeight){
350 var panes = array.filter(this.getChildren(), function(child){ // active pages
351 return child._at.domNode.style.display != "none";
352 });
353 var openSpace = this.domNode.clientHeight; // height of all panes
354 array.forEach(panes, function(child){
355 openSpace -= child._at.domNode.offsetHeight;
356 });
357 this._openSpace = openSpace > 0 ? openSpace : 0;
358 var sel = this.getSelectedPanes()[0];
359 sel.domNode.style[css3.name("transition")] = "";
360 sel.domNode.style.height = this._openSpace + "px";
361 }
362 },
363
364 _updateLast: function(){
365 // tags:
366 // private
367 var children = this.getChildren();
368 array.forEach(children, function(c, i){
369 // add "mblAccordionTitleLast" to the last, closed accordion title
370 domClass.toggle(c._at.domNode, "mblAccordionTitleLast",
371 i === children.length - 1 && !domClass.contains(c._at.domNode, "mblAccordionTitleSelected"))
372 }, this);
373 },
374
375 expand: function(/*Widget*/pane, /*boolean*/noAnimation){
376 // summary:
377 // Expands the given pane to make it visible.
378 // pane:
379 // A pane widget to expand.
380 // noAnimation:
381 // If true, the pane expands immediately without animation effect.
382 if(pane.lazy){
383 lazyLoadUtils.instantiateLazyWidgets(pane.containerNode, pane.requires);
384 pane.lazy = false;
385 }
386 var children = this.getChildren();
387 array.forEach(children, function(c, i){
388 c.domNode.style[css3.name("transition")] = noAnimation ? "" : "height "+this.duration+"s linear";
389 if(c === pane){
390 c.domNode.style.display = "";
391 var h;
392 if(this.fixedHeight){
393 h = this._openSpace;
394 }else{
395 h = parseInt(c.height || c.domNode.getAttribute("height")); // ScrollableView may have the height property
396 if(!h){
397 c.domNode.style.height = "";
398 h = c.domNode.offsetHeight;
399 c.domNode.style.height = "0px";
400 }
401 }
402 this.defer(function(){ // necessary for webkitTransition to work
403 c.domNode.style.height = h + "px";
404 });
405 this.select(pane);
406 }else if(this.singleOpen){
407 this.collapse(c, noAnimation);
408 }
409 }, this);
410 this._updateLast();
411 domAttr.set(pane.domNode, "aria-expanded", "true"); // A11Y
412 domAttr.set(pane.domNode, "aria-hidden", "false"); // A11Y
413 },
414
415 collapse: function(/*Widget*/pane, /*boolean*/noAnimation){
416 // summary:
417 // Collapses the given pane to close it.
418 // pane:
419 // A pane widget to collapse.
420 // noAnimation:
421 // If true, the pane collapses immediately without animation effect.
422 if(pane.domNode.style.display === "none"){ return; } // already collapsed
423 pane.domNode.style[css3.name("transition")] = noAnimation ? "" : "height "+this.duration+"s linear";
424 pane.domNode.style.height = "0px";
425 if(!has("css3-animations") || noAnimation){
426 pane.domNode.style.display = "none";
427 this._updateLast();
428 }else{
429 // Adding a webkitTransitionEnd handler to panes may cause conflict
430 // when the panes already have the one. (e.g. ScrollableView)
431 var _this = this;
432 _this.defer(function(){
433 pane.domNode.style.display = "none";
434 _this._updateLast();
435
436 // Need to call parent view's resize() especially when the Accordion is
437 // on a ScrollableView, the ScrollableView is scrolled to
438 // the bottom, and then expand any other pane while in the
439 // non-fixed singleOpen mode.
440 if(!_this.fixedHeight && _this.singleOpen){
441 for(var v = _this.getParent(); v; v = v.getParent()){
442 if(domClass.contains(v.domNode, "mblView")){
443 if(v && v.resize){ v.resize(); }
444 break;
445 }
446 }
447 }
448 }, this.duration*1000);
449 }
450 this.deselect(pane);
451 domAttr.set(pane.domNode, "aria-expanded", "false"); // A11Y
452 domAttr.set(pane.domNode, "aria-hidden", "true"); // A11Y
453 },
454
455 select: function(/*Widget*/pane){
456 // summary:
457 // Highlights the title bar of the given pane.
458 // pane:
459 // A pane widget to highlight.
460 pane._at.set("selected", true);
461 domAttr.set(pane._at.textBoxNode, "aria-selected", "true"); // A11Y
462 },
463
464 deselect: function(/*Widget*/pane){
465 // summary:
466 // Unhighlights the title bar of the given pane.
467 // pane:
468 // A pane widget to unhighlight.
469 pane._at.set("selected", false);
470 domAttr.set(pane._at.textBoxNode, "aria-selected", "false"); // A11Y
471 }
472 });
473
474 Accordion.ChildWidgetProperties = {
475 // summary:
476 // These properties can be specified for the children of a dojox/mobile/Accordion.
477
478 // alt: String
479 // The alternate text of the Accordion title.
480 alt: "",
481 // label: String
482 // The label of the Accordion title.
483 label: "",
484 // icon1: String
485 // The unselected icon of the Accordion title.
486 icon1: "",
487 // icon2: String
488 // The selected icon of the Accordion title.
489 icon2: "",
490 // iconPos1: String
491 // The position ("top,left,width,height") of the unselected aggregated icon of the Accordion title.
492 iconPos1: "",
493 // iconPos2: String
494 // The position ("top,left,width,height") of the selected aggregated icon of the Accordion title.
495 iconPos2: "",
496 // selected: Boolean
497 // The selected state of the Accordion title.
498 selected: false,
499 // lazy: Boolean
500 // Specifies that the Accordion child must be lazily loaded.
501 lazy: false
502 };
503
504 // Since any widget can be specified as an Accordion child, mix ChildWidgetProperties
505 // into the base widget class. (This is a hack, but it's effective.)
506 // This is for the benefit of the parser. Remove for 2.0. Also, hide from doc viewer.
507 lang.extend(WidgetBase, /*===== {} || =====*/ Accordion.ChildWidgetProperties);
508
509 return has("dojo-bidi") ? declare("dojox.mobile.Accordion", [Accordion, BidiAccordion]) : Accordion;
510 });