OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * TabSwitcher is an implementation of View that handles tab switching. |
| 7 * |
| 8 * +-----------------------------------+ |
| 9 * | Tab1 / Tab2 / Tab3 / .. | <- tab handle view |
| 10 * +-----------------------------------+ |
| 11 * | | |
| 12 * | | |
| 13 * | | |
| 14 * | stacked tab content areas | |
| 15 * | (only 1 is visible at a time) | |
| 16 * | | |
| 17 * | | |
| 18 * | | |
| 19 * +-----------------------------------+ |
| 20 * |
| 21 * @parameter {!View} tabHandleView the view that contains the tab handles. |
| 22 * |
| 23 * @constructor |
| 24 */ |
| 25 function TabSwitcherView(tabHandleView) { |
| 26 View.call(this); |
| 27 this.tabHandleView_ = tabHandleView; |
| 28 this.tabs_ = []; |
| 29 } |
| 30 |
| 31 inherits(TabSwitcherView, View); |
| 32 |
| 33 TabSwitcherView.prototype.setGeometry = function(left, top, width, height) { |
| 34 TabSwitcherView.superClass_.setGeometry.call(this, left, top, width, height); |
| 35 |
| 36 this.tabHandleView_.setGeometry( |
| 37 left, top, width, this.tabHandleView_.getHeight()); |
| 38 |
| 39 var contentTop = this.tabHandleView_.getBottom(); |
| 40 var contentHeight = height - this.tabHandleView_.getHeight(); |
| 41 |
| 42 // Position each of the tabs content areas. |
| 43 for (var i = 0; i < this.tabs_.length; ++i) { |
| 44 var tab = this.tabs_[i]; |
| 45 tab.contentView.setGeometry(left, contentTop, width, contentHeight); |
| 46 } |
| 47 }; |
| 48 |
| 49 TabSwitcherView.prototype.show = function(isVisible) { |
| 50 TabSwitcherView.superClass_.show.call(this, isVisible); |
| 51 |
| 52 this.tabHandleView_.show(isVisible); |
| 53 |
| 54 var activeTab = this.findActiveTab(); |
| 55 if (activeTab) |
| 56 activeTab.contentView.show(isVisible); |
| 57 }; |
| 58 |
| 59 /** |
| 60 * Adds a new tab (initially hidden). |
| 61 * |
| 62 * @param {String} id The ID for DOM node that will be made clickable to select |
| 63 * this tab. This is also the ID we use to identify the |
| 64 * "tab". |
| 65 * @param {!View} view The tab's actual contents. |
| 66 */ |
| 67 TabSwitcherView.prototype.addTab = function(id, contentView) { |
| 68 var tab = new TabEntry(id, contentView); |
| 69 this.tabs_.push(tab); |
| 70 |
| 71 // Attach a click handler, used to switch to the tab. |
| 72 var self = this; |
| 73 tab.getTabHandleNode().onclick = function() { |
| 74 self.switchToTab(id); |
| 75 }; |
| 76 |
| 77 // Start tabs off as hidden. |
| 78 tab.contentView.show(false); |
| 79 }; |
| 80 |
| 81 /** |
| 82 * Returns the currently selected tab, or null if there is none. |
| 83 * @returns {!TabEntry} |
| 84 */ |
| 85 TabSwitcherView.prototype.findActiveTab = function() { |
| 86 for (var i = 0; i < this.tabs_.length; ++i) { |
| 87 var tab = this.tabs_[i]; |
| 88 if (tab.active) |
| 89 return tab; |
| 90 } |
| 91 return null; |
| 92 }; |
| 93 |
| 94 /** |
| 95 * Returns the tab with ID |id|. |
| 96 * @returns {!TabEntry} |
| 97 */ |
| 98 TabSwitcherView.prototype.findTabById = function(id) { |
| 99 for (var i = 0; i < this.tabs_.length; ++i) { |
| 100 var tab = this.tabs_[i]; |
| 101 if (tab.id == id) |
| 102 return tab; |
| 103 } |
| 104 return null; |
| 105 }; |
| 106 |
| 107 /** |
| 108 * Focuses on tab with ID |id|. |
| 109 */ |
| 110 TabSwitcherView.prototype.switchToTab = function(id) { |
| 111 var oldTab = this.findActiveTab(); |
| 112 if (oldTab) |
| 113 oldTab.setSelected(false); |
| 114 |
| 115 var newTab = this.findTabById(id); |
| 116 newTab.setSelected(true); |
| 117 }; |
| 118 |
| 119 //----------------------------------------------------------------------------- |
| 120 |
| 121 /** |
| 122 * @constructor |
| 123 */ |
| 124 function TabEntry(id, contentView) { |
| 125 this.id = id; |
| 126 this.contentView = contentView; |
| 127 } |
| 128 |
| 129 TabEntry.prototype.setSelected = function(isSelected) { |
| 130 this.active = isSelected; |
| 131 changeClassName(this.getTabHandleNode(), 'selected', isSelected); |
| 132 this.contentView.show(isSelected); |
| 133 }; |
| 134 |
| 135 /** |
| 136 * Returns the DOM node that is used to select the tab. |
| 137 */ |
| 138 TabEntry.prototype.getTabHandleNode = function() { |
| 139 return document.getElementById(this.id); |
| 140 }; |
| 141 |
OLD | NEW |