initial decallbackization and promisification of frontend

This commit is contained in:
azivner 2017-11-03 20:01:32 -04:00
parent ac0e5ada6e
commit 702d47e6b0
4 changed files with 164 additions and 175 deletions

View File

@ -1,8 +1,10 @@
let globalEncryptionCallback = null; let globalEncryptionDeferred = null;
function handleEncryption(requireEncryption, modal) {
const dfd = $.Deferred();
function handleEncryption(requireEncryption, modal, callback) {
if (requireEncryption && globalDataKey === null) { if (requireEncryption && globalDataKey === null) {
globalEncryptionCallback = callback; globalEncryptionDeferred = dfd;
$("#encryption-password-dialog").dialog({ $("#encryption-password-dialog").dialog({
modal: modal, modal: modal,
@ -16,8 +18,10 @@ function handleEncryption(requireEncryption, modal, callback) {
}); });
} }
else { else {
callback(); dfd.resolve();
} }
return dfd.promise();
} }
let globalDataKey = null; let globalDataKey = null;
@ -87,10 +91,10 @@ $("#encryption-password-form").submit(() => {
} }
} }
if (globalEncryptionCallback !== null) { if (globalEncryptionDeferred !== null) {
globalEncryptionCallback(); globalEncryptionDeferred.resolve();
globalEncryptionCallback = null; globalEncryptionDeferred = null;
} }
}) })
.catch(reason => { .catch(reason => {
@ -225,27 +229,29 @@ function encryptNote(note) {
return note; return note;
} }
function encryptNoteAndSendToServer() { async function encryptNoteAndSendToServer() {
handleEncryption(true, true, () => { await handleEncryption(true, true);
const note = globalCurrentNote; const note = globalCurrentNote;
updateNoteFromInputs(note); updateNoteFromInputs(note);
encryptNote(note); encryptNote(note);
saveNoteToServer(note); await saveNoteToServer(note);
changeEncryptionOnNoteHistory(note.detail.note_id, true); await changeEncryptionOnNoteHistory(note.detail.note_id, true);
setNoteBackgroundIfEncrypted(note); setNoteBackgroundIfEncrypted(note);
});
} }
function changeEncryptionOnNoteHistory(noteId, encrypt) { async function changeEncryptionOnNoteHistory(noteId, encrypt) {
$.ajax({ const result = await $.ajax({
url: baseApiUrl + 'notes-history/' + noteId + "?encryption=" + (encrypt ? 0 : 1), url: baseApiUrl + 'notes-history/' + noteId + "?encryption=" + (encrypt ? 0 : 1),
type: 'GET', type: 'GET',
success: result => { error: () => alert("Error getting note history.")
});
for (const row of result) { for (const row of result) {
if (encrypt) { if (encrypt) {
row.note_title = encryptString(row.note_title); row.note_title = encryptString(row.note_title);
@ -258,36 +264,32 @@ function changeEncryptionOnNoteHistory(noteId, encrypt) {
row.encryption = encrypt ? 1 : 0; row.encryption = encrypt ? 1 : 0;
$.ajax({ await $.ajax({
url: baseApiUrl + 'notes-history', url: baseApiUrl + 'notes-history',
type: 'PUT', type: 'PUT',
contentType: 'application/json', contentType: 'application/json',
data: JSON.stringify(row), data: JSON.stringify(row),
success: result => console.log('Note history ' + row.note_history_id + ' de/encrypted'),
error: () => alert("Error de/encrypting note history.") error: () => alert("Error de/encrypting note history.")
}); });
break; console.log('Note history ' + row.note_history_id + ' de/encrypted');
} }
},
error: () => alert("Error getting note history.")
});
} }
function decryptNoteAndSendToServer() { async function decryptNoteAndSendToServer() {
handleEncryption(true, true, () => { await handleEncryption(true, true);
const note = globalCurrentNote; const note = globalCurrentNote;
updateNoteFromInputs(note); updateNoteFromInputs(note);
note.detail.encryption = 0; note.detail.encryption = 0;
saveNoteToServer(note); await saveNoteToServer(note);
changeEncryptionOnNoteHistory(note.detail.note_id, false); await changeEncryptionOnNoteHistory(note.detail.note_id, false);
setNoteBackgroundIfEncrypted(note); setNoteBackgroundIfEncrypted(note);
});
} }
function decryptNoteIfNecessary(note) { function decryptNoteIfNecessary(note) {
@ -301,8 +303,9 @@ function decryptNote(note) {
note.detail.note_text = decryptString(note.detail.note_text); note.detail.note_text = decryptString(note.detail.note_text);
} }
function encryptSubTree(noteId) { async function encryptSubTree(noteId) {
handleEncryption(true, true, () => { await handleEncryption(true, true);
updateSubTreeRecursively(noteId, note => { updateSubTreeRecursively(noteId, note => {
if (note.detail.encryption === null || note.detail.encryption === 0) { if (note.detail.encryption === null || note.detail.encryption === 0) {
encryptNote(note); encryptNote(note);
@ -325,11 +328,11 @@ function encryptSubTree(noteId) {
}); });
alert("Encryption finished."); alert("Encryption finished.");
});
} }
function decryptSubTree(noteId) { async function decryptSubTree(noteId) {
handleEncryption(true, true, () => { await handleEncryption(true, true);
updateSubTreeRecursively(noteId, note => { updateSubTreeRecursively(noteId, note => {
if (note.detail.encryption === 1) { if (note.detail.encryption === 1) {
decryptNote(note); decryptNote(note);
@ -352,7 +355,6 @@ function decryptSubTree(noteId) {
}); });
alert("Decryption finished."); alert("Decryption finished.");
});
} }
function updateSubTreeRecursively(noteId, updateCallback, successCallback) { function updateSubTreeRecursively(noteId, updateCallback, successCallback) {

View File

@ -61,9 +61,7 @@ $(document).tooltip({
const noteId = getNoteIdFromLink($(this).attr("href")); const noteId = getNoteIdFromLink($(this).attr("href"));
if (noteId !== null) { if (noteId !== null) {
loadNote(noteId, note => { loadNote(noteId).then(note => callback(note.detail.note_text));
callback(note.detail.note_text);
});
} }
}, },
close: function(event, ui) close: function(event, ui)

View File

@ -21,12 +21,8 @@ function noteChanged() {
isNoteChanged = true; isNoteChanged = true;
} }
function saveNoteIfChanged(callback) { async function saveNoteIfChanged() {
if (!isNoteChanged) { if (!isNoteChanged) {
if (callback) {
callback();
}
return; return;
} }
@ -36,7 +32,7 @@ function saveNoteIfChanged(callback) {
encryptNoteIfNecessary(note); encryptNoteIfNecessary(note);
saveNoteToServer(note, callback); await saveNoteToServer(note);
} }
setInterval(saveNoteIfChanged, 5000); setInterval(saveNoteIfChanged, 5000);
@ -91,25 +87,20 @@ function updateNoteFromInputs(note) {
note.detail.note_title = title; note.detail.note_title = title;
} }
function saveNoteToServer(note, callback) { async function saveNoteToServer(note) {
$.ajax({ await $.ajax({
url: baseApiUrl + 'notes/' + note.detail.note_id, url: baseApiUrl + 'notes/' + note.detail.note_id,
type: 'PUT', type: 'PUT',
data: JSON.stringify(note), data: JSON.stringify(note),
contentType: "application/json", contentType: "application/json",
success: () => {
isNoteChanged = false;
message("Saved!");
if (callback) {
callback();
}
},
error: () => { error: () => {
error("Error saving the note!"); error("Error saving the note!");
} }
}); });
isNoteChanged = false;
message("Saved!");
} }
let globalCurrentNote; let globalCurrentNote;
@ -123,7 +114,7 @@ function createNewTopLevelNote() {
let newNoteCreated = false; let newNoteCreated = false;
function createNote(node, parentKey, target, encryption) { async function createNote(node, parentKey, target, encryption) {
// if encryption isn't available (user didn't enter password yet), then note is created as unencrypted // if encryption isn't available (user didn't enter password yet), then note is created as unencrypted
// but this is quite weird since user doesn't see where the note is being created so it shouldn't occur often // but this is quite weird since user doesn't see where the note is being created so it shouldn't occur often
if (!encryption || !isEncryptionAvailable()) { if (!encryption || !isEncryptionAvailable()) {
@ -133,7 +124,7 @@ function createNote(node, parentKey, target, encryption) {
const newNoteName = "new note"; const newNoteName = "new note";
const newNoteNameEncryptedIfNecessary = encryption > 0 ? encryptString(newNoteName) : newNoteName; const newNoteNameEncryptedIfNecessary = encryption > 0 ? encryptString(newNoteName) : newNoteName;
$.ajax({ const result = await $.ajax({
url: baseApiUrl + 'notes/' + parentKey + '/children' , url: baseApiUrl + 'notes/' + parentKey + '/children' ,
type: 'POST', type: 'POST',
data: JSON.stringify({ data: JSON.stringify({
@ -142,8 +133,9 @@ function createNote(node, parentKey, target, encryption) {
target_note_id: node.key, target_note_id: node.key,
encryption: encryption encryption: encryption
}), }),
contentType: "application/json", contentType: "application/json"
success: result => { });
const newNode = { const newNode = {
title: newNoteName, title: newNoteName,
key: result.note_id, key: result.note_id,
@ -168,8 +160,6 @@ function createNote(node, parentKey, target, encryption) {
message("Created!"); message("Created!");
} }
});
}
function setTreeBasedOnEncryption(note) { function setTreeBasedOnEncryption(note) {
const node = getNodeByKey(note.detail.note_id); const node = getNodeByKey(note.detail.note_id);
@ -191,8 +181,8 @@ function setNoteBackgroundIfEncrypted(note) {
setTreeBasedOnEncryption(note); setTreeBasedOnEncryption(note);
} }
function loadNoteToEditor(noteId) { async function loadNoteToEditor(noteId) {
$.get(baseApiUrl + 'notes/' + noteId).then(note => { const note = await $.get(baseApiUrl + 'notes/' + noteId);
globalCurrentNote = note; globalCurrentNote = note;
globalCurrentNoteLoadTime = Math.floor(new Date().getTime() / 1000); globalCurrentNoteLoadTime = Math.floor(new Date().getTime() / 1000);
@ -202,7 +192,8 @@ function loadNoteToEditor(noteId) {
$("#note-title").focus().select(); $("#note-title").focus().select();
} }
handleEncryption(note.detail.encryption > 0, false, () => { await handleEncryption(note.detail.encryption > 0, false);
$("#note-detail-wrapper").show(); $("#note-detail-wrapper").show();
// this may fal if the dialog has not been previously opened // this may fal if the dialog has not been previously opened
@ -231,18 +222,16 @@ function loadNoteToEditor(noteId) {
noteChangeDisabled = false; noteChangeDisabled = false;
setNoteBackgroundIfEncrypted(note); setNoteBackgroundIfEncrypted(note);
});
});
} }
function loadNote(noteId, callback) { async function loadNote(noteId) {
$.get(baseApiUrl + 'notes/' + noteId).then(note => { const note = await $.get(baseApiUrl + 'notes/' + noteId);
if (note.detail.encryption > 0 && !isEncryptionAvailable()) { if (note.detail.encryption > 0 && !isEncryptionAvailable()) {
return; return;
} }
decryptNoteIfNecessary(note); decryptNoteIfNecessary(note);
callback(note); return note;
});
} }

View File

@ -97,7 +97,7 @@ function initFancyTree(notes, startNoteId) {
activate: (event, data) => { activate: (event, data) => {
const node = data.node.data; const node = data.node.data;
saveNoteIfChanged(() => loadNoteToEditor(node.note_id)); saveNoteIfChanged().then(() => loadNoteToEditor(node.note_id));
}, },
expand: (event, data) => { expand: (event, data) => {
setExpandedToServer(data.node.key, true); setExpandedToServer(data.node.key, true);