| 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 Default renderer for {@link goog.ui.TabBar}s. Based on the |
| 17 |
* original {@code TabPane} code. |
| 18 |
* |
| 19 |
* @author [email protected] (Attila Bodis) |
| 20 |
* @author [email protected] (Emil A. Eklund) |
| 21 |
*/ |
| 22 |
|
| 23 |
goog.provide('goog.ui.TabBarRenderer'); |
| 24 |
|
| 25 |
goog.require('goog.a11y.aria.Role'); |
| 26 |
goog.require('goog.object'); |
| 27 |
goog.require('goog.ui.ContainerRenderer'); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Default renderer for {@link goog.ui.TabBar}s, based on the {@code TabPane} |
| 33 |
* code. The tab bar's DOM structure is determined by its orientation and |
| 34 |
* location relative to tab contents. For example, a horizontal tab bar |
| 35 |
* located above tab contents looks like this: |
| 36 |
* |
| 37 |
* <div class="goog-tab-bar goog-tab-bar-horizontal goog-tab-bar-top"> |
| 38 |
* ...(tabs here)... |
| 39 |
* </div> |
| 40 |
* |
| 41 |
* @constructor |
| 42 |
* @extends {goog.ui.ContainerRenderer} |
| 43 |
*/ |
| 44 |
goog.ui.TabBarRenderer = function() { |
| 45 |
goog.ui.ContainerRenderer.call(this, goog.a11y.aria.Role.TAB_LIST); |
| 46 |
}; |
| 47 |
goog.inherits(goog.ui.TabBarRenderer, goog.ui.ContainerRenderer); |
| 48 |
goog.addSingletonGetter(goog.ui.TabBarRenderer); |
| 49 |
goog.tagUnsealableClass(goog.ui.TabBarRenderer); |
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* Default CSS class to be applied to the root element of components rendered |
| 54 |
* by this renderer. |
| 55 |
* @type {string} |
| 56 |
*/ |
| 57 |
goog.ui.TabBarRenderer.CSS_CLASS = goog.getCssName('goog-tab-bar'); |
| 58 |
|
| 59 |
|
| 60 |
/** |
| 61 |
* Returns the CSS class name to be applied to the root element of all tab bars |
| 62 |
* rendered or decorated using this renderer. |
| 63 |
* @return {string} Renderer-specific CSS class name. |
| 64 |
* @override |
| 65 |
*/ |
| 66 |
goog.ui.TabBarRenderer.prototype.getCssClass = function() { |
| 67 |
return goog.ui.TabBarRenderer.CSS_CLASS; |
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* Sets the tab bar's state based on the given CSS class name, encountered |
| 73 |
* during decoration. Overrides the superclass implementation by recognizing |
| 74 |
* class names representing tab bar orientation and location. |
| 75 |
* @param {goog.ui.Container} tabBar Tab bar to configure. |
| 76 |
* @param {string} className CSS class name. |
| 77 |
* @param {string} baseClass Base class name used as the root of state-specific |
| 78 |
* class names (typically the renderer's own class name). |
| 79 |
* @protected |
| 80 |
* @override |
| 81 |
*/ |
| 82 |
goog.ui.TabBarRenderer.prototype.setStateFromClassName = function( |
| 83 |
tabBar, className, baseClass) { |
| 84 |
// Create the class-to-location lookup table on first access. |
| 85 |
if (!this.locationByClass_) { |
| 86 |
this.createLocationByClassMap_(); |
| 87 |
} |
| 88 |
|
| 89 |
// If the class name corresponds to a location, update the tab bar's location; |
| 90 |
// otherwise let the superclass handle it. |
| 91 |
var location = this.locationByClass_[className]; |
| 92 |
if (location) { |
| 93 |
tabBar.setLocation(location); |
| 94 |
} else { |
| 95 |
goog.ui.TabBarRenderer.superClass_.setStateFromClassName.call( |
| 96 |
this, tabBar, className, baseClass); |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* Returns all CSS class names applicable to the tab bar, based on its state. |
| 103 |
* Overrides the superclass implementation by appending the location-specific |
| 104 |
* class name to the list. |
| 105 |
* @param {goog.ui.Container} tabBar Tab bar whose CSS classes are to be |
| 106 |
* returned. |
| 107 |
* @return {!Array<string>} Array of CSS class names applicable to the tab bar. |
| 108 |
* @override |
| 109 |
*/ |
| 110 |
goog.ui.TabBarRenderer.prototype.getClassNames = function(tabBar) { |
| 111 |
var classNames = |
| 112 |
goog.ui.TabBarRenderer.superClass_.getClassNames.call(this, tabBar); |
| 113 |
|
| 114 |
// Create the location-to-class lookup table on first access. |
| 115 |
if (!this.classByLocation_) { |
| 116 |
this.createClassByLocationMap_(); |
| 117 |
} |
| 118 |
|
| 119 |
// Apped the class name corresponding to the tab bar's location to the list. |
| 120 |
classNames.push(this.classByLocation_[tabBar.getLocation()]); |
| 121 |
return classNames; |
| 122 |
}; |
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* Creates the location-to-class lookup table. |
| 127 |
* @private |
| 128 |
*/ |
| 129 |
goog.ui.TabBarRenderer.prototype.createClassByLocationMap_ = function() { |
| 130 |
var baseClass = this.getCssClass(); |
| 131 |
|
| 132 |
/** |
| 133 |
* Map of locations to location-specific structural class names, |
| 134 |
* precomputed and cached on first use to minimize object allocations |
| 135 |
* and string concatenation. |
| 136 |
* @type {Object} |
| 137 |
* @private |
| 138 |
* @suppress {missingRequire} goog.ui.TabBar |
| 139 |
*/ |
| 140 |
this.classByLocation_ = goog.object.create( |
| 141 |
goog.ui.TabBar.Location.TOP, goog.getCssName(baseClass, 'top'), |
| 142 |
goog.ui.TabBar.Location.BOTTOM, goog.getCssName(baseClass, 'bottom'), |
| 143 |
goog.ui.TabBar.Location.START, goog.getCssName(baseClass, 'start'), |
| 144 |
goog.ui.TabBar.Location.END, goog.getCssName(baseClass, 'end')); |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
/** |
| 149 |
* Creates the class-to-location lookup table, used during decoration. |
| 150 |
* @private |
| 151 |
*/ |
| 152 |
goog.ui.TabBarRenderer.prototype.createLocationByClassMap_ = function() { |
| 153 |
// We need the classByLocation_ map so we can transpose it. |
| 154 |
if (!this.classByLocation_) { |
| 155 |
this.createClassByLocationMap_(); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Map of location-specific structural class names to locations, used during |
| 160 |
* element decoration. Precomputed and cached on first use to minimize object |
| 161 |
* allocations and string concatenation. |
| 162 |
* @type {Object} |
| 163 |
* @private |
| 164 |
*/ |
| 165 |
this.locationByClass_ = goog.object.transpose(this.classByLocation_); |
| 166 |
}; |