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