| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 * TabSwitcherView is an implementation of View that handles tab switching. | |
| 7 * | |
| 8 * It is comprised of many views (tabs), only one of which is visible at a | |
| 9 * time. | |
| 10 * | |
| 11 * This view represents solely the selected tab's content area -- a separate | |
| 12 * view needs to be maintained for the tab handles. | |
| 13 * | |
| 14 * @constructor | |
| 15 */ | |
| 16 function TabSwitcherView() { | |
| 17 View.call(this); | |
| 18 this.tabs_ = []; | |
| 19 } | |
| 20 | |
| 21 inherits(TabSwitcherView, View); | |
| 22 | |
| 23 TabSwitcherView.prototype.setGeometry = function(left, top, width, height) { | |
| 24 TabSwitcherView.superClass_.setGeometry.call(this, left, top, width, height); | |
| 25 | |
| 26 // Position each of the tabs content areas. | |
| 27 for (var i = 0; i < this.tabs_.length; ++i) { | |
| 28 var tab = this.tabs_[i]; | |
| 29 tab.contentView.setGeometry(left, top, width, height); | |
| 30 } | |
| 31 }; | |
| 32 | |
| 33 TabSwitcherView.prototype.show = function(isVisible) { | |
| 34 TabSwitcherView.superClass_.show.call(this, isVisible); | |
| 35 | |
| 36 var activeTab = this.findActiveTab(); | |
| 37 if (activeTab) | |
| 38 activeTab.contentView.show(isVisible); | |
| 39 }; | |
| 40 | |
| 41 /** | |
| 42 * Adds a new tab (initially hidden). | |
| 43 * | |
| 44 * @param {String} id The ID for DOM node that will be made clickable to select | |
| 45 * this tab. This is also the ID we use to identify the | |
| 46 * "tab". | |
| 47 * @param {!View} view The tab's actual contents. | |
| 48 */ | |
| 49 TabSwitcherView.prototype.addTab = function(id, contentView, switchOnClick, | |
| 50 visible) { | |
| 51 var tab = new TabEntry(id, contentView); | |
| 52 this.tabs_.push(tab); | |
| 53 | |
| 54 if (switchOnClick) { | |
| 55 // Attach a click handler, used to switch to the tab. | |
| 56 var self = this; | |
| 57 tab.getTabHandleNode().onclick = function() { | |
| 58 self.switchToTab(id, null); | |
| 59 }; | |
| 60 } | |
| 61 | |
| 62 // Start tabs off as hidden. | |
| 63 tab.contentView.show(false); | |
| 64 | |
| 65 this.showTabHandleNode(id, visible); | |
| 66 }; | |
| 67 | |
| 68 /** | |
| 69 * Returns the currently selected tab, or null if there is none. | |
| 70 * @returns {!TabEntry} | |
| 71 */ | |
| 72 TabSwitcherView.prototype.findActiveTab = function() { | |
| 73 for (var i = 0; i < this.tabs_.length; ++i) { | |
| 74 var tab = this.tabs_[i]; | |
| 75 if (tab.active) | |
| 76 return tab; | |
| 77 } | |
| 78 return null; | |
| 79 }; | |
| 80 | |
| 81 /** | |
| 82 * Returns the tab with ID |id|. | |
| 83 * @returns {!TabEntry} | |
| 84 */ | |
| 85 TabSwitcherView.prototype.findTabById = function(id) { | |
| 86 for (var i = 0; i < this.tabs_.length; ++i) { | |
| 87 var tab = this.tabs_[i]; | |
| 88 if (tab.id == id) | |
| 89 return tab; | |
| 90 } | |
| 91 return null; | |
| 92 }; | |
| 93 | |
| 94 /** | |
| 95 * Focuses on tab with ID |id|. |params| is a dictionary that will be | |
| 96 * passed to the tab's setParameters function, if it's non-null. | |
| 97 */ | |
| 98 TabSwitcherView.prototype.switchToTab = function(id, params) { | |
| 99 var oldTab = this.findActiveTab(); | |
| 100 if (oldTab) | |
| 101 oldTab.setSelected(false); | |
| 102 | |
| 103 var newTab = this.findTabById(id); | |
| 104 newTab.setSelected(true); | |
| 105 if (params) | |
| 106 newTab.contentView.setParameters(params); | |
| 107 | |
| 108 // Update data needed by newly active tab, as it may be | |
| 109 // significantly out of date. | |
| 110 if (typeof g_browser != 'undefined' && g_browser.checkForUpdatedInfo) | |
| 111 g_browser.checkForUpdatedInfo(); | |
| 112 }; | |
| 113 | |
| 114 TabSwitcherView.prototype.getAllTabIds = function() { | |
| 115 var ids = []; | |
| 116 for (var i = 0; i < this.tabs_.length; ++i) | |
| 117 ids.push(this.tabs_[i].id); | |
| 118 return ids; | |
| 119 }; | |
| 120 | |
| 121 // Shows/hides the DOM node that is used to select the tab. Will not change | |
| 122 // the active tab. | |
| 123 TabSwitcherView.prototype.showTabHandleNode = function(id, isVisible) { | |
| 124 var tab = this.findTabById(id); | |
| 125 setNodeDisplay(tab.getTabHandleNode(), isVisible); | |
| 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 $(this.id); | |
| 149 }; | |
| 150 | |
| OLD | NEW |