lychee.js 7.4 KB

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