lychee/assets/js/lychee.js

346 lines
7.4 KiB
JavaScript
Raw Normal View History

/**
* @name Lychee Module
* @description This module provides the basic functions of Lychee.
* @author Tobias Reich
* @copyright 2014 by Tobias Reich
*/
var lychee = {
2014-03-29 23:20:00 +00:00
title: "",
2014-04-05 16:30:24 +00:00
version: "2.5 b1",
version_code: "020500",
api_path: "php/api.php",
update_path: "http://lychee.electerious.com/version/index.php",
updateURL: "https://github.com/electerious/Lychee",
website: "http://lychee.electerious.com",
upload_path_thumb: "uploads/thumb/",
upload_path_big: "uploads/big/",
publicMode: false,
viewMode: false,
debugMode: true,
username: "",
checkForUpdates: false,
sorting: "",
dropbox: false,
2014-02-23 18:41:12 +00:00
dropboxKey: '',
loadingBar: $("#loading"),
header: $("header"),
content: $("#content"),
imageview: $("#imageview"),
infobox: $("#infobox"),
init: function() {
2014-02-23 18:41:12 +00:00
var params;
2014-04-05 16:30:24 +00:00
params = "init&version=" + lychee.version_code;
2014-02-23 18:41:12 +00:00
lychee.api(params, function(data) {
if (data.loggedIn!==true) {
lychee.setMode("public");
} else {
2014-02-23 21:42:15 +00:00
lychee.username = data.config.username || '';
lychee.sorting = data.config.sorting || '';
lychee.dropboxKey = data.config.dropboxKey || '';
}
// No configuration
if (data==="Warning: No configuration!") {
lychee.header.hide();
lychee.content.hide();
$("body").append(build.no_content("cog"));
settings.createConfig();
return true;
}
// No login
if (data.config.login===false) {
settings.createLogin();
}
lychee.checkForUpdates = data.config.checkForUpdates;
$(window).bind("popstate", lychee.load);
lychee.load();
});
},
api: function(params, callback, loading) {
2014-02-25 22:37:05 +00:00
if (loading===undefined) loadingBar.show();
$.ajax({
type: "POST",
url: lychee.api_path,
data: "function=" + params,
dataType: "text",
success: function(data) {
setTimeout(function() { loadingBar.hide() }, 100);
if (typeof data==="string"&&data.substring(0, 7)==="Error: ") {
lychee.error(data.substring(7, data.length), params, data);
return false;
}
if (data==="1") data = true;
else if (data==="") data = false;
if (typeof data==="string"&&data.substring(0, 1)==="{"&&data.substring(data.length-1, data.length)==="}") data = $.parseJSON(data);
if (lychee.debugMode) console.log(data);
callback(data);
},
error: function(jqXHR, textStatus, errorThrown) {
lychee.error("Server error or API not found.", params, errorThrown);
2014-03-16 12:17:29 +00:00
upload.close(true);
}
});
},
login: function() {
var user = $("input#username").val(),
password = md5($("input#password").val()),
params;
params = "login&user=" + user + "&password=" + password;
lychee.api(params, function(data) {
if (data===true) {
localStorage.setItem("username", user);
window.location.reload();
} else {
$("#password").val("").addClass("error").focus();
$(".message .button.active").removeClass("pressed");
}
});
},
loginDialog: function() {
var local_username;
$("body").append(build.signInModal());
$("#username").focus();
if (localStorage) {
local_username = localStorage.getItem("username");
2014-02-25 22:37:05 +00:00
if (local_username!==null) {
if (local_username.length>0) $("#username").val(local_username);
$("#password").focus();
}
}
if (lychee.checkForUpdates==="1") lychee.getUpdate();
},
logout: function() {
lychee.api("logout", function(data) {
window.location.reload();
});
},
goto: function(url) {
2014-02-25 22:37:05 +00:00
if (url===undefined) url = "";
document.location.hash = url;
},
load: function() {
var albumID = "",
photoID = "",
hash = document.location.hash.replace("#", "").split("/");
contextMenu.close();
2014-01-28 23:43:06 +00:00
multiselect.close();
if (hash[0]!==undefined) albumID = hash[0];
if (hash[1]!==undefined) photoID = hash[1];
if (albumID&&photoID) {
// Trash data
albums.json = null;
photo.json = null;
// Show Photo
if (lychee.content.html()===""||($("#search").length&&$("#search").val().length!==0)) {
lychee.content.hide();
album.load(albumID, true);
}
photo.load(photoID, albumID);
} else if (albumID) {
// Trash data
albums.json = null;
photo.json = null;
// Show Album
if (visible.photo()) view.photo.hide();
if (album.json&&albumID==album.json.id) view.album.title();
else album.load(albumID);
} else {
// Trash data
albums.json = null;
album.json = null;
photo.json = null;
search.code = "";
// Show Albums
if (visible.album()) view.album.hide();
if (visible.photo()) view.photo.hide();
albums.load();
}
},
getUpdate: function() {
$.ajax({
url: lychee.update_path,
success: function(data) { if (data!=lychee.version) $("#version span").show(); }
});
},
setTitle: function(title, editable) {
2014-03-29 23:20:00 +00:00
if (lychee.title==="") lychee.title = document.title;
if (title==="Albums") document.title = lychee.title;
else document.title = lychee.title + " - " + title;
if (editable) $("#title").addClass("editable");
else $("#title").removeClass("editable");
$("#title").html(title);
},
setMode: function(mode) {
$("#button_settings, #button_settings, #button_search, #search, #button_trash_album, #button_share_album, .button_add, .button_divider").remove();
$("#button_trash, #button_move, #button_share, #button_star").remove();
$(document)
.on("mouseenter", "#title.editable", function() { $(this).removeClass("editable") })
.off("click", "#title.editable")
.off("touchend", "#title.editable")
.off("contextmenu", ".photo")
.off("contextmenu", ".album")
.off("drop");
Mousetrap
.unbind(['u', 'ctrl+u'])
.unbind(['s', 'ctrl+s'])
.unbind(['r', 'ctrl+r'])
.unbind(['d', 'ctrl+d'])
.unbind(['t', 'ctrl+t'])
.unbind(['command+backspace', 'ctrl+backspace']);
if (mode==="public") {
$("header #button_signin, header #hostedwith").show();
lychee.publicMode = true;
} else if (mode==="view") {
Mousetrap.unbind('esc');
$("#button_back, a#next, a#previous").remove();
$(".no_content").remove();
lychee.publicMode = true;
lychee.viewMode = true;
}
},
animate: function(obj, animation) {
var animations = [
["fadeIn", "fadeOut"],
["contentZoomIn", "contentZoomOut"]
];
if (!obj.jQuery) obj = $(obj);
for (var i = 0; i < animations.length; i++) {
for (var x = 0; x < animations[i].length; x++) {
if (animations[i][x]==animation) {
obj.removeClass(animations[i][0] + " " + animations[i][1]).addClass(animation);
return true;
}
}
}
return false;
},
loadDropbox: function(callback) {
2014-02-23 21:42:15 +00:00
if (!lychee.dropbox&&lychee.dropboxKey) {
loadingBar.show();
var g = document.createElement("script"),
s = document.getElementsByTagName("script")[0];
g.src = "https://www.dropbox.com/static/api/1/dropins.js";
g.id = "dropboxjs";
g.type = "text/javascript";
g.async = "true";
2014-02-23 18:41:12 +00:00
g.setAttribute("data-app-key", lychee.dropboxKey);
g.onload = g.onreadystatechange = function() {
var rs = this.readyState;
if (rs&&rs!=="complete"&&rs!=="loaded") return;
lychee.dropbox = true;
loadingBar.hide();
callback();
};
s.parentNode.insertBefore(g, s);
2014-02-23 21:42:15 +00:00
} else if (lychee.dropbox&&lychee.dropboxKey) {
callback();
} else {
settings.setDropboxKey(callback);
2014-02-25 22:37:05 +00:00
}
},
error: function(errorThrown, params, data) {
console.log("Error Description: " + errorThrown);
console.log("Error Params: " + params);
console.log("Server Response: " + data);
loadingBar.show("error", errorThrown);
}
2014-02-25 22:37:05 +00:00
};