Debian Codegraph @

closure-library/closure/goog/ui/tabrenderer.js

raw
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.Tab}s. Based on the
17 * original {@code TabPane} code.
18 *
19 * @author [email protected] (Attila Bodis)
20 */
21
22 goog.provide('goog.ui.TabRenderer');
23
24 goog.require('goog.a11y.aria.Role');
25 goog.require('goog.ui.Component');
26 goog.require('goog.ui.ControlRenderer');
27
28
29
30 /**
31 * Default renderer for {@link goog.ui.Tab}s, based on the {@code TabPane} code.
32 * @constructor
33 * @extends {goog.ui.ControlRenderer}
34 */
35 goog.ui.TabRenderer = function() {
36 goog.ui.ControlRenderer.call(this);
37 };
38 goog.inherits(goog.ui.TabRenderer, goog.ui.ControlRenderer);
39 goog.addSingletonGetter(goog.ui.TabRenderer);
40
41
42 /**
43 * Default CSS class to be applied to the root element of components rendered
44 * by this renderer.
45 * @type {string}
46 */
47 goog.ui.TabRenderer.CSS_CLASS = goog.getCssName('goog-tab');
48
49
50 /**
51 * Returns the CSS class name to be applied to the root element of all tabs
52 * rendered or decorated using this renderer.
53 * @return {string} Renderer-specific CSS class name.
54 * @override
55 */
56 goog.ui.TabRenderer.prototype.getCssClass = function() {
57 return goog.ui.TabRenderer.CSS_CLASS;
58 };
59
60
61 /**
62 * Returns the ARIA role to be applied to the tab element.
63 * See http://wiki/Main/ARIA for more info.
64 * @return {goog.a11y.aria.Role} ARIA role.
65 * @override
66 */
67 goog.ui.TabRenderer.prototype.getAriaRole = function() {
68 return goog.a11y.aria.Role.TAB;
69 };
70
71
72 /**
73 * Returns the tab's contents wrapped in a DIV, with the renderer's own CSS
74 * class and additional state-specific classes applied to it. Creates the
75 * following DOM structure:
76 *
77 * <div class="goog-tab" title="Title">Content</div>
78 *
79 * @param {goog.ui.Control} tab Tab to render.
80 * @return {Element} Root element for the tab.
81 * @override
82 */
83 goog.ui.TabRenderer.prototype.createDom = function(tab) {
84 var element = goog.ui.TabRenderer.superClass_.createDom.call(this, tab);
85
86 var tooltip = tab.getTooltip();
87 if (tooltip) {
88 // Only update the element if the tab has a tooltip.
89 this.setTooltip(element, tooltip);
90 }
91
92 return element;
93 };
94
95
96 /**
97 * Decorates the element with the tab. Initializes the tab's ID, content,
98 * tooltip, and state based on the ID of the element, its title, child nodes,
99 * and CSS classes, respectively. Returns the element.
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.TabRenderer.prototype.decorate = function(tab, element) {
106 element = goog.ui.TabRenderer.superClass_.decorate.call(this, tab, element);
107
108 var tooltip = this.getTooltip(element);
109 if (tooltip) {
110 // Only update the tab if the element has a tooltip.
111 tab.setTooltipInternal(tooltip);
112 }
113
114 // If the tab is selected and hosted in a tab bar, update the tab bar's
115 // selection model.
116 if (tab.isSelected()) {
117 var tabBar = tab.getParent();
118 if (tabBar && goog.isFunction(tabBar.setSelectedTab)) {
119 // We need to temporarily deselect the tab, so the tab bar can re-select
120 // it and thereby correctly initialize its state. We use the protected
121 // setState() method to avoid dispatching useless events.
122 tab.setState(goog.ui.Component.State.SELECTED, false);
123 tabBar.setSelectedTab(tab);
124 }
125 }
126
127 return element;
128 };
129
130
131 /**
132 * Takes a tab's root element, and returns its tooltip text, or the empty
133 * string if the element has no tooltip.
134 * @param {Element} element The tab's root element.
135 * @return {string} The tooltip text (empty string if none).
136 */
137 goog.ui.TabRenderer.prototype.getTooltip = function(element) {
138 return element.title || '';
139 };
140
141
142 /**
143 * Takes a tab's root element and a tooltip string, and updates the element
144 * with the new tooltip. If the new tooltip is null or undefined, sets the
145 * element's title to the empty string.
146 * @param {Element} element The tab's root element.
147 * @param {string|null|undefined} tooltip New tooltip text (if any).
148 */
149 goog.ui.TabRenderer.prototype.setTooltip = function(element, tooltip) {
150 if (element) {
151 element.title = tooltip || '';
152 }
153 };