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(' Disable Autosave');
localStorage.setItem("autosave", "true");
$("#post").blur( function(){ save("#memo", "") } );
autosave();
}
// Function to save memo
function save(id, manually){
if ( !$('#Title').val().trim() ){
swal({
title: "Title required",
text: "In order to save please enter a title.",
type: "warning",
confirmButtonClass: "btn-danger",
confirmButtonText: "Close",
closeOnConfirm: false,
});
return;
}
$.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; i1 '+title+'';
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() {
if ( !$('#Title').val().trim() ){
swal({
title: "Title required",
text: "In order to save please enter a title.",
type: "warning",
confirmButtonClass: "btn-danger",
confirmButtonText: "Close",
closeOnConfirm: false
});
return;
}
$(this).toggleClass("btn-danger").promise().done(function(){
if ( $(this).html().match(/Enable/) ){
registerAutosave(this);
} else {
$(this).html(' 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'+l+' '+response.data.Title+'';
// 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);
}
};