From 55f04436c091c5a3f32c8fc0ef0e1a4bbf8677c7 Mon Sep 17 00:00:00 2001 From: zadam Date: Fri, 23 Oct 2020 00:11:44 +0200 Subject: [PATCH] saving expanded status as label --- src/public/app/services/attributes.js | 18 ++++++++++++++++++ src/public/app/services/note_list_renderer.js | 12 +++++++++++- src/public/app/widgets/type_widgets/book.js | 2 +- src/routes/api/attributes.js | 4 +++- 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 src/public/app/services/attributes.js diff --git a/src/public/app/services/attributes.js b/src/public/app/services/attributes.js new file mode 100644 index 000000000..aebcc8c7e --- /dev/null +++ b/src/public/app/services/attributes.js @@ -0,0 +1,18 @@ +import server from './server.js'; + +async function addLabel(noteId, name, value = "") { + await server.put(`notes/${noteId}/attribute`, { + type: 'label', + name: name, + value: value + }); +} + +async function removeAttributeById(noteId, attributeId) { + await server.remove(`notes/${noteId}/attributes/${attributeId}`); +} + +export default { + addLabel, + removeAttributeById +} diff --git a/src/public/app/services/note_list_renderer.js b/src/public/app/services/note_list_renderer.js index 071bda9bf..8b361ec26 100644 --- a/src/public/app/services/note_list_renderer.js +++ b/src/public/app/services/note_list_renderer.js @@ -1,6 +1,7 @@ import linkService from "./link.js"; import noteContentRenderer from "./note_content_renderer.js"; import treeCache from "./tree_cache.js"; +import attributeService from "./attributes.js"; const ZOOMS = { 1: { @@ -182,7 +183,7 @@ async function renderNoteListContent($noteList, parentNote, notes, page = 1, pag } } -async function renderList(notes, parentNote = null) { +async function renderList(notes, parentNote) { const $noteList = $(TPL); $noteList.find('.expand-children-button').on('click', async () => { @@ -194,6 +195,10 @@ async function renderList(notes, parentNote = null) { } await toggleCards($unexpandedCards, true); + + if (!parentNote.hasLabel('expanded')) { + await attributeService.addLabel(parentNote.noteId, 'expanded'); + } } }); @@ -201,6 +206,11 @@ async function renderList(notes, parentNote = null) { const $expandedCards = $noteList.find('.note-book-card.expanded'); await toggleCards($expandedCards, false); + + // owned is important - we shouldn't remove inherited expanded labels + for (const expandedAttr of parentNote.getOwnedLabels('expanded')) { + await attributeService.removeAttributeById(parentNote.noteId, expandedAttr.attributeId); + } }); await renderNoteListContent($noteList, parentNote, notes); diff --git a/src/public/app/widgets/type_widgets/book.js b/src/public/app/widgets/type_widgets/book.js index 727cf26d5..92308cda9 100644 --- a/src/public/app/widgets/type_widgets/book.js +++ b/src/public/app/widgets/type_widgets/book.js @@ -63,7 +63,7 @@ export default class BookTypeWidget extends TypeWidget { // const zoomLevel = parseInt(note.getLabelValue('bookZoomLevel')) || this.getDefaultZoomLevel(); // this.setZoom(zoomLevel); - this.$content.append(await noteListRenderer.renderList(await note.getChildNotes())); + this.$content.append(await noteListRenderer.renderList(await note.getChildNotes(), note)); } /** @return {boolean} true if this is "auto book" activated (empty text note) and not explicit book note */ diff --git a/src/routes/api/attributes.js b/src/routes/api/attributes.js index 64ebc53de..65d6df8c4 100644 --- a/src/routes/api/attributes.js +++ b/src/routes/api/attributes.js @@ -52,7 +52,7 @@ function updateNoteAttribute(req) { attribute.type = body.type; } - if (body.value.trim()) { + if (attribute.type === 'label' || body.value.trim()) { attribute.value = body.value; } else { @@ -62,6 +62,8 @@ function updateNoteAttribute(req) { attribute.save(); + console.log("Saving", attribute); + return { attributeId: attribute.attributeId };