lychee.js 7.1 KB

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