1 | define([ |
---|
2 | "dojo/_base/array", |
---|
3 | "dojo/_base/declare", |
---|
4 | "dojo/dom-class", |
---|
5 | "dojo/dom-construct", |
---|
6 | "dojo/dom-geometry", |
---|
7 | "dojo/dom-style", |
---|
8 | "dijit/_Contained", |
---|
9 | "dijit/_Container", |
---|
10 | "dijit/_WidgetBase", |
---|
11 | "./Heading", |
---|
12 | "./TabBarButton" |
---|
13 | ], function(array, declare, domClass, domConstruct, domGeometry, domStyle, Contained, Container, WidgetBase, Heading, TabBarButton){ |
---|
14 | |
---|
15 | /*===== |
---|
16 | var Contained = dijit._Contained; |
---|
17 | var Container = dijit._Container; |
---|
18 | var WidgetBase = dijit._WidgetBase; |
---|
19 | =====*/ |
---|
20 | |
---|
21 | // module: |
---|
22 | // dojox/mobile/TabBar |
---|
23 | // summary: |
---|
24 | // A bar widget that has buttons to control visibility of views. |
---|
25 | |
---|
26 | return declare("dojox.mobile.TabBar", [WidgetBase, Container, Contained],{ |
---|
27 | // summary: |
---|
28 | // A bar widget that has buttons to control visibility of views. |
---|
29 | // description: |
---|
30 | // TabBar is a container widget that has typically multiple |
---|
31 | // TabBarButtons which controls visibility of views. It can be used |
---|
32 | // as a tab container. |
---|
33 | |
---|
34 | // iconBase: String |
---|
35 | // The default icon path for child items. |
---|
36 | iconBase: "", |
---|
37 | |
---|
38 | // iconPos: String |
---|
39 | // The default icon position for child items. |
---|
40 | iconPos: "", |
---|
41 | |
---|
42 | // barType: String |
---|
43 | // "tabBar"(default) or "segmentedControl". |
---|
44 | barType: "tabBar", |
---|
45 | |
---|
46 | // inHeading: Boolean |
---|
47 | // A flag that indicates whether this widget is in a Heading |
---|
48 | // widget. |
---|
49 | inHeading: false, |
---|
50 | |
---|
51 | // tag: String |
---|
52 | // A name of html tag to create as domNode. |
---|
53 | tag: "UL", |
---|
54 | |
---|
55 | /* internal properties */ |
---|
56 | _fixedButtonWidth: 76, |
---|
57 | _fixedButtonMargin: 17, |
---|
58 | _largeScreenWidth: 500, |
---|
59 | |
---|
60 | buildRendering: function(){ |
---|
61 | this._clsName = this.barType == "segmentedControl" ? "mblTabButton" : "mblTabBarButton"; |
---|
62 | this.domNode = this.containerNode = this.srcNodeRef || domConstruct.create(this.tag); |
---|
63 | this.domNode.className = this.barType == "segmentedControl" ? "mblTabPanelHeader" : "mblTabBar"; |
---|
64 | }, |
---|
65 | |
---|
66 | startup: function(){ |
---|
67 | if(this._started){ return; } |
---|
68 | this.inherited(arguments); |
---|
69 | this.resize(); |
---|
70 | }, |
---|
71 | |
---|
72 | resize: function(size){ |
---|
73 | var i,w; |
---|
74 | if(size && size.w){ |
---|
75 | domGeometry.setMarginBox(this.domNode, size); |
---|
76 | w = size.w; |
---|
77 | }else{ |
---|
78 | // Calculation of the bar width varies according to its "position" value. |
---|
79 | // When the widget is used as a fixed bar, its position would be "absolute". |
---|
80 | w = domStyle.get(this.domNode, "position") === "absolute" ? |
---|
81 | domGeometry.getContentBox(this.domNode).w : domGeometry.getMarginBox(this.domNode).w; |
---|
82 | } |
---|
83 | var bw = this._fixedButtonWidth; |
---|
84 | var bm = this._fixedButtonMargin; |
---|
85 | |
---|
86 | var children = this.containerNode.childNodes; |
---|
87 | var arr = []; |
---|
88 | for(i = 0; i < children.length; i++){ |
---|
89 | var c = children[i]; |
---|
90 | if(c.nodeType != 1){ continue; } |
---|
91 | if(domClass.contains(c, this._clsName)){ |
---|
92 | arr.push(c); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | var margin; |
---|
97 | if(this.barType == "segmentedControl"){ |
---|
98 | margin = w; |
---|
99 | var totalW = 0; // total width of all the buttons |
---|
100 | for(i = 0; i < arr.length; i++){ |
---|
101 | margin -= domGeometry.getMarginBox(arr[i]).w; |
---|
102 | totalW += arr[i].offsetWidth; |
---|
103 | } |
---|
104 | margin = Math.floor(margin/2); |
---|
105 | var parent = this.getParent(); |
---|
106 | var inHeading = this.inHeading || parent instanceof Heading; |
---|
107 | this.containerNode.style.padding = (inHeading ? 0 : 3) + "px 0px 0px " + (inHeading ? 0 : margin) + "px"; |
---|
108 | if(inHeading){ |
---|
109 | domStyle.set(this.domNode, { |
---|
110 | background: "none", |
---|
111 | border: "none", |
---|
112 | width: totalW + 2 + "px" |
---|
113 | }); |
---|
114 | } |
---|
115 | domClass.add(this.domNode, "mblTabBar" + (inHeading ? "Head" : "Top")); |
---|
116 | }else{ |
---|
117 | margin = Math.floor((w - (bw + bm * 2) * arr.length) / 2); |
---|
118 | if(w < this._largeScreenWidth || margin < 0){ |
---|
119 | // If # of buttons is 4, for example, assign "25%" to each button. |
---|
120 | // More precisely, 1%(left margin) + 98%(bar width) + 1%(right margin) |
---|
121 | for(i = 0; i < arr.length; i++){ |
---|
122 | arr[i].style.width = Math.round(98/arr.length) + "%"; |
---|
123 | arr[i].style.margin = "0px"; |
---|
124 | } |
---|
125 | this.containerNode.style.padding = "0px 0px 0px 1%"; |
---|
126 | }else{ |
---|
127 | // Fixed width buttons. Mainly for larger screen such as iPad. |
---|
128 | for(i = 0; i < arr.length; i++){ |
---|
129 | arr[i].style.width = bw + "px"; |
---|
130 | arr[i].style.margin = "0 " + bm + "px"; |
---|
131 | } |
---|
132 | if(arr.length > 0){ |
---|
133 | arr[0].style.marginLeft = margin + bm + "px"; |
---|
134 | } |
---|
135 | this.containerNode.style.padding = "0px"; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | if(!array.some(this.getChildren(), function(child){ return child.iconNode1; })){ |
---|
140 | domClass.add(this.domNode, "mblTabBarNoIcons"); |
---|
141 | }else{ |
---|
142 | domClass.remove(this.domNode, "mblTabBarNoIcons"); |
---|
143 | } |
---|
144 | |
---|
145 | if(!array.some(this.getChildren(), function(child){ return child.label; })){ |
---|
146 | domClass.add(this.domNode, "mblTabBarNoText"); |
---|
147 | }else{ |
---|
148 | domClass.remove(this.domNode, "mblTabBarNoText"); |
---|
149 | } |
---|
150 | } |
---|
151 | }); |
---|
152 | |
---|
153 | }); |
---|