Files
cloudbeaver/webapp/packages/plugin-data-viewer/src/DataViewerTabService.ts
T
2021-03-15 20:02:13 +03:00

137 lines
4.3 KiB
TypeScript

/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2021 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import {
NavNodeManagerService,
INodeNavigationData,
ITab,
NodeManagerUtils
} from '@cloudbeaver/core-app';
import { injectable } from '@cloudbeaver/core-di';
import { NotificationService } from '@cloudbeaver/core-events';
import type { IExecutionContextProvider } from '@cloudbeaver/core-executor';
import {
DBObjectPageService, ObjectPage, ObjectViewerTabService, IObjectViewerTabState
} from '@cloudbeaver/plugin-object-viewer';
import { DataPresentationService } from './DataPresentationService';
import { DataViewerPanel } from './DataViewerPage/DataViewerPanel';
import { DataViewerTab } from './DataViewerPage/DataViewerTab';
import { DataViewerTableService } from './DataViewerTableService';
import type { IDataViewerPageState } from './IDataViewerPageState';
@injectable()
export class DataViewerTabService {
readonly page: ObjectPage<IDataViewerPageState>;
constructor(
private navNodeManagerService: NavNodeManagerService,
private dataViewerTableService: DataViewerTableService,
private objectViewerTabService: ObjectViewerTabService,
private dbObjectPageService: DBObjectPageService,
private notificationService: NotificationService,
private dataPresentationService: DataPresentationService
) {
this.page = this.dbObjectPageService.register({
key: 'data_viewer_data',
priority: 2,
order: 2,
getTabComponent: () => DataViewerTab,
getPanelComponent: () => DataViewerPanel,
onSelect: this.handleTabSelect.bind(this),
onRestore: this.handleTabRestore.bind(this),
onClose: this.handleTabClose.bind(this),
});
}
registerTabHandler(): void {
this.navNodeManagerService.navigator.addHandler(this.navigationHandler.bind(this));
}
private async navigationHandler(data: INodeNavigationData, contexts: IExecutionContextProvider<INodeNavigationData>) {
try {
const {
nodeInfo,
tabInfo,
trySwitchPage,
} = await contexts.getContext(this.objectViewerTabService.objectViewerTabContext);
const node = await this.navNodeManagerService.loadNode(nodeInfo);
if (!this.navNodeManagerService.isNodeHasData(node)) {
return;
}
if (tabInfo.isNewlyCreated) {
trySwitchPage(this.page);
}
// if (nodeInfo.childrenId === '') {
// tabInfo.trySwitchHandler(this.tabHandler);
// }
} catch (exception) {
this.notificationService.logException(exception, 'Data Viewer Error', 'Error in Data Viewer while processing action with database node');
}
}
private async handleTabSelect(tab: ITab<IObjectViewerTabState>) {
const node = await this.navNodeManagerService.loadNode({
nodeId: tab.handlerState.objectId,
parentId: tab.handlerState.parentId,
});
if (!this.navNodeManagerService.isNodeHasData(node)) {
return;
}
if (this.dataViewerTableService.has(tab.id)) {
return;
}
const nodeInfo = this.navNodeManagerService
.getNodeContainerInfo(tab.handlerState.objectId);
if (!nodeInfo.connectionId) {
return;
}
let model = this.dataViewerTableService.get(tab.handlerState.tableId || '');
if (!model) {
model = await this.dataViewerTableService.create(
NodeManagerUtils.connectionNodeIdToConnectionId(nodeInfo.connectionId),
tab.handlerState.objectId
);
tab.handlerState.tableId = model.id;
const pageState = this.page.getState(tab);
if (pageState) {
const presentation = this.dataPresentationService.get(pageState?.presentationId);
if (presentation) {
model.setDataFormat(presentation.dataFormat);
}
}
}
// TODO: used for initial data fetch, but can repeat request each time data tab is selected,
// so probably should be refactored and managed by presentation
if (model.source.results.length === 0) {
await model.requestData();
}
}
private async handleTabRestore(tab: ITab<IObjectViewerTabState>) {
return true;
}
private handleTabClose(tab: ITab<IObjectViewerTabState>) {
if (tab.handlerState.tableId) {
this.dataViewerTableService.removeTableModel(tab.handlerState.tableId);
}
}
}