lychee.js 6.9 KB

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