diff options
Diffstat (limited to 'static/js/app.js')
| -rw-r--r-- | static/js/app.js | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/static/js/app.js b/static/js/app.js new file mode 100644 index 0000000..4abf214 --- /dev/null +++ b/static/js/app.js @@ -0,0 +1,179 @@ +var timeInterval; +var isInterval = false; +scribble_history = []; +unshift = 0; + +function autosave() { + + // If autosave is set, it saves every 3 seconds and everytime memo looses focus + if (localStorage.getItem("autosave") == "true"){ + if ( ! isInterval ) { + timeInterval = setInterval( function(){ + save("#memo", ""); + }, 6000); + isInterval = true; + } + } +}; + +function registerAutosave(id){ + $(id).addClass("btn-danger"); + $(id).html('<span class="glyphicon glyphicon-remove-circle"></span> Disable Autosave'); + localStorage.setItem("autosave", "true"); + $("#post").blur( function(){ save("#memo", "") } ); + autosave(); +} + +// Function to save memo +function save(id, manually){ + $.ajax({ + url: "/api/new", + data: $(id).serialize(), + dataType: "json", + type: "POST", + + success: function(response){ + if ( response != null && response.success){ + addHistory(response); + if (manually === "manually"){ + if (window.location.pathname != "/" + response.data.Title) { + window.location.href = response.data.Url + "/../../"+ response.data.Title; + } + swal("Saved!", "Your notice was successfully saved!", "success"); + } else if (manually === "view") { + window.location.href = response.data.Url; + } + } else { + swal({ + title: "Saving failed", + text: "There was an error. The website says: \""+response.data.error + "\"", + type: "warning", + showCancelButton: true, + confirmButtonClass: "btn-danger", + confirmButtonText: "Retry", + closeOnConfirm: false, + }, + function(){save(id, manually);}) + } + }, + }) +}; + +/* +function search(){ + $.ajax({ + url: "/api/get", + data: $('#search').serialize(), + dataType: "json", + type: "GET", + + success: function(r){ + if(r.success == 200){ + window.location.href = r.data.Url; + } else { + window.location.href = $('#search').val(); + } + } + + error: function(r){ + console.log("Not found"); + window.location.href = $('#search').val(); + } + }) +}; +*/ + +$(document).ready(function(){ + + if(!localStorage["history"]){ + localStorage["history"] = JSON.stringify(scribble_history); + } + scribble_history = JSON.parse(localStorage["history"]); + + for(var i = 0; i<scribble_history.length; i++){ + $('#history').append(scribble_history[i].Value); + } + /* + if(unshift === 1){ + // Create sidebar entry + var url = $('#root-anchor').data("url"); + var title = $('#root-anchor').data("title"); + var newTab = '<li><a href="'+url+'"><span class="number">1</span> '+title+'</a></li>'; + scribble_history.unshift({ + Key: title, + Value: newTab + }); + } + */ + + // Register click handler on save button to avoid full page refresh + $("#save").click( function(e){ + save("#memo", "manually"); + e.preventDefault(); + }); + $(".btn-disabled").click( function(e){ + e.preventDefault(); + }); + + if( localStorage.getItem("autosave") == "true"){ + registerAutosave("#autosave"); + }; + + $('#autosave').click(function() { + $(this).toggleClass("btn-danger").promise().done(function(){ + if ( $(this).html().match(/Enable/) ){ + registerAutosave(this); + } else { + $(this).html('<span class="glyphicon glyphicon-floppy-open"></span> Enable Autosave'); + localStorage.setItem("autosave", "false") + clearInterval(timeInterval); + isInterval = false; + $("#post").off("blur"); + } + }); + }); + + $('#view').click(function(e) { + e.preventDefault(); + save("#memo", "view"); + }); + + /* + $('#search').click(function(e){ + e.preventDefault(); + search(); + }); + */ + +}); + +function addHistory(response) { + + // Do not allow first save on page + + // Do not allow duplicates + for(i=0;i<scribble_history.length;i++){ + if (scribble_history[i].Key == response.data.Title){ + return; + } + } + + // Create sidebar entry + var url = response.data.Url.replace("/view", ""); + var l = scribble_history.length +1; + var newTab = '<li><a href="'+url+'"><span class="number">'+l+'</span> '+response.data.Title+'</a></li>'; + + // Push to array + scribble_history.push({ + Key: response.data.Title, + Value: newTab + }); + + //Save in memory + localStorage["history"] = JSON.stringify(scribble_history); + + // Append to sidebar + if( $('#root-anchor').data('title') != response.data.Title) { + $('#history').append(newTab); + } +}; |
