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({
source: getAutocompleteItems(globalAllNoteIds),
source: getAutocompleteItems(glob.allNoteIds),
minLength: 0,
change: () => {
const val = $("#note-autocomplete").val();

View file

@ -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();

View file

@ -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 {

View file

@ -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;

View file

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

View file

@ -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;

View file

@ -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;

View file

@ -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);

View file

@ -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.")
});

View file

@ -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();
}

View file

@ -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);
});

View file

@ -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) {

View file

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