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