diff --git a/static/js/context_menu.js b/static/js/context_menu.js index 82e9a9b19..147a4ccc8 100644 --- a/static/js/context_menu.js +++ b/static/js/context_menu.js @@ -6,6 +6,9 @@ const contextMenuSetup = { {title: "Insert child note", cmd: "insertChildNote", uiIcon: "ui-icon-pencil"}, {title: "Delete", cmd: "delete", uiIcon: "ui-icon-trash"}, {title: "----"}, + {title: "Encrypt sub-tree", cmd: "encryptSubTree", uiIcon: "ui-icon-locked"}, + {title: "Decrypt sub-tree", cmd: "decryptSubTree", uiIcon: "ui-icon-unlocked"}, + {title: "----"}, {title: "Cut", cmd: "cut", uiIcon: "ui-icon-scissors"}, {title: "Copy / clone", cmd: "copy", uiIcon: "ui-icon-copy"}, {title: "Paste after", cmd: "pasteAfter", uiIcon: "ui-icon-clipboard"}, @@ -36,6 +39,12 @@ const contextMenuSetup = { else if (ui.cmd === "insertChildNote") { createNote(node, node.key, 'into'); } + else if (ui.cmd === "encryptSubTree") { + encryptSubTree(node.key); + } + else if (ui.cmd === "decryptSubTree") { + decryptSubTree(node.key); + } else if (ui.cmd === "cut") { globalClipboardNoteId = node.key; } diff --git a/static/js/encryption.js b/static/js/encryption.js index 9ec31d30c..18d65b26f 100644 --- a/static/js/encryption.js +++ b/static/js/encryption.js @@ -82,8 +82,6 @@ $("#encryptionPasswordForm").submit(function() { globalDataKey = key; - console.log("got the key", key); - for (const noteId of globalAllNoteIds) { const note = getNodeByKey(noteId); @@ -100,7 +98,11 @@ $("#encryptionPasswordForm").submit(function() { globalEncryptionCallback = null; } }) - .catch(reason => alert(reason)); + .catch(reason => { + console.log(reason); + + alert(reason); + }); return false; }); @@ -265,4 +267,109 @@ function decryptNoteIfNecessary(note) { function decryptNote(note) { note.detail.note_title = decryptString(note.detail.note_title); note.detail.note_text = decryptString(note.detail.note_text); +} + +function encryptSubTree(noteId) { + handleEncryption(true, true, () => { + updateSubTreeRecursively(noteId, note => { + if (note.detail.encryption === null || note.detail.encryption === 0) { + encryptNote(note); + + note.detail.encryption = 1; + + return true; + } + else { + return false; + } + }, + note => { + if (note.detail.note_id === globalCurrentNote.detail.note_id) { + loadNote(note.detail.note_id); + } + else { + setTreeBasedOnEncryption(note); + } + }); + + alert("Encryption finished."); + }); +} + +function decryptSubTree(noteId) { + handleEncryption(true, true, () => { + updateSubTreeRecursively(noteId, note => { + if (note.detail.encryption === 1) { + decryptNote(note); + + note.detail.encryption = 0; + + return true; + } + else { + return false; + } + }, + note => { + if (note.detail.note_id === globalCurrentNote.detail.note_id) { + loadNote(note.detail.note_id); + } + else { + setTreeBasedOnEncryption(note); + } + }); + + alert("Decryption finished."); + }); +} + +function updateSubTreeRecursively(noteId, updateCallback, successCallback) { + updateNoteSynchronously(noteId, updateCallback, successCallback); + + const node = getNodeByKey(noteId); + if (!node || !node.getChildren()) { + return; + } + + for (const child of node.getChildren()) { + updateSubTreeRecursively(child.key, updateCallback, successCallback); + } +} + +function updateNoteSynchronously(noteId, updateCallback, successCallback) { + $.ajax({ + url: baseUrl + 'notes/' + noteId, + type: 'GET', + async: false, + success: function (note) { + const needSave = updateCallback(note); + + if (!needSave) { + return; + } + + for (const link of note.links) { + delete link.type; + } + + $.ajax({ + url: baseUrl + 'notes/' + noteId, + type: 'PUT', + data: JSON.stringify(note), + contentType: "application/json", + async: false, + success: function () { + if (successCallback) { + successCallback(note); + } + }, + error: function () { + console.log("Updating " + noteId + " failed."); + } + }); + }, + error: function () { + console.log("Reading " + noteId + " failed."); + } + }); } \ No newline at end of file diff --git a/static/js/note.js b/static/js/note.js index cac0f7e44..e641a97b7 100644 --- a/static/js/note.js +++ b/static/js/note.js @@ -149,6 +149,11 @@ function createNote(node, parentKey, target, encryption) { }); } +function setTreeBasedOnEncryption(note) { + const node = getNodeByKey(note.detail.note_id); + node.toggleClass("encrypted", note.detail.encryption > 0); +} + function setNoteBackgroundIfEncrypted(note) { if (note.detail.encryption > 0) { $(".note-editable").addClass("encrypted"); @@ -161,8 +166,7 @@ function setNoteBackgroundIfEncrypted(note) { $("#decryptButton").hide(); } - const node = getNodeByKey(note.detail.note_id); - node.toggleClass("encrypted", note.detail.encryption > 0); + setTreeBasedOnEncryption(note); } function loadNote(noteId) {