better autosave - only once per 5 seconds with guaranteed save when changing notes

This commit is contained in:
azivner 2017-08-16 20:41:41 -04:00
parent 11742bba58
commit bcc4f22cf5
4 changed files with 27 additions and 5 deletions

View File

@ -11,11 +11,25 @@ let tags = {
let noteChangeDisabled = false; let noteChangeDisabled = false;
let isNoteChanged = false;
function noteChanged() { function noteChanged() {
if (noteChangeDisabled) { if (noteChangeDisabled) {
return; return;
} }
isNoteChanged = true;
}
function saveNoteIfChanged(callback) {
if (!isNoteChanged) {
if (callback) {
callback();
}
return;
}
let note = globalNote; let note = globalNote;
let contents = $('#noteDetail').summernote('code'); let contents = $('#noteDetail').summernote('code');
@ -36,7 +50,13 @@ function noteChanged() {
data: JSON.stringify(note), data: JSON.stringify(note),
contentType: "application/json", contentType: "application/json",
success: function(result) { success: function(result) {
isNoteChanged = false;
message("Saved!"); message("Saved!");
if (callback) {
callback();
}
}, },
error: function(result) { error: function(result) {
error("Error saving the note!"); error("Error saving the note!");
@ -44,6 +64,8 @@ function noteChanged() {
}); });
} }
setInterval(saveNoteIfChanged, 5000);
$(document).ready(function() { $(document).ready(function() {
$("#noteTitle").on('input', function() { $("#noteTitle").on('input', function() {
noteChanged(); noteChanged();

View File

@ -39,7 +39,7 @@ $(function(){
const node = data.node.data; const node = data.node.data;
const noteId = node.is_clone ? node.note_clone_id : node.note_id; const noteId = node.is_clone ? node.note_clone_id : node.note_id;
loadNote(noteId); saveNoteIfChanged(() => loadNote(noteId));
}, },
expand: function(event, data) { expand: function(event, data) {
setExpanded(data.node.key, true); setExpanded(data.node.key, true);

View File

@ -1,7 +1,7 @@
function message(str) { function message(str) {
$("#top-message").show(); $("#top-message").fadeIn(1500);
$("#top-message").html(str); $("#top-message").html(str);
$("#top-message").fadeOut(3000); $("#top-message").fadeOut(1500);
} }
function error(str) { function error(str) {

View File

@ -6,8 +6,8 @@
#top-message { #top-message {
display: none; /* initial state is hidden */ display: none; /* initial state is hidden */
background-color: greenyellow; background-color: #e0e0e0;
color: green; color: #333;
padding: 5px; padding: 5px;
border-radius: 10px; border-radius: 10px;
} }