lychee.js 6.8 KB

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