UNPKG

core-cde

Version:

204 lines 6.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ValueUpdatedArg = exports.DataSet = void 0; const tslib_1 = require("tslib"); const hyperformula_1 = require("hyperformula"); const Linq_1 = (0, tslib_1.__importDefault)(require("typescript-dotnet-commonjs/System.Linq/Linq")); const StateChanger_module_1 = require("../../stateChanger/StateChanger.module"); const Events_module_1 = require("../../events/Events.module"); const Data_module_1 = require("../Data.module"); const HF_LICENCE = 'agpl-v3'; /** * Класс набора таблиц. Объединяет все таблицы одного документа */ class DataSet { constructor() { this._hashCode = ++DataSet._counter; this.id = StateChanger_module_1.UUID.get(); this._hf = hyperformula_1.HyperFormula.buildEmpty({ licenseKey: HF_LICENCE }); this._onValuesUpdated = new Events_module_1.EventDispatcher(); this._tables = new Data_module_1.DataTableCollection(); this._hf.on('valuesUpdated', (v) => { this.raiseValuesUpdated(v); }); } /** * Возвращает ссылку на объект HyperFormula */ get hf() { return this._hf; } get onValuesUpdated() { return this._onValuesUpdated; } get hashCode() { return this._hashCode; } static fromJSON(json) { if (typeof json === 'string') { return DataSet.fromJSON(JSON.parse(json)); } else { const obj = Data_module_1.DataTableCollection.fromJSON(json.tables); const ds = new DataSet(); const assign = Object.assign(obj, null, { _dataSet: ds, id: json.id }); for (let i = 0; i < assign.count; i++) { const t = obj.get(i); t.setDataSet(ds); ds.addTable(t); } return ds; } } tableFromJSON(json) { const tab = Data_module_1.DataTable.fromJSON(json); tab.setDataSet(this); this._tables.add(tab); this.addSheet(tab); return tab; } toJSON() { return Object.assign({}, null, { tables: JSON.stringify(this._tables.toJSON()), id: this.id }); } refreshAfterRestore() { this.hf.rebuildAndRecalculate(); for (let i = 0; i < this._tables.count; i++) { const tab = this._tables.get(i); tab.refreshAfterRestore(); } } /** * Возвращает количество таблиц */ get tablesCount() { return this._tables.count; } /** * Возвращает таблицу по индексу */ getTableByIndex(index) { return this._tables.get(index); } /** * Возвращает таблицу по имени */ getTableByName(name) { const e = (0, Linq_1.default)(this._tables); return e.where(t => t.uniqueName === name).firstOrDefault(); } /** * Возвращает таблицу по имени */ getTableById(id) { const e = (0, Linq_1.default)(this._tables); return e.where(t => t.id === id).firstOrDefault(); } /** * Возвращает имена всех таблиц */ getTableNames() { const e = (0, Linq_1.default)(this._tables); const res = e.select(t => t.uniqueName).toArray(); return res; } /** * Добавляет таблицу к набору * @param tableName Имя новой таблицы */ addTable(table) { let tab; if (typeof table === 'string') { tab = Data_module_1.DataTable.createNew(this, table); } else { tab = table; } tab.setDataSet(null); this._tables.add(tab); this.addSheet(tab); tab.setDataSet(this); return tab; } /** * Удаляет таблицу из набора * @param table Объект удаляемой таблицы */ removeTable(table) { if (this._tables.indexOf(table) < 0) { return 0; } this.removeSheet(table); const res = this._tables.remove(table); table.setDataSet(null); return res; } /** * Удаляет таблицу из набора * @param index Индекс таблицы */ removeTableByIndex(index) { const table = this._tables.get(index); if (typeof table === 'undefined') { return 0; } return this.removeTable(table); } /** * Удаляет таблицу из набора * @param tableName Имя удаляемой таблицы */ removeTableByName(tableName) { const e = (0, Linq_1.default)(this._tables); const table = e.where(t => t.uniqueName === tableName).firstOrDefault(); if (typeof table === 'undefined') { return 0; } return this.removeTable(table); } /** * Получить новое имя для таблицы * @param indexTable Index таблицы * @returns Имя таблицы */ generationTableName(indexTable) { const tableName = `Новая таблица ${++indexTable}`; if (this.getTableByName(tableName)) { return this.generationTableName(indexTable); } return tableName; } // ==== PRIVATE ==== raiseValuesUpdated(values) { this._onValuesUpdated.dispatch(new ValueUpdatedArg(values)); } addSheet(table) { this._hf.addSheet(table.uniqueName); table.sheetId = this._hf.getSheetId(table.uniqueName); } removeSheet(table) { const sheetName = this._hf.getSheetName(table.sheetId); if (this._hf.isItPossibleToClearSheet(sheetName)) { this._hf.batch(() => { this._hf.clearSheet(sheetName); this._hf.removeSheet(sheetName); }); } } } exports.DataSet = DataSet; DataSet._counter = 0; class ValueUpdatedArg { constructor(values) { this.values = values; } } exports.ValueUpdatedArg = ValueUpdatedArg; //# sourceMappingURL=DataSet.js.map