Notes/src/public/app/entities/fattribute.js

80 lines
2.1 KiB
JavaScript
Raw Normal View History

import promotedAttributeDefinitionParser from '../services/promoted_attribute_definition_parser.js';
/**
* Attribute is an abstract concept which has two real uses - label (key - value pair)
* and relation (representing named relationship between source and target note)
*/
class FAttribute {
2021-04-16 22:57:37 +02:00
constructor(froca, row) {
this.froca = froca;
2020-01-29 22:32:22 +01:00
this.update(row);
}
update(row) {
/** @type {string} */
this.attributeId = row.attributeId;
/** @type {string} */
this.noteId = row.noteId;
/** @type {string} */
this.type = row.type;
/** @type {string} */
this.name = row.name;
/** @type {string} */
this.value = row.value;
2023-06-29 23:32:19 +02:00
/** @type {int} */
this.position = row.position;
/** @type {boolean} */
2020-07-13 23:27:23 +02:00
this.isInheritable = !!row.isInheritable;
}
/** @returns {FNote} */
getNote() {
2021-04-16 22:57:37 +02:00
return this.froca.notes[this.noteId];
}
/** @returns {Promise<FNote>} */
async getTargetNote() {
const targetNoteId = this.targetNoteId;
return await this.froca.getNote(targetNoteId, true);
}
2020-06-14 14:30:57 +02:00
get targetNoteId() { // alias
if (this.type !== 'relation') {
2023-02-17 16:24:47 +01:00
throw new Error(`Attribute ${this.attributeId} is not a relation`);
}
return this.value;
}
get isAutoLink() {
return this.type === 'relation' && ['internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink'].includes(this.name);
}
get toString() {
return `FAttribute(attributeId=${this.attributeId}, type=${this.type}, name=${this.name}, value=${this.value})`;
}
2020-07-01 00:02:13 +02:00
isDefinition() {
return this.type === 'label' && (this.name.startsWith('label:') || this.name.startsWith('relation:'));
}
getDefinition() {
return promotedAttributeDefinitionParser.parse(this.value);
2020-07-01 00:02:13 +02:00
}
2021-01-19 22:10:24 +01:00
isDefinitionFor(attr) {
return this.type === 'label' && this.name === `${attr.type}:${attr.name}`;
}
2021-01-19 22:10:24 +01:00
get dto() {
const dto = Object.assign({}, this);
2021-04-16 22:57:37 +02:00
delete dto.froca;
2021-01-19 22:10:24 +01:00
return dto;
}
2018-12-28 22:05:04 +01:00
}
export default FAttribute;