core-cde
Version:
286 lines • 10.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Document = void 0;
const tslib_1 = require("tslib");
const Document_actions_1 = require("./actions/Document.actions");
const Image_actions_1 = require("./actions/Image.actions");
const Link_actions_1 = require("./actions/Link.actions");
const List_actions_1 = require("./actions/List.actions");
const Page_actions_1 = require("./actions/Page.actions");
const Table_actions_1 = require("./actions/Table.actions");
const Text_actions_1 = require("./actions/Text.actions");
const Video_actions_1 = require("./actions/Video.actions");
const Widget_actions_1 = require("./actions/Widget.actions");
const Errors_1 = require("./Errors");
const LinqList_1 = require("./LinqList");
const Page_1 = require("./Page");
const Uidv4_1 = require("./Uidv4");
const Widget_1 = require("./descriptiveClasses/Widget");
const List_1 = require("./descriptiveClasses/List");
const Data_module_1 = require("../../data/Data.module");
const events_1 = (0, tslib_1.__importDefault)(require("events"));
/**
* Класс для документа
*/
class Document {
constructor(id = Uidv4_1.UUID.get(), name) {
this.id = id;
this.name = name;
/**
* Data set
*/
this._ds = new Data_module_1.DataSet();
/**
* Список пользователей которые редактируют документ
*/
this.usersView = [];
/**
* Событие изменения документа
*/
this.$eventChangeState = new events_1.default();
/**
* Список страниц
*/
this.pages = new LinqList_1.LinkedListModified();
}
/**s
* Получаем ссылку на DataSet
*/
get ds() {
return this._ds;
}
/**
* Добавить страницу на документ
* @param pageResponse Страница
*/
addPage(pageResponse) {
if (!this.pages || !pageResponse) {
throw new Errors_1.EmptyParametersError('addPage');
}
// TODO переписать
try {
if (Array.isArray(pageResponse)) {
pageResponse.forEach(page => {
const pageRes = Page_1.Page.fromJSON(page.json);
pageRes.id = page.guid;
if (Array.isArray(page.widgets)) {
page.widgets.forEach((widgetResp, index) => {
const widget = Widget_1.Widget.createWidgetByJSON(widgetResp.json, this.ds);
widget.id = widgetResp.guid;
pageRes.widgets.add(widget);
// TODO переделать, получается слишком много пересчета
if (widget instanceof List_1.List) {
List_1.List.down(pageRes.widgets.getNodeByIndex(index));
}
});
}
this.pages.add(pageRes);
});
}
}
catch (error) {
throw error;
}
}
/**
* Добавление таблиц в датасет
* @param resp Таблицы
*/
addTables(resp) {
if (!resp) {
throw new Errors_1.EmptyParametersError('addTables in DataSet');
}
try {
resp.forEach(tableResp => {
const tableFromJson = JSON.parse(tableResp.json);
const dataTableJSON = {
columns: {
colItems: JSON.stringify(tableResp.columns.map(r => JSON.parse(r.json)))
},
rows: {
rowItems: JSON.stringify(tableResp.rows.map(r => JSON.parse(r.json)))
},
id: tableFromJson.id,
uniqueName: tableFromJson.uniqueName
};
const dt = Data_module_1.DataTable.fromJSON(JSON.stringify(dataTableJSON));
dt.columnCollection.updateIndex(0);
dt.rowCollection.updateIndex(0);
this.ds.addTable(dt);
});
}
catch (error) {
throw error;
}
}
/**
* Получить ID активной станицы
*/
get pageSelectId() {
return this.pageSelect.id;
}
/**
* Активная страница
*/
get pageSelect() {
return this.pages.toArray().find(val => val.active === true);
}
/**
* Сделать активную первую страницу
*/
firstPageActive() {
if (this.pages.count > 0) {
this.makePageActive(this.pages.get(0).id);
}
}
/**
* Сделать страницу активной по id
* @param idPage ID страницы
*/
makePageActive(id) {
this.makeAllPagesInactive();
const page = this.getPageById(id);
if (page) {
page.active = true;
}
}
/**
* Получить страницу по ID
* @param id ID Страницы
* @returns Страница
*/
getPageById(id) {
return this.pages.toArray().find(page => page.id === id);
}
/**
* Получить страницу по названию
* @param name Название страницы
* @returns Страница
*/
getPageByName(name) {
return this.pages.toArray().find(page => page.name === name);
}
/**
* Получить новое имя для страницы
* @returns Имя страницы
*/
generatePageName(name) {
const countPage = this.pages.count;
return this._generatePageName(name, countPage);
}
_generatePageName(name, index) {
const pageName = `${name} ${++index}`;
if (this.getPageByName(pageName)) {
return this._generatePageName(name, index);
}
return pageName;
}
/**
* Сделать все страницы неактивными
*/
makeAllPagesInactive() {
return this.pages.toArray().forEach(page => page.active = false);
}
/**
* Предыдущая страница
* @param idCurPage id страницы
* @returns страница
*/
prevPage(idCurPage) {
const indexPage = this.pages.toArray().findIndex(page => page.id === idCurPage);
const page = this.pages.toArray()[indexPage - 1];
return page || null;
}
/**
* Следующая страница
* @param idCurPage id страницы
* @returns страница
*/
nextPage(idCurPage) {
const indexPage = this.pages.toArray().findIndex(page => page.id === idCurPage);
const page = this.pages.toArray()[indexPage + 1];
return page || null;
}
/**
* Получить состояния документа/страницы/виджета от типа действия
* @param action Тип действия
* @returns Состояние документа/страницы/виджета
*/
stateSelection(action) {
if (!action) {
throw new Errors_1.EmptyParametersError('stateSelection');
}
if (this._isDocument(action.type)) {
return this;
}
if (this._isPage(action.type)) {
return this._findStatePageById(action.pageId);
}
if (this._isWidget(action.type)) {
const page = this._findStatePageById(action.pageId);
return this._findStateWidgetById(page, action.widgetId);
}
throw new Errors_1.UnknownActionTypeError();
}
static fromJSON(json) {
try {
const pageJson = JSON.parse(json);
return new Document(pageJson.id, pageJson.name);
}
catch (error) {
throw new Errors_1.JsonParseError('Document');
}
}
toJSON() {
return Object.assign({}, null, {
id: this.id,
name: this.name,
});
}
;
// ==== PRIVATE ====
_isWidget(type) {
return (Object.values(Text_actions_1.ETextActions).includes(type) ||
Object.values(Link_actions_1.ELinkActions).includes(type) ||
Object.values(Table_actions_1.ETableActions).includes(type) ||
Object.values(Video_actions_1.EVideoActions).includes(type) ||
Object.values(Image_actions_1.EImageActions).includes(type));
}
_isPage(type) {
return (Object.values(Widget_actions_1.EWidgetActions).includes(type) ||
Object.values(List_actions_1.EListActions).includes(type) ||
[
Page_actions_1.EPageActions.createCover,
Page_actions_1.EPageActions.removeCover,
Text_actions_1.ETextActions.textWrapping,
Text_actions_1.ETextActions.textUnWrapping,
Table_actions_1.ETableActions.createWidgetTable,
Table_actions_1.ETableActions.removeWidgetTable,
].includes(type));
}
_isDocument(type) {
return [
Page_actions_1.EPageActions.createPage,
Page_actions_1.EPageActions.removePage,
Page_actions_1.EPageActions.renamePage,
Page_actions_1.EPageActions.selectPage
].includes(type) ||
Object.values(Document_actions_1.EDocActions).includes(type);
}
_findStatePageById(pageId) {
const page = this.pages.toArray().find(val => val.id === pageId);
if (!page) {
throw new Errors_1.ElementNotFoundError('findStateWidgetById');
}
return page;
}
_findStateWidgetById(page, widgetId) {
const widget = page.widgets.toArray().find(val => val.id === widgetId);
if (!widget) {
throw new Errors_1.ElementNotFoundError('findStateWidgetById');
}
return widget;
}
}
exports.Document = Document;
//# sourceMappingURL=Document.js.map