Notes/src/public/app/services/load_results.ts

198 lines
5.5 KiB
TypeScript
Raw Normal View History

2024-07-25 00:09:34 +03:00
import { EntityChange } from "../../../services/entity_changes_interface.js";
interface BranchRow {
branchId: string;
componentId: string;
}
interface AttributeRow {
attributeId: string;
componentId: string;
}
interface RevisionRow {
revisionId: string;
noteId: string;
componentId: string;
}
interface ContentNoteIdToComponentIdRow {
noteId: string;
componentId: string;
}
interface AttachmentRow {}
interface ContentNoteIdToComponentIdRow {
noteId: string;
componentId: string;
}
2020-02-05 22:46:20 +01:00
export default class LoadResults {
2024-07-25 00:09:34 +03:00
private entities: Record<string, Record<string, unknown>>;
private noteIdToComponentId: Record<string, string[]>;
private componentIdToNoteIds: Record<string, string[]>;
private branchRows: BranchRow[];
private attributeRows: AttributeRow[];
private revisionRows: RevisionRow[];
private noteReorderings: string[];
private contentNoteIdToComponentId: ContentNoteIdToComponentIdRow[];
private optionNames: string[];
private attachmentRows: AttachmentRow[];
constructor(entityChanges: EntityChange[]) {
2021-08-20 21:42:06 +02:00
this.entities = {};
for (const {entityId, entityName, entity} of entityChanges) {
if (entity) {
this.entities[entityName] = this.entities[entityName] || [];
this.entities[entityName][entityId] = entity;
}
}
2020-01-29 21:38:58 +01:00
this.noteIdToComponentId = {};
this.componentIdToNoteIds = {};
this.branchRows = [];
2020-01-30 22:38:31 +01:00
this.attributeRows = [];
2020-01-30 22:38:31 +01:00
this.noteReorderings = [];
this.revisionRows = [];
2020-01-31 22:32:24 +01:00
this.contentNoteIdToComponentId = [];
this.optionNames = [];
2023-04-01 23:55:04 +02:00
this.attachmentRows = [];
2020-01-27 22:58:03 +01:00
}
2024-07-25 00:09:34 +03:00
getEntityRow(entityName: string, entityId: string) {
2021-08-20 21:42:06 +02:00
return this.entities[entityName]?.[entityId];
}
2024-07-25 00:09:34 +03:00
addNote(noteId: string, componentId: string) {
this.noteIdToComponentId[noteId] = this.noteIdToComponentId[noteId] || [];
2020-01-27 22:58:03 +01:00
if (!this.noteIdToComponentId[noteId].includes(componentId)) {
this.noteIdToComponentId[noteId].push(componentId);
2020-01-27 22:58:03 +01:00
}
this.componentIdToNoteIds[componentId] = this.componentIdToNoteIds[componentId] || [];
2020-01-27 22:58:03 +01:00
if (!this.componentIdToNoteIds[componentId]) {
this.componentIdToNoteIds[componentId].push(noteId);
2020-01-27 22:58:03 +01:00
}
}
2024-07-25 00:09:34 +03:00
addBranch(branchId: string, componentId: string) {
this.branchRows.push({branchId, componentId});
2020-01-29 20:14:02 +01:00
}
getBranchRows() {
return this.branchRows
.map(row => this.getEntityRow("branches", row.branchId))
2020-01-30 22:38:31 +01:00
.filter(branch => !!branch);
2020-01-29 21:38:58 +01:00
}
2024-07-25 00:09:34 +03:00
addNoteReordering(parentNoteId: string, componentId: string) {
2020-01-30 22:38:31 +01:00
this.noteReorderings.push(parentNoteId);
2020-01-29 20:14:02 +01:00
}
2020-01-29 21:38:58 +01:00
getNoteReorderings() {
2020-01-30 22:38:31 +01:00
return this.noteReorderings;
2020-01-29 21:38:58 +01:00
}
2024-07-25 00:09:34 +03:00
addAttribute(attributeId: string, componentId: string) {
this.attributeRows.push({attributeId, componentId});
2020-01-29 20:14:02 +01:00
}
getAttributeRows(componentId = 'none') {
return this.attributeRows
.filter(row => row.componentId !== componentId)
.map(row => this.getEntityRow("attributes", row.attributeId))
2020-01-30 22:38:31 +01:00
.filter(attr => !!attr);
2020-01-29 21:38:58 +01:00
}
2024-07-25 00:09:34 +03:00
addRevision(revisionId: string, noteId: string, componentId: string) {
this.revisionRows.push({revisionId, noteId, componentId});
2020-01-29 21:38:58 +01:00
}
2024-07-25 00:09:34 +03:00
hasRevisionForNote(noteId: string) {
return !!this.revisionRows.find(row => row.noteId === noteId);
2020-01-29 21:38:58 +01:00
}
2020-01-27 22:58:03 +01:00
getNoteIds() {
return Object.keys(this.noteIdToComponentId);
2020-01-27 22:58:03 +01:00
}
2024-07-25 00:09:34 +03:00
isNoteReloaded(noteId: string, componentId = null) {
2020-01-28 21:54:28 +01:00
if (!noteId) {
return false;
}
const componentIds = this.noteIdToComponentId[noteId];
2022-01-12 19:32:23 +01:00
return componentIds && componentIds.find(sId => sId !== componentId) !== undefined;
2020-01-27 22:58:03 +01:00
}
2020-01-31 22:32:24 +01:00
2024-07-25 00:09:34 +03:00
addNoteContent(noteId: string, componentId: string) {
this.contentNoteIdToComponentId.push({noteId, componentId});
2020-01-31 22:32:24 +01:00
}
2024-07-25 00:09:34 +03:00
isNoteContentReloaded(noteId: string, componentId: string) {
2020-01-31 22:32:24 +01:00
if (!noteId) {
return false;
}
return this.contentNoteIdToComponentId.find(l => l.noteId === noteId && l.componentId !== componentId);
2020-01-31 22:32:24 +01:00
}
2024-07-25 00:09:34 +03:00
addOption(name: string) {
this.optionNames.push(name);
}
2024-07-25 00:09:34 +03:00
isOptionReloaded(name: string) {
return this.optionNames.includes(name);
}
2023-06-05 21:14:33 +02:00
getOptionNames() {
return this.optionNames;
}
2024-07-25 00:09:34 +03:00
addAttachmentRow(attachment: AttachmentRow) {
this.attachmentRows.push(attachment);
2023-04-01 23:55:04 +02:00
}
getAttachmentRows() {
return this.attachmentRows;
2023-04-01 23:55:04 +02:00
}
/**
2023-01-05 23:38:41 +01:00
* @returns {boolean} true if there are changes which could affect the attributes (including inherited ones)
* notably changes in note itself should not have any effect on attributes
*/
hasAttributeRelatedChanges() {
return this.branchRows.length > 0
|| this.attributeRows.length > 0;
}
isEmpty() {
return Object.keys(this.noteIdToComponentId).length === 0
&& this.branchRows.length === 0
&& this.attributeRows.length === 0
2020-03-15 17:18:50 +01:00
&& this.noteReorderings.length === 0
&& this.revisionRows.length === 0
&& this.contentNoteIdToComponentId.length === 0
&& this.optionNames.length === 0
&& this.attachmentRows.length === 0;
}
isEmptyForTree() {
return Object.keys(this.noteIdToComponentId).length === 0
&& this.branchRows.length === 0
&& this.attributeRows.length === 0
&& this.noteReorderings.length === 0;
}
}