| 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 |
/** |
| 16 |
* @fileoverview Rounded corner tab renderer for {@link goog.ui.Tab}s. |
| 17 |
* |
| 18 |
* @author [email protected] (Attila Bodis) |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.ui.RoundedTabRenderer'); |
| 22 |
|
| 23 |
goog.require('goog.dom'); |
| 24 |
goog.require('goog.dom.TagName'); |
| 25 |
goog.require('goog.ui.Tab'); |
| 26 |
goog.require('goog.ui.TabBar'); |
| 27 |
goog.require('goog.ui.TabRenderer'); |
| 28 |
goog.require('goog.ui.registry'); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* Rounded corner tab renderer for {@link goog.ui.Tab}s. |
| 34 |
* @constructor |
| 35 |
* @extends {goog.ui.TabRenderer} |
| 36 |
* @final |
| 37 |
*/ |
| 38 |
goog.ui.RoundedTabRenderer = function() { |
| 39 |
goog.ui.TabRenderer.call(this); |
| 40 |
}; |
| 41 |
goog.inherits(goog.ui.RoundedTabRenderer, goog.ui.TabRenderer); |
| 42 |
goog.addSingletonGetter(goog.ui.RoundedTabRenderer); |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* Default CSS class to be applied to the root element of components rendered |
| 47 |
* by this renderer. |
| 48 |
* @type {string} |
| 49 |
*/ |
| 50 |
goog.ui.RoundedTabRenderer.CSS_CLASS = goog.getCssName('goog-rounded-tab'); |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* Returns the CSS class name to be applied to the root element of all tabs |
| 55 |
* rendered or decorated using this renderer. |
| 56 |
* @return {string} Renderer-specific CSS class name. |
| 57 |
* @override |
| 58 |
*/ |
| 59 |
goog.ui.RoundedTabRenderer.prototype.getCssClass = function() { |
| 60 |
return goog.ui.RoundedTabRenderer.CSS_CLASS; |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
/** |
| 65 |
* Creates the tab's DOM structure, based on the containing tab bar's location |
| 66 |
* relative to tab contents. For example, the DOM for a tab in a tab bar |
| 67 |
* located above tab contents would look like this: |
| 68 |
* |
| 69 |
* <div class="goog-rounded-tab" title="..."> |
| 70 |
* <table class="goog-rounded-tab-table"> |
| 71 |
* <tbody> |
| 72 |
* <tr> |
| 73 |
* <td nowrap> |
| 74 |
* <div class="goog-rounded-tab-outer-edge"></div> |
| 75 |
* <div class="goog-rounded-tab-inner-edge"></div> |
| 76 |
* </td> |
| 77 |
* </tr> |
| 78 |
* <tr> |
| 79 |
* <td nowrap> |
| 80 |
* <div class="goog-rounded-tab-caption">Hello, world</div> |
| 81 |
* </td> |
| 82 |
* </tr> |
| 83 |
* </tbody> |
| 84 |
* </table> |
| 85 |
* </div> |
| 86 |
* |
| 87 |
* @param {goog.ui.Control} tab Tab to render. |
| 88 |
* @return {Element} Root element for the tab. |
| 89 |
* @override |
| 90 |
*/ |
| 91 |
goog.ui.RoundedTabRenderer.prototype.createDom = function(tab) { |
| 92 |
return this.decorate( |
| 93 |
tab, goog.ui.RoundedTabRenderer.superClass_.createDom.call(this, tab)); |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* Decorates the element with the tab. Overrides the superclass implementation |
| 99 |
* by wrapping the tab's content in a table that implements rounded corners. |
| 100 |
* @param {goog.ui.Control} tab Tab to decorate the element. |
| 101 |
* @param {Element} element Element to decorate. |
| 102 |
* @return {Element} Decorated element. |
| 103 |
* @override |
| 104 |
*/ |
| 105 |
goog.ui.RoundedTabRenderer.prototype.decorate = function(tab, element) { |
| 106 |
var tabBar = tab.getParent(); |
| 107 |
|
| 108 |
if (!this.getContentElement(element)) { |
| 109 |
// The element to be decorated doesn't appear to have the full tab DOM, |
| 110 |
// so we have to create it. |
| 111 |
element.appendChild( |
| 112 |
this.createTab( |
| 113 |
tab.getDomHelper(), element.childNodes, tabBar.getLocation())); |
| 114 |
} |
| 115 |
|
| 116 |
return goog.ui.RoundedTabRenderer.superClass_.decorate.call( |
| 117 |
this, tab, element); |
| 118 |
}; |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* Creates a table implementing a rounded corner tab. |
| 123 |
* @param {goog.dom.DomHelper} dom DOM helper to use for element construction. |
| 124 |
* @param {goog.ui.ControlContent} caption Text caption or DOM structure |
| 125 |
* to display as the tab's caption. |
| 126 |
* @param {goog.ui.TabBar.Location} location Tab bar location relative to the |
| 127 |
* tab contents. |
| 128 |
* @return {!Element} Table implementing a rounded corner tab. |
| 129 |
* @protected |
| 130 |
*/ |
| 131 |
goog.ui.RoundedTabRenderer.prototype.createTab = function( |
| 132 |
dom, caption, location) { |
| 133 |
var rows = []; |
| 134 |
|
| 135 |
if (location != goog.ui.TabBar.Location.BOTTOM) { |
| 136 |
// This is a left, right, or top tab, so it needs a rounded top edge. |
| 137 |
rows.push(this.createEdge(dom, /* isTopEdge */ true)); |
| 138 |
} |
| 139 |
rows.push(this.createCaption(dom, caption)); |
| 140 |
if (location != goog.ui.TabBar.Location.TOP) { |
| 141 |
// This is a left, right, or bottom tab, so it needs a rounded bottom edge. |
| 142 |
rows.push(this.createEdge(dom, /* isTopEdge */ false)); |
| 143 |
} |
| 144 |
|
| 145 |
return dom.createDom( |
| 146 |
goog.dom.TagName.TABLE, { |
| 147 |
'cellPadding': 0, |
| 148 |
'cellSpacing': 0, |
| 149 |
'className': goog.getCssName(this.getStructuralCssClass(), 'table') |
| 150 |
}, |
| 151 |
dom.createDom(goog.dom.TagName.TBODY, null, rows)); |
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
/** |
| 156 |
* Creates a table row implementing the tab caption. |
| 157 |
* @param {goog.dom.DomHelper} dom DOM helper to use for element construction. |
| 158 |
* @param {goog.ui.ControlContent} caption Text caption or DOM structure |
| 159 |
* to display as the tab's caption. |
| 160 |
* @return {!Element} Tab caption table row. |
| 161 |
* @protected |
| 162 |
*/ |
| 163 |
goog.ui.RoundedTabRenderer.prototype.createCaption = function(dom, caption) { |
| 164 |
var baseClass = this.getStructuralCssClass(); |
| 165 |
return dom.createDom( |
| 166 |
goog.dom.TagName.TR, null, |
| 167 |
dom.createDom( |
| 168 |
goog.dom.TagName.TD, {'noWrap': true}, |
| 169 |
dom.createDom( |
| 170 |
goog.dom.TagName.DIV, goog.getCssName(baseClass, 'caption'), |
| 171 |
caption))); |
| 172 |
}; |
| 173 |
|
| 174 |
|
| 175 |
/** |
| 176 |
* Creates a table row implementing a rounded tab edge. |
| 177 |
* @param {goog.dom.DomHelper} dom DOM helper to use for element construction. |
| 178 |
* @param {boolean} isTopEdge Whether to create a top or bottom edge. |
| 179 |
* @return {!Element} Rounded tab edge table row. |
| 180 |
* @protected |
| 181 |
*/ |
| 182 |
goog.ui.RoundedTabRenderer.prototype.createEdge = function(dom, isTopEdge) { |
| 183 |
var baseClass = this.getStructuralCssClass(); |
| 184 |
var inner = dom.createDom( |
| 185 |
goog.dom.TagName.DIV, goog.getCssName(baseClass, 'inner-edge')); |
| 186 |
var outer = dom.createDom( |
| 187 |
goog.dom.TagName.DIV, goog.getCssName(baseClass, 'outer-edge')); |
| 188 |
return dom.createDom( |
| 189 |
goog.dom.TagName.TR, null, |
| 190 |
dom.createDom( |
| 191 |
goog.dom.TagName.TD, {'noWrap': true}, |
| 192 |
isTopEdge ? [outer, inner] : [inner, outer])); |
| 193 |
}; |
| 194 |
|
| 195 |
|
| 196 |
/** @override */ |
| 197 |
goog.ui.RoundedTabRenderer.prototype.getContentElement = function(element) { |
| 198 |
var baseClass = this.getStructuralCssClass(); |
| 199 |
return element && |
| 200 |
goog.dom.getElementsByTagNameAndClass( |
| 201 |
goog.dom.TagName.DIV, goog.getCssName(baseClass, 'caption'), |
| 202 |
element)[0]; |
| 203 |
}; |
| 204 |
|
| 205 |
|
| 206 |
// Register a decorator factory function for goog.ui.Tabs using the rounded |
| 207 |
// tab renderer. |
| 208 |
goog.ui.registry.setDecoratorByClassName( |
| 209 |
goog.ui.RoundedTabRenderer.CSS_CLASS, function() { |
| 210 |
return new goog.ui.Tab(null, goog.ui.RoundedTabRenderer.getInstance()); |
| 211 |
}); |