2018-03-25 13:41:29 -04:00
|
|
|
import treeService from './tree.js';
|
2018-03-25 13:02:39 -04:00
|
|
|
import noteDetail from './note_detail.js';
|
2018-03-25 11:09:17 -04:00
|
|
|
import utils from './utils.js';
|
|
|
|
import server from './server.js';
|
2018-03-25 21:16:57 -04:00
|
|
|
import protectedSessionHolder from './protected_session_holder.js';
|
2018-03-25 21:29:35 -04:00
|
|
|
import infoService from "./info.js";
|
2018-03-25 11:09:17 -04:00
|
|
|
|
|
|
|
const $dialog = $("#protected-session-password-dialog");
|
|
|
|
const $passwordForm = $("#protected-session-password-form");
|
|
|
|
const $password = $("#protected-session-password");
|
|
|
|
const $noteDetailWrapper = $("#note-detail-wrapper");
|
|
|
|
const $protectButton = $("#protect-button");
|
|
|
|
const $unprotectButton = $("#unprotect-button");
|
|
|
|
|
|
|
|
let protectedSessionDeferred = null;
|
|
|
|
|
|
|
|
function ensureProtectedSession(requireProtectedSession, modal) {
|
|
|
|
const dfd = $.Deferred();
|
|
|
|
|
2018-03-25 21:16:57 -04:00
|
|
|
if (requireProtectedSession && !protectedSessionHolder.isProtectedSessionAvailable()) {
|
2018-03-25 11:09:17 -04:00
|
|
|
protectedSessionDeferred = dfd;
|
|
|
|
|
|
|
|
if (treeService.getCurrentNode().data.isProtected) {
|
|
|
|
$noteDetailWrapper.hide();
|
|
|
|
}
|
2017-11-04 19:57:40 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
$dialog.dialog({
|
|
|
|
modal: modal,
|
|
|
|
width: 400,
|
|
|
|
open: () => {
|
|
|
|
if (!modal) {
|
|
|
|
// dialog steals focus for itself, which is not what we want for non-modal (viewing)
|
|
|
|
treeService.getCurrentNode().setFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dfd.resolve();
|
2017-09-06 22:06:43 -04:00
|
|
|
}
|
2017-11-03 20:01:32 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
return dfd.promise();
|
|
|
|
}
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
async function setupProtectedSession() {
|
|
|
|
const password = $password.val();
|
|
|
|
$password.val("");
|
2018-01-26 22:32:44 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
const response = await enterProtectedSession(password);
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (!response.success) {
|
2018-03-25 21:29:35 -04:00
|
|
|
infoService.showError("Wrong password.");
|
2018-03-25 11:09:17 -04:00
|
|
|
return;
|
2017-11-04 18:18:55 -04:00
|
|
|
}
|
2017-09-08 20:55:24 -04:00
|
|
|
|
2018-03-25 21:16:57 -04:00
|
|
|
protectedSessionHolder.setProtectedSessionId(response.protectedSessionId);
|
2017-11-10 22:55:19 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
$dialog.dialog("close");
|
2017-09-06 23:13:39 -04:00
|
|
|
|
2018-03-25 13:02:39 -04:00
|
|
|
noteDetail.reload();
|
2018-03-25 11:09:17 -04:00
|
|
|
treeService.reload();
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (protectedSessionDeferred !== null) {
|
|
|
|
ensureDialogIsClosed($dialog, $password);
|
2017-09-17 12:46:14 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
$noteDetailWrapper.show();
|
2017-11-14 23:01:23 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
protectedSessionDeferred.resolve();
|
2018-01-26 22:32:44 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
protectedSessionDeferred = null;
|
2017-11-08 22:33:08 -05:00
|
|
|
}
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
function ensureDialogIsClosed() {
|
|
|
|
// this may fal if the dialog has not been previously opened
|
|
|
|
try {
|
|
|
|
$dialog.dialog('close');
|
2017-11-15 00:10:11 -05:00
|
|
|
}
|
2018-03-25 11:09:17 -04:00
|
|
|
catch (e) {}
|
2017-11-15 00:10:11 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
$password.val('');
|
|
|
|
}
|
2017-11-10 22:55:19 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
async function enterProtectedSession(password) {
|
|
|
|
return await server.post('login/protected', {
|
|
|
|
password: password
|
|
|
|
});
|
|
|
|
}
|
2017-11-10 22:55:19 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
async function protectNoteAndSendToServer() {
|
|
|
|
await ensureProtectedSession(true, true);
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2018-03-25 13:02:39 -04:00
|
|
|
const note = noteDetail.getCurrentNote();
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2018-03-25 13:02:39 -04:00
|
|
|
noteDetail.updateNoteFromInputs(note);
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2018-03-25 23:25:17 -04:00
|
|
|
note.isProtected = true;
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2018-03-25 13:02:39 -04:00
|
|
|
await noteDetail.saveNoteToServer(note);
|
2017-12-25 09:30:37 -05:00
|
|
|
|
2018-03-25 23:25:17 -04:00
|
|
|
treeService.setProtected(note.noteId, note.isProtected);
|
2017-11-02 23:36:58 -04:00
|
|
|
|
2018-03-25 13:02:39 -04:00
|
|
|
noteDetail.setNoteBackgroundIfProtected(note);
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-11-02 23:55:22 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
async function unprotectNoteAndSendToServer() {
|
|
|
|
await ensureProtectedSession(true, true);
|
2017-11-03 20:01:32 -04:00
|
|
|
|
2018-03-25 13:02:39 -04:00
|
|
|
const note = noteDetail.getCurrentNote();
|
2017-11-02 23:36:58 -04:00
|
|
|
|
2018-03-25 13:02:39 -04:00
|
|
|
noteDetail.updateNoteFromInputs(note);
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2018-03-25 23:25:17 -04:00
|
|
|
note.isProtected = false;
|
2017-09-06 22:06:43 -04:00
|
|
|
|
2018-03-25 13:02:39 -04:00
|
|
|
await noteDetail.saveNoteToServer(note);
|
2017-12-25 09:30:37 -05:00
|
|
|
|
2018-03-25 23:25:17 -04:00
|
|
|
treeService.setProtected(note.noteId, note.isProtected);
|
2017-09-17 12:46:14 -04:00
|
|
|
|
2018-03-25 13:02:39 -04:00
|
|
|
noteDetail.setNoteBackgroundIfProtected(note);
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function protectSubTree(noteId, protect) {
|
|
|
|
await ensureProtectedSession(true, true);
|
2017-11-22 20:46:42 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
await server.put('notes/' + noteId + "/protect-sub-tree/" + (protect ? 1 : 0));
|
2017-11-15 00:04:26 -05:00
|
|
|
|
2018-03-25 21:29:35 -04:00
|
|
|
infoService.showMessage("Request to un/protect sub tree has finished successfully");
|
2017-11-15 00:04:26 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
treeService.reload();
|
2018-03-25 13:02:39 -04:00
|
|
|
noteDetail.reload();
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-11-15 00:04:26 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
$passwordForm.submit(() => {
|
|
|
|
setupProtectedSession();
|
2017-11-08 22:33:08 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
return false;
|
|
|
|
});
|
2017-11-08 22:33:08 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
$protectButton.click(protectNoteAndSendToServer);
|
|
|
|
$unprotectButton.click(unprotectNoteAndSendToServer);
|
|
|
|
|
|
|
|
export default {
|
|
|
|
ensureProtectedSession,
|
|
|
|
protectNoteAndSendToServer,
|
|
|
|
unprotectNoteAndSendToServer,
|
|
|
|
protectSubTree,
|
|
|
|
ensureDialogIsClosed
|
|
|
|
};
|