mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-12-28 21:32:05 +00:00
With this change TabsBase objects will retain a reference to the TabsManager they belong to, so they can ask it if they're the active tab or not. This removes the possibility for bugs like activating an unmanaged tab, or having more than one active tab, etc.
55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import * as ko from "knockout";
|
|
import * as ViewModels from "../../Contracts/ViewModels";
|
|
import TabsBase from "./TabsBase";
|
|
|
|
export class TabsManager {
|
|
public openedTabs = ko.observableArray<TabsBase>([]);
|
|
public activeTab = ko.observable<TabsBase>();
|
|
|
|
public activateNewTab(tab: TabsBase): void {
|
|
this.openedTabs.push(tab);
|
|
this.activateTab(tab);
|
|
}
|
|
|
|
public activateTab(tab: TabsBase): void {
|
|
if (this.openedTabs().includes(tab)) {
|
|
tab.manager = this;
|
|
this.activeTab(tab);
|
|
tab.onActivate();
|
|
}
|
|
}
|
|
|
|
public getTabs(tabKind: ViewModels.CollectionTabKind, comparator?: (tab: TabsBase) => boolean): TabsBase[] {
|
|
return this.openedTabs().filter((tab) => tab.tabKind === tabKind && (!comparator || comparator(tab)));
|
|
}
|
|
|
|
public refreshActiveTab(comparator: (tab: TabsBase) => boolean): void {
|
|
// ensures that the tab selects/highlights the right node based on resource tree expand/collapse state
|
|
this.activeTab() && comparator(this.activeTab()) && this.activeTab().onActivate();
|
|
}
|
|
|
|
public closeTabsByComparator(comparator: (tab: TabsBase) => boolean): void {
|
|
this.openedTabs()
|
|
.filter(comparator)
|
|
.forEach((tab) => tab.onCloseTabButtonClick());
|
|
}
|
|
|
|
public closeTab(tab: TabsBase): void {
|
|
const tabIndex = this.openedTabs().indexOf(tab);
|
|
if (tabIndex !== -1) {
|
|
this.openedTabs.remove(tab);
|
|
tab.manager = undefined;
|
|
|
|
if (this.openedTabs().length === 0) {
|
|
this.activeTab(undefined);
|
|
}
|
|
|
|
if (tab === this.activeTab()) {
|
|
const tabToTheRight = this.openedTabs()[tabIndex];
|
|
const lastOpenTab = this.openedTabs()[this.openedTabs().length - 1];
|
|
this.activateTab(tabToTheRight ?? lastOpenTab);
|
|
}
|
|
}
|
|
}
|
|
}
|