@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
102 lines (101 loc) • 4.3 kB
JavaScript
import Emitter from '../../Utilities/Emitter';
import { Subject } from 'rxjs';
import Helper from '../Helpers/Helper';
const MAX_TIMEFRAME_SIZE = 86400000;
export class DataService {
adaptable;
emitter;
cellDataChangeLogSubject$;
cellDataChangeLog$;
rowDataChangeLogSubject$;
rowDataChangeLog$;
undoChangeLog;
undoChangeTimers;
constructor(adaptable) {
this.adaptable = adaptable;
this.adaptable = adaptable;
this.emitter = new Emitter();
this.cellDataChangeLogSubject$ = new Subject();
this.cellDataChangeLog$ = this.cellDataChangeLogSubject$.asObservable();
this.rowDataChangeLogSubject$ = new Subject();
this.rowDataChangeLog$ = this.rowDataChangeLogSubject$.asObservable();
this.undoChangeLog = new Map();
this.undoChangeTimers = new Map();
}
destroy() {
this.emitter.destroy();
this.emitter = null;
this.cellDataChangeLogSubject$.complete();
this.cellDataChangeLogSubject$ = null;
this.cellDataChangeLog$ = null;
this.rowDataChangeLogSubject$.complete();
this.rowDataChangeLogSubject$ = null;
this.rowDataChangeLog$ = null;
this.undoChangeLog.clear();
this.undoChangeLog = null;
this.undoChangeTimers.clear();
this.undoChangeTimers = null;
}
on = (eventName, callback) => this.emitter.on(eventName, callback);
emit = (eventName, data) => this.emitter.emit(eventName, data);
CreateCellDataChangedEvent(cellDataChangedInfo) {
if (cellDataChangedInfo.newValue != cellDataChangedInfo.oldValue) {
const validationRules = this.adaptable.api.internalApi
.getValidationService()
.getValidationRulesForDataChange(cellDataChangedInfo);
if (validationRules.length) {
cellDataChangedInfo.preventEdit = true;
}
this.emitter.emitSync('CellDataChanged', cellDataChangedInfo);
this.adaptable.api.eventApi.internalApi.fireCellChangedEvent(cellDataChangedInfo);
const dataChangeLogEntry = this.extractDataChangeLogEntry(cellDataChangedInfo);
this.cellDataChangeLogSubject$.next(dataChangeLogEntry);
}
}
CreateRowDataChangedEvent(rowDataChangedInfo) {
this.emitter.emitSync('RowDataChanged', rowDataChangedInfo);
this.adaptable.api.eventApi.internalApi.fireRowChangedEvent(rowDataChangedInfo);
this.rowDataChangeLogSubject$.next(rowDataChangedInfo);
}
logUndoChange(change) {
const { primaryKeyValue, column, oldValue, newValue } = change;
const undoChangeKey = this.getUndoChangeKey(primaryKeyValue, column.columnId, oldValue, newValue);
this.undoChangeLog.set(undoChangeKey, change);
const UNDO_WAIT = 2000;
const timeoutId = setTimeout(() => {
this.adaptable.logger.warn(`Undo change was not handled within timeout: column="${change.column}", primaryKey="${change.primaryKeyValue}", from=${change.newValue} to=${change.oldValue}.`);
this.extractUndoChange(change);
}, UNDO_WAIT);
this.undoChangeTimers.set(undoChangeKey, timeoutId);
}
extractUndoChange(change) {
const { primaryKeyValue, column, oldValue, newValue } = change;
const undoChangeKey = this.getUndoChangeKey(primaryKeyValue, column.columnId, newValue, oldValue);
const result = this.undoChangeLog.get(undoChangeKey);
this.undoChangeLog.delete(undoChangeKey);
const timeoutId = this.undoChangeTimers.get(undoChangeKey);
if (timeoutId) {
clearTimeout(timeoutId);
}
return result;
}
getUndoChangeKey(primaryKeyValue, columnId, previousValue, newValue) {
return JSON.stringify({
primaryKeyValue,
columnId,
previousValue,
newValue,
});
}
extractDataChangeLogEntry(cellDataChangedInfo) {
const rowData = cellDataChangedInfo.rowData
? Helper.cloneObject(cellDataChangedInfo.rowData)
: null;
const rowNode = rowData ? { data: rowData } : null;
return {
...cellDataChangedInfo,
rowNode,
rowData,
};
}
}