protected session in a component now works

This commit is contained in:
zadam 2019-05-05 19:48:30 +02:00
parent 9a97fe09ee
commit 61696f0287
5 changed files with 61 additions and 69 deletions

View File

@ -14,6 +14,7 @@ import noteDetailImage from "./note_detail_image.js";
import noteDetailSearch from "./note_detail_search.js"; import noteDetailSearch from "./note_detail_search.js";
import noteDetailRender from "./note_detail_render.js"; import noteDetailRender from "./note_detail_render.js";
import noteDetailRelationMap from "./note_detail_relation_map.js"; import noteDetailRelationMap from "./note_detail_relation_map.js";
import noteDetailProtectedSession from "./note_detail_protected_session.js";
const $noteTabContentsContainer = $("#note-tab-container"); const $noteTabContentsContainer = $("#note-tab-container");
@ -24,7 +25,8 @@ const componentClasses = {
'image': noteDetailImage, 'image': noteDetailImage,
'search': noteDetailSearch, 'search': noteDetailSearch,
'render': noteDetailRender, 'render': noteDetailRender,
'relation-map': noteDetailRelationMap 'relation-map': noteDetailRelationMap,
'protected-session': noteDetailProtectedSession
}; };
let tabIdCounter = 1; let tabIdCounter = 1;
@ -89,9 +91,19 @@ class NoteContext {
console.log(`Switched tab ${this.tabId} to ${this.noteId}`); console.log(`Switched tab ${this.tabId} to ${this.noteId}`);
} }
getComponent(type) { getComponent() {
if (!type) { let type = this.note.type;
type = this.note.type;
if (this.note.isProtected) {
if (protectedSessionHolder.isProtectedSessionAvailable()) {
protectedSessionHolder.touchProtectedSession();
}
else {
type = 'protected-session';
// user shouldn't be able to edit note title
this.$noteTitle.prop("readonly", true);
}
} }
if (!(type in this.components)) { if (!(type in this.components)) {

View File

@ -73,20 +73,6 @@ async function saveNotesIfChanged() {
$savedIndicator.fadeIn(); $savedIndicator.fadeIn();
} }
async function handleProtectedSession() {
const newSessionCreated = await protectedSessionService.ensureProtectedSession(getActiveNote().isProtected, false);
if (getActiveNote().isProtected) {
protectedSessionHolder.touchProtectedSession();
}
// this might be important if we focused on protected note when not in protected note and we got a dialog
// to login, but we chose instead to come to another node - at that point the dialog is still visible and this will close it.
protectedSessionService.ensureDialogIsClosed();
return newSessionCreated;
}
/** @type {NoteContext[]} */ /** @type {NoteContext[]} */
let noteContexts = []; let noteContexts = [];
@ -175,15 +161,9 @@ async function loadNoteDetail(noteId, newTab = false) {
ctx.$noteDetailComponents.hide(); ctx.$noteDetailComponents.hide();
const newSessionCreated = await handleProtectedSession();
if (newSessionCreated) {
// in such case we're reloading note anyway so no need to continue here.
return;
}
ctx.$noteTitle.removeAttr("readonly"); // this can be set by protected session service ctx.$noteTitle.removeAttr("readonly"); // this can be set by protected session service
await ctx.getComponent(ctx.note.type).show(ctx); await ctx.getComponent().show(ctx);
} }
finally { finally {
ctx.noteChangeDisabled = false; ctx.noteChangeDisabled = false;
@ -192,7 +172,7 @@ async function loadNoteDetail(noteId, newTab = false) {
treeService.setBranchBackgroundBasedOnProtectedStatus(noteId); treeService.setBranchBackgroundBasedOnProtectedStatus(noteId);
// after loading new note make sure editor is scrolled to the top // after loading new note make sure editor is scrolled to the top
ctx.getComponent(ctx.note.type).scrollToTop(); ctx.getComponent().scrollToTop();
fireDetailLoaded(); fireDetailLoaded();

View File

@ -1,18 +1,46 @@
class NoteDetailImage { import protectedSessionService from './protected_session.js';
class NoteDetailProtectedSession {
/** /**
* @param {NoteContext} ctx * @param {NoteContext} ctx
*/ */
constructor(ctx) { constructor(ctx) {
this.ctx = ctx; this.ctx = ctx;
this.$component = ctx.$noteTabContent.find(".protected-session-password-component"); this.$component = ctx.$noteTabContent.find(".protected-session-password-component");
const $passwordForms = ctx.$noteTabContent.find(".protected-session-password-form"); this.$passwordForm = ctx.$noteTabContent.find(".protected-session-password-form");
const $passwordInputs = ctx.$noteTabContent.find(".protected-session-password"); this.$passwordInput = ctx.$noteTabContent.find(".protected-session-password");
const $passwordInModal = ctx.$noteTabContent.find(".protected-session-password-in-modal");
const $protectButton = ctx.$noteTabContent.find(".protect-button"); this.$protectButton = ctx.$noteTabContent.find(".protect-button");
const $unprotectButton = ctx.$noteTabContent.find(".unprotect-button"); this.$protectButton.click(protectedSessionService.protectNoteAndSendToServer);
this.$unprotectButton = ctx.$noteTabContent.find(".unprotect-button");
this.$unprotectButton.click(protectedSessionService.unprotectNoteAndSendToServer);
this.$passwordForm.submit(() => {
const password = this.$passwordInput.val();
this.$passwordInput.val("");
protectedSessionService.setupProtectedSession(password);
return false;
});
} }
show() { show() {
this.$component.show(); this.$component.show();
} }
}
getContent() {}
focus() {}
onNoteChange() {}
cleanup() {}
scrollToTop() {
this.$component.scrollTop(0);
}
}
export default NoteDetailProtectedSession;

View File

@ -5,17 +5,8 @@ import server from './server.js';
import protectedSessionHolder from './protected_session_holder.js'; import protectedSessionHolder from './protected_session_holder.js';
import infoService from "./info.js"; import infoService from "./info.js";
const $dialog = $("#protected-session-password-dialog");
const $component = $("#protected-session-password-component");
const $passwordForms = $(".protected-session-password-form");
const $passwordInputs = $(".protected-session-password");
const $passwordInModal = $("#protected-session-password-in-modal");
const $tabContent = $(".note-tab-content");
const $protectButton = $("#protect-button");
const $unprotectButton = $("#unprotect-button");
const $enterProtectedSessionButton = $("#enter-protected-session-button"); const $enterProtectedSessionButton = $("#enter-protected-session-button");
const $leaveProtectedSessionButton = $("#leave-protected-session-button"); const $leaveProtectedSessionButton = $("#leave-protected-session-button");
const $noteTitle = $("#note-title");
let protectedSessionDeferred = null; let protectedSessionDeferred = null;
@ -39,14 +30,7 @@ function ensureProtectedSession(requireProtectedSession, modal) {
// using deferred instead of promise because it allows resolving from outside // using deferred instead of promise because it allows resolving from outside
protectedSessionDeferred = dfd; protectedSessionDeferred = dfd;
// user shouldn't be able to edit note title
$noteTitle.prop("readonly", true);
if (modal) { if (modal) {
if (treeService.getActiveNode().data.isProtected) {
$tabContent.hide();
}
$dialog.modal(); $dialog.modal();
} }
else { else {
@ -61,8 +45,6 @@ function ensureProtectedSession(requireProtectedSession, modal) {
} }
async function setupProtectedSession(password) { async function setupProtectedSession(password) {
$passwordInputs.val("");
const response = await enterProtectedSessionOnServer(password); const response = await enterProtectedSessionOnServer(password);
if (!response.success) { if (!response.success) {
@ -72,8 +54,6 @@ async function setupProtectedSession(password) {
protectedSessionHolder.setProtectedSessionId(response.protectedSessionId); protectedSessionHolder.setProtectedSessionId(response.protectedSessionId);
$dialog.modal("hide");
await treeService.reload(); await treeService.reload();
// it's important that tree has been already reloaded at this point // it's important that tree has been already reloaded at this point
@ -83,8 +63,6 @@ async function setupProtectedSession(password) {
if (protectedSessionDeferred !== null) { if (protectedSessionDeferred !== null) {
ensureDialogIsClosed(); ensureDialogIsClosed();
$tabContent.show();
protectedSessionDeferred.resolve(true); protectedSessionDeferred.resolve(true);
protectedSessionDeferred = null; protectedSessionDeferred = null;
@ -166,24 +144,13 @@ async function protectSubtree(noteId, protect) {
noteDetailService.reload(); noteDetailService.reload();
} }
$passwordForms.submit(function() { // needs to stay as function
const password = $(this).find(".protected-session-password").val();
setupProtectedSession(password);
return false;
});
$protectButton.click(protectNoteAndSendToServer);
$unprotectButton.click(unprotectNoteAndSendToServer);
$dialog.on("shown.bs.modal", e => $passwordInModal.focus());
export default { export default {
ensureProtectedSession, ensureProtectedSession,
protectSubtree, protectSubtree,
ensureDialogIsClosed, ensureDialogIsClosed,
enterProtectedSession, enterProtectedSession,
leaveProtectedSession, leaveProtectedSession,
protectNoteAndSendToServer protectNoteAndSendToServer,
unprotectNoteAndSendToServer,
setupProtectedSession
}; };

View File

@ -826,4 +826,9 @@ a.external:after, a[href^="http://"]:after, a[href^="https://"]:after {
.jam-empty { .jam-empty {
width: 1em; width: 1em;
display: inline-block; display: inline-block;
}
.protected-session-password-component {
width: 300px;
margin: 30px auto auto;
} }