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

View File

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

View File

@ -21,12 +21,8 @@ function noteChanged() {
isNoteChanged = true;
}
function saveNoteIfChanged(callback) {
async function saveNoteIfChanged() {
if (!isNoteChanged) {
if (callback) {
callback();
}
return;
}
@ -36,7 +32,7 @@ function saveNoteIfChanged(callback) {
encryptNoteIfNecessary(note);
saveNoteToServer(note, callback);
await saveNoteToServer(note);
}
setInterval(saveNoteIfChanged, 5000);
@ -91,25 +87,20 @@ function updateNoteFromInputs(note) {
note.detail.note_title = title;
}
function saveNoteToServer(note, callback) {
$.ajax({
async function saveNoteToServer(note) {
await $.ajax({
url: baseApiUrl + 'notes/' + note.detail.note_id,
type: 'PUT',
data: JSON.stringify(note),
contentType: "application/json",
success: () => {
isNoteChanged = false;
message("Saved!");
if (callback) {
callback();
}
},
error: () => {
error("Error saving the note!");
}
});
isNoteChanged = false;
message("Saved!");
}
let globalCurrentNote;
@ -123,7 +114,7 @@ function createNewTopLevelNote() {
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
// 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()) {
@ -133,7 +124,7 @@ function createNote(node, parentKey, target, encryption) {
const newNoteName = "new note";
const newNoteNameEncryptedIfNecessary = encryption > 0 ? encryptString(newNoteName) : newNoteName;
$.ajax({
const result = await $.ajax({
url: baseApiUrl + 'notes/' + parentKey + '/children' ,
type: 'POST',
data: JSON.stringify({
@ -142,33 +133,32 @@ function createNote(node, parentKey, target, encryption) {
target_note_id: node.key,
encryption: encryption
}),
contentType: "application/json",
success: result => {
const newNode = {
title: newNoteName,
key: result.note_id,
note_id: result.note_id,
encryption: encryption,
extraClasses: encryption ? "encrypted" : ""
};
globalAllNoteIds.push(result.note_id);
newNoteCreated = true;
if (target === 'after') {
node.appendSibling(newNode).setActive(true);
}
else {
node.addChildren(newNode).setActive(true);
node.folder = true;
node.renderTitle();
}
message("Created!");
}
contentType: "application/json"
});
const newNode = {
title: newNoteName,
key: result.note_id,
note_id: result.note_id,
encryption: encryption,
extraClasses: encryption ? "encrypted" : ""
};
globalAllNoteIds.push(result.note_id);
newNoteCreated = true;
if (target === 'after') {
node.appendSibling(newNode).setActive(true);
}
else {
node.addChildren(newNode).setActive(true);
node.folder = true;
node.renderTitle();
}
message("Created!");
}
function setTreeBasedOnEncryption(note) {
@ -191,58 +181,57 @@ function setNoteBackgroundIfEncrypted(note) {
setTreeBasedOnEncryption(note);
}
function loadNoteToEditor(noteId) {
$.get(baseApiUrl + 'notes/' + noteId).then(note => {
globalCurrentNote = note;
globalCurrentNoteLoadTime = Math.floor(new Date().getTime() / 1000);
async function loadNoteToEditor(noteId) {
const note = await $.get(baseApiUrl + 'notes/' + noteId);
globalCurrentNote = note;
globalCurrentNoteLoadTime = Math.floor(new Date().getTime() / 1000);
if (newNoteCreated) {
newNoteCreated = false;
if (newNoteCreated) {
newNoteCreated = false;
$("#note-title").focus().select();
}
$("#note-title").focus().select();
}
handleEncryption(note.detail.encryption > 0, false, () => {
$("#note-detail-wrapper").show();
await handleEncryption(note.detail.encryption > 0, false);
// this may fal if the dialog has not been previously opened
try {
$("#encryption-password-dialog").dialog('close');
}
catch(e) {}
$("#note-detail-wrapper").show();
$("#encryption-password").val('');
// this may fal if the dialog has not been previously opened
try {
$("#encryption-password-dialog").dialog('close');
}
catch(e) {}
decryptNoteIfNecessary(note);
$("#encryption-password").val('');
$("#note-title").val(note.detail.note_title);
decryptNoteIfNecessary(note);
noteChangeDisabled = true;
$("#note-title").val(note.detail.note_title);
// Clear contents and remove all stored history. This is to prevent undo from going across notes
$('#note-detail').summernote('reset');
noteChangeDisabled = true;
$('#note-detail').summernote('code', note.detail.note_text);
// Clear contents and remove all stored history. This is to prevent undo from going across notes
$('#note-detail').summernote('reset');
document.location.hash = noteId;
$('#note-detail').summernote('code', note.detail.note_text);
addRecentNote(noteId, note.detail.note_id);
document.location.hash = noteId;
noteChangeDisabled = false;
addRecentNote(noteId, note.detail.note_id);
setNoteBackgroundIfEncrypted(note);
});
});
noteChangeDisabled = false;
setNoteBackgroundIfEncrypted(note);
}
function loadNote(noteId, callback) {
$.get(baseApiUrl + 'notes/' + noteId).then(note => {
if (note.detail.encryption > 0 && !isEncryptionAvailable()) {
return;
}
async function loadNote(noteId) {
const note = await $.get(baseApiUrl + 'notes/' + noteId);
decryptNoteIfNecessary(note);
if (note.detail.encryption > 0 && !isEncryptionAvailable()) {
return;
}
callback(note);
});
decryptNoteIfNecessary(note);
return note;
}

View File

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