TabBar.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. define("dojox/mobile/TabBar", [
  2. "dojo/_base/array",
  3. "dojo/_base/declare",
  4. "dojo/dom-class",
  5. "dojo/dom-construct",
  6. "dojo/dom-geometry",
  7. "dojo/dom-style",
  8. "dijit/_Contained",
  9. "dijit/_Container",
  10. "dijit/_WidgetBase",
  11. "./Heading",
  12. "./TabBarButton"
  13. ], function(array, declare, domClass, domConstruct, domGeometry, domStyle, Contained, Container, WidgetBase, Heading, TabBarButton){
  14. /*=====
  15. var Contained = dijit._Contained;
  16. var Container = dijit._Container;
  17. var WidgetBase = dijit._WidgetBase;
  18. =====*/
  19. // module:
  20. // dojox/mobile/TabBar
  21. // summary:
  22. // A bar widget that has buttons to control visibility of views.
  23. return declare("dojox.mobile.TabBar", [WidgetBase, Container, Contained],{
  24. // summary:
  25. // A bar widget that has buttons to control visibility of views.
  26. // description:
  27. // TabBar is a container widget that has typically multiple
  28. // TabBarButtons which controls visibility of views. It can be used
  29. // as a tab container.
  30. // iconBase: String
  31. // The default icon path for child items.
  32. iconBase: "",
  33. // iconPos: String
  34. // The default icon position for child items.
  35. iconPos: "",
  36. // barType: String
  37. // "tabBar"(default) or "segmentedControl".
  38. barType: "tabBar",
  39. // inHeading: Boolean
  40. // A flag that indicates whether this widget is in a Heading
  41. // widget.
  42. inHeading: false,
  43. // tag: String
  44. // A name of html tag to create as domNode.
  45. tag: "UL",
  46. /* internal properties */
  47. _fixedButtonWidth: 76,
  48. _fixedButtonMargin: 17,
  49. _largeScreenWidth: 500,
  50. buildRendering: function(){
  51. this._clsName = this.barType == "segmentedControl" ? "mblTabButton" : "mblTabBarButton";
  52. this.domNode = this.containerNode = this.srcNodeRef || domConstruct.create(this.tag);
  53. this.domNode.className = this.barType == "segmentedControl" ? "mblTabPanelHeader" : "mblTabBar";
  54. },
  55. startup: function(){
  56. if(this._started){ return; }
  57. this.inherited(arguments);
  58. this.resize();
  59. },
  60. resize: function(size){
  61. var i,w;
  62. if(size && size.w){
  63. domGeometry.setMarginBox(this.domNode, size);
  64. w = size.w;
  65. }else{
  66. // Calculation of the bar width varies according to its "position" value.
  67. // When the widget is used as a fixed bar, its position would be "absolute".
  68. w = domStyle.get(this.domNode, "position") === "absolute" ?
  69. domGeometry.getContentBox(this.domNode).w : domGeometry.getMarginBox(this.domNode).w;
  70. }
  71. var bw = this._fixedButtonWidth;
  72. var bm = this._fixedButtonMargin;
  73. var children = this.containerNode.childNodes;
  74. var arr = [];
  75. for(i = 0; i < children.length; i++){
  76. var c = children[i];
  77. if(c.nodeType != 1){ continue; }
  78. if(domClass.contains(c, this._clsName)){
  79. arr.push(c);
  80. }
  81. }
  82. var margin;
  83. if(this.barType == "segmentedControl"){
  84. margin = w;
  85. var totalW = 0; // total width of all the buttons
  86. for(i = 0; i < arr.length; i++){
  87. margin -= domGeometry.getMarginBox(arr[i]).w;
  88. totalW += arr[i].offsetWidth;
  89. }
  90. margin = Math.floor(margin/2);
  91. var parent = this.getParent();
  92. var inHeading = this.inHeading || parent instanceof Heading;
  93. this.containerNode.style.padding = (inHeading ? 0 : 3) + "px 0px 0px " + (inHeading ? 0 : margin) + "px";
  94. if(inHeading){
  95. domStyle.set(this.domNode, {
  96. background: "none",
  97. border: "none",
  98. width: totalW + 2 + "px"
  99. });
  100. }
  101. domClass.add(this.domNode, "mblTabBar" + (inHeading ? "Head" : "Top"));
  102. }else{
  103. margin = Math.floor((w - (bw + bm * 2) * arr.length) / 2);
  104. if(w < this._largeScreenWidth || margin < 0){
  105. // If # of buttons is 4, for example, assign "25%" to each button.
  106. // More precisely, 1%(left margin) + 98%(bar width) + 1%(right margin)
  107. for(i = 0; i < arr.length; i++){
  108. arr[i].style.width = Math.round(98/arr.length) + "%";
  109. arr[i].style.margin = "0px";
  110. }
  111. this.containerNode.style.padding = "0px 0px 0px 1%";
  112. }else{
  113. // Fixed width buttons. Mainly for larger screen such as iPad.
  114. for(i = 0; i < arr.length; i++){
  115. arr[i].style.width = bw + "px";
  116. arr[i].style.margin = "0 " + bm + "px";
  117. }
  118. if(arr.length > 0){
  119. arr[0].style.marginLeft = margin + bm + "px";
  120. }
  121. this.containerNode.style.padding = "0px";
  122. }
  123. }
  124. if(!array.some(this.getChildren(), function(child){ return child.iconNode1; })){
  125. domClass.add(this.domNode, "mblTabBarNoIcons");
  126. }else{
  127. domClass.remove(this.domNode, "mblTabBarNoIcons");
  128. }
  129. if(!array.some(this.getChildren(), function(child){ return child.label; })){
  130. domClass.add(this.domNode, "mblTabBarNoText");
  131. }else{
  132. domClass.remove(this.domNode, "mblTabBarNoText");
  133. }
  134. }
  135. });
  136. });