roundedtabrenderer.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Rounded corner tab renderer for {@link goog.ui.Tab}s.
  16. *
  17. * @author attila@google.com (Attila Bodis)
  18. */
  19. goog.provide('goog.ui.RoundedTabRenderer');
  20. goog.require('goog.dom');
  21. goog.require('goog.dom.TagName');
  22. goog.require('goog.ui.Tab');
  23. goog.require('goog.ui.TabBar');
  24. goog.require('goog.ui.TabRenderer');
  25. goog.require('goog.ui.registry');
  26. /**
  27. * Rounded corner tab renderer for {@link goog.ui.Tab}s.
  28. * @constructor
  29. * @extends {goog.ui.TabRenderer}
  30. * @final
  31. */
  32. goog.ui.RoundedTabRenderer = function() {
  33. goog.ui.TabRenderer.call(this);
  34. };
  35. goog.inherits(goog.ui.RoundedTabRenderer, goog.ui.TabRenderer);
  36. goog.addSingletonGetter(goog.ui.RoundedTabRenderer);
  37. /**
  38. * Default CSS class to be applied to the root element of components rendered
  39. * by this renderer.
  40. * @type {string}
  41. */
  42. goog.ui.RoundedTabRenderer.CSS_CLASS = goog.getCssName('goog-rounded-tab');
  43. /**
  44. * Returns the CSS class name to be applied to the root element of all tabs
  45. * rendered or decorated using this renderer.
  46. * @return {string} Renderer-specific CSS class name.
  47. * @override
  48. */
  49. goog.ui.RoundedTabRenderer.prototype.getCssClass = function() {
  50. return goog.ui.RoundedTabRenderer.CSS_CLASS;
  51. };
  52. /**
  53. * Creates the tab's DOM structure, based on the containing tab bar's location
  54. * relative to tab contents. For example, the DOM for a tab in a tab bar
  55. * located above tab contents would look like this:
  56. *
  57. * <div class="goog-rounded-tab" title="...">
  58. * <table class="goog-rounded-tab-table">
  59. * <tbody>
  60. * <tr>
  61. * <td nowrap>
  62. * <div class="goog-rounded-tab-outer-edge"></div>
  63. * <div class="goog-rounded-tab-inner-edge"></div>
  64. * </td>
  65. * </tr>
  66. * <tr>
  67. * <td nowrap>
  68. * <div class="goog-rounded-tab-caption">Hello, world</div>
  69. * </td>
  70. * </tr>
  71. * </tbody>
  72. * </table>
  73. * </div>
  74. *
  75. * @param {goog.ui.Control} tab Tab to render.
  76. * @return {Element} Root element for the tab.
  77. * @override
  78. */
  79. goog.ui.RoundedTabRenderer.prototype.createDom = function(tab) {
  80. return this.decorate(
  81. tab, goog.ui.RoundedTabRenderer.superClass_.createDom.call(this, tab));
  82. };
  83. /**
  84. * Decorates the element with the tab. Overrides the superclass implementation
  85. * by wrapping the tab's content in a table that implements rounded corners.
  86. * @param {goog.ui.Control} tab Tab to decorate the element.
  87. * @param {Element} element Element to decorate.
  88. * @return {Element} Decorated element.
  89. * @override
  90. */
  91. goog.ui.RoundedTabRenderer.prototype.decorate = function(tab, element) {
  92. var tabBar = tab.getParent();
  93. if (!this.getContentElement(element)) {
  94. // The element to be decorated doesn't appear to have the full tab DOM,
  95. // so we have to create it.
  96. element.appendChild(
  97. this.createTab(
  98. tab.getDomHelper(), element.childNodes, tabBar.getLocation()));
  99. }
  100. return goog.ui.RoundedTabRenderer.superClass_.decorate.call(
  101. this, tab, element);
  102. };
  103. /**
  104. * Creates a table implementing a rounded corner tab.
  105. * @param {goog.dom.DomHelper} dom DOM helper to use for element construction.
  106. * @param {goog.ui.ControlContent} caption Text caption or DOM structure
  107. * to display as the tab's caption.
  108. * @param {goog.ui.TabBar.Location} location Tab bar location relative to the
  109. * tab contents.
  110. * @return {!Element} Table implementing a rounded corner tab.
  111. * @protected
  112. */
  113. goog.ui.RoundedTabRenderer.prototype.createTab = function(
  114. dom, caption, location) {
  115. var rows = [];
  116. if (location != goog.ui.TabBar.Location.BOTTOM) {
  117. // This is a left, right, or top tab, so it needs a rounded top edge.
  118. rows.push(this.createEdge(dom, /* isTopEdge */ true));
  119. }
  120. rows.push(this.createCaption(dom, caption));
  121. if (location != goog.ui.TabBar.Location.TOP) {
  122. // This is a left, right, or bottom tab, so it needs a rounded bottom edge.
  123. rows.push(this.createEdge(dom, /* isTopEdge */ false));
  124. }
  125. return dom.createDom(
  126. goog.dom.TagName.TABLE, {
  127. 'cellPadding': 0,
  128. 'cellSpacing': 0,
  129. 'className': goog.getCssName(this.getStructuralCssClass(), 'table')
  130. },
  131. dom.createDom(goog.dom.TagName.TBODY, null, rows));
  132. };
  133. /**
  134. * Creates a table row implementing the tab caption.
  135. * @param {goog.dom.DomHelper} dom DOM helper to use for element construction.
  136. * @param {goog.ui.ControlContent} caption Text caption or DOM structure
  137. * to display as the tab's caption.
  138. * @return {!Element} Tab caption table row.
  139. * @protected
  140. */
  141. goog.ui.RoundedTabRenderer.prototype.createCaption = function(dom, caption) {
  142. var baseClass = this.getStructuralCssClass();
  143. return dom.createDom(
  144. goog.dom.TagName.TR, null,
  145. dom.createDom(
  146. goog.dom.TagName.TD, {'noWrap': true},
  147. dom.createDom(
  148. goog.dom.TagName.DIV, goog.getCssName(baseClass, 'caption'),
  149. caption)));
  150. };
  151. /**
  152. * Creates a table row implementing a rounded tab edge.
  153. * @param {goog.dom.DomHelper} dom DOM helper to use for element construction.
  154. * @param {boolean} isTopEdge Whether to create a top or bottom edge.
  155. * @return {!Element} Rounded tab edge table row.
  156. * @protected
  157. */
  158. goog.ui.RoundedTabRenderer.prototype.createEdge = function(dom, isTopEdge) {
  159. var baseClass = this.getStructuralCssClass();
  160. var inner = dom.createDom(
  161. goog.dom.TagName.DIV, goog.getCssName(baseClass, 'inner-edge'));
  162. var outer = dom.createDom(
  163. goog.dom.TagName.DIV, goog.getCssName(baseClass, 'outer-edge'));
  164. return dom.createDom(
  165. goog.dom.TagName.TR, null,
  166. dom.createDom(
  167. goog.dom.TagName.TD, {'noWrap': true},
  168. isTopEdge ? [outer, inner] : [inner, outer]));
  169. };
  170. /** @override */
  171. goog.ui.RoundedTabRenderer.prototype.getContentElement = function(element) {
  172. var baseClass = this.getStructuralCssClass();
  173. return element &&
  174. goog.dom.getElementsByTagNameAndClass(
  175. goog.dom.TagName.DIV, goog.getCssName(baseClass, 'caption'),
  176. element)[0];
  177. };
  178. // Register a decorator factory function for goog.ui.Tabs using the rounded
  179. // tab renderer.
  180. goog.ui.registry.setDecoratorByClassName(
  181. goog.ui.RoundedTabRenderer.CSS_CLASS, function() {
  182. return new goog.ui.Tab(null, goog.ui.RoundedTabRenderer.getInstance());
  183. });