lychee.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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.2",
  10. api_path: "php/api.php",
  11. update_path: "http://lychee.electerious.com/version/index.php",
  12. updateURL: "https://github.com/electerious/Lychee",
  13. website: "http://lychee.electerious.com",
  14. upload_path_thumb: "uploads/thumb/",
  15. upload_path_big: "uploads/big/",
  16. publicMode: false,
  17. viewMode: false,
  18. debugMode: false,
  19. username: "",
  20. checkForUpdates: false,
  21. sorting: "",
  22. dropbox: false,
  23. dropboxKey: '',
  24. loadingBar: $("#loading"),
  25. header: $("header"),
  26. content: $("#content"),
  27. imageview: $("#imageview"),
  28. infobox: $("#infobox"),
  29. init: function() {
  30. var params;
  31. params = "init&version=" + escape(lychee.version);
  32. lychee.api(params, function(data) {
  33. if (data.loggedIn!==true) {
  34. lychee.setMode("public");
  35. } else {
  36. lychee.username = data.config.username || '';
  37. lychee.sorting = data.config.sorting || '';
  38. lychee.dropboxKey = data.config.dropboxKey || '';
  39. }
  40. // No configuration
  41. if (data==="Warning: No configuration!") {
  42. lychee.header.hide();
  43. lychee.content.hide();
  44. $("body").append(build.no_content("cog"));
  45. settings.createConfig();
  46. return true;
  47. }
  48. // No login
  49. if (data.config.login===false) {
  50. settings.createLogin();
  51. }
  52. lychee.checkForUpdates = data.config.checkForUpdates;
  53. $(window).bind("popstate", lychee.load);
  54. lychee.load();
  55. });
  56. },
  57. api: function(params, callback, loading) {
  58. if (loading===undefined) loadingBar.show();
  59. $.ajax({
  60. type: "POST",
  61. url: lychee.api_path,
  62. data: "function=" + params,
  63. dataType: "text",
  64. success: function(data) {
  65. setTimeout(function() { loadingBar.hide() }, 100);
  66. if (typeof data==="string"&&data.substring(0, 7)==="Error: ") {
  67. lychee.error(data.substring(7, data.length), params, data);
  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("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("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. contextMenu.close();
  124. multiselect.close();
  125. if (hash[0]!==undefined) albumID = hash[0];
  126. if (hash[1]!==undefined) photoID = hash[1];
  127. if (albumID&&photoID) {
  128. // Trash data
  129. albums.json = null;
  130. photo.json = null;
  131. // Show Photo
  132. if (lychee.content.html()===""||($("#search").length&&$("#search").val().length!==0)) {
  133. lychee.content.hide();
  134. album.load(albumID, true);
  135. }
  136. photo.load(photoID, albumID);
  137. } else if (albumID) {
  138. // Trash data
  139. albums.json = null;
  140. photo.json = null;
  141. // Show Album
  142. if (visible.photo()) view.photo.hide();
  143. if (album.json&&albumID==album.json.id) view.album.title();
  144. else album.load(albumID);
  145. } else {
  146. // Trash data
  147. albums.json = null;
  148. album.json = null;
  149. photo.json = null;
  150. search.code = "";
  151. // Show Albums
  152. if (visible.album()) view.album.hide();
  153. if (visible.photo()) view.photo.hide();
  154. albums.load();
  155. }
  156. },
  157. getUpdate: function() {
  158. $.ajax({
  159. url: lychee.update_path,
  160. success: function(data) { if (data!=lychee.version) $("#version span").show(); }
  161. });
  162. },
  163. setTitle: function(title, editable) {
  164. if (lychee.title==="") lychee.title = document.title;
  165. if (title==="Albums") document.title = lychee.title;
  166. else document.title = lychee.title + " - " + title;
  167. if (editable) $("#title").addClass("editable");
  168. else $("#title").removeClass("editable");
  169. $("#title").html(title);
  170. },
  171. setMode: function(mode) {
  172. $("#button_settings, #button_settings, #button_search, #search, #button_trash_album, #button_share_album, .button_add, .button_divider").remove();
  173. $("#button_trash, #button_move, #button_share, #button_star").remove();
  174. $(document)
  175. .on("mouseenter", "#title.editable", function() { $(this).removeClass("editable") })
  176. .off("click", "#title.editable")
  177. .off("touchend", "#title.editable")
  178. .off("contextmenu", ".photo")
  179. .off("contextmenu", ".album")
  180. .off("drop");
  181. Mousetrap
  182. .unbind('n')
  183. .unbind('u')
  184. .unbind('s')
  185. .unbind('backspace');
  186. if (mode==="public") {
  187. $("header #button_signin, header #hostedwith").show();
  188. lychee.publicMode = true;
  189. } else if (mode==="view") {
  190. Mousetrap.unbind('esc');
  191. $("#button_back, a#next, a#previous").remove();
  192. $(".no_content").remove();
  193. lychee.publicMode = true;
  194. lychee.viewMode = true;
  195. }
  196. },
  197. animate: function(obj, animation) {
  198. var animations = [
  199. ["fadeIn", "fadeOut"],
  200. ["contentZoomIn", "contentZoomOut"]
  201. ];
  202. if (!obj.jQuery) obj = $(obj);
  203. for (var i = 0; i < animations.length; i++) {
  204. for (var x = 0; x < animations[i].length; x++) {
  205. if (animations[i][x]==animation) {
  206. obj.removeClass(animations[i][0] + " " + animations[i][1]).addClass(animation);
  207. return true;
  208. }
  209. }
  210. }
  211. return false;
  212. },
  213. loadDropbox: function(callback) {
  214. if (!lychee.dropbox&&lychee.dropboxKey) {
  215. loadingBar.show();
  216. var g = document.createElement("script"),
  217. s = document.getElementsByTagName("script")[0];
  218. g.src = "https://www.dropbox.com/static/api/1/dropins.js";
  219. g.id = "dropboxjs";
  220. g.type = "text/javascript";
  221. g.async = "true";
  222. g.setAttribute("data-app-key", lychee.dropboxKey);
  223. g.onload = g.onreadystatechange = function() {
  224. var rs = this.readyState;
  225. if (rs&&rs!=="complete"&&rs!=="loaded") return;
  226. lychee.dropbox = true;
  227. loadingBar.hide();
  228. callback();
  229. };
  230. s.parentNode.insertBefore(g, s);
  231. } else if (lychee.dropbox&&lychee.dropboxKey) {
  232. callback();
  233. } else {
  234. settings.setDropboxKey(callback);
  235. }
  236. },
  237. error: function(errorThrown, params, data) {
  238. console.log("Error Description: " + errorThrown);
  239. console.log("Error Params: " + params);
  240. console.log("Server Response: " + data);
  241. loadingBar.show("error", errorThrown);
  242. }
  243. };