mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
initial decallbackization and promisification of frontend
This commit is contained in:
parent
ac0e5ada6e
commit
702d47e6b0
@ -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,27 +229,29 @@ function encryptNote(note) {
|
||||
return note;
|
||||
}
|
||||
|
||||
function encryptNoteAndSendToServer() {
|
||||
handleEncryption(true, true, () => {
|
||||
async function encryptNoteAndSendToServer() {
|
||||
await handleEncryption(true, true);
|
||||
|
||||
const note = globalCurrentNote;
|
||||
|
||||
updateNoteFromInputs(note);
|
||||
|
||||
encryptNote(note);
|
||||
|
||||
saveNoteToServer(note);
|
||||
await saveNoteToServer(note);
|
||||
|
||||
changeEncryptionOnNoteHistory(note.detail.note_id, true);
|
||||
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 => {
|
||||
error: () => alert("Error getting note history.")
|
||||
});
|
||||
|
||||
for (const row of result) {
|
||||
if (encrypt) {
|
||||
row.note_title = encryptString(row.note_title);
|
||||
@ -258,36 +264,32 @@ function changeEncryptionOnNoteHistory(noteId, encrypt) {
|
||||
|
||||
row.encryption = encrypt ? 1 : 0;
|
||||
|
||||
$.ajax({
|
||||
await $.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;
|
||||
console.log('Note history ' + row.note_history_id + ' de/encrypted');
|
||||
}
|
||||
},
|
||||
error: () => alert("Error getting note history.")
|
||||
});
|
||||
}
|
||||
|
||||
function decryptNoteAndSendToServer() {
|
||||
handleEncryption(true, true, () => {
|
||||
async function decryptNoteAndSendToServer() {
|
||||
await handleEncryption(true, true);
|
||||
|
||||
const note = globalCurrentNote;
|
||||
|
||||
updateNoteFromInputs(note);
|
||||
|
||||
note.detail.encryption = 0;
|
||||
|
||||
saveNoteToServer(note);
|
||||
await saveNoteToServer(note);
|
||||
|
||||
changeEncryptionOnNoteHistory(note.detail.note_id, false);
|
||||
await changeEncryptionOnNoteHistory(note.detail.note_id, false);
|
||||
|
||||
setNoteBackgroundIfEncrypted(note);
|
||||
});
|
||||
}
|
||||
|
||||
function decryptNoteIfNecessary(note) {
|
||||
@ -301,8 +303,9 @@ function decryptNote(note) {
|
||||
note.detail.note_text = decryptString(note.detail.note_text);
|
||||
}
|
||||
|
||||
function encryptSubTree(noteId) {
|
||||
handleEncryption(true, true, () => {
|
||||
async function encryptSubTree(noteId) {
|
||||
await handleEncryption(true, true);
|
||||
|
||||
updateSubTreeRecursively(noteId, note => {
|
||||
if (note.detail.encryption === null || note.detail.encryption === 0) {
|
||||
encryptNote(note);
|
||||
@ -325,11 +328,11 @@ function encryptSubTree(noteId) {
|
||||
});
|
||||
|
||||
alert("Encryption finished.");
|
||||
});
|
||||
}
|
||||
|
||||
function decryptSubTree(noteId) {
|
||||
handleEncryption(true, true, () => {
|
||||
async function decryptSubTree(noteId) {
|
||||
await handleEncryption(true, true);
|
||||
|
||||
updateSubTreeRecursively(noteId, note => {
|
||||
if (note.detail.encryption === 1) {
|
||||
decryptNote(note);
|
||||
@ -352,7 +355,6 @@ function decryptSubTree(noteId) {
|
||||
});
|
||||
|
||||
alert("Decryption finished.");
|
||||
});
|
||||
}
|
||||
|
||||
function updateSubTreeRecursively(noteId, updateCallback, successCallback) {
|
||||
|
@ -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)
|
||||
|
@ -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,8 +133,9 @@ function createNote(node, parentKey, target, encryption) {
|
||||
target_note_id: node.key,
|
||||
encryption: encryption
|
||||
}),
|
||||
contentType: "application/json",
|
||||
success: result => {
|
||||
contentType: "application/json"
|
||||
});
|
||||
|
||||
const newNode = {
|
||||
title: newNoteName,
|
||||
key: result.note_id,
|
||||
@ -168,8 +160,6 @@ function createNote(node, parentKey, target, encryption) {
|
||||
|
||||
message("Created!");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function setTreeBasedOnEncryption(note) {
|
||||
const node = getNodeByKey(note.detail.note_id);
|
||||
@ -191,8 +181,8 @@ function setNoteBackgroundIfEncrypted(note) {
|
||||
setTreeBasedOnEncryption(note);
|
||||
}
|
||||
|
||||
function loadNoteToEditor(noteId) {
|
||||
$.get(baseApiUrl + 'notes/' + noteId).then(note => {
|
||||
async function loadNoteToEditor(noteId) {
|
||||
const note = await $.get(baseApiUrl + 'notes/' + noteId);
|
||||
globalCurrentNote = note;
|
||||
globalCurrentNoteLoadTime = Math.floor(new Date().getTime() / 1000);
|
||||
|
||||
@ -202,7 +192,8 @@ function loadNoteToEditor(noteId) {
|
||||
$("#note-title").focus().select();
|
||||
}
|
||||
|
||||
handleEncryption(note.detail.encryption > 0, false, () => {
|
||||
await handleEncryption(note.detail.encryption > 0, false);
|
||||
|
||||
$("#note-detail-wrapper").show();
|
||||
|
||||
// this may fal if the dialog has not been previously opened
|
||||
@ -231,18 +222,16 @@ function loadNoteToEditor(noteId) {
|
||||
noteChangeDisabled = false;
|
||||
|
||||
setNoteBackgroundIfEncrypted(note);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function loadNote(noteId, callback) {
|
||||
$.get(baseApiUrl + 'notes/' + noteId).then(note => {
|
||||
async function loadNote(noteId) {
|
||||
const note = await $.get(baseApiUrl + 'notes/' + noteId);
|
||||
|
||||
if (note.detail.encryption > 0 && !isEncryptionAvailable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
decryptNoteIfNecessary(note);
|
||||
|
||||
callback(note);
|
||||
});
|
||||
return note;
|
||||
}
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user