source: Dev/branches/rest-dojo-ui/client/dojox/mobile/TabBarButton.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 7.0 KB
Line 
1define([
2        "dojo/_base/declare",
3        "dojo/_base/lang",
4        "dojo/_base/window",
5        "dojo/dom-class",
6        "dojo/dom-construct",
7        "dijit/registry",       // registry.byNode
8        "./common",
9        "./_ItemBase"
10], function(declare, lang, win, domClass, domConstruct, registry, common, ItemBase){
11
12/*=====
13        var ItemBase = dojox.mobile._ItemBase;
14=====*/
15
16        // module:
17        //              dojox/mobile/TabBarButton
18        // summary:
19        //              A button widget that is placed in the TabBar widget.
20
21        return declare("dojox.mobile.TabBarButton", ItemBase,{
22                // summary:
23                //              A button widget that is placed in the TabBar widget.
24                // description:
25                //              TabBarButton is a button that is placed in the TabBar widget. It
26                //              is a subclass of dojox.mobile._ItemBase just like ListItem or
27                //              IconItem. So, unlike Button, it has similar capability as
28                //              ListItem or IconItem, such as icon support, transition, etc.
29
30                // icon1: String
31                //              A path for the unselected (typically dark) icon. If icon is not
32                //              specified, the iconBase parameter of the parent widget is used.
33                icon1: "",
34
35                // icon2: String
36                //              A path for the selected (typically highlight) icon. If icon is
37                //              not specified, the iconBase parameter of the parent widget or
38                //              icon1 is used.
39                icon2: "",
40
41                // iconPos1: String
42                //              The position of an aggregated unselected (typically dark)
43                //              icon. IconPos1 is comma separated values like
44                //              top,left,width,height (ex. "0,0,29,29"). If iconPos1 is not
45                //              specified, the iconPos parameter of the parent widget is used.
46                iconPos1: "",
47
48                // iconPos2: String
49                //              The position of an aggregated selected (typically highlight)
50                //              icon. IconPos2 is comma separated values like
51                //              top,left,width,height (ex. "0,0,29,29"). If iconPos2 is not
52                //              specified, the iconPos parameter of the parent widget or
53                //              iconPos1 is used.
54                iconPos2: "",
55
56                // selected: Boolean
57                //              If true, the button is in the selected status.
58                selected: false,
59
60                // transition: String
61                //              A type of animated transition effect.
62                transition: "none",
63
64                // tag: String
65                //              A name of html tag to create as domNode.
66                tag: "LI",
67
68                /* internal properties */       
69                selectOne: true,
70
71       
72                inheritParams: function(){
73                        // summary:
74                        //              Overrides dojox.mobile._ItemBase.inheritParams().
75                        if(this.icon && !this.icon1){ this.icon1 = this.icon; }
76                        var parent = this.getParent();
77                        if(parent){
78                                if(!this.transition){ this.transition = parent.transition; }
79                                if(this.icon1 && parent.iconBase &&
80                                        parent.iconBase.charAt(parent.iconBase.length - 1) === '/'){
81                                        this.icon1 = parent.iconBase + this.icon1;
82                                }
83                                if(!this.icon1){ this.icon1 = parent.iconBase; }
84                                if(!this.iconPos1){ this.iconPos1 = parent.iconPos; }
85                                if(this.icon2 && parent.iconBase &&
86                                        parent.iconBase.charAt(parent.iconBase.length - 1) === '/'){
87                                        this.icon2 = parent.iconBase + this.icon2;
88                                }
89                                if(!this.icon2){ this.icon2 = parent.iconBase || this.icon1; }
90                                if(!this.iconPos2){ this.iconPos2 = parent.iconPos || this.iconPos1; }
91                        }
92                },
93       
94                buildRendering: function(){
95                        var a = this.anchorNode = domConstruct.create("A", {className:"mblTabBarButtonAnchor"});
96                        this.connect(a, "onclick", "onClick");
97       
98                        this.box = domConstruct.create("DIV", {className:"mblTabBarButtonTextBox"}, a);
99                        var box = this.box;
100                        var label = "";
101                        var r = this.srcNodeRef;
102                        if(r){
103                                for(var i = 0, len = r.childNodes.length; i < len; i++){
104                                        var n = r.firstChild;
105                                        if(n.nodeType === 3){
106                                                label += lang.trim(n.nodeValue);
107                                        }
108                                        box.appendChild(n);
109                                }
110                        }
111                        if(!this.label){
112                                this.label = label;
113                        }
114       
115                        this.domNode = this.srcNodeRef || domConstruct.create(this.tag);
116                        this.containerNode = this.domNode;
117                        this.domNode.appendChild(a);
118                        if(this.domNode.className.indexOf("mblDomButton") != -1){
119                                // deprecated. TODO: remove this code in 1.8
120                                var domBtn = domConstruct.create("DIV", null, a);
121                                common.createDomButton(this.domNode, null, domBtn);
122                                domClass.add(this.domNode, "mblTabButtonDomButton");
123                                domClass.add(domBtn, "mblTabButtonDomButtonClass");
124                        }
125                        if((this.icon1 || this.icon).indexOf("mblDomButton") != -1){
126                                domClass.add(this.domNode, "mblTabButtonDomButton");
127                        }
128                },
129       
130                startup: function(){
131                        if(this._started){ return; }
132                        this.inheritParams();
133                        var parent = this.getParent();
134       
135                        var _clsName = parent ? parent._clsName : "mblTabBarButton";
136                        domClass.add(this.domNode, _clsName + (this.selected ? " mblTabButtonSelected" : ""));
137       
138                        if(parent && parent.barType == "segmentedControl"){
139                                // proper className may not be set when created dynamically
140                                domClass.remove(this.domNode, "mblTabBarButton");
141                                domClass.add(this.domNode, parent._clsName);
142                                this.box.className = "";
143                        }
144                        this.set({icon1:this.icon1, icon2:this.icon2});
145                        this.inherited(arguments);
146                },
147       
148                select: function(){
149                        // summary:
150                        //              Makes this widget in the selected state.
151                        if(arguments[0]){ // deselect
152                                this.selected = false;
153                                domClass.remove(this.domNode, "mblTabButtonSelected");
154                        }else{ // select
155                                this.selected = true;
156                                domClass.add(this.domNode, "mblTabButtonSelected");
157                                for(var i = 0, c = this.domNode.parentNode.childNodes; i < c.length; i++){
158                                        if(c[i].nodeType != 1){ continue; }
159                                        var w = registry.byNode(c[i]); // sibling widget
160                                        if(w && w != this){
161                                                w.deselect();
162                                        }
163                                }
164                        }
165                        if(this.iconNode1){
166                                this.iconNode1.style.visibility = this.selected ? "hidden" : "";
167                        }
168                        if(this.iconNode2){
169                                this.iconNode2.style.visibility = this.selected ? "" : "hidden";
170                        }
171                },
172               
173                deselect: function(){
174                        // summary:
175                        //              Makes this widget in the deselected state.
176                        this.select(true);
177                },
178       
179                onClick: function(e){
180                        this.defaultClickAction();
181                },
182       
183                _setIcon: function(icon, pos, num, sel){
184                        var i = "icon" + num, n = "iconNode" + num, p = "iconPos" + num;
185                        if(icon){ this[i] = icon; }
186                        if(pos){
187                                if(this[p] === pos){ return; }
188                                this[p] = pos;
189                        }
190                        if(icon && icon !== "none"){
191                                if(!this.iconDivNode){
192                                        this.iconDivNode = domConstruct.create("DIV", {className:"mblTabBarButtonDiv"}, this.anchorNode, "first");
193                                }
194                                if(!this[n]){
195                                        this[n] = domConstruct.create("div", {className:"mblTabBarButtonIcon"}, this.iconDivNode);
196                                }else{
197                                        domConstruct.empty(this[n]);
198                                }
199                                common.createIcon(icon, this[p], null, this.alt, this[n]);
200                                if(this[p]){
201                                        domClass.add(this[n].firstChild, "mblTabBarButtonSpriteIcon");
202                                }
203                                domClass.remove(this.iconDivNode, "mblTabBarButtonNoIcon");
204                                this[n].style.visibility = sel ? "hidden" : "";
205                        }else if(this.iconDivNode){
206                                domClass.add(this.iconDivNode, "mblTabBarButtonNoIcon");
207                        }
208                },
209       
210                _setIcon1Attr: function(icon){
211                        this._setIcon(icon, null, 1, this.selected);
212                },
213       
214                _setIcon2Attr: function(icon){
215                        this._setIcon(icon, null, 2, !this.selected);
216                },
217       
218                _setIconPos1Attr: function(pos){
219                        this._setIcon(null, pos, 1, this.selected);
220                },
221       
222                _setIconPos2Attr: function(pos){
223                        this._setIcon(null, pos, 2, !this.selected);
224                },
225
226                _setLabelAttr: function(/*String*/text){
227                        this.label = text;
228                        this.box.innerHTML = this._cv ? this._cv(text) : text;
229                }
230        });
231});
Note: See TracBrowser for help on using the repository browser.