TabBar.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.mobile.TabBar"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.mobile.TabBar"] = true;
  8. dojo.provide("dojox.mobile.TabBar");
  9. dojo.require("dojox.mobile");
  10. dojo.declare(
  11. "dojox.mobile.TabBar",
  12. dijit._WidgetBase,
  13. {
  14. iconBase: "",
  15. iconPos: "",
  16. barType: "tabBar", // "tabBar"(default) or "segmentedControl"
  17. inHeading: false,
  18. _fixedButtonWidth: 76,
  19. _fixedButtonMargin: 17,
  20. _largeScreenWidth: 500,
  21. buildRendering: function(){
  22. this._clsName = this.barType == "segmentedControl" ? "mblTabButton" : "mblTabBarButton";
  23. this.domNode = this.containerNode = this.srcNodeRef || dojo.doc.createElement("H1");
  24. this.domNode.className = this.barType == "segmentedControl" ? "mblTabPanelHeader" : "mblTabBar";
  25. },
  26. postCreate: function(){
  27. if(dojo.global.onorientationchange !== undefined){
  28. this.connect(dojo.global, "onorientationchange", "onResize");
  29. }else{
  30. this.connect(dojo.global, "onresize", "onResize");
  31. }
  32. },
  33. startup: function(){
  34. var _this = this;
  35. setTimeout(function(){ // to get proper dimension
  36. _this.onResize();
  37. }, 0);
  38. },
  39. onResize: function(){
  40. var i;
  41. var w = dojo.marginBox(this.domNode.parentNode).w;
  42. var bw = this._fixedButtonWidth;
  43. var bm = this._fixedButtonMargin;
  44. var children = this.containerNode.childNodes;
  45. var arr = [];
  46. for(i = 0; i < children.length; i++){
  47. var c = children[i];
  48. if(c.nodeType != 1){ continue; }
  49. if(dojo.hasClass(c, this._clsName)){
  50. arr.push(c);
  51. }
  52. }
  53. var margin;
  54. if(this.barType == "segmentedControl"){
  55. margin = w;
  56. var totalW = 0; // total width of all the buttons
  57. for(i = 0; i < arr.length; i++){
  58. margin -= dojo.marginBox(arr[i]).w;
  59. arr[i].style.marginTop = "3px";
  60. totalW += arr[i].offsetWidth;
  61. }
  62. margin = Math.floor(margin/2);
  63. var parent = dijit.getEnclosingWidget(this.domNode.parentNode);
  64. var inHeading = this.inHeading || parent instanceof dojox.mobile.Heading;
  65. this.containerNode.style.padding = "3px 0px 0px " + (inHeading ? 0 : margin) + "px";
  66. if(inHeading){
  67. dojo.style(this.domNode, {
  68. background: "none",
  69. border: "none",
  70. width: totalW + 2 + "px"
  71. });
  72. }
  73. }else{
  74. margin = Math.floor((w - (bw + bm * 2) * arr.length) / 2);
  75. if(w < this._largeScreenWidth || margin < 0){
  76. // If # of buttons is 4, for example, assign "25%" to each button.
  77. // More precisely, 1%(left margin) + 98%(bar width) + 1%(right margin)
  78. for(i = 0; i < arr.length; i++){
  79. arr[i].style.width = Math.round(98/arr.length) + "%";
  80. arr[i].style.margin = "0px";
  81. }
  82. this.containerNode.style.padding = "0px 0px 0px 1%";
  83. }else{
  84. // Fixed width buttons. Mainly for larger screen such as iPad.
  85. for(i = 0; i < arr.length; i++){
  86. arr[i].style.width = bw + "px";
  87. arr[i].style.margin = "0 " + bm + "px";
  88. }
  89. this.containerNode.style.padding = "0px 0px 0px " + margin + "px";
  90. }
  91. }
  92. }
  93. });
  94. dojo.declare(
  95. "dojox.mobile.TabBarButton",
  96. dojox.mobile.AbstractItem,
  97. {
  98. icon1: "", // unselected (dark) icon
  99. icon2: "", // selected (highlight) icon
  100. iconPos1: "", // unselected (dark) icon position
  101. iconPos2: "", // selected (highlight) icon position
  102. selected: false,
  103. transition: "none",
  104. tag: "LI",
  105. selectOne: true,
  106. inheritParams: function(){
  107. var parent = this.getParentWidget();
  108. this.parent = parent;
  109. if(parent){
  110. if(!this.transition){ this.transition = parent.transition; }
  111. if(!this.icon1){ this.icon1 = parent.iconBase; }
  112. if(!this.iconPos1){ this.iconPos1 = parent.iconPos; }
  113. if(!this.icon2){ this.icon2 = parent.iconBase || this.icon1; }
  114. if(!this.iconPos2){ this.iconPos2 = parent.iconPos || this.iconPos1; }
  115. }
  116. },
  117. buildRendering: function(){
  118. this.inheritParams();
  119. this.anchorNode = dojo.create("A", {className:"mblTabBarButtonAnchor"});
  120. var a = this.anchorNode;
  121. this.connect(a, "onclick", "onClick");
  122. var div = dojo.create("DIV", {className:"mblTabBarButtonDiv"}, a);
  123. var divInner = dojo.create("DIV", {className:"mblTabBarButtonDiv mblTabBarButtonDivInner"}, div);
  124. this.img1 = dojo.create("IMG", {className:"mblTabBarButtonIcon", src:this.icon1}, divInner);
  125. this.img1.style.visibility = this.selected ? "hidden" : "";
  126. dojox.mobile.setupIcon(this.img1, this.iconPos1);
  127. this.img1.onload = function(){
  128. // iPhone and Windows Safari sometimes fail to draw icon images.
  129. // For some reason, this code solves the problem.
  130. // Other browsers, including Chrome, do not have this problem.
  131. this.style.width = this.width + "px";
  132. this.style.height = this.height + "px";
  133. };
  134. this.img2 = dojo.create("IMG", {className:"mblTabBarButtonIcon", src:this.icon2}, divInner);
  135. this.img2.style.visibility = this.selected ? "" : "hidden";
  136. dojox.mobile.setupIcon(this.img2, this.iconPos2);
  137. this.img2.onload = function(){
  138. this.style.width = this.width + "px";
  139. this.style.height = this.height + "px";
  140. };
  141. this.box = dojo.create("DIV", {className:"mblTabBarButtonTextBox"}, a);
  142. var box = this.box;
  143. var r = this.srcNodeRef;
  144. if(r){
  145. for(var i = 0, len = r.childNodes.length; i < len; i++){
  146. box.appendChild(r.firstChild);
  147. }
  148. }
  149. if(this.label){
  150. box.appendChild(dojo.doc.createTextNode(this.label));
  151. }
  152. this.domNode = this.srcNodeRef || dojo.create(this.tag);
  153. this.containerNode = this.domNode;
  154. var _clsName = this.parent ? this.parent._clsName : "mblTabBarButton";
  155. dojo.addClass(this.domNode, _clsName + (this.selected ? " mblTabButtonSelected" : ""));
  156. this.domNode.appendChild(a);
  157. this.createDomButton(this.domNode, a);
  158. },
  159. startup: function(){
  160. var parent = this.getParentWidget();
  161. this.parent = parent;
  162. if(parent && parent.barType == "segmentedControl"){
  163. // proper className may not be set when created dynamically
  164. dojo.removeClass(this.domNode, "mblTabBarButton");
  165. dojo.addClass(this.domNode, parent._clsName);
  166. this.box.className = "";
  167. }
  168. },
  169. select: function(deselect){
  170. if(deselect){
  171. this.selected = false;
  172. dojo.removeClass(this.domNode, "mblTabButtonSelected");
  173. }else{
  174. this.selected = true;
  175. dojo.addClass(this.domNode, "mblTabButtonSelected");
  176. for(var i = 0, c = this.domNode.parentNode.childNodes; i < c.length; i++){
  177. if(c[i].nodeType != 1){ continue; }
  178. var w = dijit.byNode(c[i]); // sibling widget
  179. if(w && w != this){
  180. w.select(true);
  181. }
  182. }
  183. }
  184. this.img1.style.visibility = this.selected ? "hidden" : "";
  185. this.img2.style.visibility = this.selected ? "" : "hidden";
  186. },
  187. onClick: function(e){
  188. this.defaultClickAction();
  189. }
  190. });
  191. }