Notes/src/share/shaca/entities/attribute.js

81 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-10-16 22:13:34 +02:00
"use strict";
2021-10-17 14:44:59 +02:00
const AbstractEntity = require('./abstract_entity');
2021-10-16 22:13:34 +02:00
2021-10-17 14:44:59 +02:00
class Attribute extends AbstractEntity {
constructor([attributeId, noteId, type, name, value, isInheritable, position]) {
super();
2021-10-16 22:13:34 +02:00
/** @param {string} */
this.attributeId = attributeId;
/** @param {string} */
this.noteId = noteId;
/** @param {string} */
this.type = type;
/** @param {string} */
this.name = name;
/** @param {int} */
this.position = position;
/** @param {string} */
this.value = value;
/** @param {boolean} */
this.isInheritable = !!isInheritable;
2021-10-17 14:44:59 +02:00
this.shaca.attributes[this.attributeId] = this;
this.shaca.notes[this.noteId].ownedAttributes.push(this);
2021-10-16 22:13:34 +02:00
const targetNote = this.targetNote;
if (targetNote) {
targetNote.targetRelations.push(this);
}
}
get isAffectingSubtree() {
return this.isInheritable
|| (this.type === 'relation' && this.name === 'template');
}
get targetNoteId() { // alias
return this.type === 'relation' ? this.value : undefined;
}
isAutoLink() {
return this.type === 'relation' && ['internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink'].includes(this.name);
}
get note() {
2021-10-17 14:44:59 +02:00
return this.shaca.notes[this.noteId];
2021-10-16 22:13:34 +02:00
}
get targetNote() {
if (this.type === 'relation') {
2021-10-17 14:44:59 +02:00
return this.shaca.notes[this.value];
2021-10-16 22:13:34 +02:00
}
}
/**
* @returns {Note|null}
*/
getNote() {
2021-10-17 14:44:59 +02:00
return this.shaca.getNote(this.noteId);
2021-10-16 22:13:34 +02:00
}
/**
* @returns {Note|null}
*/
getTargetNote() {
if (this.type !== 'relation') {
throw new Error(`Attribute ${this.attributeId} is not relation`);
}
if (!this.value) {
return null;
}
2021-10-17 14:44:59 +02:00
return this.shaca.getNote(this.value);
2021-10-16 22:13:34 +02:00
}
}
module.exports = Attribute;