From a42bbba0e54956cb7ba6a54b87d810d970359314 Mon Sep 17 00:00:00 2001 From: azivner Date: Fri, 17 Aug 2018 15:21:59 +0200 Subject: [PATCH] unprotecting note outside of protected session is not forbidden because it could overwrite previous note --- src/public/javascripts/services/note_detail.js | 7 ++++--- src/public/javascripts/services/protected_session.js | 11 +++++++++-- src/public/javascripts/services/tree.js | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/public/javascripts/services/note_detail.js b/src/public/javascripts/services/note_detail.js index bc33bac99..ed1bb08e7 100644 --- a/src/public/javascripts/services/note_detail.js +++ b/src/public/javascripts/services/note_detail.js @@ -119,11 +119,12 @@ async function saveNoteIfChanged() { } function setNoteBackgroundIfProtected(note) { - const isProtected = !!note.isProtected; + const isProtected = note.isProtected; $noteDetailWrapper.toggleClass("protected", isProtected); $protectButton.toggleClass("active", isProtected); $unprotectButton.toggleClass("active", !isProtected); + $unprotectButton.prop("disabled", !protectedSessionHolder.isProtectedSessionAvailable()); } let isNewNoteCreated = false; @@ -157,8 +158,6 @@ async function loadNoteDetail(noteId) { setNoteBackgroundIfProtected(currentNote); - await handleProtectedSession(); - $noteDetailWrapper.show(); noteChangeDisabled = true; @@ -171,6 +170,8 @@ async function loadNoteDetail(noteId) { $noteDetailComponents.hide(); + await handleProtectedSession(); + await getComponent(currentNote.type).show(); } finally { diff --git a/src/public/javascripts/services/protected_session.js b/src/public/javascripts/services/protected_session.js index 4f14907db..33f4aa944 100644 --- a/src/public/javascripts/services/protected_session.js +++ b/src/public/javascripts/services/protected_session.js @@ -32,6 +32,7 @@ function ensureProtectedSession(requireProtectedSession, modal) { const dfd = $.Deferred(); if (requireProtectedSession && !protectedSessionHolder.isProtectedSessionAvailable()) { + // using deferred instead of promise because it allows resolving from outside protectedSessionDeferred = dfd; if (treeService.getCurrentNode().data.isProtected) { @@ -39,7 +40,6 @@ function ensureProtectedSession(requireProtectedSession, modal) { } $dialog.dialog({ - // modal: modal, // everything is now non-modal, because modal dialog caused weird high CPU usage on opening // and tearing of text input modal: false, @@ -128,7 +128,14 @@ async function unprotectNoteAndSendToServer() { return; } - await ensureProtectedSession(true, true); + if (!protectedSessionHolder.isProtectedSessionAvailable()) { + console.log("Unprotecting notes outside of protected session is not allowed."); + // the reason is that it's not easy to handle even with ensureProtectedSession, + // because we would first have to make sure the note is loaded and only then unprotect + // we used to have a bug where we would overwrite the previous note with unprotected content. + + return; + } const note = noteDetailService.getCurrentNote(); note.isProtected = false; diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js index b0f1fbb5b..5e02567ef 100644 --- a/src/public/javascripts/services/tree.js +++ b/src/public/javascripts/services/tree.js @@ -419,7 +419,7 @@ function scrollToCurrentNote() { } function setBranchBackgroundBasedOnProtectedStatus(noteId) { - getNodesByNoteId(noteId).map(node => node.toggleClass("protected", !!node.data.isProtected)); + getNodesByNoteId(noteId).map(node => node.toggleClass("protected", node.data.isProtected)); } function setProtected(noteId, isProtected) {