lychee.js 6.8 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 b2",
  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").focus();
  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. multiselect.close();
  119. if (hash[0]!==undefined) albumID = hash[0];
  120. if (hash[1]!==undefined) photoID = hash[1];
  121. if (albumID&&photoID) {
  122. // Trash data
  123. albums.json = null;
  124. photo.json = null;
  125. // Show Photo
  126. if (lychee.content.html()===""||($("#search").length&&$("#search").val().length!==0)) {
  127. lychee.content.hide();
  128. album.load(albumID, true);
  129. }
  130. photo.load(photoID, albumID);
  131. } else if (albumID) {
  132. // Trash data
  133. albums.json = null;
  134. photo.json = null;
  135. // Show Album
  136. if (visible.photo()) view.photo.hide();
  137. if (album.json&&albumID==album.json.id) view.album.title();
  138. else album.load(albumID);
  139. } else {
  140. // Trash data
  141. albums.json = null;
  142. album.json = null;
  143. photo.json = null;
  144. search.code = "";
  145. // Show Albums
  146. if (visible.album()) view.album.hide();
  147. if (visible.photo()) view.photo.hide();
  148. albums.load();
  149. }
  150. },
  151. getUpdate: function() {
  152. $.ajax({
  153. url: lychee.update_path,
  154. success: function(data) { if (data!=lychee.version) $("#version span").show(); }
  155. });
  156. },
  157. setTitle: function(title, editable) {
  158. if (title==="Albums") document.title = "Lychee";
  159. else document.title = "Lychee - " + title;
  160. if (editable) $("#title").addClass("editable");
  161. else $("#title").removeClass("editable");
  162. $("#title").html(title);
  163. },
  164. setMode: function(mode) {
  165. $("#button_settings, #button_settings, #button_search, #search, #button_trash_album, #button_share_album, .button_add, .button_divider").remove();
  166. $("#button_trash, #button_move, #button_share, #button_star").remove();
  167. $(document)
  168. .on("mouseenter", "#title.editable", function() { $(this).removeClass("editable") })
  169. .off("click", "#title.editable")
  170. .off("touchend", "#title.editable")
  171. .off("contextmenu", ".photo")
  172. .off("contextmenu", ".album")
  173. .off("drop");
  174. Mousetrap
  175. .unbind('n')
  176. .unbind('u')
  177. .unbind('s')
  178. .unbind('backspace');
  179. if (mode==="public") {
  180. $("header #button_signin, header #hostedwith").show();
  181. lychee.publicMode = true;
  182. } else if (mode==="view") {
  183. Mousetrap.unbind('esc');
  184. $("#button_back, a#next, a#previous").remove();
  185. $(".no_content").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. }