2017-11-05 07:38:50 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-11-05 05:54:27 +08:00
|
|
|
const noteEditor = (function() {
|
|
|
|
const noteTitleEl = $("#note-title");
|
|
|
|
const noteDetailEl = $('#note-detail');
|
2017-11-15 11:21:56 +08:00
|
|
|
const protectButton = $("#protect-button");
|
|
|
|
const unprotectButton = $("#unprotect-button");
|
2017-11-05 05:54:27 +08:00
|
|
|
const noteDetailWrapperEl = $("#note-detail-wrapper");
|
|
|
|
|
|
|
|
let currentNote = null;
|
|
|
|
|
|
|
|
let noteChangeDisabled = false;
|
|
|
|
|
|
|
|
let isNoteChanged = false;
|
|
|
|
|
|
|
|
function getCurrentNote() {
|
|
|
|
return currentNote;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentNoteId() {
|
|
|
|
return currentNote ? currentNote.detail.note_id : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function noteChanged() {
|
|
|
|
if (noteChangeDisabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isNoteChanged = true;
|
|
|
|
}
|
|
|
|
|
2017-11-05 23:06:49 +08:00
|
|
|
async function reload() {
|
|
|
|
// no saving here
|
|
|
|
|
|
|
|
await loadNoteToEditor(getCurrentNoteId());
|
|
|
|
}
|
|
|
|
|
|
|
|
async function switchToNote(noteId) {
|
|
|
|
if (getCurrentNoteId() !== noteId) {
|
|
|
|
await saveNoteIfChanged();
|
|
|
|
|
|
|
|
await loadNoteToEditor(noteId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 05:54:27 +08:00
|
|
|
async function saveNoteIfChanged() {
|
|
|
|
if (!isNoteChanged) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const note = noteEditor.getCurrentNote();
|
|
|
|
|
|
|
|
updateNoteFromInputs(note);
|
|
|
|
|
|
|
|
await saveNoteToServer(note);
|
2017-11-15 11:50:56 +08:00
|
|
|
|
|
|
|
if (note.detail.is_protected) {
|
|
|
|
protected_session.touchProtectedSession();
|
|
|
|
}
|
2017-11-05 05:54:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateNoteFromInputs(note) {
|
2017-11-30 09:47:01 +08:00
|
|
|
note.detail.note_text = noteDetailEl.summernote('code');
|
2017-11-05 05:54:27 +08:00
|
|
|
|
|
|
|
const title = noteTitleEl.val();
|
|
|
|
|
|
|
|
note.detail.note_title = title;
|
2017-11-23 08:58:56 +08:00
|
|
|
|
2017-11-29 08:38:33 +08:00
|
|
|
noteTree.setNoteTitle(note.detail.note_id, title);
|
2017-11-05 05:54:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function saveNoteToServer(note) {
|
2017-11-29 09:52:38 +08:00
|
|
|
await server.put('notes/' + note.detail.note_id, note);
|
2017-11-05 05:54:27 +08:00
|
|
|
|
|
|
|
isNoteChanged = false;
|
|
|
|
|
2017-11-09 11:33:08 +08:00
|
|
|
showMessage("Saved!");
|
2017-11-05 05:54:27 +08:00
|
|
|
}
|
|
|
|
|
2017-11-15 11:21:56 +08:00
|
|
|
function setNoteBackgroundIfProtected(note) {
|
2017-11-15 10:54:12 +08:00
|
|
|
if (note.detail.is_protected) {
|
2017-11-15 11:21:56 +08:00
|
|
|
$(".note-editable").addClass("protected");
|
|
|
|
protectButton.hide();
|
|
|
|
unprotectButton.show();
|
2017-11-05 05:54:27 +08:00
|
|
|
}
|
|
|
|
else {
|
2017-11-15 11:21:56 +08:00
|
|
|
$(".note-editable").removeClass("protected");
|
|
|
|
protectButton.show();
|
|
|
|
unprotectButton.hide();
|
2017-11-05 05:54:27 +08:00
|
|
|
}
|
|
|
|
|
2017-11-19 06:05:50 +08:00
|
|
|
noteTree.setCurrentNoteTreeBasedOnProtectedStatus();
|
2017-11-05 05:54:27 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 08:40:47 +08:00
|
|
|
let isNewNoteCreated = false;
|
|
|
|
|
|
|
|
function newNoteCreated() {
|
|
|
|
isNewNoteCreated = true;
|
|
|
|
}
|
|
|
|
|
2017-11-05 05:54:27 +08:00
|
|
|
async function loadNoteToEditor(noteId) {
|
2017-11-29 09:52:38 +08:00
|
|
|
currentNote = await server.get('notes/' + noteId);
|
2017-11-05 05:54:27 +08:00
|
|
|
|
2017-11-24 08:40:47 +08:00
|
|
|
if (isNewNoteCreated) {
|
|
|
|
isNewNoteCreated = false;
|
2017-11-05 05:54:27 +08:00
|
|
|
|
|
|
|
noteTitleEl.focus().select();
|
|
|
|
}
|
|
|
|
|
2017-11-15 11:36:36 +08:00
|
|
|
await protected_session.ensureProtectedSession(currentNote.detail.is_protected, false);
|
2017-11-05 05:54:27 +08:00
|
|
|
|
2017-11-15 11:50:56 +08:00
|
|
|
if (currentNote.detail.is_protected) {
|
|
|
|
protected_session.touchProtectedSession();
|
|
|
|
}
|
|
|
|
|
2017-11-15 13:10:11 +08:00
|
|
|
// 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.
|
|
|
|
protected_session.ensureDialogIsClosed();
|
|
|
|
|
2017-11-05 05:54:27 +08:00
|
|
|
noteDetailWrapperEl.show();
|
|
|
|
|
|
|
|
noteChangeDisabled = true;
|
|
|
|
|
2017-11-30 10:13:12 +08:00
|
|
|
noteTitleEl.val(currentNote.detail.note_title);
|
|
|
|
|
2017-11-05 05:54:27 +08:00
|
|
|
// Clear contents and remove all stored history. This is to prevent undo from going across notes
|
|
|
|
noteDetailEl.summernote('reset');
|
|
|
|
|
2017-11-05 09:02:56 +08:00
|
|
|
noteDetailEl.summernote('code', currentNote.detail.note_text);
|
2017-11-05 05:54:27 +08:00
|
|
|
|
|
|
|
noteChangeDisabled = false;
|
|
|
|
|
2017-11-15 11:21:56 +08:00
|
|
|
setNoteBackgroundIfProtected(currentNote);
|
2017-11-07 11:21:11 +08:00
|
|
|
|
|
|
|
showAppIfHidden();
|
2017-11-05 05:54:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function loadNote(noteId) {
|
2017-11-29 09:52:38 +08:00
|
|
|
return await server.get('notes/' + noteId);
|
2017-11-05 05:54:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$(document).ready(() => {
|
2017-11-30 10:13:12 +08:00
|
|
|
noteTitleEl.on('input', () => {
|
|
|
|
noteChanged();
|
|
|
|
|
|
|
|
const title = noteTitleEl.val();
|
|
|
|
|
|
|
|
noteTree.setNoteTitle(getCurrentNoteId(), title);
|
|
|
|
});
|
2017-11-05 05:54:27 +08:00
|
|
|
|
|
|
|
noteDetailEl.summernote({
|
|
|
|
airMode: true,
|
|
|
|
height: 300,
|
|
|
|
callbacks: {
|
|
|
|
onChange: noteChanged
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// so that tab jumps from note title (which has tabindex 1)
|
2017-11-05 06:18:55 +08:00
|
|
|
$(".note-editable").attr("tabindex", 2);
|
2017-11-05 05:54:27 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
setInterval(saveNoteIfChanged, 5000);
|
|
|
|
|
|
|
|
return {
|
2017-11-05 23:06:49 +08:00
|
|
|
reload,
|
|
|
|
switchToNote,
|
2017-11-05 05:54:27 +08:00
|
|
|
saveNoteIfChanged,
|
|
|
|
updateNoteFromInputs,
|
|
|
|
saveNoteToServer,
|
2017-11-15 11:21:56 +08:00
|
|
|
setNoteBackgroundIfProtected,
|
2017-11-05 05:54:27 +08:00
|
|
|
loadNote,
|
|
|
|
getCurrentNote,
|
|
|
|
getCurrentNoteId,
|
2017-11-24 08:40:47 +08:00
|
|
|
newNoteCreated
|
2017-11-05 05:54:27 +08:00
|
|
|
};
|
|
|
|
})();
|