diff --git a/public/javascripts/add_link.js b/public/javascripts/add_link.js index cd94ebe23..fcea40634 100644 --- a/public/javascripts/add_link.js +++ b/public/javascripts/add_link.js @@ -17,7 +17,7 @@ $(document).bind('keydown', 'alt+l', () => { } $("#note-autocomplete").autocomplete({ - source: getAutocompleteItems(globalAllNoteIds), + source: getAutocompleteItems(glob.allNoteIds), minLength: 0, change: () => { const val = $("#note-autocomplete").val(); diff --git a/public/javascripts/context_menu.js b/public/javascripts/context_menu.js index 495929a91..8f5ad4523 100644 --- a/public/javascripts/context_menu.js +++ b/public/javascripts/context_menu.js @@ -1,21 +1,21 @@ function pasteAfter(node) { - const subjectNode = getNodeByKey(globalClipboardNoteId); + const subjectNode = getNodeByKey(glob.clipboardNoteId); moveAfterNode(subjectNode, node); - globalClipboardNoteId = null; + glob.clipboardNoteId = null; } function pasteInto(node) { - const subjectNode = getNodeByKey(globalClipboardNoteId); + const subjectNode = getNodeByKey(glob.clipboardNoteId); moveToNode(subjectNode, node); - globalClipboardNoteId = null; + glob.clipboardNoteId = null; } function cut(node) { - globalClipboardNoteId = node.key; + glob.clipboardNoteId = node.key; } const contextMenuSetup = { @@ -37,8 +37,8 @@ const contextMenuSetup = { beforeOpen: (event, ui) => { const node = $.ui.fancytree.getNode(ui.target); // Modify menu entries depending on node status - globalTree.contextmenu("enableEntry", "pasteAfter", globalClipboardNoteId !== null); - globalTree.contextmenu("enableEntry", "pasteInto", globalClipboardNoteId !== null); + glob.tree.contextmenu("enableEntry", "pasteAfter", glob.clipboardNoteId !== null); + glob.tree.contextmenu("enableEntry", "pasteInto", glob.clipboardNoteId !== null); // Activate node on right-click node.setActive(); diff --git a/public/javascripts/encryption.js b/public/javascripts/encryption.js index 780b3ded7..47cdc38d2 100644 --- a/public/javascripts/encryption.js +++ b/public/javascripts/encryption.js @@ -1,10 +1,10 @@ -let globalEncryptionDeferred = null; +glob.encryptionDeferred = null; function handleEncryption(requireEncryption, modal) { const dfd = $.Deferred(); - if (requireEncryption && globalDataKey === null) { - globalEncryptionDeferred = dfd; + if (requireEncryption && glob.dataKey === null) { + glob.encryptionDeferred = dfd; $("#encryption-password-dialog").dialog({ modal: modal, @@ -12,7 +12,7 @@ function handleEncryption(requireEncryption, modal) { open: () => { if (!modal) { // dialog steals focus for itself, which is not what we want for non-modal (viewing) - getNodeByKey(globalCurrentNote.detail.note_id).setFocus(); + getNodeByKey(glob.currentNote.detail.note_id).setFocus(); } } }); @@ -24,14 +24,14 @@ function handleEncryption(requireEncryption, modal) { return dfd.promise(); } -let globalDataKey = null; -let globalLastEncryptionOperationDate = null; +glob.dataKey = null; +glob.lastEncryptionOperationDate = null; function getDataKey(password) { - return computeScrypt(password, globalEncryptionSalt, (key, resolve, reject) => { + return computeScrypt(password, glob.encryptionSalt, (key, resolve, reject) => { const dataKeyAes = getDataKeyAes(key); - const decryptedDataKey = decrypt(dataKeyAes, globalEncryptedDataKey); + const decryptedDataKey = decrypt(dataKeyAes, glob.encryptedDataKey); if (decryptedDataKey === false) { reject("Wrong password."); @@ -77,7 +77,7 @@ function decryptTreeItems() { return; } - for (const noteId of globalAllNoteIds) { + for (const noteId of glob.allNoteIds) { const note = getNodeByKey(noteId); if (note.data.encryption > 0) { @@ -95,14 +95,14 @@ $("#encryption-password-form").submit(() => { getDataKey(password).then(key => { $("#encryption-password-dialog").dialog("close"); - globalDataKey = key; + glob.dataKey = key; decryptTreeItems(); - if (globalEncryptionDeferred !== null) { - globalEncryptionDeferred.resolve(); + if (glob.encryptionDeferred !== null) { + glob.encryptionDeferred.resolve(); - globalEncryptionDeferred = null; + glob.encryptionDeferred = null; } }) .catch(reason => { @@ -115,12 +115,12 @@ $("#encryption-password-form").submit(() => { }); function resetEncryptionSession() { - globalDataKey = null; + glob.dataKey = null; - if (globalCurrentNote.detail.encryption > 0) { - loadNoteToEditor(globalCurrentNote.detail.note_id); + if (glob.currentNote.detail.encryption > 0) { + loadNoteToEditor(glob.currentNote.detail.note_id); - for (const noteId of globalAllNoteIds) { + for (const noteId of glob.allNoteIds) { const note = getNodeByKey(noteId); if (note.data.encryption > 0) { @@ -131,19 +131,19 @@ function resetEncryptionSession() { } setInterval(() => { - if (globalLastEncryptionOperationDate !== null && new Date().getTime() - globalLastEncryptionOperationDate.getTime() > globalEncryptionSessionTimeout * 1000) { + if (glob.lastEncryptionOperationDate !== null && new Date().getTime() - glob.lastEncryptionOperationDate.getTime() > glob.encryptionSessionTimeout * 1000) { resetEncryptionSession(); } }, 5000); function isEncryptionAvailable() { - return globalDataKey !== null; + return glob.dataKey !== null; } function getDataAes() { - globalLastEncryptionOperationDate = new Date(); + glob.lastEncryptionOperationDate = new Date(); - return new aesjs.ModeOfOperation.ctr(globalDataKey, new aesjs.Counter(5)); + return new aesjs.ModeOfOperation.ctr(glob.dataKey, new aesjs.Counter(5)); } function getDataKeyAes(key) { @@ -240,7 +240,7 @@ function encryptNote(note) { async function encryptNoteAndSendToServer() { await handleEncryption(true, true); - const note = globalCurrentNote; + const note = glob.currentNote; updateNoteFromInputs(note); @@ -287,7 +287,7 @@ async function changeEncryptionOnNoteHistory(noteId, encrypt) { async function decryptNoteAndSendToServer() { await handleEncryption(true, true); - const note = globalCurrentNote; + const note = glob.currentNote; updateNoteFromInputs(note); @@ -327,7 +327,7 @@ async function encryptSubTree(noteId) { } }, note => { - if (note.detail.note_id === globalCurrentNote.detail.note_id) { + if (note.detail.note_id === glob.currentNote.detail.note_id) { loadNoteToEditor(note.detail.note_id); } else { @@ -354,7 +354,7 @@ async function decryptSubTree(noteId) { } }, note => { - if (note.detail.note_id === globalCurrentNote.detail.note_id) { + if (note.detail.note_id === glob.currentNote.detail.note_id) { loadNoteToEditor(note.detail.note_id); } else { diff --git a/public/javascripts/init.js b/public/javascripts/init.js index 069b54264..c22b3ef33 100644 --- a/public/javascripts/init.js +++ b/public/javascripts/init.js @@ -1,3 +1,5 @@ +glob = {}; + // hot keys are active also inside inputs and content editables jQuery.hotkeys.options.filterInputAcceptingElements = true; jQuery.hotkeys.options.filterContentEditable = true; diff --git a/public/javascripts/jump_to_note.js b/public/javascripts/jump_to_note.js index 59e2dc8e6..188649214 100644 --- a/public/javascripts/jump_to_note.js +++ b/public/javascripts/jump_to_note.js @@ -7,7 +7,7 @@ function showJumpToNote() { }); $("#jump-to-note-autocomplete").autocomplete({ - source: getAutocompleteItems(globalAllNoteIds), + source: getAutocompleteItems(glob.allNoteIds), minLength: 0 }); } diff --git a/public/javascripts/note.js b/public/javascripts/note.js index d5f8a7179..040c1e1c3 100644 --- a/public/javascripts/note.js +++ b/public/javascripts/note.js @@ -26,7 +26,7 @@ async function saveNoteIfChanged() { return; } - const note = globalCurrentNote; + const note = glob.currentNote; updateNoteFromInputs(note); @@ -103,11 +103,11 @@ async function saveNoteToServer(note) { message("Saved!"); } -let globalCurrentNote; -let globalCurrentNoteLoadTime; +glob.currentNote = null; +glob.currentNoteLoadTime = null; function createNewTopLevelNote() { - let rootNode = globalTree.fancytree("getRootNode"); + let rootNode = glob.tree.fancytree("getRootNode"); createNote(rootNode, "root", "into"); } @@ -144,7 +144,7 @@ async function createNote(node, parentKey, target, encryption) { extraClasses: encryption ? "encrypted" : "" }; - globalAllNoteIds.push(result.note_id); + glob.allNoteIds.push(result.note_id); newNoteCreated = true; @@ -183,8 +183,8 @@ function setNoteBackgroundIfEncrypted(note) { async function loadNoteToEditor(noteId) { const note = await $.get(baseApiUrl + 'notes/' + noteId); - globalCurrentNote = note; - globalCurrentNoteLoadTime = Math.floor(new Date().getTime() / 1000); + glob.currentNote = note; + glob.currentNoteLoadTime = Math.floor(new Date().getTime() / 1000); if (newNoteCreated) { newNoteCreated = false; diff --git a/public/javascripts/note_history.js b/public/javascripts/note_history.js index 8f0345d87..7764ce89a 100644 --- a/public/javascripts/note_history.js +++ b/public/javascripts/note_history.js @@ -1,7 +1,7 @@ -let globalHistoryItems = null; +glob.historyItems = null; function showCurrentNoteHistory() { - showNoteHistoryDialog(globalCurrentNote.detail.note_id); + showNoteHistoryDialog(glob.currentNote.detail.note_id); } function showNoteHistoryDialog(noteId, noteHistoryId) { @@ -18,7 +18,7 @@ function showNoteHistoryDialog(noteId, noteHistoryId) { url: baseApiUrl + 'notes-history/' + noteId, type: 'GET', success: result => { - globalHistoryItems = result; + glob.historyItems = result; for (const row of result) { const dateModified = getDateFromTS(row.date_modified_to); @@ -46,7 +46,7 @@ $(document).bind('keydown', 'alt+h', showCurrentNoteHistory); $("#note-history-list").on('change', () => { const optVal = $("#note-history-list").find(":selected").val(); - const historyItem = globalHistoryItems.find(r => r.note_history_id === optVal); + const historyItem = glob.historyItems.find(r => r.note_history_id === optVal); let noteTitle = historyItem.note_title; let noteText = historyItem.note_text; diff --git a/public/javascripts/recent_notes.js b/public/javascripts/recent_notes.js index 02586f93c..c0a6ad25f 100644 --- a/public/javascripts/recent_notes.js +++ b/public/javascripts/recent_notes.js @@ -1,13 +1,13 @@ -let globalRecentNotes = []; +glob.recentNotes = []; function addRecentNote(noteTreeId, noteContentId) { setTimeout(() => { // we include the note into recent list only if the user stayed on the note at least 5 seconds - if (noteTreeId === globalCurrentNote.detail.note_id || noteContentId === globalCurrentNote.detail.note_id) { + if (noteTreeId === glob.currentNote.detail.note_id || noteContentId === glob.currentNote.detail.note_id) { // if it's already there, remove the note - globalRecentNotes = globalRecentNotes.filter(note => note !== noteTreeId); + glob.recentNotes = glob.recentNotes.filter(note => note !== noteTreeId); - globalRecentNotes.unshift(noteTreeId); + glob.recentNotes.unshift(noteTreeId); } }, 1500); } @@ -25,7 +25,7 @@ function showRecentNotes() { recentNotesSelectBox.find('option').remove(); // remove the current note - let recNotes = globalRecentNotes.filter(note => note !== globalCurrentNote.detail.note_id); + let recNotes = glob.recentNotes.filter(note => note !== glob.currentNote.detail.note_id); $.each(recNotes, (key, valueNoteId) => { let noteTitle = getFullName(valueNoteId); diff --git a/public/javascripts/settings.js b/public/javascripts/settings.js index 0e6ca7240..d87d95d1f 100644 --- a/public/javascripts/settings.js +++ b/public/javascripts/settings.js @@ -44,7 +44,7 @@ $("#change-password-form").submit(() => { // encryption password changed so current encryption session is invalid and needs to be cleared resetEncryptionSession(); - globalEncryptedDataKey = result.new_encrypted_data_key; + glob.encryptedDataKey = result.new_encrypted_data_key; alert("Password has been changed."); @@ -74,7 +74,7 @@ $("#encryption-timeout-form").submit(() => { success: () => { alert("Encryption timeout has been changed."); - globalEncryptionSessionTimeout = encryptionTimeout; + glob.encryptionSessionTimeout = encryptionTimeout; }, error: () => alert("Error occurred during changing encryption timeout.") }); diff --git a/public/javascripts/status.js b/public/javascripts/status.js index b600579c3..6e4a0b9c4 100644 --- a/public/javascripts/status.js +++ b/public/javascripts/status.js @@ -4,9 +4,9 @@ async function checkStatus() { type: 'POST', contentType: "application/json", data: JSON.stringify({ - treeLoadTime: globalTreeLoadTime, - currentNoteId: globalCurrentNote ? globalCurrentNote.detail.note_id : null, - currentNoteDateModified: globalCurrentNoteLoadTime + treeLoadTime: glob.treeLoadTime, + currentNoteId: glob.currentNote ? glob.currentNote.detail.note_id : null, + currentNoteDateModified: glob.currentNoteLoadTime }), statusCode: { 401: () => { @@ -27,7 +27,7 @@ async function checkStatus() { console.log("Reloading tree because of background changes"); // this will also reload the note content - await globalTree.fancytree('getTree').reload(treeResp.notes); + await glob.tree.fancytree('getTree').reload(treeResp.notes); decryptTreeItems(); } diff --git a/public/javascripts/tree.js b/public/javascripts/tree.js index 3679d842b..e059d9420 100644 --- a/public/javascripts/tree.js +++ b/public/javascripts/tree.js @@ -40,15 +40,13 @@ const keybindings = { } }; -let globalAllNoteIds = []; - -const globalTree = $("#tree"); - -let globalClipboardNoteId = null; +glob.allNoteIds = []; +glob.tree = $("#tree"); +glob.clipboardNoteId = null; function prepareNoteTree(notes) { for (const note of notes) { - globalAllNoteIds.push(note.note_id); + glob.allNoteIds.push(note.note_id); if (note.encryption > 0) { note.title = "[encrypted]"; @@ -83,13 +81,13 @@ function setExpandedToServer(note_id, is_expanded) { }); } -let globalEncryptionSalt; -let globalEncryptionSessionTimeout; -let globalEncryptedDataKey; -let globalTreeLoadTime; +glob.encryptionSalt; +glob.encryptionSessionTimeout; +glob.encryptedDataKey; +glob.treeLoadTime; function initFancyTree(notes, startNoteId) { - globalTree.fancytree({ + glob.tree.fancytree({ autoScroll: true, extensions: ["hotkeys", "filter", "dnd"], source: notes, @@ -176,17 +174,17 @@ function initFancyTree(notes, startNoteId) { } }); - globalTree.contextmenu(contextMenuSetup); + glob.tree.contextmenu(contextMenuSetup); } function loadTree() { return $.get(baseApiUrl + 'tree').then(resp => { const notes = resp.notes; let startNoteId = resp.start_note_id; - globalEncryptionSalt = resp.password_derived_key_salt; - globalEncryptionSessionTimeout = resp.encryption_session_timeout; - globalEncryptedDataKey = resp.encrypted_data_key; - globalTreeLoadTime = resp.tree_load_time; + glob.encryptionSalt = resp.password_derived_key_salt; + glob.encryptionSessionTimeout = resp.encryption_session_timeout; + glob.encryptedDataKey = resp.encrypted_data_key; + glob.treeLoadTime = resp.tree_load_time; // add browser ID header to all AJAX requests $.ajaxSetup({ @@ -213,7 +211,7 @@ $(() => { }); function collapseTree() { - globalTree.fancytree("getRootNode").visit(node => { + glob.tree.fancytree("getRootNode").visit(node => { node.setExpanded(false); }); } @@ -221,7 +219,7 @@ function collapseTree() { $(document).bind('keydown', 'alt+c', collapseTree); function scrollToCurrentNote() { - const node = getNodeByKey(globalCurrentNote.detail.note_id); + const node = getNodeByKey(glob.currentNote.detail.note_id); if (node) { node.makeVisible({scrollIntoView: true}); @@ -252,7 +250,7 @@ function toggleSearch() { function resetSearch() { $("input[name=search]").val(""); - const tree = globalTree.fancytree("getTree"); + const tree = glob.tree.fancytree("getTree"); tree.clearFilter(); } @@ -271,7 +269,7 @@ $("input[name=search]").keyup(e => { console.log("search: ", resp); // Pass a string to perform case insensitive matching - const tree = globalTree.fancytree("getTree"); + const tree = glob.tree.fancytree("getTree"); tree.filterBranches(node => { return resp.includes(node.data.note_id); }); diff --git a/public/javascripts/tree_mutations.js b/public/javascripts/tree_mutations.js index cc7d74a03..e0c39da38 100644 --- a/public/javascripts/tree_mutations.js +++ b/public/javascripts/tree_mutations.js @@ -47,10 +47,10 @@ function deleteNode(node) { node.getParent().renderTitle(); } - globalAllNoteIds = globalAllNoteIds.filter(e => e !== node.key); + glob.allNoteIds = glob.allNoteIds.filter(e => e !== node.key); // remove from recent notes - globalRecentNotes = globalRecentNotes.filter(note => note !== node.key); + glob.recentNotes = glob.recentNotes.filter(note => note !== node.key); let next = node.getNextSibling(); if (!next) { diff --git a/public/javascripts/tree_utils.js b/public/javascripts/tree_utils.js index 2b419125f..a5cbcbce9 100644 --- a/public/javascripts/tree_utils.js +++ b/public/javascripts/tree_utils.js @@ -7,7 +7,7 @@ function getParentEncryption(node) { } function getNodeByKey(noteId) { - return globalTree.fancytree('getNodeByKey', noteId); + return glob.tree.fancytree('getNodeByKey', noteId); } function getNoteTitle(noteId) {