trilium/docs/frontend_api/entities_note_short.js.html

568 lines
17 KiB
HTML
Raw Normal View History

2018-08-30 02:44:35 +08:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: entities/note_short.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: entities/note_short.js</h1>
<section>
<article>
2018-12-23 05:28:49 +08:00
<pre class="prettyprint source linenums"><code>import server from '../services/server.js';
import Attribute from './attribute.js';
2020-10-01 04:48:30 +08:00
import noteAttributeCache from "../services/note_attribute_cache.js";
2018-12-23 05:28:49 +08:00
const LABEL = 'label';
const RELATION = 'relation';
/**
2020-10-01 04:48:30 +08:00
* FIXME: since there's no "full note" anymore we can rename this to Note
*
2018-08-30 02:44:35 +08:00
* This note's representation is used in note tree and is kept in TreeCache.
*/
class NoteShort {
2019-10-26 16:00:26 +08:00
/**
* @param {TreeCache} treeCache
* @param {Object.&lt;string, Object>} row
*/
2020-02-02 18:44:08 +08:00
constructor(treeCache, row) {
2018-08-30 02:44:35 +08:00
this.treeCache = treeCache;
2020-02-02 18:44:08 +08:00
/** @type {string[]} */
this.attributes = [];
/** @type {string[]} */
this.targetRelations = [];
/** @type {string[]} */
this.parents = [];
/** @type {string[]} */
this.children = [];
/** @type {Object.&lt;string, string>} */
this.parentToBranch = {};
/** @type {Object.&lt;string, string>} */
this.childToBranch = {};
this.update(row);
}
update(row) {
2018-08-30 02:44:35 +08:00
/** @param {string} */
this.noteId = row.noteId;
/** @param {string} */
this.title = row.title;
/** @param {boolean} */
2020-03-08 18:41:42 +08:00
this.isProtected = !!row.isProtected;
2018-08-30 02:44:35 +08:00
/** @param {string} one of 'text', 'code', 'file' or 'render' */
this.type = row.type;
/** @param {string} content-type, e.g. "application/json" */
this.mime = row.mime;
/** @param {boolean} */
this.isDeleted = row.isDeleted;
2019-10-26 16:00:26 +08:00
}
addParent(parentNoteId, branchId) {
2020-10-01 04:48:30 +08:00
if (parentNoteId === 'none') {
return;
}
2019-10-26 16:00:26 +08:00
if (!this.parents.includes(parentNoteId)) {
this.parents.push(parentNoteId);
}
this.parentToBranch[parentNoteId] = branchId;
}
addChild(childNoteId, branchId) {
if (!this.children.includes(childNoteId)) {
this.children.push(childNoteId);
}
this.childToBranch[childNoteId] = branchId;
2020-10-01 04:48:30 +08:00
this.sortChildren();
}
sortChildren() {
2019-10-26 16:00:26 +08:00
const branchIdPos = {};
for (const branchId of Object.values(this.childToBranch)) {
branchIdPos[branchId] = this.treeCache.getBranch(branchId).notePosition;
2019-10-26 16:00:26 +08:00
}
this.children.sort((a, b) => branchIdPos[this.childToBranch[a]] &lt; branchIdPos[this.childToBranch[b]] ? -1 : 1);
2018-08-30 02:44:35 +08:00
}
/** @returns {boolean} */
isJson() {
return this.mime === "application/json";
}
async getContent() {
// we're not caching content since these objects are in treeCache and as such pretty long lived
const note = await server.get("notes/" + this.noteId);
return note.content;
}
async getJsonContent() {
const content = await this.getContent();
try {
return JSON.parse(content);
}
catch (e) {
console.log(`Cannot parse content of note ${this.noteId}: `, e.message);
return null;
}
}
2020-03-08 18:41:42 +08:00
/** @returns {string[]} */
getBranchIds() {
return Object.values(this.parentToBranch);
}
/** @returns {Branch[]} */
getBranches() {
2019-10-26 16:00:26 +08:00
const branchIds = Object.values(this.parentToBranch);
2018-08-30 02:44:35 +08:00
return this.treeCache.getBranches(branchIds);
}
/** @returns {boolean} */
hasChildren() {
2019-10-26 16:00:26 +08:00
return this.children.length > 0;
2018-08-30 02:44:35 +08:00
}
/** @returns {Branch[]} */
getChildBranches() {
// don't use Object.values() to guarantee order
const branchIds = this.children.map(childNoteId => this.childToBranch[childNoteId]);
2018-08-30 02:44:35 +08:00
2019-10-26 16:00:26 +08:00
return this.treeCache.getBranches(branchIds);
2018-08-30 02:44:35 +08:00
}
/** @returns {string[]} */
getParentNoteIds() {
2019-10-26 16:00:26 +08:00
return this.parents;
2018-08-30 02:44:35 +08:00
}
/** @returns {NoteShort[]} */
getParentNotes() {
return this.treeCache.getNotesFromCache(this.parents);
2018-08-30 02:44:35 +08:00
}
/** @returns {string[]} */
getChildNoteIds() {
2019-10-26 16:00:26 +08:00
return this.children;
2018-08-30 02:44:35 +08:00
}
/** @returns {Promise&lt;NoteShort[]>} */
async getChildNotes() {
2019-10-26 16:00:26 +08:00
return await this.treeCache.getNotes(this.children);
2018-08-30 02:44:35 +08:00
}
2020-02-02 18:44:08 +08:00
/**
* @param {string} [type] - (optional) attribute type to filter
* @param {string} [name] - (optional) attribute name to filter
* @returns {Attribute[]} all note's attributes, including inherited ones
*/
getOwnedAttributes(type, name) {
const attrs = this.attributes
.map(attributeId => this.treeCache.attributes[attributeId])
2020-10-01 04:48:30 +08:00
.filter(Boolean); // filter out nulls;
2020-02-02 18:44:08 +08:00
2020-10-01 04:48:30 +08:00
return this.__filterAttrs(attrs, type, name);
2020-02-02 18:44:08 +08:00
}
2018-12-23 05:28:49 +08:00
/**
2019-12-04 05:53:17 +08:00
* @param {string} [type] - (optional) attribute type to filter
* @param {string} [name] - (optional) attribute name to filter
* @returns {Attribute[]} all note's attributes, including inherited ones
2018-12-23 05:28:49 +08:00
*/
getAttributes(type, name) {
2020-10-01 04:48:30 +08:00
return this.__filterAttrs(this.__getCachedAttributes([]), type, name);
}
2018-12-23 05:28:49 +08:00
2020-10-01 04:48:30 +08:00
__getCachedAttributes(path) {
// notes/clones cannot form tree cycles, it is possible to create attribute inheritance cycle via templates
// when template instance is a parent of template itself
if (path.includes(this.noteId)) {
return [];
}
2020-02-02 18:44:08 +08:00
2020-10-01 04:48:30 +08:00
if (!(this.noteId in noteAttributeCache.attributes)) {
const newPath = [...path, this.noteId];
const attrArrs = [ this.getOwnedAttributes() ];
2020-02-02 18:44:08 +08:00
2020-10-01 04:48:30 +08:00
if (this.noteId !== 'root') {
for (const parentNote of this.getParentNotes()) {
// these virtual parent-child relationships are also loaded into frontend tree cache
if (parentNote.type !== 'search') {
attrArrs.push(parentNote.__getInheritableAttributes(newPath));
}
}
2020-02-02 18:44:08 +08:00
}
2020-10-01 04:48:30 +08:00
for (const templateAttr of attrArrs.flat().filter(attr => attr.type === 'relation' &amp;&amp; attr.name === 'template')) {
const templateNote = this.treeCache.notes[templateAttr.value];
if (templateNote &amp;&amp; templateNote.noteId !== this.noteId) {
attrArrs.push(templateNote.__getCachedAttributes(newPath));
2020-04-08 01:19:20 +08:00
}
2020-02-02 18:44:08 +08:00
}
2020-10-01 04:48:30 +08:00
noteAttributeCache.attributes[this.noteId] = [];
const addedAttributeIds = new Set();
for (const attr of attrArrs.flat()) {
if (!addedAttributeIds.has(attr.attributeId)) {
addedAttributeIds.add(attr.attributeId);
2020-02-02 18:44:08 +08:00
2020-10-01 04:48:30 +08:00
noteAttributeCache.attributes[this.noteId].push(attr);
}
}
}
return noteAttributeCache.attributes[this.noteId];
2020-02-02 18:44:08 +08:00
}
__filterAttrs(attributes, type, name) {
2020-10-01 04:48:30 +08:00
if (!type &amp;&amp; !name) {
return attributes;
} else if (type &amp;&amp; name) {
2020-02-02 18:44:08 +08:00
return attributes.filter(attr => attr.type === type &amp;&amp; attr.name === name);
} else if (type) {
return attributes.filter(attr => attr.type === type);
} else if (name) {
return attributes.filter(attr => attr.name === name);
2018-12-23 05:28:49 +08:00
}
}
2020-10-01 04:48:30 +08:00
__getInheritableAttributes(path) {
const attrs = this.__getCachedAttributes(path);
2020-02-02 18:44:08 +08:00
return attrs.filter(attr => attr.isInheritable);
}
/**
* @param {string} [name] - label name to filter
* @returns {Attribute[]} all note's labels (attributes with type label), including inherited ones
*/
getOwnedLabels(name) {
return this.getOwnedAttributes(LABEL, name);
}
2018-12-23 05:28:49 +08:00
/**
* @param {string} [name] - label name to filter
* @returns {Attribute[]} all note's labels (attributes with type label), including inherited ones
2018-12-23 05:28:49 +08:00
*/
getLabels(name) {
return this.getAttributes(LABEL, name);
2018-12-23 05:28:49 +08:00
}
2020-02-02 18:44:08 +08:00
/**
* @param {string} [name] - relation name to filter
* @returns {Attribute[]} all note's relations (attributes with type relation), including inherited ones
*/
getOwnedRelations(name) {
return this.getOwnedAttributes(RELATION, name);
}
2018-12-23 05:28:49 +08:00
/**
* @param {string} [name] - relation name to filter
* @returns {Attribute[]} all note's relations (attributes with type relation), including inherited ones
2018-12-23 05:28:49 +08:00
*/
getRelations(name) {
return this.getAttributes(RELATION, name);
2018-12-23 05:28:49 +08:00
}
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {boolean} true if note has an attribute with given type and name (including inherited)
2018-12-23 05:28:49 +08:00
*/
hasAttribute(type, name) {
return !!this.getAttribute(type, name);
2018-12-23 05:28:49 +08:00
}
2020-02-02 18:44:08 +08:00
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {boolean} true if note has an attribute with given type and name (including inherited)
*/
hasOwnedAttribute(type, name) {
return !!this.getOwnedAttribute(type, name);
}
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {Attribute} attribute of given type and name. If there's more such attributes, first is returned. Returns null if there's no such attribute belonging to this note.
*/
getOwnedAttribute(type, name) {
const attributes = this.getOwnedAttributes(type, name);
return attributes.length > 0 ? attributes[0] : 0;
}
2018-12-23 05:28:49 +08:00
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {Attribute} attribute of given type and name. If there's more such attributes, first is returned. Returns null if there's no such attribute belonging to this note.
2018-12-23 05:28:49 +08:00
*/
getAttribute(type, name) {
const attributes = this.getAttributes(type, name);
2018-12-23 05:28:49 +08:00
2020-02-02 18:44:08 +08:00
return attributes.length > 0 ? attributes[0] : 0;
}
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {string} attribute value of given type and name or null if no such attribute exists.
*/
getOwnedAttributeValue(type, name) {
const attr = this.getOwnedAttribute(type, name);
return attr ? attr.value : null;
2018-12-23 05:28:49 +08:00
}
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {string} attribute value of given type and name or null if no such attribute exists.
2018-12-23 05:28:49 +08:00
*/
getAttributeValue(type, name) {
const attr = this.getAttribute(type, name);
2018-12-23 05:28:49 +08:00
return attr ? attr.value : null;
}
2020-02-02 18:44:08 +08:00
/**
* @param {string} name - label name
* @returns {boolean} true if label exists (excluding inherited)
*/
hasOwnedLabel(name) { return this.hasOwnedAttribute(LABEL, name); }
2018-12-23 05:28:49 +08:00
/**
* @param {string} name - label name
* @returns {boolean} true if label exists (including inherited)
2018-12-23 05:28:49 +08:00
*/
hasLabel(name) { return this.hasAttribute(LABEL, name); }
2018-12-23 05:28:49 +08:00
2020-02-02 18:44:08 +08:00
/**
* @param {string} name - relation name
* @returns {boolean} true if relation exists (excluding inherited)
*/
hasOwnedRelation(name) { return this.hasOwnedAttribute(RELATION, name); }
2018-12-23 05:28:49 +08:00
/**
* @param {string} name - relation name
* @returns {boolean} true if relation exists (including inherited)
2018-12-23 05:28:49 +08:00
*/
hasRelation(name) { return this.hasAttribute(RELATION, name); }
2018-12-23 05:28:49 +08:00
2020-02-02 18:44:08 +08:00
/**
* @param {string} name - label name
* @returns {Attribute} label if it exists, null otherwise
*/
getOwnedLabel(name) { return this.getOwnedAttribute(LABEL, name); }
2018-12-23 05:28:49 +08:00
/**
* @param {string} name - label name
* @returns {Attribute} label if it exists, null otherwise
2018-12-23 05:28:49 +08:00
*/
getLabel(name) { return this.getAttribute(LABEL, name); }
2018-12-23 05:28:49 +08:00
2020-02-02 18:44:08 +08:00
/**
* @param {string} name - relation name
* @returns {Attribute} relation if it exists, null otherwise
*/
getOwnedRelation(name) { return this.getOwnedAttribute(RELATION, name); }
2018-12-23 05:28:49 +08:00
/**
* @param {string} name - relation name
* @returns {Attribute} relation if it exists, null otherwise
2018-12-23 05:28:49 +08:00
*/
getRelation(name) { return this.getAttribute(RELATION, name); }
2018-12-23 05:28:49 +08:00
2020-02-02 18:44:08 +08:00
/**
* @param {string} name - label name
* @returns {string} label value if label exists, null otherwise
*/
getOwnedLabelValue(name) { return this.getOwnedAttributeValue(LABEL, name); }
2018-12-23 05:28:49 +08:00
/**
* @param {string} name - label name
* @returns {string} label value if label exists, null otherwise
2018-12-23 05:28:49 +08:00
*/
getLabelValue(name) { return this.getAttributeValue(LABEL, name); }
2018-12-23 05:28:49 +08:00
2020-02-02 18:44:08 +08:00
/**
* @param {string} name - relation name
* @returns {string} relation value if relation exists, null otherwise
*/
getOwnedRelationValue(name) { return this.getOwnedAttributeValue(RELATION, name); }
2018-12-23 05:28:49 +08:00
/**
* @param {string} name - relation name
* @returns {string} relation value if relation exists, null otherwise
2018-12-23 05:28:49 +08:00
*/
getRelationValue(name) { return this.getAttributeValue(RELATION, name); }
2018-12-23 05:28:49 +08:00
/**
* @param {string} name
* @returns {Promise&lt;NoteShort>|null} target note of the relation or null (if target is empty or note was not found)
2018-12-23 05:28:49 +08:00
*/
async getRelationTarget(name) {
const targets = await this.getRelationTargets(name);
2018-12-23 05:28:49 +08:00
return targets.length > 0 ? targets[0] : null;
}
/**
* @param {string} [name] - relation name to filter
* @returns {Promise&lt;NoteShort[]>}
*/
async getRelationTargets(name) {
const relations = this.getRelations(name);
const targets = [];
for (const relation of relations) {
targets.push(await this.treeCache.getNote(relation.value));
}
return targets;
2018-12-23 05:28:49 +08:00
}
2020-10-01 04:48:30 +08:00
/**
* @returns {NoteShort[]}
*/
getTemplateNotes() {
const relations = this.getRelations('template');
return relations.map(rel => this.treeCache.notes[rel.value]);
}
hasAncestor(ancestorNote) {
if (this.noteId === ancestorNote.noteId) {
return true;
}
for (const templateNote of this.getTemplateNotes()) {
if (templateNote.hasAncestor(ancestorNote)) {
return true;
}
}
for (const parentNote of this.getParentNotes()) {
if (parentNote.hasAncestor(ancestorNote)) {
return true;
}
}
return false;
}
2018-12-23 05:28:49 +08:00
/**
* Clear note's attributes cache to force fresh reload for next attribute request.
* Cache is note instance scoped.
*/
2020-02-02 18:44:08 +08:00
invalidateAttributeCache() {
2019-12-04 05:53:17 +08:00
this.__attributeCache = null;
2018-12-23 05:28:49 +08:00
}
/**
* Get relations which target this note
*
2020-02-02 18:44:08 +08:00
* @returns {Attribute[]}
*/
2020-02-02 18:44:08 +08:00
getTargetRelations() {
return this.targetRelations
.map(attributeId => this.treeCache.attributes[attributeId]);
}
2020-10-01 04:48:30 +08:00
/**
* Get relations which target this note
*
* @returns {NoteShort[]}
*/
async getTargetRelationSourceNotes() {
const targetRelations = this.getTargetRelations();
return await this.treeCache.getNotes(targetRelations.map(tr => tr.noteId));
}
/**
* Return note complement which is most importantly note's content
*
* @return {Promise&lt;NoteComplement>}
*/
async getNoteComplement() {
return await this.treeCache.getNoteComplement(this.noteId);
}
2018-08-30 02:44:35 +08:00
get toString() {
return `Note(noteId=${this.noteId}, title=${this.title})`;
}
get dto() {
const dto = Object.assign({}, this);
delete dto.treeCache;
return dto;
}
2020-03-08 18:41:42 +08:00
getCssClass() {
const labels = this.getLabels('cssClass');
2020-03-08 18:41:42 +08:00
return labels.map(l => l.value).join(' ');
}
2018-08-30 02:44:35 +08:00
}
2020-10-01 04:48:30 +08:00
export default NoteShort;
</code></pre>
2018-08-30 02:44:35 +08:00
</article>
</section>
</div>
<nav>
2020-04-22 05:14:55 +08:00
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Branch.html">Branch</a></li><li><a href="FrontendScriptApi.html">FrontendScriptApi</a></li><li><a href="NoteComplement.html">NoteComplement</a></li><li><a href="NoteShort.html">NoteShort</a></li></ul><h3>Global</h3><ul><li><a href="global.html#decorateWidget">decorateWidget</a></li><li><a href="global.html#doRenderBody">doRenderBody</a></li><li><a href="global.html#widgetCollapsedStateChangedEvent">widgetCollapsedStateChangedEvent</a></li></ul>
2018-08-30 02:44:35 +08:00
</nav>
<br class="clear">
<footer>
2020-10-01 04:48:30 +08:00
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.6</a>
2018-08-30 02:44:35 +08:00
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>