cosmos-explorer/src/Explorer/Tabs/TabsBase.ts

164 lines
5.8 KiB
TypeScript
Raw Normal View History

2021-01-20 15:15:01 +00:00
import * as ko from "knockout";
import * as Constants from "../../Common/Constants";
import * as ThemeUtility from "../../Common/ThemeUtility";
2021-01-20 15:15:01 +00:00
import * as DataModels from "../../Contracts/DataModels";
import * as ViewModels from "../../Contracts/ViewModels";
import { useNotificationConsole } from "../../hooks/useNotificationConsole";
2021-07-09 05:32:22 +01:00
import { useTabs } from "../../hooks/useTabs";
import { Action, ActionModifiers } from "../../Shared/Telemetry/TelemetryConstants";
2021-01-20 15:15:01 +00:00
import * as TelemetryProcessor from "../../Shared/Telemetry/TelemetryProcessor";
import { CommandButtonComponentProps } from "../Controls/CommandButton/CommandButtonComponent";
import Explorer from "../Explorer";
2021-05-28 21:20:59 +01:00
import { useCommandBar } from "../Menus/CommandBar/CommandBarComponentAdapter";
2021-06-24 19:56:33 +01:00
import { useSelectedNode } from "../useSelectedNode";
import { WaitsForTemplateViewModel } from "../WaitsForTemplateViewModel";
2021-01-20 15:15:01 +00:00
// TODO: Use specific actions for logging telemetry data
export default class TabsBase extends WaitsForTemplateViewModel {
private static id = 0;
public readonly index: number;
2021-01-20 15:15:01 +00:00
public closeTabButton: ViewModels.Button;
public node: ViewModels.TreeNode;
public collection: ViewModels.CollectionBase;
public database: ViewModels.Database;
public rid: string;
public tabId = `tab${TabsBase.id++}`;
2021-01-20 15:15:01 +00:00
public tabKind: ViewModels.CollectionTabKind;
public tabTitle: ko.Observable<string>;
public tabPath: ko.Observable<string>;
2021-04-19 21:11:48 +01:00
public isExecutionError = ko.observable(false);
public isExecuting = ko.observable(false);
2021-01-20 15:15:01 +00:00
public pendingNotification?: ko.Observable<DataModels.Notification>;
protected _theme: string;
public onLoadStartKey: number;
constructor(options: ViewModels.TabOptions) {
super();
this.index = options.index;
2021-01-20 15:15:01 +00:00
this._theme = ThemeUtility.getMonacoTheme(options.theme);
this.node = options.node;
this.collection = options.collection;
this.database = options.database;
this.rid = options.rid || (this.collection && this.collection.rid) || "";
this.tabKind = options.tabKind;
this.tabTitle = ko.observable<string>(options.title);
this.tabPath =
ko.observable(options.tabPath ?? "") ||
2021-01-20 15:15:01 +00:00
(this.collection &&
ko.observable<string>(`${this.collection.databaseId}>${this.collection.id()}>${this.tabTitle()}`));
this.pendingNotification = ko.observable<DataModels.Notification>(undefined);
this.onLoadStartKey = options.onLoadStartKey;
this.closeTabButton = {
enabled: ko.computed<boolean>(() => {
return true;
}),
visible: ko.computed<boolean>(() => {
return true;
}),
};
}
public onCloseTabButtonClick(): void {
2021-07-09 05:32:22 +01:00
useTabs.getState().closeTab(this);
2021-01-20 15:15:01 +00:00
TelemetryProcessor.trace(Action.Tab, ActionModifiers.Close, {
tabName: this.constructor.name,
dataExplorerArea: Constants.Areas.Tab,
tabTitle: this.tabTitle(),
tabId: this.tabId,
});
}
public onTabClick(): void {
2021-07-09 05:32:22 +01:00
useTabs.getState().activateTab(this);
2021-01-20 15:15:01 +00:00
}
protected updateSelectedNode(): void {
const relatedDatabase = (this.collection && this.collection.getDatabase()) || this.database;
2021-06-24 19:56:33 +01:00
const setSelectedNode = useSelectedNode.getState().setSelectedNode;
2021-01-20 15:15:01 +00:00
if (relatedDatabase && !relatedDatabase.isDatabaseExpanded()) {
2021-06-24 19:56:33 +01:00
setSelectedNode(relatedDatabase);
2021-01-20 15:15:01 +00:00
} else if (this.collection && !this.collection.isCollectionExpanded()) {
2021-06-24 19:56:33 +01:00
setSelectedNode(this.collection);
2021-01-20 15:15:01 +00:00
} else {
2021-06-24 19:56:33 +01:00
setSelectedNode(this.node);
2021-01-20 15:15:01 +00:00
}
}
private onSpaceOrEnterKeyPress(event: KeyboardEvent, callback: () => void): boolean {
if (event.keyCode === Constants.KeyCodes.Space || event.keyCode === Constants.KeyCodes.Enter) {
callback();
event.stopPropagation();
return false;
}
return true;
}
public onKeyPressActivate = (source: any, event: KeyboardEvent): boolean => {
return this.onSpaceOrEnterKeyPress(event, () => this.onTabClick());
};
public onKeyPressClose = (source: any, event: KeyboardEvent): boolean => {
return this.onSpaceOrEnterKeyPress(event, () => this.onCloseTabButtonClick());
};
/** @deprecated this is no longer observable, bind to comparisons with manager.activeTab() instead */
public isActive() {
2021-07-09 05:32:22 +01:00
return this === useTabs.getState().activeTab;
}
2021-01-20 15:15:01 +00:00
public onActivate(): void {
this.updateSelectedNode();
2021-04-19 21:11:48 +01:00
this.collection?.selectedSubnodeKind(this.tabKind);
this.database?.selectedSubnodeKind(this.tabKind);
2021-01-20 15:15:01 +00:00
this.updateNavbarWithTabsButtons();
TelemetryProcessor.trace(Action.Tab, ActionModifiers.Open, {
tabName: this.constructor.name,
dataExplorerArea: Constants.Areas.Tab,
tabTitle: this.tabTitle(),
tabId: this.tabId,
});
}
public onErrorDetailsClick = (src: any, event: MouseEvent): boolean => {
useNotificationConsole.getState().expandConsole();
useNotificationConsole.getState().expandConsole();
2021-01-20 15:15:01 +00:00
return false;
};
public onErrorDetailsKeyPress = (src: any, event: KeyboardEvent): boolean => {
if (event.keyCode === Constants.KeyCodes.Space || event.keyCode === Constants.KeyCodes.Enter) {
this.onErrorDetailsClick(src, null);
return false;
}
return true;
};
2021-04-19 21:11:48 +01:00
public refresh() {
2021-01-20 15:15:01 +00:00
location.reload();
}
public getContainer(): Explorer {
2021-01-20 15:15:01 +00:00
return (this.collection && this.collection.container) || (this.database && this.database.container);
}
/** Renders a Javascript object to be displayed inside Monaco Editor */
public renderObjectForEditor(value: any, replacer: any, space: string | number): string {
2021-01-20 15:15:01 +00:00
return JSON.stringify(value, replacer, space);
}
/**
* @return buttons that are displayed in the navbar
*/
protected getTabsButtons(): CommandButtonComponentProps[] {
return [];
}
public updateNavbarWithTabsButtons = (): void => {
2021-01-20 15:15:01 +00:00
if (this.isActive()) {
2021-05-28 21:20:59 +01:00
useCommandBar.getState().setContextButtons(this.getTabsButtons());
2021-01-20 15:15:01 +00:00
}
};
}