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

124 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-02-05 22:46:20 +01:00
export default class LoadResults {
2020-01-29 21:38:58 +01:00
constructor(treeCache) {
this.treeCache = treeCache;
2020-01-29 20:14:02 +01:00
this.noteIdToSourceId = {};
2020-01-27 22:58:03 +01:00
this.sourceIdToNoteIds = {};
2020-01-30 22:38:31 +01:00
this.branches = [];
this.attributes = [];
this.noteReorderings = [];
this.noteRevisions = [];
2020-01-31 22:32:24 +01:00
this.contentNoteIdToSourceId = [];
this.options = [];
2020-01-27 22:58:03 +01:00
}
2020-03-17 21:39:26 +01:00
addNote(noteId, sourceId) {
2020-01-29 20:14:02 +01:00
this.noteIdToSourceId[noteId] = this.noteIdToSourceId[noteId] || [];
2020-01-27 22:58:03 +01:00
2020-01-29 20:14:02 +01:00
if (!this.noteIdToSourceId[noteId].includes(sourceId)) {
this.noteIdToSourceId[noteId].push(sourceId);
2020-01-27 22:58:03 +01:00
}
this.sourceIdToNoteIds[sourceId] = this.sourceIdToNoteIds[sourceId] || [];
if (!this.sourceIdToNoteIds[sourceId]) {
this.sourceIdToNoteIds[sourceId].push(noteId);
}
}
2020-01-29 20:14:02 +01:00
addBranch(branchId, sourceId) {
2020-01-30 22:38:31 +01:00
this.branches.push({branchId, sourceId});
2020-01-29 20:14:02 +01:00
}
2020-01-29 21:38:58 +01:00
getBranches() {
2020-01-30 22:38:31 +01:00
return this.branches
.map(row => this.treeCache.branches[row.branchId])
.filter(branch => !!branch);
2020-01-29 21:38:58 +01:00
}
2020-01-29 20:14:02 +01:00
addNoteReordering(parentNoteId, sourceId) {
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
}
2020-01-29 20:14:02 +01:00
addAttribute(attributeId, sourceId) {
2020-01-30 22:38:31 +01:00
this.attributes.push({attributeId, sourceId});
2020-01-29 20:14:02 +01:00
}
getAttributes(sourceId = 'none') {
2020-01-30 22:38:31 +01:00
return this.attributes
.filter(row => row.sourceId !== sourceId)
2020-01-30 22:38:31 +01:00
.map(row => this.treeCache.attributes[row.attributeId])
.filter(attr => !!attr);
2020-01-29 21:38:58 +01:00
}
addNoteRevision(noteRevisionId, noteId, sourceId) {
2020-01-30 22:38:31 +01:00
this.noteRevisions.push({noteRevisionId, noteId, sourceId});
2020-01-29 21:38:58 +01:00
}
hasNoteRevisionForNote(noteId) {
2020-01-30 22:38:31 +01:00
return !!this.noteRevisions.find(nr => nr.noteId === noteId);
2020-01-29 21:38:58 +01:00
}
2020-01-27 22:58:03 +01:00
getNoteIds() {
2020-01-29 20:14:02 +01:00
return Object.keys(this.noteIdToSourceId);
2020-01-27 22:58:03 +01:00
}
2020-02-01 19:25:37 +01:00
isNoteReloaded(noteId, sourceId = null) {
2020-01-28 21:54:28 +01:00
if (!noteId) {
return false;
}
2020-01-29 20:14:02 +01:00
const sourceIds = this.noteIdToSourceId[noteId];
2020-01-27 22:58:03 +01:00
return sourceIds && !!sourceIds.find(sId => sId !== sourceId);
}
2020-01-31 22:32:24 +01:00
addNoteContent(noteId, sourceId) {
this.contentNoteIdToSourceId.push({noteId, sourceId});
}
isNoteContentReloaded(noteId, sourceId) {
if (!noteId) {
return false;
}
return this.contentNoteIdToSourceId.find(l => l.noteId === noteId && l.sourceId !== sourceId);
}
addOption(name) {
this.options.push(name);
}
isOptionReloaded(name) {
this.options.includes(name);
}
/**
* @return {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() {
2020-08-12 23:58:32 +02:00
return this.branches.length > 0
|| this.attributes.length > 0;
}
isEmpty() {
2020-03-15 17:18:50 +01:00
return Object.keys(this.noteIdToSourceId).length === 0
&& this.branches.length === 0
&& this.attributes.length === 0
&& this.noteReorderings.length === 0
&& this.noteRevisions.length === 0
&& this.contentNoteIdToSourceId.length === 0
&& this.options.length === 0;
}
}