tabbar.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 Tab bar UI component.
  16. *
  17. * @author attila@google.com (Attila Bodis)
  18. * @see ../demos/tabbar.html
  19. */
  20. goog.provide('goog.ui.TabBar');
  21. goog.provide('goog.ui.TabBar.Location');
  22. goog.require('goog.ui.Component.EventType');
  23. goog.require('goog.ui.Container');
  24. goog.require('goog.ui.Container.Orientation');
  25. // We need to include following dependency because of the magic with
  26. // goog.ui.registry.setDecoratorByClassName
  27. goog.require('goog.ui.Tab');
  28. goog.require('goog.ui.TabBarRenderer');
  29. goog.require('goog.ui.registry');
  30. /**
  31. * Tab bar UI component. A tab bar contains tabs, rendered above, below,
  32. * before, or after tab contents. Tabs in tab bars dispatch the following
  33. * events:
  34. * <ul>
  35. * <li>{@link goog.ui.Component.EventType.ACTION} when activated via the
  36. * keyboard or the mouse,
  37. * <li>{@link goog.ui.Component.EventType.SELECT} when selected, and
  38. * <li>{@link goog.ui.Component.EventType.UNSELECT} when deselected.
  39. * </ul>
  40. * Clients may listen for all of the above events on the tab bar itself, and
  41. * refer to the event target to identify the tab that dispatched the event.
  42. * When an unselected tab is clicked for the first time, it dispatches both a
  43. * {@code SELECT} event and an {@code ACTION} event; subsequent clicks on an
  44. * already selected tab only result in {@code ACTION} events.
  45. *
  46. * @param {goog.ui.TabBar.Location=} opt_location Tab bar location; defaults to
  47. * {@link goog.ui.TabBar.Location.TOP}.
  48. * @param {goog.ui.TabBarRenderer=} opt_renderer Renderer used to render or
  49. * decorate the container; defaults to {@link goog.ui.TabBarRenderer}.
  50. * @param {goog.dom.DomHelper=} opt_domHelper DOM helper, used for document
  51. * interaction.
  52. * @constructor
  53. * @extends {goog.ui.Container}
  54. */
  55. goog.ui.TabBar = function(opt_location, opt_renderer, opt_domHelper) {
  56. this.setLocation(opt_location || goog.ui.TabBar.Location.TOP);
  57. goog.ui.Container.call(
  58. this, this.getOrientation(),
  59. opt_renderer || goog.ui.TabBarRenderer.getInstance(), opt_domHelper);
  60. this.listenToTabEvents_();
  61. };
  62. goog.inherits(goog.ui.TabBar, goog.ui.Container);
  63. goog.tagUnsealableClass(goog.ui.TabBar);
  64. /**
  65. * Tab bar location relative to tab contents.
  66. * @enum {string}
  67. */
  68. goog.ui.TabBar.Location = {
  69. // Above tab contents.
  70. TOP: 'top',
  71. // Below tab contents.
  72. BOTTOM: 'bottom',
  73. // To the left of tab contents (to the right if the page is right-to-left).
  74. START: 'start',
  75. // To the right of tab contents (to the left if the page is right-to-left).
  76. END: 'end'
  77. };
  78. /**
  79. * Tab bar location; defaults to {@link goog.ui.TabBar.Location.TOP}.
  80. * @type {goog.ui.TabBar.Location}
  81. * @private
  82. */
  83. goog.ui.TabBar.prototype.location_;
  84. /**
  85. * Whether keyboard navigation should change the selected tab, or just move
  86. * the highlight. Defaults to true.
  87. * @type {boolean}
  88. * @private
  89. */
  90. goog.ui.TabBar.prototype.autoSelectTabs_ = true;
  91. /**
  92. * The currently selected tab (null if none).
  93. * @type {goog.ui.Control?}
  94. * @private
  95. */
  96. goog.ui.TabBar.prototype.selectedTab_ = null;
  97. /**
  98. * @override
  99. */
  100. goog.ui.TabBar.prototype.enterDocument = function() {
  101. goog.ui.TabBar.superClass_.enterDocument.call(this);
  102. this.listenToTabEvents_();
  103. };
  104. /** @override */
  105. goog.ui.TabBar.prototype.disposeInternal = function() {
  106. goog.ui.TabBar.superClass_.disposeInternal.call(this);
  107. this.selectedTab_ = null;
  108. };
  109. /**
  110. * Removes the tab from the tab bar. Overrides the superclass implementation
  111. * by deselecting the tab being removed. Since {@link #removeChildAt} uses
  112. * {@link #removeChild} internally, we only need to override this method.
  113. * @param {string|goog.ui.Component} tab Tab to remove.
  114. * @param {boolean=} opt_unrender Whether to call {@code exitDocument} on the
  115. * removed tab, and detach its DOM from the document (defaults to false).
  116. * @return {goog.ui.Control} The removed tab, if any.
  117. * @override
  118. */
  119. goog.ui.TabBar.prototype.removeChild = function(tab, opt_unrender) {
  120. // This actually only accepts goog.ui.Controls. There's a TODO
  121. // on the superclass method to fix this.
  122. this.deselectIfSelected(/** @type {goog.ui.Control} */ (tab));
  123. return goog.ui.TabBar.superClass_.removeChild.call(this, tab, opt_unrender);
  124. };
  125. /**
  126. * @return {goog.ui.TabBar.Location} Tab bar location relative to tab contents.
  127. */
  128. goog.ui.TabBar.prototype.getLocation = function() {
  129. return this.location_;
  130. };
  131. /**
  132. * Sets the location of the tab bar relative to tab contents.
  133. * @param {goog.ui.TabBar.Location} location Tab bar location relative to tab
  134. * contents.
  135. * @throws {Error} If the tab bar has already been rendered.
  136. */
  137. goog.ui.TabBar.prototype.setLocation = function(location) {
  138. // setOrientation() will take care of throwing an error if already rendered.
  139. this.setOrientation(goog.ui.TabBar.getOrientationFromLocation(location));
  140. this.location_ = location;
  141. };
  142. /**
  143. * @return {boolean} Whether keyboard navigation should change the selected tab,
  144. * or just move the highlight.
  145. */
  146. goog.ui.TabBar.prototype.isAutoSelectTabs = function() {
  147. return this.autoSelectTabs_;
  148. };
  149. /**
  150. * Enables or disables auto-selecting tabs using the keyboard. If auto-select
  151. * is enabled, keyboard navigation switches tabs immediately, otherwise it just
  152. * moves the highlight.
  153. * @param {boolean} enable Whether keyboard navigation should change the
  154. * selected tab, or just move the highlight.
  155. */
  156. goog.ui.TabBar.prototype.setAutoSelectTabs = function(enable) {
  157. this.autoSelectTabs_ = enable;
  158. };
  159. /**
  160. * Highlights the tab at the given index in response to a keyboard event.
  161. * Overrides the superclass implementation by also selecting the tab if
  162. * {@link #isAutoSelectTabs} returns true.
  163. * @param {number} index Index of tab to highlight.
  164. * @protected
  165. * @override
  166. */
  167. goog.ui.TabBar.prototype.setHighlightedIndexFromKeyEvent = function(index) {
  168. goog.ui.TabBar.superClass_.setHighlightedIndexFromKeyEvent.call(this, index);
  169. if (this.autoSelectTabs_) {
  170. // Immediately select the tab.
  171. this.setSelectedTabIndex(index);
  172. }
  173. };
  174. /**
  175. * @return {goog.ui.Control?} The currently selected tab (null if none).
  176. */
  177. goog.ui.TabBar.prototype.getSelectedTab = function() {
  178. return this.selectedTab_;
  179. };
  180. /**
  181. * Selects the given tab.
  182. * @param {goog.ui.Control?} tab Tab to select (null to select none).
  183. */
  184. goog.ui.TabBar.prototype.setSelectedTab = function(tab) {
  185. if (tab) {
  186. // Select the tab and have it dispatch a SELECT event, to be handled in
  187. // handleTabSelect() below.
  188. tab.setSelected(true);
  189. } else if (this.getSelectedTab()) {
  190. // De-select the currently selected tab and have it dispatch an UNSELECT
  191. // event, to be handled in handleTabUnselect() below.
  192. this.getSelectedTab().setSelected(false);
  193. }
  194. };
  195. /**
  196. * @return {number} Index of the currently selected tab (-1 if none).
  197. */
  198. goog.ui.TabBar.prototype.getSelectedTabIndex = function() {
  199. return this.indexOfChild(this.getSelectedTab());
  200. };
  201. /**
  202. * Selects the tab at the given index.
  203. * @param {number} index Index of the tab to select (-1 to select none).
  204. */
  205. goog.ui.TabBar.prototype.setSelectedTabIndex = function(index) {
  206. this.setSelectedTab(/** @type {goog.ui.Tab} */ (this.getChildAt(index)));
  207. };
  208. /**
  209. * If the specified tab is the currently selected tab, deselects it, and
  210. * selects the closest selectable tab in the tab bar (first looking before,
  211. * then after the deselected tab). Does nothing if the argument is not the
  212. * currently selected tab. Called internally when a tab is removed, hidden,
  213. * or disabled, to ensure that another tab is selected instead.
  214. * @param {goog.ui.Control?} tab Tab to deselect (if any).
  215. * @protected
  216. */
  217. goog.ui.TabBar.prototype.deselectIfSelected = function(tab) {
  218. if (tab && tab == this.getSelectedTab()) {
  219. var index = this.indexOfChild(tab);
  220. // First look for the closest selectable tab before this one.
  221. for (var i = index - 1;
  222. tab = /** @type {goog.ui.Tab} */ (this.getChildAt(i)); i--) {
  223. if (this.isSelectableTab(tab)) {
  224. this.setSelectedTab(tab);
  225. return;
  226. }
  227. }
  228. // Next, look for the closest selectable tab after this one.
  229. for (var j = index + 1;
  230. tab = /** @type {goog.ui.Tab} */ (this.getChildAt(j)); j++) {
  231. if (this.isSelectableTab(tab)) {
  232. this.setSelectedTab(tab);
  233. return;
  234. }
  235. }
  236. // If all else fails, just set the selection to null.
  237. this.setSelectedTab(null);
  238. }
  239. };
  240. /**
  241. * Returns true if the tab is selectable, false otherwise. Only visible and
  242. * enabled tabs are selectable.
  243. * @param {goog.ui.Control} tab Tab to check.
  244. * @return {boolean} Whether the tab is selectable.
  245. * @protected
  246. */
  247. goog.ui.TabBar.prototype.isSelectableTab = function(tab) {
  248. return tab.isVisible() && tab.isEnabled();
  249. };
  250. /**
  251. * Handles {@code SELECT} events dispatched by tabs as they become selected.
  252. * @param {goog.events.Event} e Select event to handle.
  253. * @protected
  254. */
  255. goog.ui.TabBar.prototype.handleTabSelect = function(e) {
  256. if (this.selectedTab_ && this.selectedTab_ != e.target) {
  257. // Deselect currently selected tab.
  258. this.selectedTab_.setSelected(false);
  259. }
  260. this.selectedTab_ = /** @type {goog.ui.Tab} */ (e.target);
  261. };
  262. /**
  263. * Handles {@code UNSELECT} events dispatched by tabs as they become deselected.
  264. * @param {goog.events.Event} e Unselect event to handle.
  265. * @protected
  266. */
  267. goog.ui.TabBar.prototype.handleTabUnselect = function(e) {
  268. if (e.target == this.selectedTab_) {
  269. this.selectedTab_ = null;
  270. }
  271. };
  272. /**
  273. * Handles {@code DISABLE} events displayed by tabs.
  274. * @param {goog.events.Event} e Disable event to handle.
  275. * @protected
  276. */
  277. goog.ui.TabBar.prototype.handleTabDisable = function(e) {
  278. this.deselectIfSelected(/** @type {goog.ui.Tab} */ (e.target));
  279. };
  280. /**
  281. * Handles {@code HIDE} events displayed by tabs.
  282. * @param {goog.events.Event} e Hide event to handle.
  283. * @protected
  284. */
  285. goog.ui.TabBar.prototype.handleTabHide = function(e) {
  286. this.deselectIfSelected(/** @type {goog.ui.Tab} */ (e.target));
  287. };
  288. /**
  289. * Handles focus events dispatched by the tab bar's key event target. If no tab
  290. * is currently highlighted, highlights the selected tab or the first tab if no
  291. * tab is selected either.
  292. * @param {goog.events.Event} e Focus event to handle.
  293. * @protected
  294. * @override
  295. */
  296. goog.ui.TabBar.prototype.handleFocus = function(e) {
  297. if (!this.getHighlighted()) {
  298. this.setHighlighted(
  299. this.getSelectedTab() ||
  300. /** @type {goog.ui.Tab} */ (this.getChildAt(0)));
  301. }
  302. };
  303. /**
  304. * Subscribes to events dispatched by tabs.
  305. * @private
  306. */
  307. goog.ui.TabBar.prototype.listenToTabEvents_ = function() {
  308. // Listen for SELECT, UNSELECT, DISABLE, and HIDE events dispatched by tabs.
  309. this.getHandler()
  310. .listen(this, goog.ui.Component.EventType.SELECT, this.handleTabSelect)
  311. .listen(
  312. this, goog.ui.Component.EventType.UNSELECT, this.handleTabUnselect)
  313. .listen(this, goog.ui.Component.EventType.DISABLE, this.handleTabDisable)
  314. .listen(this, goog.ui.Component.EventType.HIDE, this.handleTabHide);
  315. };
  316. /**
  317. * Returns the {@link goog.ui.Container.Orientation} that is implied by the
  318. * given {@link goog.ui.TabBar.Location}.
  319. * @param {goog.ui.TabBar.Location} location Tab bar location.
  320. * @return {goog.ui.Container.Orientation} Corresponding orientation.
  321. */
  322. goog.ui.TabBar.getOrientationFromLocation = function(location) {
  323. return location == goog.ui.TabBar.Location.START ||
  324. location == goog.ui.TabBar.Location.END ?
  325. goog.ui.Container.Orientation.VERTICAL :
  326. goog.ui.Container.Orientation.HORIZONTAL;
  327. };
  328. // Register a decorator factory function for goog.ui.TabBars.
  329. goog.ui.registry.setDecoratorByClassName(
  330. goog.ui.TabBarRenderer.CSS_CLASS,
  331. function() { return new goog.ui.TabBar(); });