From b381331029cfa73f97f1fe257d842a501a2ab102 Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 24 Jul 2021 11:28:47 +0200 Subject: [PATCH] refactoring of date notes route --- db/TODO.txt | 1 + src/services/date_notes.js | 138 +++++++++++++++++++------------------ 2 files changed, 73 insertions(+), 66 deletions(-) diff --git a/db/TODO.txt b/db/TODO.txt index 1e2fc2b3f..727557383 100644 --- a/db/TODO.txt +++ b/db/TODO.txt @@ -8,3 +8,4 @@ * readOnly=auto - like without readOnly (used to override inherited readOnly) * readOnly=never - like autoReadOnlyDisabled - remove focusOnAttributesKeyboardShortcut +- rename white theme to "light" theme (it's not completely white and matches well to dark theme) diff --git a/src/services/date_notes.js b/src/services/date_notes.js index 9b55224ca..847354dc8 100644 --- a/src/services/date_notes.js +++ b/src/services/date_notes.js @@ -66,27 +66,26 @@ function getYearNote(dateStr, rootNote) { const yearStr = dateStr.substr(0, 4); - let yearNote = attributeService.getNoteWithLabel(YEAR_LABEL, yearStr); + let yearNote = attributeService.getNoteWithLabel(YEAR_LABEL, yearStr) + || getNoteStartingWith(rootNote.noteId, yearStr); - if (!yearNote) { - yearNote = getNoteStartingWith(rootNote.noteId, yearStr); - - if (!yearNote) { - sql.transactional(() => { - yearNote = createNote(rootNote, yearStr); - - attributeService.createLabel(yearNote.noteId, YEAR_LABEL, yearStr); - attributeService.createLabel(yearNote.noteId, 'sorted'); - - const yearTemplateAttr = rootNote.getOwnedAttribute('relation', 'yearTemplate'); - - if (yearTemplateAttr) { - attributeService.createRelation(yearNote.noteId, 'template', yearTemplateAttr.value); - } - }); - } + if (yearNote) { + return yearNote; } + sql.transactional(() => { + yearNote = createNote(rootNote, yearStr); + + attributeService.createLabel(yearNote.noteId, YEAR_LABEL, yearStr); + attributeService.createLabel(yearNote.noteId, 'sorted'); + + const yearTemplateAttr = rootNote.getOwnedAttribute('relation', 'yearTemplate'); + + if (yearTemplateAttr) { + attributeService.createRelation(yearNote.noteId, 'template', yearTemplateAttr.value); + } + }); + return yearNote; } @@ -110,31 +109,35 @@ function getMonthNote(dateStr, rootNote) { let monthNote = attributeService.getNoteWithLabel(MONTH_LABEL, monthStr); - if (!monthNote) { - const yearNote = getYearNote(dateStr, rootNote); - - monthNote = getNoteStartingWith(yearNote.noteId, monthNumber); - - if (!monthNote) { - const dateObj = dateUtils.parseLocalDate(dateStr); - - const noteTitle = getMonthNoteTitle(rootNote, monthNumber, dateObj); - - sql.transactional(() => { - monthNote = createNote(yearNote, noteTitle); - - attributeService.createLabel(monthNote.noteId, MONTH_LABEL, monthStr); - attributeService.createLabel(monthNote.noteId, 'sorted'); - - const monthTemplateAttr = rootNote.getOwnedAttribute('relation', 'monthTemplate'); - - if (monthTemplateAttr) { - attributeService.createRelation(monthNote.noteId, 'template', monthTemplateAttr.value); - } - }); - } + if (monthNote) { + return monthNote; } + const yearNote = getYearNote(dateStr, rootNote); + + monthNote = getNoteStartingWith(yearNote.noteId, monthNumber); + + if (monthNote) { + return monthNote; + } + + const dateObj = dateUtils.parseLocalDate(dateStr); + + const noteTitle = getMonthNoteTitle(rootNote, monthNumber, dateObj); + + sql.transactional(() => { + monthNote = createNote(yearNote, noteTitle); + + attributeService.createLabel(monthNote.noteId, MONTH_LABEL, monthStr); + attributeService.createLabel(monthNote.noteId, 'sorted'); + + const monthTemplateAttr = rootNote.getOwnedAttribute('relation', 'monthTemplate'); + + if (monthTemplateAttr) { + attributeService.createRelation(monthNote.noteId, 'template', monthTemplateAttr.value); + } + }); + return monthNote; } @@ -152,35 +155,38 @@ function getDateNoteTitle(rootNote, dayNumber, dateObj) { /** @return {Note} */ function getDateNote(dateStr) { - const rootNote = getRootCalendarNote(); - let dateNote = attributeService.getNoteWithLabel(DATE_LABEL, dateStr); - if (!dateNote) { - const monthNote = getMonthNote(dateStr, rootNote); - const dayNumber = dateStr.substr(8, 2); - - dateNote = getNoteStartingWith(monthNote.noteId, dayNumber); - - if (!dateNote) { - const dateObj = dateUtils.parseLocalDate(dateStr); - - const noteTitle = getDateNoteTitle(rootNote, dayNumber, dateObj); - - sql.transactional(() => { - dateNote = createNote(monthNote, noteTitle); - - attributeService.createLabel(dateNote.noteId, DATE_LABEL, dateStr.substr(0, 10)); - - const dateTemplateAttr = rootNote.getOwnedAttribute('relation', 'dateTemplate'); - - if (dateTemplateAttr) { - attributeService.createRelation(dateNote.noteId, 'template', dateTemplateAttr.value); - } - }); - } + if (dateNote) { + return dateNote; } + const rootNote = getRootCalendarNote(); + const monthNote = getMonthNote(dateStr, rootNote); + const dayNumber = dateStr.substr(8, 2); + + dateNote = getNoteStartingWith(monthNote.noteId, dayNumber); + + if (dateNote) { + return dateNote; + } + + const dateObj = dateUtils.parseLocalDate(dateStr); + + const noteTitle = getDateNoteTitle(rootNote, dayNumber, dateObj); + + sql.transactional(() => { + dateNote = createNote(monthNote, noteTitle); + + attributeService.createLabel(dateNote.noteId, DATE_LABEL, dateStr.substr(0, 10)); + + const dateTemplateAttr = rootNote.getOwnedAttribute('relation', 'dateTemplate'); + + if (dateTemplateAttr) { + attributeService.createRelation(dateNote.noteId, 'template', dateTemplateAttr.value); + } + }); + return dateNote; }