moved all global variables into glob object

This commit is contained in:
azivner 2017-11-04 00:05:08 -04:00
parent 3892666961
commit 44cfff09d9
13 changed files with 79 additions and 79 deletions

View File

@ -17,7 +17,7 @@ $(document).bind('keydown', 'alt+l', () => {
} }
$("#note-autocomplete").autocomplete({ $("#note-autocomplete").autocomplete({
source: getAutocompleteItems(globalAllNoteIds), source: getAutocompleteItems(glob.allNoteIds),
minLength: 0, minLength: 0,
change: () => { change: () => {
const val = $("#note-autocomplete").val(); const val = $("#note-autocomplete").val();

View File

@ -1,21 +1,21 @@
function pasteAfter(node) { function pasteAfter(node) {
const subjectNode = getNodeByKey(globalClipboardNoteId); const subjectNode = getNodeByKey(glob.clipboardNoteId);
moveAfterNode(subjectNode, node); moveAfterNode(subjectNode, node);
globalClipboardNoteId = null; glob.clipboardNoteId = null;
} }
function pasteInto(node) { function pasteInto(node) {
const subjectNode = getNodeByKey(globalClipboardNoteId); const subjectNode = getNodeByKey(glob.clipboardNoteId);
moveToNode(subjectNode, node); moveToNode(subjectNode, node);
globalClipboardNoteId = null; glob.clipboardNoteId = null;
} }
function cut(node) { function cut(node) {
globalClipboardNoteId = node.key; glob.clipboardNoteId = node.key;
} }
const contextMenuSetup = { const contextMenuSetup = {
@ -37,8 +37,8 @@ const contextMenuSetup = {
beforeOpen: (event, ui) => { beforeOpen: (event, ui) => {
const node = $.ui.fancytree.getNode(ui.target); const node = $.ui.fancytree.getNode(ui.target);
// Modify menu entries depending on node status // Modify menu entries depending on node status
globalTree.contextmenu("enableEntry", "pasteAfter", globalClipboardNoteId !== null); glob.tree.contextmenu("enableEntry", "pasteAfter", glob.clipboardNoteId !== null);
globalTree.contextmenu("enableEntry", "pasteInto", globalClipboardNoteId !== null); glob.tree.contextmenu("enableEntry", "pasteInto", glob.clipboardNoteId !== null);
// Activate node on right-click // Activate node on right-click
node.setActive(); node.setActive();

View File

@ -1,10 +1,10 @@
let globalEncryptionDeferred = null; glob.encryptionDeferred = null;
function handleEncryption(requireEncryption, modal) { function handleEncryption(requireEncryption, modal) {
const dfd = $.Deferred(); const dfd = $.Deferred();
if (requireEncryption && globalDataKey === null) { if (requireEncryption && glob.dataKey === null) {
globalEncryptionDeferred = dfd; glob.encryptionDeferred = dfd;
$("#encryption-password-dialog").dialog({ $("#encryption-password-dialog").dialog({
modal: modal, modal: modal,
@ -12,7 +12,7 @@ function handleEncryption(requireEncryption, modal) {
open: () => { open: () => {
if (!modal) { if (!modal) {
// dialog steals focus for itself, which is not what we want for non-modal (viewing) // 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(); return dfd.promise();
} }
let globalDataKey = null; glob.dataKey = null;
let globalLastEncryptionOperationDate = null; glob.lastEncryptionOperationDate = null;
function getDataKey(password) { function getDataKey(password) {
return computeScrypt(password, globalEncryptionSalt, (key, resolve, reject) => { return computeScrypt(password, glob.encryptionSalt, (key, resolve, reject) => {
const dataKeyAes = getDataKeyAes(key); const dataKeyAes = getDataKeyAes(key);
const decryptedDataKey = decrypt(dataKeyAes, globalEncryptedDataKey); const decryptedDataKey = decrypt(dataKeyAes, glob.encryptedDataKey);
if (decryptedDataKey === false) { if (decryptedDataKey === false) {
reject("Wrong password."); reject("Wrong password.");
@ -77,7 +77,7 @@ function decryptTreeItems() {
return; return;
} }
for (const noteId of globalAllNoteIds) { for (const noteId of glob.allNoteIds) {
const note = getNodeByKey(noteId); const note = getNodeByKey(noteId);
if (note.data.encryption > 0) { if (note.data.encryption > 0) {
@ -95,14 +95,14 @@ $("#encryption-password-form").submit(() => {
getDataKey(password).then(key => { getDataKey(password).then(key => {
$("#encryption-password-dialog").dialog("close"); $("#encryption-password-dialog").dialog("close");
globalDataKey = key; glob.dataKey = key;
decryptTreeItems(); decryptTreeItems();
if (globalEncryptionDeferred !== null) { if (glob.encryptionDeferred !== null) {
globalEncryptionDeferred.resolve(); glob.encryptionDeferred.resolve();
globalEncryptionDeferred = null; glob.encryptionDeferred = null;
} }
}) })
.catch(reason => { .catch(reason => {
@ -115,12 +115,12 @@ $("#encryption-password-form").submit(() => {
}); });
function resetEncryptionSession() { function resetEncryptionSession() {
globalDataKey = null; glob.dataKey = null;
if (globalCurrentNote.detail.encryption > 0) { if (glob.currentNote.detail.encryption > 0) {
loadNoteToEditor(globalCurrentNote.detail.note_id); loadNoteToEditor(glob.currentNote.detail.note_id);
for (const noteId of globalAllNoteIds) { for (const noteId of glob.allNoteIds) {
const note = getNodeByKey(noteId); const note = getNodeByKey(noteId);
if (note.data.encryption > 0) { if (note.data.encryption > 0) {
@ -131,19 +131,19 @@ function resetEncryptionSession() {
} }
setInterval(() => { 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(); resetEncryptionSession();
} }
}, 5000); }, 5000);
function isEncryptionAvailable() { function isEncryptionAvailable() {
return globalDataKey !== null; return glob.dataKey !== null;
} }
function getDataAes() { 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) { function getDataKeyAes(key) {
@ -240,7 +240,7 @@ function encryptNote(note) {
async function encryptNoteAndSendToServer() { async function encryptNoteAndSendToServer() {
await handleEncryption(true, true); await handleEncryption(true, true);
const note = globalCurrentNote; const note = glob.currentNote;
updateNoteFromInputs(note); updateNoteFromInputs(note);
@ -287,7 +287,7 @@ async function changeEncryptionOnNoteHistory(noteId, encrypt) {
async function decryptNoteAndSendToServer() { async function decryptNoteAndSendToServer() {
await handleEncryption(true, true); await handleEncryption(true, true);
const note = globalCurrentNote; const note = glob.currentNote;
updateNoteFromInputs(note); updateNoteFromInputs(note);
@ -327,7 +327,7 @@ async function encryptSubTree(noteId) {
} }
}, },
note => { 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); loadNoteToEditor(note.detail.note_id);
} }
else { else {
@ -354,7 +354,7 @@ async function decryptSubTree(noteId) {
} }
}, },
note => { 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); loadNoteToEditor(note.detail.note_id);
} }
else { else {

View File

@ -1,3 +1,5 @@
glob = {};
// hot keys are active also inside inputs and content editables // hot keys are active also inside inputs and content editables
jQuery.hotkeys.options.filterInputAcceptingElements = true; jQuery.hotkeys.options.filterInputAcceptingElements = true;
jQuery.hotkeys.options.filterContentEditable = true; jQuery.hotkeys.options.filterContentEditable = true;

View File

@ -7,7 +7,7 @@ function showJumpToNote() {
}); });
$("#jump-to-note-autocomplete").autocomplete({ $("#jump-to-note-autocomplete").autocomplete({
source: getAutocompleteItems(globalAllNoteIds), source: getAutocompleteItems(glob.allNoteIds),
minLength: 0 minLength: 0
}); });
} }

View File

@ -26,7 +26,7 @@ async function saveNoteIfChanged() {
return; return;
} }
const note = globalCurrentNote; const note = glob.currentNote;
updateNoteFromInputs(note); updateNoteFromInputs(note);
@ -103,11 +103,11 @@ async function saveNoteToServer(note) {
message("Saved!"); message("Saved!");
} }
let globalCurrentNote; glob.currentNote = null;
let globalCurrentNoteLoadTime; glob.currentNoteLoadTime = null;
function createNewTopLevelNote() { function createNewTopLevelNote() {
let rootNode = globalTree.fancytree("getRootNode"); let rootNode = glob.tree.fancytree("getRootNode");
createNote(rootNode, "root", "into"); createNote(rootNode, "root", "into");
} }
@ -144,7 +144,7 @@ async function createNote(node, parentKey, target, encryption) {
extraClasses: encryption ? "encrypted" : "" extraClasses: encryption ? "encrypted" : ""
}; };
globalAllNoteIds.push(result.note_id); glob.allNoteIds.push(result.note_id);
newNoteCreated = true; newNoteCreated = true;
@ -183,8 +183,8 @@ function setNoteBackgroundIfEncrypted(note) {
async function loadNoteToEditor(noteId) { async function loadNoteToEditor(noteId) {
const note = await $.get(baseApiUrl + 'notes/' + noteId); const note = await $.get(baseApiUrl + 'notes/' + noteId);
globalCurrentNote = note; glob.currentNote = note;
globalCurrentNoteLoadTime = Math.floor(new Date().getTime() / 1000); glob.currentNoteLoadTime = Math.floor(new Date().getTime() / 1000);
if (newNoteCreated) { if (newNoteCreated) {
newNoteCreated = false; newNoteCreated = false;

View File

@ -1,7 +1,7 @@
let globalHistoryItems = null; glob.historyItems = null;
function showCurrentNoteHistory() { function showCurrentNoteHistory() {
showNoteHistoryDialog(globalCurrentNote.detail.note_id); showNoteHistoryDialog(glob.currentNote.detail.note_id);
} }
function showNoteHistoryDialog(noteId, noteHistoryId) { function showNoteHistoryDialog(noteId, noteHistoryId) {
@ -18,7 +18,7 @@ function showNoteHistoryDialog(noteId, noteHistoryId) {
url: baseApiUrl + 'notes-history/' + noteId, url: baseApiUrl + 'notes-history/' + noteId,
type: 'GET', type: 'GET',
success: result => { success: result => {
globalHistoryItems = result; glob.historyItems = result;
for (const row of result) { for (const row of result) {
const dateModified = getDateFromTS(row.date_modified_to); const dateModified = getDateFromTS(row.date_modified_to);
@ -46,7 +46,7 @@ $(document).bind('keydown', 'alt+h', showCurrentNoteHistory);
$("#note-history-list").on('change', () => { $("#note-history-list").on('change', () => {
const optVal = $("#note-history-list").find(":selected").val(); 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 noteTitle = historyItem.note_title;
let noteText = historyItem.note_text; let noteText = historyItem.note_text;

View File

@ -1,13 +1,13 @@
let globalRecentNotes = []; glob.recentNotes = [];
function addRecentNote(noteTreeId, noteContentId) { function addRecentNote(noteTreeId, noteContentId) {
setTimeout(() => { setTimeout(() => {
// we include the note into recent list only if the user stayed on the note at least 5 seconds // 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 // 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); }, 1500);
} }
@ -25,7 +25,7 @@ function showRecentNotes() {
recentNotesSelectBox.find('option').remove(); recentNotesSelectBox.find('option').remove();
// remove the current note // 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) => { $.each(recNotes, (key, valueNoteId) => {
let noteTitle = getFullName(valueNoteId); let noteTitle = getFullName(valueNoteId);

View File

@ -44,7 +44,7 @@ $("#change-password-form").submit(() => {
// encryption password changed so current encryption session is invalid and needs to be cleared // encryption password changed so current encryption session is invalid and needs to be cleared
resetEncryptionSession(); resetEncryptionSession();
globalEncryptedDataKey = result.new_encrypted_data_key; glob.encryptedDataKey = result.new_encrypted_data_key;
alert("Password has been changed."); alert("Password has been changed.");
@ -74,7 +74,7 @@ $("#encryption-timeout-form").submit(() => {
success: () => { success: () => {
alert("Encryption timeout has been changed."); alert("Encryption timeout has been changed.");
globalEncryptionSessionTimeout = encryptionTimeout; glob.encryptionSessionTimeout = encryptionTimeout;
}, },
error: () => alert("Error occurred during changing encryption timeout.") error: () => alert("Error occurred during changing encryption timeout.")
}); });

View File

@ -4,9 +4,9 @@ async function checkStatus() {
type: 'POST', type: 'POST',
contentType: "application/json", contentType: "application/json",
data: JSON.stringify({ data: JSON.stringify({
treeLoadTime: globalTreeLoadTime, treeLoadTime: glob.treeLoadTime,
currentNoteId: globalCurrentNote ? globalCurrentNote.detail.note_id : null, currentNoteId: glob.currentNote ? glob.currentNote.detail.note_id : null,
currentNoteDateModified: globalCurrentNoteLoadTime currentNoteDateModified: glob.currentNoteLoadTime
}), }),
statusCode: { statusCode: {
401: () => { 401: () => {
@ -27,7 +27,7 @@ async function checkStatus() {
console.log("Reloading tree because of background changes"); console.log("Reloading tree because of background changes");
// this will also reload the note content // this will also reload the note content
await globalTree.fancytree('getTree').reload(treeResp.notes); await glob.tree.fancytree('getTree').reload(treeResp.notes);
decryptTreeItems(); decryptTreeItems();
} }

View File

@ -40,15 +40,13 @@ const keybindings = {
} }
}; };
let globalAllNoteIds = []; glob.allNoteIds = [];
glob.tree = $("#tree");
const globalTree = $("#tree"); glob.clipboardNoteId = null;
let globalClipboardNoteId = null;
function prepareNoteTree(notes) { function prepareNoteTree(notes) {
for (const note of notes) { for (const note of notes) {
globalAllNoteIds.push(note.note_id); glob.allNoteIds.push(note.note_id);
if (note.encryption > 0) { if (note.encryption > 0) {
note.title = "[encrypted]"; note.title = "[encrypted]";
@ -83,13 +81,13 @@ function setExpandedToServer(note_id, is_expanded) {
}); });
} }
let globalEncryptionSalt; glob.encryptionSalt;
let globalEncryptionSessionTimeout; glob.encryptionSessionTimeout;
let globalEncryptedDataKey; glob.encryptedDataKey;
let globalTreeLoadTime; glob.treeLoadTime;
function initFancyTree(notes, startNoteId) { function initFancyTree(notes, startNoteId) {
globalTree.fancytree({ glob.tree.fancytree({
autoScroll: true, autoScroll: true,
extensions: ["hotkeys", "filter", "dnd"], extensions: ["hotkeys", "filter", "dnd"],
source: notes, source: notes,
@ -176,17 +174,17 @@ function initFancyTree(notes, startNoteId) {
} }
}); });
globalTree.contextmenu(contextMenuSetup); glob.tree.contextmenu(contextMenuSetup);
} }
function loadTree() { function loadTree() {
return $.get(baseApiUrl + 'tree').then(resp => { return $.get(baseApiUrl + 'tree').then(resp => {
const notes = resp.notes; const notes = resp.notes;
let startNoteId = resp.start_note_id; let startNoteId = resp.start_note_id;
globalEncryptionSalt = resp.password_derived_key_salt; glob.encryptionSalt = resp.password_derived_key_salt;
globalEncryptionSessionTimeout = resp.encryption_session_timeout; glob.encryptionSessionTimeout = resp.encryption_session_timeout;
globalEncryptedDataKey = resp.encrypted_data_key; glob.encryptedDataKey = resp.encrypted_data_key;
globalTreeLoadTime = resp.tree_load_time; glob.treeLoadTime = resp.tree_load_time;
// add browser ID header to all AJAX requests // add browser ID header to all AJAX requests
$.ajaxSetup({ $.ajaxSetup({
@ -213,7 +211,7 @@ $(() => {
}); });
function collapseTree() { function collapseTree() {
globalTree.fancytree("getRootNode").visit(node => { glob.tree.fancytree("getRootNode").visit(node => {
node.setExpanded(false); node.setExpanded(false);
}); });
} }
@ -221,7 +219,7 @@ function collapseTree() {
$(document).bind('keydown', 'alt+c', collapseTree); $(document).bind('keydown', 'alt+c', collapseTree);
function scrollToCurrentNote() { function scrollToCurrentNote() {
const node = getNodeByKey(globalCurrentNote.detail.note_id); const node = getNodeByKey(glob.currentNote.detail.note_id);
if (node) { if (node) {
node.makeVisible({scrollIntoView: true}); node.makeVisible({scrollIntoView: true});
@ -252,7 +250,7 @@ function toggleSearch() {
function resetSearch() { function resetSearch() {
$("input[name=search]").val(""); $("input[name=search]").val("");
const tree = globalTree.fancytree("getTree"); const tree = glob.tree.fancytree("getTree");
tree.clearFilter(); tree.clearFilter();
} }
@ -271,7 +269,7 @@ $("input[name=search]").keyup(e => {
console.log("search: ", resp); console.log("search: ", resp);
// Pass a string to perform case insensitive matching // Pass a string to perform case insensitive matching
const tree = globalTree.fancytree("getTree"); const tree = glob.tree.fancytree("getTree");
tree.filterBranches(node => { tree.filterBranches(node => {
return resp.includes(node.data.note_id); return resp.includes(node.data.note_id);
}); });

View File

@ -47,10 +47,10 @@ function deleteNode(node) {
node.getParent().renderTitle(); node.getParent().renderTitle();
} }
globalAllNoteIds = globalAllNoteIds.filter(e => e !== node.key); glob.allNoteIds = glob.allNoteIds.filter(e => e !== node.key);
// remove from recent notes // remove from recent notes
globalRecentNotes = globalRecentNotes.filter(note => note !== node.key); glob.recentNotes = glob.recentNotes.filter(note => note !== node.key);
let next = node.getNextSibling(); let next = node.getNextSibling();
if (!next) { if (!next) {

View File

@ -7,7 +7,7 @@ function getParentEncryption(node) {
} }
function getNodeByKey(noteId) { function getNodeByKey(noteId) {
return globalTree.fancytree('getNodeByKey', noteId); return glob.tree.fancytree('getNodeByKey', noteId);
} }
function getNoteTitle(noteId) { function getNoteTitle(noteId) {