2017-08-30 10:25:58 +08:00
|
|
|
const tags = {
|
2017-06-12 04:04:07 +08:00
|
|
|
1: "<b>",
|
|
|
|
2: "</b>",
|
|
|
|
3: "<i>",
|
|
|
|
4: "</i>",
|
|
|
|
5: "<u>",
|
|
|
|
6: "</u>",
|
|
|
|
9: "<s>",
|
|
|
|
10: "</s>"
|
|
|
|
};
|
|
|
|
|
2017-06-12 09:04:04 +08:00
|
|
|
let noteChangeDisabled = false;
|
|
|
|
|
2017-08-17 08:41:41 +08:00
|
|
|
let isNoteChanged = false;
|
|
|
|
|
2017-06-12 04:04:07 +08:00
|
|
|
function noteChanged() {
|
2017-06-12 09:04:04 +08:00
|
|
|
if (noteChangeDisabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:41:41 +08:00
|
|
|
isNoteChanged = true;
|
|
|
|
}
|
|
|
|
|
2017-09-05 09:28:07 +08:00
|
|
|
function updateNoteFromInputs(note) {
|
2017-06-12 04:04:07 +08:00
|
|
|
let contents = $('#noteDetail').summernote('code');
|
|
|
|
|
2017-08-22 08:34:17 +08:00
|
|
|
html2notecase(contents, note);
|
|
|
|
|
2017-06-12 04:04:07 +08:00
|
|
|
let title = $('#noteTitle').val();
|
|
|
|
|
2017-09-04 03:08:17 +08:00
|
|
|
getNodeByKey(note.detail.note_id).setTitle(title);
|
2017-06-12 04:04:07 +08:00
|
|
|
|
|
|
|
note.detail.note_title = title;
|
2017-09-05 09:28:07 +08:00
|
|
|
}
|
2017-06-12 04:04:07 +08:00
|
|
|
|
2017-09-05 09:28:07 +08:00
|
|
|
function saveNoteToServer(note, callback) {
|
2017-06-12 04:04:07 +08:00
|
|
|
$.ajax({
|
2017-08-24 09:43:02 +08:00
|
|
|
url: baseUrl + 'notes/' + note.detail.note_id,
|
2017-06-12 04:04:07 +08:00
|
|
|
type: 'PUT',
|
|
|
|
data: JSON.stringify(note),
|
|
|
|
contentType: "application/json",
|
2017-09-05 09:28:07 +08:00
|
|
|
success: function () {
|
2017-08-17 08:41:41 +08:00
|
|
|
isNoteChanged = false;
|
|
|
|
|
2017-06-12 04:04:07 +08:00
|
|
|
message("Saved!");
|
2017-08-17 08:41:41 +08:00
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
2017-08-14 09:42:10 +08:00
|
|
|
},
|
2017-09-05 09:28:07 +08:00
|
|
|
error: function () {
|
2017-08-14 09:42:10 +08:00
|
|
|
error("Error saving the note!");
|
2017-06-12 04:04:07 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-05 09:28:07 +08:00
|
|
|
function saveNoteIfChanged(callback) {
|
|
|
|
if (!isNoteChanged) {
|
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const note = globalNote;
|
|
|
|
|
|
|
|
updateNoteFromInputs(note);
|
|
|
|
|
2017-09-06 11:48:41 +08:00
|
|
|
encryptNoteIfNecessary(note);
|
|
|
|
|
|
|
|
saveNoteToServer(note, callback);
|
2017-09-05 09:28:07 +08:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:41:41 +08:00
|
|
|
setInterval(saveNoteIfChanged, 5000);
|
|
|
|
|
2017-06-12 04:04:07 +08:00
|
|
|
$(document).ready(function() {
|
|
|
|
$("#noteTitle").on('input', function() {
|
|
|
|
noteChanged();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#noteDetail').summernote({
|
|
|
|
airMode: true,
|
2017-06-12 09:04:04 +08:00
|
|
|
height: 300,
|
2017-06-12 04:04:07 +08:00
|
|
|
callbacks: {
|
|
|
|
onChange: noteChanged
|
|
|
|
}
|
|
|
|
});
|
2017-08-26 07:30:26 +08:00
|
|
|
|
|
|
|
// so that tab jumps from note title (which has tabindex 1)
|
|
|
|
$(".note-editable").attr("tabindex", 2);
|
2017-06-12 04:04:07 +08:00
|
|
|
});
|
|
|
|
|
2017-09-07 10:06:43 +08:00
|
|
|
let globalNote;
|
2017-06-12 04:04:07 +08:00
|
|
|
|
2017-06-12 09:04:04 +08:00
|
|
|
function createNewTopLevelNote() {
|
2017-09-07 09:34:54 +08:00
|
|
|
let rootNode = globalTree.fancytree("getRootNode");
|
2017-06-12 09:04:04 +08:00
|
|
|
|
|
|
|
createNote(rootNode, "root", "into");
|
|
|
|
}
|
|
|
|
|
2017-06-14 10:36:56 +08:00
|
|
|
let newNoteCreated = false;
|
|
|
|
|
2017-09-09 10:43:02 +08:00
|
|
|
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()) {
|
|
|
|
encryption = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newNoteName = "new note";
|
|
|
|
const newNoteNameEncryptedIfNecessary = encryption > 0 ? encryptString(newNoteName) : newNoteName;
|
2017-06-12 04:04:07 +08:00
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: baseUrl + 'notes/' + parentKey + '/children' ,
|
|
|
|
type: 'POST',
|
|
|
|
data: JSON.stringify({
|
2017-09-09 10:43:02 +08:00
|
|
|
note_title: newNoteNameEncryptedIfNecessary,
|
2017-06-12 04:04:07 +08:00
|
|
|
target: target,
|
2017-09-09 10:43:02 +08:00
|
|
|
target_note_id: node.key,
|
|
|
|
encryption: encryption
|
2017-06-12 04:04:07 +08:00
|
|
|
}),
|
|
|
|
contentType: "application/json",
|
|
|
|
success: function(result) {
|
2017-09-09 11:14:42 +08:00
|
|
|
const newNode = {
|
2017-09-09 10:43:02 +08:00
|
|
|
title: newNoteName,
|
|
|
|
key: result.note_id,
|
|
|
|
note_id: result.note_id,
|
2017-09-09 11:14:42 +08:00
|
|
|
encryption: encryption,
|
|
|
|
extraClasses: encryption ? "encrypted" : ""
|
2017-06-12 04:04:07 +08:00
|
|
|
};
|
|
|
|
|
2017-09-04 06:50:56 +08:00
|
|
|
globalAllNoteIds.push(result.note_id);
|
2017-08-27 22:30:32 +08:00
|
|
|
|
2017-06-14 10:36:56 +08:00
|
|
|
newNoteCreated = true;
|
|
|
|
|
2017-08-22 09:31:23 +08:00
|
|
|
if (target === 'after') {
|
2017-06-12 04:04:07 +08:00
|
|
|
node.appendSibling(newNode).setActive(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.addChildren(newNode).setActive(true);
|
|
|
|
|
|
|
|
node.folder = true;
|
|
|
|
node.renderTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
message("Created!");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-07 09:11:19 +08:00
|
|
|
globalRecentNotes = [];
|
2017-08-23 10:40:54 +08:00
|
|
|
|
2017-09-05 09:37:55 +08:00
|
|
|
function setNoteBackgroundIfEncrypted(note) {
|
|
|
|
if (note.detail.encryption > 0) {
|
|
|
|
$(".note-editable").addClass("encrypted");
|
2017-09-06 10:01:22 +08:00
|
|
|
$("#encryptButton").hide();
|
|
|
|
$("#decryptButton").show();
|
2017-09-05 09:37:55 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$(".note-editable").removeClass("encrypted");
|
2017-09-06 10:01:22 +08:00
|
|
|
$("#encryptButton").show();
|
|
|
|
$("#decryptButton").hide();
|
2017-09-05 09:37:55 +08:00
|
|
|
}
|
2017-09-09 11:41:29 +08:00
|
|
|
|
|
|
|
const node = getNodeByKey(note.detail.note_id);
|
|
|
|
node.toggleClass("encrypted", note.detail.encryption > 0);
|
2017-09-05 09:37:55 +08:00
|
|
|
}
|
|
|
|
|
2017-06-12 04:04:07 +08:00
|
|
|
function loadNote(noteId) {
|
|
|
|
$.get(baseUrl + 'notes/' + noteId).then(function(note) {
|
|
|
|
globalNote = note;
|
|
|
|
|
2017-06-14 10:36:56 +08:00
|
|
|
if (newNoteCreated) {
|
|
|
|
newNoteCreated = false;
|
|
|
|
|
|
|
|
$("#noteTitle").focus().select();
|
|
|
|
}
|
|
|
|
|
2017-09-06 11:51:50 +08:00
|
|
|
handleEncryption(note.detail.encryption > 0, false, () => {
|
2017-09-06 11:48:41 +08:00
|
|
|
$("#noteDetailWrapper").show();
|
2017-09-07 10:03:53 +08:00
|
|
|
|
|
|
|
// this may fal if the dialog has not been previously opened
|
2017-09-06 11:48:41 +08:00
|
|
|
try {
|
|
|
|
$("#encryptionPasswordDialog").dialog('close');
|
|
|
|
}
|
|
|
|
catch(e) {}
|
|
|
|
|
|
|
|
$("#encryptionPassword").val('');
|
|
|
|
|
2017-09-07 11:13:39 +08:00
|
|
|
decryptNoteIfNecessary(note);
|
|
|
|
|
|
|
|
$("#noteTitle").val(note.detail.note_title);
|
2017-06-12 09:04:04 +08:00
|
|
|
|
2017-09-05 09:28:07 +08:00
|
|
|
let noteText = notecase2html(note);
|
2017-08-29 09:41:04 +08:00
|
|
|
|
2017-09-05 09:28:07 +08:00
|
|
|
noteChangeDisabled = true;
|
2017-06-12 09:04:04 +08:00
|
|
|
|
2017-09-05 09:28:07 +08:00
|
|
|
// Clear contents and remove all stored history. This is to prevent undo from going across notes
|
|
|
|
$('#noteDetail').summernote('reset');
|
2017-08-23 09:32:03 +08:00
|
|
|
|
2017-09-05 09:28:07 +08:00
|
|
|
$('#noteDetail').summernote('code', noteText);
|
2017-08-16 10:32:30 +08:00
|
|
|
|
2017-09-05 09:28:07 +08:00
|
|
|
document.location.hash = noteId;
|
2017-08-23 10:40:54 +08:00
|
|
|
|
2017-09-05 09:28:07 +08:00
|
|
|
$(window).resize(); // to trigger resizing of editor
|
|
|
|
|
|
|
|
addRecentNote(noteId, note.detail.note_id);
|
|
|
|
|
|
|
|
noteChangeDisabled = false;
|
2017-09-05 09:37:55 +08:00
|
|
|
|
|
|
|
setNoteBackgroundIfEncrypted(note);
|
2017-09-05 09:28:07 +08:00
|
|
|
});
|
2017-06-12 04:04:07 +08:00
|
|
|
});
|
2017-08-22 08:34:17 +08:00
|
|
|
}
|
|
|
|
|
2017-08-27 22:30:32 +08:00
|
|
|
function addRecentNote(noteTreeId, noteContentId) {
|
2017-08-24 07:57:44 +08:00
|
|
|
const origDate = new Date();
|
2017-08-23 10:40:54 +08:00
|
|
|
|
2017-08-24 07:57:44 +08:00
|
|
|
setTimeout(function() {
|
|
|
|
// we include the note into recent list only if the user stayed on the note at least 5 seconds
|
2017-08-24 11:42:26 +08:00
|
|
|
if (noteTreeId === globalNote.detail.note_id || noteContentId === globalNote.detail.note_id) {
|
2017-08-24 07:57:44 +08:00
|
|
|
// if it's already there, remove the note
|
2017-09-07 09:11:19 +08:00
|
|
|
globalRecentNotes = globalRecentNotes.filter(note => note !== noteTreeId);
|
2017-08-24 07:57:44 +08:00
|
|
|
|
2017-09-07 09:11:19 +08:00
|
|
|
globalRecentNotes.unshift(noteTreeId);
|
2017-08-24 07:57:44 +08:00
|
|
|
}
|
|
|
|
}, 1500);
|
2017-08-23 10:40:54 +08:00
|
|
|
}
|
|
|
|
|