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