2018-03-26 01:41:29 +08:00
|
|
|
import treeService from './tree.js';
|
2018-04-08 21:25:35 +08:00
|
|
|
import noteDetailService from './note_detail.js';
|
2018-03-25 23:09:17 +08:00
|
|
|
import utils from './utils.js';
|
|
|
|
import server from './server.js';
|
2018-03-26 09:16:57 +08:00
|
|
|
import protectedSessionHolder from './protected_session_holder.js';
|
2018-03-26 09:29:35 +08:00
|
|
|
import infoService from "./info.js";
|
2018-03-25 23:09:17 +08: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");
|
2018-06-01 08:00:39 +08:00
|
|
|
const $protectedSessionOnButton = $("#protected-session-on");
|
|
|
|
const $protectedSessionOffButton = $("#protected-session-off");
|
2018-03-25 23:09:17 +08:00
|
|
|
|
|
|
|
let protectedSessionDeferred = null;
|
|
|
|
|
2018-06-01 08:00:39 +08:00
|
|
|
async function enterProtectedSession() {
|
|
|
|
if (!protectedSessionHolder.isProtectedSessionAvailable()) {
|
|
|
|
await ensureProtectedSession(true, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function leaveProtectedSession() {
|
|
|
|
if (protectedSessionHolder.isProtectedSessionAvailable()) {
|
|
|
|
utils.reloadApp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 21:03:23 +08:00
|
|
|
/** returned promise resolves with true if new protected session was established, false if no action was necessary */
|
2018-03-25 23:09:17 +08:00
|
|
|
function ensureProtectedSession(requireProtectedSession, modal) {
|
|
|
|
const dfd = $.Deferred();
|
|
|
|
|
2018-03-26 09:16:57 +08:00
|
|
|
if (requireProtectedSession && !protectedSessionHolder.isProtectedSessionAvailable()) {
|
2018-08-17 21:21:59 +08:00
|
|
|
// using deferred instead of promise because it allows resolving from outside
|
2018-03-25 23:09:17 +08:00
|
|
|
protectedSessionDeferred = dfd;
|
|
|
|
|
|
|
|
if (treeService.getCurrentNode().data.isProtected) {
|
|
|
|
$noteDetailWrapper.hide();
|
|
|
|
}
|
2017-11-05 07:57:40 +08:00
|
|
|
|
2018-11-09 06:48:49 +08:00
|
|
|
$dialog.toggleClass("modalless", !modal);
|
2018-11-07 00:47:40 +08:00
|
|
|
$dialog.modal();
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
else {
|
2018-08-28 21:03:23 +08:00
|
|
|
dfd.resolve(false);
|
2017-09-07 10:06:43 +08:00
|
|
|
}
|
2017-11-04 08:01:32 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
return dfd.promise();
|
|
|
|
}
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function setupProtectedSession() {
|
|
|
|
const password = $password.val();
|
|
|
|
$password.val("");
|
2018-01-27 11:32:44 +08:00
|
|
|
|
2018-06-01 08:00:39 +08:00
|
|
|
const response = await enterProtectedSessionOnServer(password);
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (!response.success) {
|
2018-03-26 09:29:35 +08:00
|
|
|
infoService.showError("Wrong password.");
|
2018-03-25 23:09:17 +08:00
|
|
|
return;
|
2017-11-05 06:18:55 +08:00
|
|
|
}
|
2017-09-09 08:55:24 +08:00
|
|
|
|
2018-03-26 09:16:57 +08:00
|
|
|
protectedSessionHolder.setProtectedSessionId(response.protectedSessionId);
|
2017-11-11 11:55:19 +08:00
|
|
|
|
2018-11-07 00:47:40 +08:00
|
|
|
$dialog.modal("hide");
|
2017-09-07 11:13:39 +08:00
|
|
|
|
2018-11-09 21:36:27 +08:00
|
|
|
await noteDetailService.reload();
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
treeService.reload();
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (protectedSessionDeferred !== null) {
|
|
|
|
ensureDialogIsClosed($dialog, $password);
|
2017-09-18 00:46:14 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$noteDetailWrapper.show();
|
2017-11-15 12:01:23 +08:00
|
|
|
|
2018-08-28 21:03:23 +08:00
|
|
|
protectedSessionDeferred.resolve(true);
|
2018-06-04 08:42:25 +08:00
|
|
|
protectedSessionDeferred = null;
|
2018-01-27 11:32:44 +08:00
|
|
|
|
2018-06-01 08:00:39 +08:00
|
|
|
$protectedSessionOnButton.addClass('active');
|
|
|
|
$protectedSessionOffButton.removeClass('active');
|
2017-11-09 11:33:08 +08:00
|
|
|
}
|
2018-09-01 19:18:55 +08:00
|
|
|
|
|
|
|
infoService.showMessage("Protected session has been started.");
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
function ensureDialogIsClosed() {
|
2018-11-14 15:23:34 +08:00
|
|
|
// this may fal if the dialog has not been previously opened (not sure if still true with Bootstrap modal)
|
2018-03-25 23:09:17 +08:00
|
|
|
try {
|
2018-11-07 00:47:40 +08:00
|
|
|
$dialog.modal('hide');
|
2017-11-15 13:10:11 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
catch (e) {}
|
2017-11-15 13:10:11 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$password.val('');
|
|
|
|
}
|
2017-11-11 11:55:19 +08:00
|
|
|
|
2018-06-01 08:00:39 +08:00
|
|
|
async function enterProtectedSessionOnServer(password) {
|
2018-03-25 23:09:17 +08:00
|
|
|
return await server.post('login/protected', {
|
|
|
|
password: password
|
|
|
|
});
|
|
|
|
}
|
2017-11-11 11:55:19 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function protectNoteAndSendToServer() {
|
2018-06-02 23:47:16 +08:00
|
|
|
if (noteDetailService.getCurrentNote().isProtected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
await ensureProtectedSession(true, true);
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2018-04-08 21:25:35 +08:00
|
|
|
const note = noteDetailService.getCurrentNote();
|
2018-03-26 11:25:17 +08:00
|
|
|
note.isProtected = true;
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2018-04-08 21:25:35 +08:00
|
|
|
await noteDetailService.saveNote(note);
|
2017-12-25 22:30:37 +08:00
|
|
|
|
2018-03-26 11:25:17 +08:00
|
|
|
treeService.setProtected(note.noteId, note.isProtected);
|
2017-11-03 11:36:58 +08:00
|
|
|
|
2018-11-09 21:36:27 +08:00
|
|
|
noteDetailService.setNoteBackgroundIfProtected(note);console.log(note);
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-03 11:55:22 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function unprotectNoteAndSendToServer() {
|
2018-11-09 21:36:27 +08:00
|
|
|
const currentNote = noteDetailService.getCurrentNote();
|
|
|
|
|
|
|
|
if (!currentNote.isProtected) {
|
|
|
|
infoService.showAndLogError(`Note ${currentNote.noteId} is not protected`);
|
|
|
|
|
2018-06-02 23:47:16 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-17 21:21:59 +08:00
|
|
|
if (!protectedSessionHolder.isProtectedSessionAvailable()) {
|
|
|
|
console.log("Unprotecting notes outside of protected session is not allowed.");
|
|
|
|
// the reason is that it's not easy to handle even with ensureProtectedSession,
|
|
|
|
// because we would first have to make sure the note is loaded and only then unprotect
|
|
|
|
// we used to have a bug where we would overwrite the previous note with unprotected content.
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2017-11-04 08:01:32 +08:00
|
|
|
|
2018-11-09 21:36:27 +08:00
|
|
|
currentNote.isProtected = false;
|
2017-09-07 10:06:43 +08:00
|
|
|
|
2018-11-09 21:36:27 +08:00
|
|
|
await noteDetailService.saveNote(currentNote);
|
2017-12-25 22:30:37 +08:00
|
|
|
|
2018-11-09 21:36:27 +08:00
|
|
|
treeService.setProtected(currentNote.noteId, currentNote.isProtected);
|
2017-09-18 00:46:14 +08:00
|
|
|
|
2018-11-09 21:36:27 +08:00
|
|
|
noteDetailService.setNoteBackgroundIfProtected(currentNote);
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
|
2018-09-01 00:22:53 +08:00
|
|
|
async function protectSubtree(noteId, protect) {
|
2018-03-25 23:09:17 +08:00
|
|
|
await ensureProtectedSession(true, true);
|
2017-11-23 09:46:42 +08:00
|
|
|
|
2018-04-02 08:33:10 +08:00
|
|
|
await server.put('notes/' + noteId + "/protect/" + (protect ? 1 : 0));
|
2017-11-15 13:04:26 +08:00
|
|
|
|
2018-03-26 09:29:35 +08:00
|
|
|
infoService.showMessage("Request to un/protect sub tree has finished successfully");
|
2017-11-15 13:04:26 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
treeService.reload();
|
2018-04-08 21:25:35 +08:00
|
|
|
noteDetailService.reload();
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-15 13:04:26 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$passwordForm.submit(() => {
|
|
|
|
setupProtectedSession();
|
2017-11-09 11:33:08 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
return false;
|
|
|
|
});
|
2017-11-09 11:33:08 +08:00
|
|
|
|
2018-11-09 06:48:49 +08:00
|
|
|
// this doesn't work, event is not triggered :/
|
|
|
|
$dialog.on("show.bs.modal", e => function() {
|
|
|
|
if ($(this).hasClass("modalless")) {
|
|
|
|
// return "stolen" focus to tree
|
|
|
|
treeService.getCurrentNode().setFocus();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$password.focus();
|
|
|
|
}
|
|
|
|
});
|
2018-11-07 00:47:40 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$protectButton.click(protectNoteAndSendToServer);
|
|
|
|
$unprotectButton.click(unprotectNoteAndSendToServer);
|
|
|
|
|
2018-11-14 15:23:34 +08:00
|
|
|
$dialog.on("shown.bs.modal", e => $password.focus());
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
export default {
|
|
|
|
ensureProtectedSession,
|
2018-09-01 00:22:53 +08:00
|
|
|
protectSubtree,
|
2018-06-01 08:00:39 +08:00
|
|
|
ensureDialogIsClosed,
|
|
|
|
enterProtectedSession,
|
|
|
|
leaveProtectedSession
|
2018-03-25 23:09:17 +08:00
|
|
|
};
|