2021-09-30 18:26:13 +08:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>JSDoc: Source: becca/entities/attribute.js</title>
|
|
|
|
|
|
|
|
<script src="scripts/prettify/prettify.js"> </script>
|
|
|
|
<script src="scripts/prettify/lang-css.js"> </script>
|
|
|
|
<!--[if lt IE 9]>
|
|
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
|
|
<![endif]-->
|
|
|
|
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
|
|
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<div id="main">
|
|
|
|
|
|
|
|
<h1 class="page-title">Source: becca/entities/attribute.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<article>
|
|
|
|
<pre class="prettyprint source linenums"><code>"use strict";
|
|
|
|
|
2022-01-11 02:54:38 +08:00
|
|
|
const Note = require('./note');
|
|
|
|
const AbstractEntity = require("./abstract_entity");
|
|
|
|
const sql = require("../../services/sql");
|
|
|
|
const dateUtils = require("../../services/date_utils");
|
2021-09-30 18:26:13 +08:00
|
|
|
const promotedAttributeDefinitionParser = require("../../services/promoted_attribute_definition_parser");
|
|
|
|
|
2021-11-11 04:30:54 +08: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)
|
2022-04-16 06:17:32 +08:00
|
|
|
*
|
|
|
|
* @extends AbstractEntity
|
2021-11-11 04:30:54 +08:00
|
|
|
*/
|
2021-09-30 18:26:13 +08:00
|
|
|
class Attribute extends AbstractEntity {
|
|
|
|
static get entityName() { return "attributes"; }
|
|
|
|
static get primaryKeyName() { return "attributeId"; }
|
|
|
|
static get hashedProperties() { return ["attributeId", "noteId", "type", "name", "value", "isInheritable"]; }
|
|
|
|
|
|
|
|
constructor(row) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
if (!row) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateFromRow(row);
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFromRow(row) {
|
|
|
|
this.update([
|
|
|
|
row.attributeId,
|
|
|
|
row.noteId,
|
|
|
|
row.type,
|
|
|
|
row.name,
|
|
|
|
row.value,
|
|
|
|
row.isInheritable,
|
|
|
|
row.position,
|
|
|
|
row.utcDateModified
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
update([attributeId, noteId, type, name, value, isInheritable, position, utcDateModified]) {
|
2021-11-11 04:30:54 +08:00
|
|
|
/** @type {string} */
|
2021-09-30 18:26:13 +08:00
|
|
|
this.attributeId = attributeId;
|
2021-11-11 04:30:54 +08:00
|
|
|
/** @type {string} */
|
2021-09-30 18:26:13 +08:00
|
|
|
this.noteId = noteId;
|
2021-11-11 04:30:54 +08:00
|
|
|
/** @type {string} */
|
2021-09-30 18:26:13 +08:00
|
|
|
this.type = type;
|
2021-11-11 04:30:54 +08:00
|
|
|
/** @type {string} */
|
2021-09-30 18:26:13 +08:00
|
|
|
this.name = name;
|
2021-11-11 04:30:54 +08:00
|
|
|
/** @type {int} */
|
2021-09-30 18:26:13 +08:00
|
|
|
this.position = position;
|
2021-11-11 04:30:54 +08:00
|
|
|
/** @type {string} */
|
2022-01-15 02:53:59 +08:00
|
|
|
this.value = value || "";
|
2021-11-11 04:30:54 +08:00
|
|
|
/** @type {boolean} */
|
2021-09-30 18:26:13 +08:00
|
|
|
this.isInheritable = !!isInheritable;
|
2021-11-11 04:30:54 +08:00
|
|
|
/** @type {string} */
|
2021-09-30 18:26:13 +08:00
|
|
|
this.utcDateModified = utcDateModified;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2022-12-15 06:51:56 +08:00
|
|
|
this.validate();
|
|
|
|
|
2021-09-30 18:26:13 +08:00
|
|
|
if (this.attributeId) {
|
|
|
|
this.becca.attributes[this.attributeId] = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(this.noteId in this.becca.notes)) {
|
|
|
|
// entities can come out of order in sync, create skeleton which will be filled later
|
2021-11-11 04:30:54 +08:00
|
|
|
this.becca.addNote(this.noteId, new Note({noteId: this.noteId}));
|
2021-09-30 18:26:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.becca.notes[this.noteId].ownedAttributes.push(this);
|
|
|
|
|
|
|
|
const key = `${this.type}-${this.name.toLowerCase()}`;
|
|
|
|
this.becca.attributeIndex[key] = this.becca.attributeIndex[key] || [];
|
|
|
|
this.becca.attributeIndex[key].push(this);
|
|
|
|
|
|
|
|
const targetNote = this.targetNote;
|
|
|
|
|
|
|
|
if (targetNote) {
|
|
|
|
targetNote.targetRelations.push(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 06:51:56 +08:00
|
|
|
validate() {
|
|
|
|
if (!["label", "relation"].includes(this.type)) {
|
2022-12-22 21:57:00 +08:00
|
|
|
throw new Error(`Invalid attribute type '${this.type}' in attribute '${this.attributeId}' of note '${this.noteId}'`);
|
2022-12-15 06:51:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.name?.trim()) {
|
2022-12-22 21:57:00 +08:00
|
|
|
throw new Error(`Invalid empty name in attribute '${this.attributeId}' of note '${this.noteId}'`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.type === 'relation' && !(this.value in this.becca.notes)) {
|
|
|
|
throw new Error(`Cannot save relation '${this.name}' of note '${this.noteId}' since it target not existing note '${this.value}'.`);
|
2022-12-15 06:51:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 18:26:13 +08:00
|
|
|
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() {
|
|
|
|
return this.becca.notes[this.noteId];
|
|
|
|
}
|
|
|
|
|
|
|
|
get targetNote() {
|
|
|
|
if (this.type === 'relation') {
|
|
|
|
return this.becca.notes[this.value];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Note|null}
|
|
|
|
*/
|
|
|
|
getNote() {
|
2022-12-15 06:51:56 +08:00
|
|
|
const note = this.becca.getNote(this.noteId);
|
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
throw new Error(`Note '${this.noteId}' of attribute '${this.attributeId}', type '${this.type}', name '${this.name}' does not exist.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return note;
|
2021-09-30 18:26:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Note|null}
|
|
|
|
*/
|
|
|
|
getTargetNote() {
|
|
|
|
if (this.type !== 'relation') {
|
|
|
|
throw new Error(`Attribute ${this.attributeId} is not relation`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.value) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.becca.getNote(this.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
isDefinition() {
|
|
|
|
return this.type === 'label' && (this.name.startsWith('label:') || this.name.startsWith('relation:'));
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefinition() {
|
|
|
|
return promotedAttributeDefinitionParser.parse(this.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefinedName() {
|
|
|
|
if (this.type === 'label' && this.name.startsWith('label:')) {
|
|
|
|
return this.name.substr(6);
|
|
|
|
} else if (this.type === 'label' && this.name.startsWith('relation:')) {
|
|
|
|
return this.name.substr(9);
|
|
|
|
} else {
|
|
|
|
return this.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-11 04:30:54 +08:00
|
|
|
get isDeleted() {
|
|
|
|
return !(this.attributeId in this.becca.attributes);
|
|
|
|
}
|
|
|
|
|
2021-09-30 18:26:13 +08:00
|
|
|
beforeSaving() {
|
2022-12-15 06:51:56 +08:00
|
|
|
this.validate();
|
2021-09-30 18:26:13 +08:00
|
|
|
|
2022-12-22 21:57:00 +08:00
|
|
|
if (!this.value) {
|
2021-09-30 18:26:13 +08:00
|
|
|
// null value isn't allowed
|
|
|
|
this.value = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.position === undefined) {
|
|
|
|
// TODO: can be calculated from becca
|
|
|
|
this.position = 1 + sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM attributes WHERE noteId = ?`, [this.noteId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.isInheritable) {
|
|
|
|
this.isInheritable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.utcDateModified = dateUtils.utcNowDateTime();
|
|
|
|
|
|
|
|
super.beforeSaving();
|
|
|
|
|
|
|
|
this.becca.attributes[this.attributeId] = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPojo() {
|
|
|
|
return {
|
|
|
|
attributeId: this.attributeId,
|
|
|
|
noteId: this.noteId,
|
|
|
|
type: this.type,
|
|
|
|
name: this.name,
|
|
|
|
position: this.position,
|
|
|
|
value: this.value,
|
|
|
|
isInheritable: this.isInheritable,
|
|
|
|
utcDateModified: this.utcDateModified,
|
|
|
|
isDeleted: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
createClone(type, name, value, isInheritable) {
|
|
|
|
return new Attribute({
|
|
|
|
noteId: this.noteId,
|
|
|
|
type: type,
|
|
|
|
name: name,
|
|
|
|
value: value,
|
|
|
|
position: this.position,
|
|
|
|
isInheritable: isInheritable,
|
|
|
|
utcDateModified: this.utcDateModified
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Attribute;
|
|
|
|
</code></pre>
|
|
|
|
</article>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<nav>
|
2022-04-16 06:17:32 +08:00
|
|
|
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-sql.html">sql</a></li></ul><h3>Classes</h3><ul><li><a href="AbstractEntity.html">AbstractEntity</a></li><li><a href="Attribute.html">Attribute</a></li><li><a href="BackendScriptApi.html">BackendScriptApi</a></li><li><a href="Branch.html">Branch</a></li><li><a href="EtapiToken.html">EtapiToken</a></li><li><a href="Note.html">Note</a></li><li><a href="NoteRevision.html">NoteRevision</a></li><li><a href="Option.html">Option</a></li><li><a href="RecentNote.html">RecentNote</a></li></ul><h3><a href="global.html">Global</a></h3>
|
2021-09-30 18:26:13 +08:00
|
|
|
</nav>
|
|
|
|
|
|
|
|
<br class="clear">
|
|
|
|
|
|
|
|
<footer>
|
2022-11-08 04:26:13 +08:00
|
|
|
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.0</a>
|
2021-09-30 18:26:13 +08:00
|
|
|
</footer>
|
|
|
|
|
|
|
|
<script> prettyPrint(); </script>
|
|
|
|
<script src="scripts/linenumber.js"> </script>
|
|
|
|
</body>
|
|
|
|
</html>
|