Debian Codegraph @

dojox/mobile/TabBarButton.js

raw symbols SCIP
1 define([
2 "dojo/_base/connect",
3 "dojo/_base/declare",
4 "dojo/_base/event",
5 "dojo/_base/lang",
6 "dojo/dom",
7 "dojo/dom-class",
8 "dojo/dom-construct",
9 "dojo/dom-style",
10 "dojo/dom-attr",
11 "./common",
12 "./View",
13 "./iconUtils",
14 "./_ItemBase",
15 "./Badge",
16 "./sniff",
17 "dojo/has!dojo-bidi?dojox/mobile/bidi/TabBarButton"
18 ], function(connect, declare, event, lang, dom, domClass, domConstruct, domStyle, domAttr, common, View, iconUtils, ItemBase, Badge, has, BidiTabBarButton){
19
20 // module:
21 // dojox/mobile/TabBarButton
22
23 var TabBarButton = declare(has("dojo-bidi") ? "dojox.mobile.NonBidiTabBarButton" : "dojox.mobile.TabBarButton", ItemBase,{
24 // summary:
25 // A button widget that is placed in the TabBar widget.
26 // description:
27 // TabBarButton is a button that is placed in the TabBar widget. It
28 // is a subclass of dojox/mobile/_ItemBase just like ListItem or
29 // IconItem. So, unlike Button, it has similar capability as
30 // ListItem or IconItem, such as icon support, transition, etc.
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 button is in the selected state.
60 selected: false,
61
62 // transition: String
63 // A type of animated transition effect.
64 transition: "none",
65
66 // tag: String
67 // A name of html tag to create as domNode.
68 tag: "li",
69
70 // badge: String
71 // A string to show on a badge. (ex. "12")
72 badge: "",
73
74 // badgeClass: [const] String
75 // A CSS class name of a badge DOM node.
76 badgeClass: "mblDomButtonRedBadge",
77
78 /* internal properties */
79 baseClass: "mblTabBarButton",
80 // closeIcon: [private] String
81 // CSS class for the close icon.
82 closeIcon: "mblDomButtonWhiteCross",
83
84 _selStartMethod: "touch",
85 _selEndMethod: "touch",
86
87 // _moveTo: String
88 // id of destination view
89 _moveTo: "",
90
91 destroy: function(){
92 if(this.badgeObj){
93 delete this.badgeObj;
94 }
95 this.inherited(arguments);
96 },
97
98 inheritParams: function(){
99 // summary:
100 // Overrides dojox/mobile/_ItemBase.inheritParams().
101 if(this.icon && !this.icon1){ this.icon1 = this.icon; }
102 var parent = this.getParent();
103 if(parent){
104 if(!this.transition){ this.transition = parent.transition; }
105 if(this.icon1 && parent.iconBase &&
106 parent.iconBase.charAt(parent.iconBase.length - 1) === '/'){
107 this.icon1 = parent.iconBase + this.icon1;
108 }
109 if(!this.icon1){ this.icon1 = parent.iconBase; }
110 if(!this.iconPos1){ this.iconPos1 = parent.iconPos; }
111 if(this.icon2 && parent.iconBase &&
112 parent.iconBase.charAt(parent.iconBase.length - 1) === '/'){
113 this.icon2 = parent.iconBase + this.icon2;
114 }
115 if(!this.icon2){ this.icon2 = parent.iconBase || this.icon1; }
116 if(!this.iconPos2){ this.iconPos2 = parent.iconPos || this.iconPos1; }
117
118 if(parent.closable){
119 if(!this.icon1){
120 this.icon1 = this.closeIcon;
121 }
122 if(!this.icon2){
123 this.icon2 = this.closeIcon;
124 }
125 domClass.add(this.domNode, "mblTabBarButtonClosable");
126 }
127 }
128 },
129
130 buildRendering: function(){
131 this.domNode = this.srcNodeRef || domConstruct.create(this.tag);
132 if(this.srcNodeRef){
133 if(!this.label){
134 this.label = lang.trim(this.srcNodeRef.innerHTML);
135 }
136 this.srcNodeRef.innerHTML = "";
137 }
138
139 this.labelNode = this.box = domConstruct.create("div", {className:"mblTabBarButtonLabel"}, this.domNode);
140
141 domAttr.set(this.domNode, "role", "tab");
142 domAttr.set(this.domNode, "aria-selected", "false");
143 this._moveTo = this._getMoveToId();
144 if(this._moveTo){
145 var tabPanelNode = dom.byId(this._moveTo);
146 if(tabPanelNode){
147 domAttr.set(this.domNode, "aria-controls", this._moveTo);
148 domAttr.set(tabPanelNode, "role", "tabpanel");
149 domAttr.set(tabPanelNode, "aria-labelledby", this.id);
150 }
151 }
152
153 this.inherited(arguments);
154 },
155
156 startup: function(){
157 if(this._started){ return; }
158
159 this._dragstartHandle = this.connect(this.domNode, "ondragstart", event.stop);
160 this.connect(this.domNode, "onkeydown", "_onClick"); // for desktop browsers
161 var parent = this.getParent();
162 if(parent && parent.closable){
163 this._clickCloseHandler = this.connect(this.iconDivNode, "onclick", "_onCloseButtonClick");
164 this._keydownCloseHandler = this.connect(this.iconDivNode, "onkeydown", "_onCloseButtonClick"); // for desktop browsers
165 this.iconDivNode.tabIndex = "0";
166 }
167
168 this.inherited(arguments);
169 if(!this._isOnLine){
170 this._isOnLine = true;
171 // retry applying the attribute for which the custom setter delays the actual
172 // work until _isOnLine is true.
173 this.set({
174 icon: this._pendingIcon !== undefined ? this._pendingIcon : this.icon,
175 icon1:this.icon1,
176 icon2:this.icon2});
177 // Not needed anymore (this code executes only once per life cycle):
178 delete this._pendingIcon;
179 }
180 common.setSelectable(this.domNode, false);
181 },
182
183 onClose: function(e){
184 // summary:
185 // Called when the parent is a dojox/mobile/TabBar whose closable property is true, and the user clicked the close button.
186 connect.publish("/dojox/mobile/tabClose", [this]);
187 return this.getParent().onCloseButtonClick(this);
188 },
189
190 _onCloseButtonClick: function(e){
191 if(e && e.type === "keydown" && e.keyCode !== 13){ return; }
192 if(this.onCloseButtonClick(e) === false){ return; } // user's click action
193 if(this.onClose()){
194 this.destroy();
195 }
196 },
197
198 onCloseButtonClick: function(/*Event*/ /*===== e =====*/){
199 // summary:
200 // User defined function to handle clicks
201 // when the parent is a dojox/mobile/TabBar whose closable property is true.
202 // tags:
203 // callback
204 },
205
206 _onClick: function(e){
207 // summary:
208 // Internal handler for click events.
209 // tags:
210 // private
211 if(e && e.type === "keydown" && e.keyCode !== 13){ return; }
212 if(this.onClick(e) === false){ return; } // user's click action
213 this.defaultClickAction(e);
214 },
215
216 onClick: function(/*Event*/ /*===== e =====*/){
217 // summary:
218 // User defined function to handle clicks
219 // tags:
220 // callback
221 },
222
223 _setIcon: function(icon, n){
224 if(!this.getParent()){ return; } // icon may be invalid because inheritParams is not called yet
225 this._set("icon" + n, icon);
226 if(!this.iconDivNode){
227 this.iconDivNode = domConstruct.create("div", {className:"mblTabBarButtonIconArea"}, this.domNode, "first");
228 // mblTabBarButtonDiv -> mblTabBarButtonIconArea
229 }
230 if(!this["iconParentNode" + n]){
231 this["iconParentNode" + n] = domConstruct.create("div", {className:"mblTabBarButtonIconParent mblTabBarButtonIconParent" + n}, this.iconDivNode);
232 // mblTabBarButtonIcon -> mblTabBarButtonIconParent
233 }
234 this["iconNode" + n] = iconUtils.setIcon(icon, this["iconPos" + n],
235 this["iconNode" + n], this.alt, this["iconParentNode" + n]);
236 this["icon" + n] = icon;
237 domClass.toggle(this.domNode, "mblTabBarButtonHasIcon", icon && icon !== "none");
238 },
239
240 _getMoveToId: function(){
241 // summary:
242 // Return the id of the destination view.
243 // If there is no id, return an empty string.
244 if(this.moveTo){
245 if(this.moveTo === "#"){ return ""; }
246 var toId = "";
247 if(typeof(this.moveTo) === "object" && this.moveTo.moveTo){
248 toId = this.moveTo.moveTo;
249 }else{
250 toId = this.moveTo;
251 }
252 if(toId){
253 toId = View.prototype.convertToId(toId);
254 }
255 return toId;
256 }
257 },
258
259 _setIcon1Attr: function(icon){
260 this._setIcon(icon, 1);
261 },
262
263 _setIcon2Attr: function(icon){
264 this._setIcon(icon, 2);
265 },
266
267 _getBadgeAttr: function(){
268 return this.badgeObj && this.badgeObj.domNode.parentNode &&
269 this.badgeObj.domNode.parentNode.nodeType == 1 ? this.badgeObj.getValue() : null;
270 },
271
272 _setBadgeAttr: function(/*String*/value){
273 if(!this.badgeObj){
274 this.badgeObj = new Badge({
275 fontSize: 11,
276 className: this.badgeClass
277 });
278 domStyle.set(this.badgeObj.domNode, {
279 position: "absolute",
280 top: "0px",
281 right: "0px"
282 });
283 }
284 this.badgeObj.setValue(value);
285 if(value){
286 this.domNode.appendChild(this.badgeObj.domNode);
287 }else{
288 if(this.domNode === this.badgeObj.domNode.parentNode){
289 this.domNode.removeChild(this.badgeObj.domNode);
290 }
291 }
292 },
293
294 _setSelectedAttr: function(/*Boolean*/selected){
295 // summary:
296 // Makes this widget in the selected or unselected state.
297 this.inherited(arguments);
298 domClass.toggle(this.domNode, "mblTabBarButtonSelected", selected);
299 domAttr.set(this.domNode, "aria-selected", selected ? "true" : "false");
300 if(this._moveTo){
301 var tabPanelNode = dom.byId(this._moveTo);
302 if(tabPanelNode){
303 domAttr.set(tabPanelNode, "aria-hidden", selected ? "false" : "true");
304 }
305 }
306 }
307 });
308
309 return has("dojo-bidi")?declare("dojox.mobile.TabBarButton", [TabBarButton, BidiTabBarButton]):TabBarButton;
310 });