lychee.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /**
  2. * @name Lychee Module
  3. * @description This module provides the basic functions of Lychee.
  4. * @author Tobias Reich
  5. * @copyright 2014 by Tobias Reich
  6. */
  7. var lychee = {
  8. title: "",
  9. version: "2.5.5",
  10. version_code: "020505",
  11. api_path: "php/api.php",
  12. update_path: "http://lychee.electerious.com/version/index.php",
  13. updateURL: "https://github.com/electerious/Lychee",
  14. website: "http://lychee.electerious.com",
  15. publicMode: false,
  16. viewMode: false,
  17. debugMode: false,
  18. username: "",
  19. checkForUpdates: false,
  20. sorting: "",
  21. dropbox: false,
  22. dropboxKey: '',
  23. loadingBar: $("#loading"),
  24. header: $("header"),
  25. content: $("#content"),
  26. imageview: $("#imageview"),
  27. infobox: $("#infobox"),
  28. init: function() {
  29. var params;
  30. params = "init&version=" + lychee.version_code;
  31. lychee.api(params, function(data) {
  32. if (data.loggedIn!==true) {
  33. lychee.setMode("public");
  34. } else {
  35. lychee.username = data.config.username || '';
  36. lychee.sorting = data.config.sorting || '';
  37. lychee.dropboxKey = data.config.dropboxKey || '';
  38. }
  39. // No configuration
  40. if (data==="Warning: No configuration!") {
  41. lychee.header.hide();
  42. lychee.content.hide();
  43. $("body").append(build.no_content("cog"));
  44. settings.createConfig();
  45. return true;
  46. }
  47. // No login
  48. if (data.config.login===false) {
  49. settings.createLogin();
  50. }
  51. lychee.checkForUpdates = data.config.checkForUpdates;
  52. $(window).bind("popstate", lychee.load);
  53. lychee.load();
  54. });
  55. },
  56. api: function(params, callback, loading) {
  57. if (loading===undefined) loadingBar.show();
  58. $.ajax({
  59. type: "POST",
  60. url: lychee.api_path,
  61. data: "function=" + params,
  62. dataType: "text",
  63. success: function(data) {
  64. setTimeout(function() { loadingBar.hide() }, 100);
  65. if (typeof data==="string"&&data.substring(0, 7)==="Error: ") {
  66. lychee.error(data.substring(7, data.length), params, data);
  67. upload.close(true);
  68. return false;
  69. }
  70. if (data==="1") data = true;
  71. else if (data==="") data = false;
  72. if (typeof data==="string"&&data.substring(0, 1)==="{"&&data.substring(data.length-1, data.length)==="}") data = $.parseJSON(data);
  73. if (lychee.debugMode) console.log(data);
  74. callback(data);
  75. },
  76. error: function(jqXHR, textStatus, errorThrown) {
  77. lychee.error("Server error or API not found.", params, errorThrown);
  78. upload.close(true);
  79. }
  80. });
  81. },
  82. login: function() {
  83. var user = $("input#username").val(),
  84. password = md5($("input#password").val()),
  85. params;
  86. params = "login&user=" + user + "&password=" + password;
  87. lychee.api(params, function(data) {
  88. if (data===true) {
  89. localStorage.setItem("lychee_username", user);
  90. window.location.reload();
  91. } else {
  92. $("#password").val("").addClass("error").focus();
  93. $(".message .button.active").removeClass("pressed");
  94. }
  95. });
  96. },
  97. loginDialog: function() {
  98. var local_username;
  99. $("body").append(build.signInModal());
  100. $("#username").focus();
  101. if (localStorage) {
  102. local_username = localStorage.getItem("lychee_username");
  103. if (local_username!==null) {
  104. if (local_username.length>0) $("#username").val(local_username);
  105. $("#password").focus();
  106. }
  107. }
  108. if (lychee.checkForUpdates==="1") lychee.getUpdate();
  109. },
  110. logout: function() {
  111. lychee.api("logout", function(data) {
  112. window.location.reload();
  113. });
  114. },
  115. goto: function(url) {
  116. if (url===undefined) url = "";
  117. document.location.hash = url;
  118. },
  119. load: function() {
  120. var albumID = "",
  121. photoID = "",
  122. hash = document.location.hash.replace("#", "").split("/");
  123. $(".no_content").remove();
  124. contextMenu.close();
  125. multiselect.close();
  126. if (hash[0]!==undefined) albumID = hash[0];
  127. if (hash[1]!==undefined) photoID = hash[1];
  128. if (albumID&&photoID) {
  129. // Trash data
  130. albums.json = null;
  131. photo.json = null;
  132. // Show Photo
  133. if (lychee.content.html()===""||($("#search").length&&$("#search").val().length!==0)) {
  134. lychee.content.hide();
  135. album.load(albumID, true);
  136. }
  137. photo.load(photoID, albumID);
  138. } else if (albumID) {
  139. // Trash data
  140. albums.json = null;
  141. photo.json = null;
  142. // Show Album
  143. if (visible.photo()) view.photo.hide();
  144. if (album.json&&albumID==album.json.id) view.album.title();
  145. else album.load(albumID);
  146. } else {
  147. // Trash data
  148. albums.json = null;
  149. album.json = null;
  150. photo.json = null;
  151. search.code = "";
  152. // Show Albums
  153. if (visible.album()) view.album.hide();
  154. if (visible.photo()) view.photo.hide();
  155. albums.load();
  156. }
  157. },
  158. getUpdate: function() {
  159. $.ajax({
  160. url: lychee.update_path,
  161. success: function(data) { if (data!=lychee.version) $("#version span").show(); }
  162. });
  163. },
  164. setTitle: function(title, editable) {
  165. if (lychee.title==="") lychee.title = document.title;
  166. if (title==="Albums") document.title = lychee.title;
  167. else document.title = lychee.title + " - " + title;
  168. if (editable) $("#title").addClass("editable");
  169. else $("#title").removeClass("editable");
  170. $("#title").html(title);
  171. },
  172. setMode: function(mode) {
  173. $("#button_settings, #button_settings, #button_search, #search, #button_trash_album, #button_share_album, .button_add, .button_divider").remove();
  174. $("#button_trash, #button_move, #button_share, #button_star").remove();
  175. $(document)
  176. .on("mouseenter", "#title.editable", function() { $(this).removeClass("editable") })
  177. .off("click", "#title.editable")
  178. .off("touchend", "#title.editable")
  179. .off("contextmenu", ".photo")
  180. .off("contextmenu", ".album")
  181. .off("drop");
  182. Mousetrap
  183. .unbind(['u', 'ctrl+u'])
  184. .unbind(['s', 'ctrl+s'])
  185. .unbind(['r', 'ctrl+r'])
  186. .unbind(['d', 'ctrl+d'])
  187. .unbind(['t', 'ctrl+t'])
  188. .unbind(['command+backspace', 'ctrl+backspace']);
  189. if (mode==="public") {
  190. $("header #button_signin, header #hostedwith").show();
  191. lychee.publicMode = true;
  192. } else if (mode==="view") {
  193. Mousetrap.unbind('esc');
  194. $("#button_back, a#next, a#previous").remove();
  195. $(".no_content").remove();
  196. lychee.publicMode = true;
  197. lychee.viewMode = true;
  198. }
  199. },
  200. animate: function(obj, animation) {
  201. var animations = [
  202. ["fadeIn", "fadeOut"],
  203. ["contentZoomIn", "contentZoomOut"]
  204. ];
  205. if (!obj.jQuery) obj = $(obj);
  206. for (var i = 0; i < animations.length; i++) {
  207. for (var x = 0; x < animations[i].length; x++) {
  208. if (animations[i][x]==animation) {
  209. obj.removeClass(animations[i][0] + " " + animations[i][1]).addClass(animation);
  210. return true;
  211. }
  212. }
  213. }
  214. return false;
  215. },
  216. loadDropbox: function(callback) {
  217. if (!lychee.dropbox&&lychee.dropboxKey) {
  218. loadingBar.show();
  219. var g = document.createElement("script"),
  220. s = document.getElementsByTagName("script")[0];
  221. g.src = "https://www.dropbox.com/static/api/1/dropins.js";
  222. g.id = "dropboxjs";
  223. g.type = "text/javascript";
  224. g.async = "true";
  225. g.setAttribute("data-app-key", lychee.dropboxKey);
  226. g.onload = g.onreadystatechange = function() {
  227. var rs = this.readyState;
  228. if (rs&&rs!=="complete"&&rs!=="loaded") return;
  229. lychee.dropbox = true;
  230. loadingBar.hide();
  231. callback();
  232. };
  233. s.parentNode.insertBefore(g, s);
  234. } else if (lychee.dropbox&&lychee.dropboxKey) {
  235. callback();
  236. } else {
  237. settings.setDropboxKey(callback);
  238. }
  239. },
  240. removeHTML: function(html) {
  241. var tmp = document.createElement("DIV");
  242. tmp.innerHTML = html;
  243. return tmp.textContent || tmp.innerText;
  244. },
  245. error: function(errorThrown, params, data) {
  246. console.log("Error Description: " + errorThrown);
  247. console.log("Error Params: " + params);
  248. console.log("Server Response: " + data);
  249. loadingBar.show("error", errorThrown);
  250. }
  251. };