87 lines
2.4 KiB
JavaScript
Raw Normal View History

2018-08-23 12:55:45 +02:00
/**
* This note's representation is used in note tree and is kept in TreeCache.
* Its notable omission is the note content.
*/
class NoteShort {
constructor(treeCache, row) {
this.treeCache = treeCache;
2018-08-23 12:55:45 +02:00
/** @param {string} */
this.noteId = row.noteId;
2018-08-23 12:55:45 +02:00
/** @param {string} */
this.title = row.title;
2018-08-23 12:55:45 +02:00
/** @param {boolean} */
this.isProtected = row.isProtected;
2018-08-23 12:55:45 +02:00
/** @param {string} one of 'text', 'code', 'file' or 'render' */
this.type = row.type;
2018-08-23 12:55:45 +02:00
/** @param {string} content-type, e.g. "application/json" */
this.mime = row.mime;
2018-08-23 12:55:45 +02:00
/** @param {boolean} */
this.archived = row.archived;
2018-08-13 10:59:31 +02:00
this.cssClass = row.cssClass;
}
2018-08-23 12:55:45 +02:00
/** @returns {boolean} */
2018-03-25 23:25:17 -04:00
isJson() {
return this.mime === "application/json";
}
2018-08-23 15:33:19 +02:00
/** @returns {Promise<Branch[]>} */
async getBranches() {
const branchIds = this.treeCache.parents[this.noteId].map(
parentNoteId => this.treeCache.getBranchIdByChildParent(this.noteId, parentNoteId));
return this.treeCache.getBranches(branchIds);
}
2018-08-23 12:55:45 +02:00
/** @returns {boolean} */
hasChildren() {
return this.treeCache.children[this.noteId]
&& this.treeCache.children[this.noteId].length > 0;
}
2018-08-23 15:33:19 +02:00
/** @returns {Promise<Branch[]>} */
async getChildBranches() {
if (!this.treeCache.children[this.noteId]) {
2018-04-10 21:08:00 -04:00
return [];
}
const branchIds = this.treeCache.children[this.noteId].map(
childNoteId => this.treeCache.getBranchIdByChildParent(childNoteId, this.noteId));
return await this.treeCache.getBranches(branchIds);
}
2018-08-23 15:33:19 +02:00
/** @returns {string[]} */
2018-04-16 23:34:56 -04:00
getParentNoteIds() {
return this.treeCache.parents[this.noteId] || [];
}
2018-08-23 15:33:19 +02:00
/** @returns {Promise<NoteShort[]>} */
async getParentNotes() {
2018-04-16 23:34:56 -04:00
return await this.treeCache.getNotes(this.getParentNoteIds());
}
2018-08-23 15:33:19 +02:00
/** @returns {string[]} */
2018-04-16 23:34:56 -04:00
getChildNoteIds() {
return this.treeCache.children[this.noteId] || [];
}
2018-08-23 15:33:19 +02:00
/** @returns {Promise<NoteShort[]>} */
async getChildNotes() {
2018-04-16 23:34:56 -04:00
return await this.treeCache.getNotes(this.getChildNoteIds());
}
get toString() {
return `Note(noteId=${this.noteId}, title=${this.title})`;
}
2018-04-08 08:21:49 -04:00
get dto() {
const dto = Object.assign({}, this);
delete dto.treeCache;
delete dto.archived;
2018-04-08 08:21:49 -04:00
return dto;
}
}
export default NoteShort;