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

155 lines
4.3 KiB
JavaScript
Raw Normal View History

2020-02-05 22:46:20 +01:00
export default class LoadResults {
2021-08-20 21:42:06 +02:00
constructor(entityChanges) {
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
}
getEntityRow(entityName, entityId) {
2021-08-20 21:42:06 +02:00
return this.entities[entityName]?.[entityId];
}
addNote(noteId, componentId) {
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
}
}
addBranch(branchId, componentId) {
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
}
addNoteReordering(parentNoteId, componentId) {
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
}
addAttribute(attributeId, componentId) {
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
}
addRevision(revisionId, noteId, componentId) {
this.revisionRows.push({revisionId, noteId, componentId});
2020-01-29 21:38:58 +01:00
}
hasRevisionForNote(noteId) {
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
}
isNoteReloaded(noteId, 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
2023-03-15 22:44:08 +01:00
addNoteContent(noteIds, componentId) {
for (const noteId of noteIds || []) {
2023-03-15 22:44:08 +01:00
this.contentNoteIdToComponentId.push({noteId, componentId});
}
2020-01-31 22:32:24 +01:00
}
isNoteContentReloaded(noteId, componentId) {
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
}
addOption(name) {
this.optionNames.push(name);
}
isOptionReloaded(name) {
return this.optionNames.includes(name);
}
addAttachmentRow(attachment) {
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;
}
}