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, switchOnClick) { |
| 68 var tab = new TabEntry(id, contentView); |
| 69 this.tabs_.push(tab); |
| 70 |
| 71 if (switchOnClick) { |
| 72 // Attach a click handler, used to switch to the tab. |
| 73 var self = this; |
| 74 tab.getTabHandleNode().onclick = function() { |
| 75 self.switchToTab(id); |
| 76 }; |
| 77 } |
| 78 |
| 79 // Start tabs off as hidden. |
| 80 tab.contentView.show(false); |
| 81 }; |
| 82 |
| 83 /** |
| 84 * Returns the currently selected tab, or null if there is none. |
| 85 * @returns {!TabEntry} |
| 86 */ |
| 87 TabSwitcherView.prototype.findActiveTab = function() { |
| 88 for (var i = 0; i < this.tabs_.length; ++i) { |
| 89 var tab = this.tabs_[i]; |
| 90 if (tab.active) |
| 91 return tab; |
| 92 } |
| 93 return null; |
| 94 }; |
| 95 |
| 96 /** |
| 97 * Returns the tab with ID |id|. |
| 98 * @returns {!TabEntry} |
| 99 */ |
| 100 TabSwitcherView.prototype.findTabById = function(id) { |
| 101 for (var i = 0; i < this.tabs_.length; ++i) { |
| 102 var tab = this.tabs_[i]; |
| 103 if (tab.id == id) |
| 104 return tab; |
| 105 } |
| 106 return null; |
| 107 }; |
| 108 |
| 109 /** |
| 110 * Focuses on tab with ID |id|. |
| 111 */ |
| 112 TabSwitcherView.prototype.switchToTab = function(id) { |
| 113 var oldTab = this.findActiveTab(); |
| 114 if (oldTab) |
| 115 oldTab.setSelected(false); |
| 116 |
| 117 var newTab = this.findTabById(id); |
| 118 newTab.setSelected(true); |
| 119 }; |
| 120 |
| 121 TabSwitcherView.prototype.getAllTabIds = function() { |
| 122 var ids = []; |
| 123 for (var i = 0; i < this.tabs_.length; ++i) |
| 124 ids.push(this.tabs_[i].id); |
| 125 return ids; |
| 126 }; |
| 127 |
| 128 //----------------------------------------------------------------------------- |
| 129 |
| 130 /** |
| 131 * @constructor |
| 132 */ |
| 133 function TabEntry(id, contentView) { |
| 134 this.id = id; |
| 135 this.contentView = contentView; |
| 136 } |
| 137 |
| 138 TabEntry.prototype.setSelected = function(isSelected) { |
| 139 this.active = isSelected; |
| 140 changeClassName(this.getTabHandleNode(), 'selected', isSelected); |
| 141 this.contentView.show(isSelected); |
| 142 }; |
| 143 |
| 144 /** |
| 145 * Returns the DOM node that is used to select the tab. |
| 146 */ |
| 147 TabEntry.prototype.getTabHandleNode = function() { |
| 148 return document.getElementById(this.id); |
| 149 }; |
| 150 |
OLD | NEW |