2020-05-17 09:48:24 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-08-27 23:54:02 +02:00
|
|
|
const Note = require('./note.js');
|
2021-04-25 20:00:42 +02:00
|
|
|
const AbstractEntity = require("./abstract_entity.js");
|
|
|
|
const sql = require("../../sql.js");
|
|
|
|
const dateUtils = require("../../date_utils.js");
|
|
|
|
|
|
|
|
class Branch extends AbstractEntity {
|
|
|
|
static get entityName() { return "branches"; }
|
|
|
|
static get primaryKeyName() { return "branchId"; }
|
|
|
|
// notePosition is not part of hash because it would produce a lot of updates in case of reordering
|
|
|
|
static get hashedProperties() { return ["branchId", "noteId", "parentNoteId", "prefix"]; }
|
2020-08-27 22:37:57 +02:00
|
|
|
|
2021-04-30 22:13:13 +02:00
|
|
|
constructor(row) {
|
2021-04-25 20:00:42 +02:00
|
|
|
super();
|
|
|
|
|
2020-05-16 23:12:29 +02:00
|
|
|
/** @param {string} */
|
|
|
|
this.branchId = row.branchId;
|
|
|
|
/** @param {string} */
|
|
|
|
this.noteId = row.noteId;
|
|
|
|
/** @param {string} */
|
|
|
|
this.parentNoteId = row.parentNoteId;
|
|
|
|
/** @param {string} */
|
|
|
|
this.prefix = row.prefix;
|
2020-12-11 22:06:12 +01:00
|
|
|
/** @param {int} */
|
|
|
|
this.notePosition = row.notePosition;
|
|
|
|
/** @param {boolean} */
|
2020-12-12 12:07:15 +01:00
|
|
|
this.isExpanded = !!row.isExpanded;
|
2020-05-16 23:12:29 +02:00
|
|
|
|
|
|
|
if (this.branchId === 'root') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:37:57 +02:00
|
|
|
const childNote = this.childNote;
|
2020-05-16 23:12:29 +02:00
|
|
|
const parentNote = this.parentNote;
|
|
|
|
|
|
|
|
childNote.parents.push(parentNote);
|
|
|
|
childNote.parentBranches.push(this);
|
|
|
|
|
|
|
|
parentNote.children.push(childNote);
|
|
|
|
|
2021-04-16 23:00:08 +02:00
|
|
|
this.becca.branches[this.branchId] = this;
|
|
|
|
this.becca.childParentToBranch[`${this.noteId}-${this.parentNoteId}`] = this;
|
2020-05-16 23:12:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @return {Note} */
|
2020-08-27 22:37:57 +02:00
|
|
|
get childNote() {
|
2021-04-16 23:00:08 +02:00
|
|
|
if (!(this.noteId in this.becca.notes)) {
|
2020-08-27 22:37:57 +02:00
|
|
|
// entities can come out of order in sync, create skeleton which will be filled later
|
2021-05-02 21:34:57 +02:00
|
|
|
this.becca.notes[this.noteId] = new Note({noteId: this.noteId});
|
2020-08-27 22:37:57 +02:00
|
|
|
}
|
|
|
|
|
2021-04-16 23:00:08 +02:00
|
|
|
return this.becca.notes[this.noteId];
|
2020-08-27 22:37:57 +02:00
|
|
|
}
|
2020-05-16 23:12:29 +02:00
|
|
|
|
2021-04-25 22:02:32 +02:00
|
|
|
getNote() {
|
|
|
|
return this.childNote;
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:37:57 +02:00
|
|
|
/** @return {Note} */
|
|
|
|
get parentNote() {
|
2021-04-16 23:00:08 +02:00
|
|
|
if (!(this.parentNoteId in this.becca.notes)) {
|
2020-08-27 22:37:57 +02:00
|
|
|
// entities can come out of order in sync, create skeleton which will be filled later
|
2021-05-02 21:34:57 +02:00
|
|
|
this.becca.notes[this.parentNoteId] = new Note({noteId: this.parentNoteId});
|
2020-05-16 23:12:29 +02:00
|
|
|
}
|
|
|
|
|
2021-04-16 23:00:08 +02:00
|
|
|
return this.becca.notes[this.parentNoteId];
|
2020-05-16 23:12:29 +02:00
|
|
|
}
|
2021-02-04 22:05:32 +01:00
|
|
|
|
2021-05-08 21:10:58 +02:00
|
|
|
beforeSaving() {
|
|
|
|
if (this.notePosition === undefined || this.notePosition === null) {
|
|
|
|
// TODO finding new position can be refactored into becca
|
|
|
|
const maxNotePos = sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [this.parentNoteId]);
|
|
|
|
this.notePosition = maxNotePos === null ? 0 : maxNotePos + 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.isExpanded) {
|
|
|
|
this.isExpanded = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.utcDateModified = dateUtils.utcNowDateTime();
|
|
|
|
|
|
|
|
super.beforeSaving();
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:19:18 +02:00
|
|
|
getPojo() {
|
2021-05-08 21:10:58 +02:00
|
|
|
return {
|
2021-04-25 20:00:42 +02:00
|
|
|
branchId: this.branchId,
|
|
|
|
noteId: this.noteId,
|
|
|
|
parentNoteId: this.parentNoteId,
|
|
|
|
prefix: this.prefix,
|
|
|
|
notePosition: this.notePosition,
|
2021-04-25 21:19:18 +02:00
|
|
|
isExpanded: this.isExpanded,
|
2021-05-09 11:12:53 +02:00
|
|
|
isDeleted: false,
|
2021-05-08 21:10:58 +02:00
|
|
|
utcDateModified: this.utcDateModified,
|
|
|
|
// not used for anything, will be later dropped
|
|
|
|
utcDateCreated: dateUtils.utcNowDateTime()
|
2021-04-25 20:00:42 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
createClone(parentNoteId, notePosition) {
|
|
|
|
return new Branch({
|
|
|
|
noteId: this.noteId,
|
|
|
|
parentNoteId: parentNoteId,
|
|
|
|
notePosition: notePosition,
|
|
|
|
prefix: this.prefix,
|
|
|
|
isExpanded: this.isExpanded
|
|
|
|
});
|
|
|
|
}
|
2020-05-16 23:12:29 +02:00
|
|
|
}
|
2020-05-17 09:48:24 +02:00
|
|
|
|
|
|
|
module.exports = Branch;
|