2013-05-03 11:57:08 +00:00
|
|
|
/**
|
2014-01-22 10:12:51 +00:00
|
|
|
* @name Lychee Module
|
|
|
|
* @description This module provides the basic functions of Lychee.
|
|
|
|
* @author Tobias Reich
|
|
|
|
* @copyright 2014 by Tobias Reich
|
2013-05-03 11:57:08 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
var lychee = {
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2014-03-29 23:20:00 +00:00
|
|
|
title: "",
|
2014-09-19 21:32:35 +00:00
|
|
|
version: "2.6.3",
|
|
|
|
version_code: "020603",
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
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",
|
2013-09-03 09:59:30 +00:00
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
publicMode: false,
|
|
|
|
viewMode: false,
|
2014-06-15 17:11:54 +00:00
|
|
|
debugMode: false,
|
2013-06-17 18:40:04 +00:00
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
username: "",
|
|
|
|
checkForUpdates: false,
|
|
|
|
sorting: "",
|
2014-07-23 19:24:25 +00:00
|
|
|
location: "",
|
2013-09-03 09:59:30 +00:00
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
dropbox: false,
|
2014-02-23 18:41:12 +00:00
|
|
|
dropboxKey: '',
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
loadingBar: $("#loading"),
|
|
|
|
header: $("header"),
|
|
|
|
content: $("#content"),
|
|
|
|
imageview: $("#imageview"),
|
|
|
|
infobox: $("#infobox"),
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
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) {
|
2014-01-22 10:12:51 +00:00
|
|
|
|
|
|
|
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 || '';
|
2014-07-23 19:24:25 +00:00
|
|
|
lychee.location = data.config.location || '';
|
2014-01-22 10:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// No configuration
|
|
|
|
if (data==="Warning: No configuration!") {
|
|
|
|
lychee.header.hide();
|
|
|
|
lychee.content.hide();
|
|
|
|
$("body").append(build.no_content("cog"));
|
|
|
|
settings.createConfig();
|
|
|
|
return true;
|
|
|
|
}
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
// No login
|
|
|
|
if (data.config.login===false) {
|
|
|
|
settings.createLogin();
|
|
|
|
}
|
2013-06-17 18:40:04 +00:00
|
|
|
|
|
|
|
lychee.checkForUpdates = data.config.checkForUpdates;
|
|
|
|
$(window).bind("popstate", lychee.load);
|
|
|
|
lychee.load();
|
2014-01-22 10:12:51 +00:00
|
|
|
|
2013-05-03 11:57:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
api: function(params, callback, loading) {
|
2013-06-17 18:40:04 +00:00
|
|
|
|
2014-02-25 22:37:05 +00:00
|
|
|
if (loading===undefined) loadingBar.show();
|
2013-05-03 11:57:08 +00:00
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type: "POST",
|
|
|
|
url: lychee.api_path,
|
|
|
|
data: "function=" + params,
|
2014-01-22 10:12:51 +00:00
|
|
|
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);
|
2014-04-12 14:42:06 +00:00
|
|
|
upload.close(true);
|
2014-01-22 10:12:51 +00:00
|
|
|
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);
|
2014-01-22 10:12:51 +00:00
|
|
|
|
|
|
|
}
|
2013-05-03 11:57:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
login: function() {
|
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
var user = $("input#username").val(),
|
2014-04-01 19:33:29 +00:00
|
|
|
password = md5($("input#password").val()),
|
2013-09-03 09:59:30 +00:00
|
|
|
params;
|
2013-05-03 11:57:08 +00:00
|
|
|
|
|
|
|
params = "login&user=" + user + "&password=" + password;
|
2014-01-22 10:12:51 +00:00
|
|
|
lychee.api(params, function(data) {
|
2014-10-08 21:16:29 +00:00
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
if (data===true) {
|
2014-10-08 21:16:29 +00:00
|
|
|
|
|
|
|
// Use 'try' to catch a thrown error when Safari is in private mode
|
|
|
|
try { localStorage.setItem("lychee_username", user); }
|
|
|
|
|
2013-06-17 18:40:04 +00:00
|
|
|
window.location.reload();
|
2014-10-08 21:16:29 +00:00
|
|
|
|
2013-05-03 11:57:08 +00:00
|
|
|
} else {
|
2014-10-08 21:16:29 +00:00
|
|
|
|
|
|
|
// Show error and reactive button
|
2014-02-01 19:27:25 +00:00
|
|
|
$("#password").val("").addClass("error").focus();
|
2013-05-03 11:57:08 +00:00
|
|
|
$(".message .button.active").removeClass("pressed");
|
2014-10-08 21:16:29 +00:00
|
|
|
|
2013-05-03 11:57:08 +00:00
|
|
|
}
|
2014-10-08 21:16:29 +00:00
|
|
|
|
2013-05-03 11:57:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
loginDialog: function() {
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
var local_username;
|
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
$("body").append(build.signInModal());
|
|
|
|
$("#username").focus();
|
|
|
|
if (localStorage) {
|
2014-05-11 18:11:39 +00:00
|
|
|
local_username = localStorage.getItem("lychee_username");
|
2014-02-25 22:37:05 +00:00
|
|
|
if (local_username!==null) {
|
2013-09-03 09:59:30 +00:00
|
|
|
if (local_username.length>0) $("#username").val(local_username);
|
|
|
|
$("#password").focus();
|
|
|
|
}
|
|
|
|
}
|
2014-01-22 10:12:51 +00:00
|
|
|
if (lychee.checkForUpdates==="1") lychee.getUpdate();
|
2013-05-03 11:57:08 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
logout: function() {
|
2013-06-17 18:40:04 +00:00
|
|
|
|
2014-08-31 20:11:04 +00:00
|
|
|
lychee.api("logout", function() {
|
2013-09-03 09:59:30 +00:00
|
|
|
window.location.reload();
|
2013-06-17 18:40:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
goto: function(url) {
|
2013-06-17 18:40:04 +00:00
|
|
|
|
2014-08-17 18:54:57 +00:00
|
|
|
if (url===undefined) url = "#";
|
|
|
|
else url = "#" + url;
|
|
|
|
|
|
|
|
history.pushState(null, null, url);
|
|
|
|
lychee.load();
|
2013-06-17 18:40:04 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-05-03 11:57:08 +00:00
|
|
|
load: function() {
|
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
var albumID = "",
|
|
|
|
photoID = "",
|
2014-01-22 10:12:51 +00:00
|
|
|
hash = document.location.hash.replace("#", "").split("/");
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2014-05-30 19:07:15 +00:00
|
|
|
$(".no_content").remove();
|
2013-09-03 09:59:30 +00:00
|
|
|
contextMenu.close();
|
2014-01-28 23:43:06 +00:00
|
|
|
multiselect.close();
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
if (hash[0]!==undefined) albumID = hash[0];
|
|
|
|
if (hash[1]!==undefined) photoID = hash[1];
|
2013-05-03 11:57:08 +00:00
|
|
|
|
|
|
|
if (albumID&&photoID) {
|
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
// Trash data
|
|
|
|
photo.json = null;
|
|
|
|
|
|
|
|
// Show Photo
|
2014-01-22 10:12:51 +00:00
|
|
|
if (lychee.content.html()===""||($("#search").length&&$("#search").val().length!==0)) {
|
2013-05-03 11:57:08 +00:00
|
|
|
lychee.content.hide();
|
2013-09-03 09:59:30 +00:00
|
|
|
album.load(albumID, true);
|
2013-05-03 11:57:08 +00:00
|
|
|
}
|
2013-09-03 09:59:30 +00:00
|
|
|
photo.load(photoID, albumID);
|
2013-05-03 11:57:08 +00:00
|
|
|
|
|
|
|
} else if (albumID) {
|
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
// Trash data
|
|
|
|
photo.json = null;
|
|
|
|
|
2013-05-03 11:57:08 +00:00
|
|
|
// Show Album
|
2013-09-03 09:59:30 +00:00
|
|
|
if (visible.photo()) view.photo.hide();
|
|
|
|
if (album.json&&albumID==album.json.id) view.album.title();
|
|
|
|
else album.load(albumID);
|
2013-05-03 11:57:08 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
// Trash data
|
|
|
|
album.json = null;
|
|
|
|
photo.json = null;
|
|
|
|
search.code = "";
|
|
|
|
|
2013-05-03 11:57:08 +00:00
|
|
|
// Show Albums
|
2014-01-22 10:12:51 +00:00
|
|
|
if (visible.album()) view.album.hide();
|
2013-09-03 09:59:30 +00:00
|
|
|
if (visible.photo()) view.photo.hide();
|
2013-05-03 11:57:08 +00:00
|
|
|
albums.load();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
getUpdate: function() {
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
$.ajax({
|
|
|
|
url: lychee.update_path,
|
2014-08-08 21:03:26 +00:00
|
|
|
success: function(data) { if (parseInt(data)>parseInt(lychee.version_code)) $("#version span").show(); }
|
2013-09-03 09:59:30 +00:00
|
|
|
});
|
2013-05-03 11:57:08 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
setTitle: function(title, editable) {
|
2013-09-03 09:59:30 +00:00
|
|
|
|
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;
|
2013-09-03 09:59:30 +00:00
|
|
|
|
|
|
|
if (editable) $("#title").addClass("editable");
|
|
|
|
else $("#title").removeClass("editable");
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
$("#title").html(title);
|
2013-05-03 11:57:08 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
setMode: function(mode) {
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
$("#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();
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
$(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");
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
Mousetrap
|
2014-04-05 14:03:14 +00:00
|
|
|
.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']);
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
if (mode==="public") {
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
$("header #button_signin, header #hostedwith").show();
|
2013-09-03 09:59:30 +00:00
|
|
|
lychee.publicMode = true;
|
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
} else if (mode==="view") {
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
Mousetrap.unbind('esc');
|
|
|
|
$("#button_back, a#next, a#previous").remove();
|
2014-02-14 18:03:26 +00:00
|
|
|
$(".no_content").remove();
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
lychee.publicMode = true;
|
|
|
|
lychee.viewMode = true;
|
|
|
|
|
|
|
|
}
|
2013-05-03 11:57:08 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
animate: function(obj, animation) {
|
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
var animations = [
|
2013-05-03 11:57:08 +00:00
|
|
|
["fadeIn", "fadeOut"],
|
|
|
|
["contentZoomIn", "contentZoomOut"]
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!obj.jQuery) obj = $(obj);
|
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
for (var i = 0; i < animations.length; i++) {
|
2013-05-03 11:57:08 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-05-11 22:15:01 +00:00
|
|
|
escapeHTML: function(s) {
|
|
|
|
|
|
|
|
return s.replace(/&/g, '&')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>');
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-03 09:59:30 +00:00
|
|
|
loadDropbox: function(callback) {
|
|
|
|
|
2014-02-23 21:42:15 +00:00
|
|
|
if (!lychee.dropbox&&lychee.dropboxKey) {
|
2013-09-03 09:59:30 +00:00
|
|
|
|
|
|
|
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);
|
2013-09-03 09:59:30 +00:00
|
|
|
g.onload = g.onreadystatechange = function() {
|
|
|
|
var rs = this.readyState;
|
2014-01-22 10:12:51 +00:00
|
|
|
if (rs&&rs!=="complete"&&rs!=="loaded") return;
|
2013-09-03 09:59:30 +00:00
|
|
|
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
|
|
|
}
|
2013-09-03 09:59:30 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-06-14 19:38:37 +00:00
|
|
|
removeHTML: function(html) {
|
|
|
|
|
|
|
|
var tmp = document.createElement("DIV");
|
|
|
|
tmp.innerHTML = html;
|
|
|
|
return tmp.textContent || tmp.innerText;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
error: function(errorThrown, params, data) {
|
2013-05-03 11:57:08 +00:00
|
|
|
|
2014-08-16 18:18:24 +00:00
|
|
|
console.error({
|
|
|
|
description: errorThrown,
|
|
|
|
params: params,
|
|
|
|
response: data
|
|
|
|
});
|
|
|
|
|
2014-01-22 10:12:51 +00:00
|
|
|
loadingBar.show("error", errorThrown);
|
2013-05-03 11:57:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-25 22:37:05 +00:00
|
|
|
};
|