// Global variables
var kindlemail = "";
var currentpath = "";
var searchTimeout = null;
$(document).ready(function() {
getContents();
$("#bookshome").on("click", function() { setPath("/"); });
$("#curpath").on("click", function() { setPath($("#curpath").data("newpath")); });
$("#amazonlink").on("click", function() {
$("#edit_kindle").val(kindlemail);
$("#setkindle").dialog("open");
});
$("#setkindle").dialog({
autoOpen: false,
height: 225,
width: 450,
closeOnEscape: true,
draggable: false,
modal: true,
close: function() {
$("#edit_kindle").val("");
},
buttons: [
{
text: "Save",
click: function() { setKindleMail($("#edit_kindle").val()); }
},
{
text: "Cancel",
click: function() { $(this).dialog("close"); }
}
]
});
$("#search").on("change keyup", function() {
if ( searchTimeout ) clearTimeout(searchTimeout);
if ( $("#search").val().length > 2 ) searchTimeout = setTimeout(getContents, 500);
});
});
function showKindleMail() {
$("#amazonlink").html("KindleMail: " + ((kindlemail != "") ? kindlemail : "Click to Set"));
}
function redirectToLogin() {
console.log("Redirecting for login...");
window.location.replace("index.php");
}
function getContents() {
if ( searchTimeout ) clearTimeout(searchTimeout);
$.ajax({
url : 'ajax/getcontents.php',
data : {search: $("#search").val()},
dataType : 'json',
success : function(data, stat, jqo) {
if ( data.validated == false ) redirectToLogin();
if ( data.kindlemail != kindlemail ) {
kindlemail = data.kindlemail;
showKindleMail();
}
if ( data.bookdir != currentpath ) {
currentpath = data.bookdir;
if ( data.bookdirname != "" ) {
$("#curpath").text(data.bookdirname);
$("#curpath").data("newpath", data.bookdir);
$("#curpath").removeClass("hide");
} else {
$("#curpath").addClass("hide");
}
}
var contents = "";
data.contents.forEach(function(item) {
if ( item.folder == true ) {
contents += '
' + item.displayname + "
";
} else {
contents += '";
}
});
$("#foldercontents").html(contents);
$(".folder").on("click", function(e) { setPath($(e.currentTarget).data("newpath")); });
$(".tokindle").on("click", function(e) {
var target = $(e.currentTarget);
sendToKindle(target.data("fileid"), target.data("filename")); });
}
});
}
function setPath(newpath) {
$.ajax({
url: 'ajax/setpath.php',
data: {newpath: newpath},
dataType: 'json',
success: function(data, stat, jqo) {
if ( data.validated == false ) redirectToLogin();
if ( data.changed == true ) {
$("#search").val("");
getContents();
}
}
});
}
function setKindleMail(email) {
$.ajax({
url: 'ajax/setkindle.php',
data: {kindlemail: email},
dataType: 'json',
success: function(data, stat, jqo) {
if ( data.validated == false ) redirectToLogin();
if ( data.success == false ) {
var myToast = $.toast({
text : "Error: Invalid Email Address
",
showHideTransition : 'slide',
bgColor : '#cc0000',
textColor : '#eee',
hideAfter : 4000,
textAlign : 'left',
position : 'top-right'
});
} else {
kindlemail = email;
showKindleMail();
$("#setkindle").dialog("close");
$("#edit_kindle").val("");
var myToast = $.toast({
text : "Kindle Email Address set successfully
",
showHideTransition : 'slide',
bgColor : '#137c1e',
textColor : '#eee',
hideAfter : 4000,
textAlign : 'left',
position : 'top-right'
});
}
}
});
}
function sendToKindle(fileid, filename) {
var myToast = $.toast({
text : "Sending: " + filename + "
",
showHideTransition : 'slide',
bgColor : '#033e9e',
textColor : '#eee',
hideAfter : false,
textAlign : 'left',
position : 'top-right'
});
$.ajax({
url : 'ajax/sendtokindle.php',
data : {fileid: fileid},
dataType : 'json',
success : function(data, stat, jqo) {
if ( data.validated == false ) redirectToLogin();
myToast.reset();
var message = "";
message = "Sent File: " + data.filename + "
";
message += "To: " + data.tomail + "
";
message += "Don't forget to add \"books@jaj.com\" ";
message += "to your approved senders.
";
$.toast({
text : message,
showHideTransition : 'slide',
bgColor : '#137c1e',
textColor : '#eee',
hideAfter : 10000,
textAlign : 'left',
position : 'top-right'
});
}
});
}