74 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-05-17 09:48:24 +02:00
"use strict";
2020-08-27 23:54:02 +02:00
const Note = require('./note.js');
2020-05-16 23:12:29 +02:00
class Attribute {
2021-04-16 23:00:08 +02:00
constructor(becca, row) {
/** @param {Becca} */
this.becca = becca;
2020-05-16 23:12:29 +02:00
/** @param {string} */
this.attributeId = row.attributeId;
/** @param {string} */
this.noteId = row.noteId;
/** @param {string} */
this.type = row.type;
/** @param {string} */
this.name = row.name;
2020-12-11 22:06:12 +01:00
/** @param {int} */
this.position = row.position;
2020-05-16 23:12:29 +02:00
/** @param {string} */
2020-12-13 23:27:42 +01:00
this.value = row.value;
2020-05-16 23:12:29 +02:00
/** @param {boolean} */
this.isInheritable = !!row.isInheritable;
2021-04-16 23:00:08 +02:00
this.becca.attributes[this.attributeId] = this;
2021-04-16 23:00:08 +02:00
if (!(this.noteId in this.becca.notes)) {
// entities can come out of order in sync, create skeleton which will be filled later
2021-04-16 23:00:08 +02:00
this.becca.notes[this.noteId] = new Note(this.becca, {noteId: this.noteId});
}
2021-04-16 23:00:08 +02:00
this.becca.notes[this.noteId].ownedAttributes.push(this);
2020-05-16 23:12:29 +02:00
2020-12-14 22:12:26 +01:00
const key = `${this.type}-${this.name.toLowerCase()}`;
2021-04-16 23:00:08 +02:00
this.becca.attributeIndex[key] = this.becca.attributeIndex[key] || [];
this.becca.attributeIndex[key].push(this);
2020-05-16 23:12:29 +02:00
const targetNote = this.targetNote;
if (targetNote) {
targetNote.targetRelations.push(this);
}
}
get isAffectingSubtree() {
return this.isInheritable
|| (this.type === 'relation' && this.name === 'template');
}
isAutoLink() {
return this.type === 'relation' && ['internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink'].includes(this.name);
}
2020-05-16 23:12:29 +02:00
get note() {
2021-04-16 23:00:08 +02:00
return this.becca.notes[this.noteId];
2020-05-16 23:12:29 +02:00
}
get targetNote() {
if (this.type === 'relation') {
2021-04-16 23:00:08 +02:00
return this.becca.notes[this.value];
2020-05-16 23:12:29 +02:00
}
}
2021-02-10 22:56:23 +01:00
// for logging etc
get pojo() {
const pojo = {...this};
2021-04-16 23:00:08 +02:00
delete pojo.becca;
2021-02-10 22:56:23 +01:00
return pojo;
}
2020-05-16 23:12:29 +02:00
}
2020-05-17 09:48:24 +02:00
module.exports = Attribute;