Debian Codegraph @

closure-library/closure/goog/ui/editor/tabpane.js

raw
1 // Copyright 2010 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 Tabbed pane with style and functionality specific to
17 * Editor dialogs.
18 *
19 * @author [email protected] (Robby Walker)
20 */
21
22 goog.provide('goog.ui.editor.TabPane');
23
24 goog.require('goog.asserts');
25 goog.require('goog.dom');
26 goog.require('goog.dom.InputType');
27 goog.require('goog.dom.TagName');
28 goog.require('goog.dom.classlist');
29 goog.require('goog.events.EventHandler');
30 goog.require('goog.events.EventType');
31 goog.require('goog.style');
32 goog.require('goog.ui.Component');
33 goog.require('goog.ui.Control');
34 goog.require('goog.ui.Tab');
35 goog.require('goog.ui.TabBar');
36
37
38
39 /**
40 * Creates a new Editor-style tab pane.
41 * @param {goog.dom.DomHelper} dom The dom helper for the window to create this
42 * tab pane in.
43 * @param {string=} opt_caption Optional caption of the tab pane.
44 * @constructor
45 * @extends {goog.ui.Component}
46 * @final
47 */
48 goog.ui.editor.TabPane = function(dom, opt_caption) {
49 goog.ui.editor.TabPane.base(this, 'constructor', dom);
50
51 /**
52 * The event handler used to register events.
53 * @type {goog.events.EventHandler<!goog.ui.editor.TabPane>}
54 * @private
55 */
56 this.eventHandler_ = new goog.events.EventHandler(this);
57 this.registerDisposable(this.eventHandler_);
58
59 /**
60 * The tab bar used to render the tabs.
61 * @type {goog.ui.TabBar}
62 * @private
63 */
64 this.tabBar_ =
65 new goog.ui.TabBar(goog.ui.TabBar.Location.START, undefined, this.dom_);
66 this.tabBar_.setFocusable(false);
67
68 /**
69 * The content element.
70 * @private
71 */
72 this.tabContent_ = this.dom_.createDom(
73 goog.dom.TagName.DIV, {className: goog.getCssName('goog-tab-content')});
74
75 /**
76 * The currently selected radio button.
77 * @type {Element}
78 * @private
79 */
80 this.selectedRadio_ = null;
81
82 /**
83 * The currently visible tab content.
84 * @type {Element}
85 * @private
86 */
87 this.visibleContent_ = null;
88
89
90 // Add the caption as the first element in the tab bar.
91 if (opt_caption) {
92 var captionControl = new goog.ui.Control(opt_caption, undefined, this.dom_);
93 captionControl.addClassName(goog.getCssName('tr-tabpane-caption'));
94 captionControl.setEnabled(false);
95 this.tabBar_.addChild(captionControl, true);
96 }
97 };
98 goog.inherits(goog.ui.editor.TabPane, goog.ui.Component);
99
100
101 /**
102 * @return {string} The ID of the content element for the current tab.
103 */
104 goog.ui.editor.TabPane.prototype.getCurrentTabId = function() {
105 return this.tabBar_.getSelectedTab().getId();
106 };
107
108
109 /**
110 * Selects the tab with the given id.
111 * @param {string} id Id of the tab to select.
112 */
113 goog.ui.editor.TabPane.prototype.setSelectedTabId = function(id) {
114 this.tabBar_.setSelectedTab(this.tabBar_.getChild(id));
115 };
116
117
118 /**
119 * Adds a tab to the tab pane.
120 * @param {string} id The id of the tab to add.
121 * @param {string} caption The caption of the tab.
122 * @param {string} tooltip The tooltip for the tab.
123 * @param {string} groupName for the radio button group.
124 * @param {Element} content The content element to show when this tab is
125 * selected.
126 */
127 goog.ui.editor.TabPane.prototype.addTab = function(
128 id, caption, tooltip, groupName, content) {
129 var radio = this.dom_.createDom(
130 goog.dom.TagName.INPUT,
131 {name: groupName, type: goog.dom.InputType.RADIO});
132
133 var tab = new goog.ui.Tab(
134 [radio, this.dom_.createTextNode(caption)], undefined, this.dom_);
135 tab.setId(id);
136 tab.setTooltip(tooltip);
137 this.tabBar_.addChild(tab, true);
138
139 // When you navigate the radio buttons with TAB and then the Arrow keys on
140 // Chrome and FF, you get a CLICK event on them, and the radio button
141 // is selected. You don't get a SELECT at all. We listen for SELECT
142 // nonetheless because it's possible that some browser will issue only
143 // SELECT.
144 this.eventHandler_.listen(
145 radio, [goog.events.EventType.SELECT, goog.events.EventType.CLICK],
146 goog.bind(this.tabBar_.setSelectedTab, this.tabBar_, tab));
147
148 content.id = id + '-tab';
149 this.tabContent_.appendChild(content);
150 goog.style.setElementShown(content, false);
151 };
152
153
154 /** @override */
155 goog.ui.editor.TabPane.prototype.enterDocument = function() {
156 goog.ui.editor.TabPane.base(this, 'enterDocument');
157
158 // Get the root element and add a class name to it.
159 var root = this.getElement();
160 goog.asserts.assert(root);
161 goog.dom.classlist.add(root, goog.getCssName('tr-tabpane'));
162
163 // Add the tabs.
164 this.addChild(this.tabBar_, true);
165 this.eventHandler_.listen(
166 this.tabBar_, goog.ui.Component.EventType.SELECT, this.handleTabSelect_);
167
168 // Add the tab content.
169 root.appendChild(this.tabContent_);
170
171 // Add an element to clear the tab float.
172 root.appendChild(this.dom_.createDom(goog.dom.TagName.DIV, {
173 className: goog.getCssName('goog-tab-bar-clear')
174 }));
175 };
176
177
178 /**
179 * Handles a tab change.
180 * @param {goog.events.Event} e The browser change event.
181 * @private
182 */
183 goog.ui.editor.TabPane.prototype.handleTabSelect_ = function(e) {
184 var tab = /** @type {goog.ui.Tab} */ (e.target);
185
186 // Show the tab content.
187 if (this.visibleContent_) {
188 goog.style.setElementShown(this.visibleContent_, false);
189 }
190 this.visibleContent_ = this.dom_.getElement(tab.getId() + '-tab');
191 goog.style.setElementShown(this.visibleContent_, true);
192
193 // Select the appropriate radio button (and deselect the current one).
194 if (this.selectedRadio_) {
195 this.selectedRadio_.checked = false;
196 }
197 this.selectedRadio_ = goog.dom.getElementsByTagName(
198 goog.dom.TagName.INPUT, tab.getElementStrict())[0];
199 this.selectedRadio_.checked = true;
200 };