TabBarButton.js 7.0 KB

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