From 45ee959c11a3bec01a62ad159662776b8ba9f9dc Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 7 Sep 2019 21:40:18 +0200 Subject: [PATCH] date notes are now created with template relations --- src/entities/note.js | 4 ++++ src/public/javascripts/services/note_type.js | 6 +++--- src/services/attributes.js | 10 +++++++++ src/services/date_notes.js | 22 ++++++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/entities/note.js b/src/entities/note.js index 8524d9a69..b2f7a4b11 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -195,6 +195,8 @@ class Note extends Entity { /** * @returns {Promise} attributes belonging to this specific note (excludes inherited attributes) + * + * This method can be significantly faster than the getAttributes() */ async getOwnedAttributes(type, name) { let query = `SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ?`; @@ -215,6 +217,8 @@ class Note extends Entity { /** * @returns {Promise} attribute belonging to this specific note (excludes inherited attributes) + * + * This method can be significantly faster than the getAttribute() */ async getOwnedAttribute(type, name) { const attrs = await this.getOwnedAttributes(type, name); diff --git a/src/public/javascripts/services/note_type.js b/src/public/javascripts/services/note_type.js index bb9f6d035..da95a5a7b 100644 --- a/src/public/javascripts/services/note_type.js +++ b/src/public/javascripts/services/note_type.js @@ -35,6 +35,9 @@ export default class NoteTypeContext { () => ["file", "image", "search"].includes(this.ctx.note.type)); this.$noteTypeDesc.text(await this.findTypeTitle(this.ctx.note.type, this.ctx.note.mime)); + + this.$executeScriptButton.toggle(this.ctx.note.mime.startsWith('application/javascript')); + this.$renderButton.toggle(this.ctx.note.type === 'render'); } /** actual body is rendered lazily on note-type button click */ @@ -87,9 +90,6 @@ export default class NoteTypeContext { this.$noteTypeDropdown.append($mimeLink); } - - this.$executeScriptButton.toggle(this.ctx.note.mime.startsWith('application/javascript')); - this.$renderButton.toggle(this.ctx.note.type === 'render'); } async findTypeTitle(type, mime) { diff --git a/src/services/attributes.js b/src/services/attributes.js index ff6a5e487..8f4102b27 100644 --- a/src/services/attributes.js +++ b/src/services/attributes.js @@ -72,6 +72,15 @@ async function createLabel(noteId, name, value = "") { }); } +async function createRelation(noteId, name, targetNoteId) { + return await createAttribute({ + noteId: noteId, + type: 'relation', + name: name, + value: targetNoteId + }); +} + async function createAttribute(attribute) { return await new Attribute(attribute).save(); } @@ -114,6 +123,7 @@ module.exports = { getNotesWithLabels, getNoteWithLabel, createLabel, + createRelation, createAttribute, getAttributeNames, isAttributeType, diff --git a/src/services/date_notes.js b/src/services/date_notes.js index 89d282179..7887b4b40 100644 --- a/src/services/date_notes.js +++ b/src/services/date_notes.js @@ -67,6 +67,12 @@ async function getYearNote(dateStr, rootNote) { await attributeService.createLabel(yearNote.noteId, YEAR_LABEL, yearStr); await attributeService.createLabel(yearNote.noteId, 'sorted'); + + const yearTemplateAttr = await rootNote.getOwnedAttribute('relation', 'yearTemplate'); + + if (yearTemplateAttr) { + await attributeService.createRelation(yearNote.noteId, 'template', yearTemplateAttr.value); + } } return yearNote; @@ -83,6 +89,10 @@ async function getMonthNoteTitle(rootNote, monthNumber, dateObj) { /** @return {Promise} */ async function getMonthNote(dateStr, rootNote) { + if (!rootNote) { + rootNote = await getRootCalendarNote(); + } + const monthStr = dateStr.substr(0, 7); const monthNumber = dateStr.substr(5, 2); @@ -103,6 +113,12 @@ async function getMonthNote(dateStr, rootNote) { await attributeService.createLabel(monthNote.noteId, MONTH_LABEL, monthStr); await attributeService.createLabel(monthNote.noteId, 'sorted'); + + const monthTemplateAttr = await rootNote.getOwnedAttribute('relation', 'monthTemplate'); + + if (monthTemplateAttr) { + await attributeService.createRelation(monthNote.noteId, 'template', monthTemplateAttr.value); + } } return monthNote; @@ -141,6 +157,12 @@ async function getDateNote(dateStr) { } await attributeService.createLabel(dateNote.noteId, DATE_LABEL, dateStr.substr(0, 10)); + + const dateTemplateAttr = await rootNote.getOwnedAttribute('relation', 'dateTemplate'); + + if (dateTemplateAttr) { + await attributeService.createRelation(dateNote.noteId, 'template', dateTemplateAttr.value); + } } return dateNote;