Merge pull request #130 from dbeaver/feat-data-readonly

feat(plugin-data-viewer): read-only mode
This commit is contained in:
Serge Rider
2020-08-03 14:46:02 +03:00
committed by GitHub
3 changed files with 29 additions and 1 deletions
@@ -10,6 +10,7 @@ import { TableSelection } from './TableSelection/TableSelection';
export type AgGridContext = {
selection: TableSelection;
isReadonly(): boolean;
isCellEdited(rowId: number, column: string): boolean;
revertCellValue(rowNumber: number, column: string): void;
onEditSave(): void;
@@ -25,7 +25,7 @@ import {
} from '@ag-grid-community/core';
import { injectable, IInitializableController, IDestructibleController } from '@cloudbeaver/core-di';
import {
TableViewerModel, SortModel, AgGridRow, IAgGridCol
TableViewerModel, SortModel, AgGridRow, IAgGridCol, AccessMode
} from '@cloudbeaver/plugin-data-viewer';
import { AgGridContext } from './AgGridContext';
@@ -46,6 +46,7 @@ export class AgGridTableController implements IInitializableController, IDestruc
*/
private readonly context: AgGridContext = {
selection: this.selection,
isReadonly: () => this.gridModel.access === AccessMode.Readonly,
isCellEdited: this.isCellEdited.bind(this),
revertCellValue: this.revertCellValue.bind(this),
onEditSave: this.onEditSave.bind(this),
@@ -265,6 +266,10 @@ function mapDataToColumns(columns?: IAgGridCol[]): ColDef[] {
headerName: v.label,
field: `${v.position}`,
type: v.dataKind,
editable: (params: any) => {
const context: AgGridContext = params.context;
return !context.isReadonly();
},
valueGetter: (params: ValueGetterParams) => {
if (!params.data) {
return '';
@@ -28,6 +28,11 @@ export const fetchingSettings = {
fetchDefault: 200,
};
export enum AccessMode {
Default,
Readonly
}
export type AgGridRow = any[];
export type SortMode = 'asc' | 'desc' | null;
@@ -68,6 +73,7 @@ export interface ITableViewerModelOptions {
executionContext?: IExecutionContext | null; // will be filled before fist data fetch
sourceName?: string; // TODO: refactor it, used for showing sql query for export
noLoaderWhileRequestingDataAsync?: boolean;
access?: AccessMode;
requestDataAsync(
model: TableViewerModel,
rowOffset: number,
@@ -92,6 +98,9 @@ export class TableViewerModel {
executionContext: IExecutionContext | null;
sourceName?: string;
noLoaderWhileRequestingDataAsync?: boolean;
@observable access: AccessMode;
requestDataAsync: (
model: TableViewerModel,
rowOffset: number,
@@ -147,6 +156,7 @@ export class TableViewerModel {
this.executionContext = options.executionContext || null;
this.sourceName = options.sourceName;
this.noLoaderWhileRequestingDataAsync = options.noLoaderWhileRequestingDataAsync;
this.access = options.access || AccessMode.Default;
this.requestDataAsync = options.requestDataAsync;
this._saveChanges = options.saveChanges;
this.resetSubject = new Subject();
@@ -214,6 +224,10 @@ export class TableViewerModel {
}
isEdited(): boolean {
if (this.access === AccessMode.Readonly) {
return false;
}
return this.tableEditor.isEdited();
}
@@ -230,6 +244,10 @@ export class TableViewerModel {
}
async saveChanges(): Promise<void> {
if (this.access === AccessMode.Readonly) {
return;
}
const diffs = this.tableEditor.getChanges();
if (!diffs.length) {
@@ -304,6 +322,10 @@ export class TableViewerModel {
}
onCellEditingStopped(rowNumber: number, column: string, value: any) {
if (this.access === AccessMode.Readonly) {
return;
}
this.tableEditor.editCellValue(rowNumber, column, value);
}