lychee.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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.6.3",
  10. version_code: "020603",
  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. publicMode: false,
  16. viewMode: false,
  17. debugMode: false,
  18. username: "",
  19. checkForUpdates: false,
  20. sorting: "",
  21. location: "",
  22. dropbox: false,
  23. dropboxKey: '',
  24. loadingBar: $("#loading"),
  25. header: $("header"),
  26. content: $("#content"),
  27. imageview: $("#imageview"),
  28. infobox: $("#infobox"),
  29. init: function() {
  30. var params;
  31. params = "init&version=" + lychee.version_code;
  32. lychee.api(params, function(data) {
  33. if (data.loggedIn!==true) {
  34. lychee.setMode("public");
  35. } else {
  36. lychee.username = data.config.username || '';
  37. lychee.sorting = data.config.sorting || '';
  38. lychee.dropboxKey = data.config.dropboxKey || '';
  39. lychee.location = data.config.location || '';
  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. // Use 'try' to catch a thrown error when Safari is in private mode
  92. try { localStorage.setItem("lychee_username", user); }
  93. window.location.reload();
  94. } else {
  95. // Show error and reactive button
  96. $("#password").val("").addClass("error").focus();
  97. $(".message .button.active").removeClass("pressed");
  98. }
  99. });
  100. },
  101. loginDialog: function() {
  102. var local_username;
  103. $("body").append(build.signInModal());
  104. $("#username").focus();
  105. if (localStorage) {
  106. local_username = localStorage.getItem("lychee_username");
  107. if (local_username!==null) {
  108. if (local_username.length>0) $("#username").val(local_username);
  109. $("#password").focus();
  110. }
  111. }
  112. if (lychee.checkForUpdates==="1") lychee.getUpdate();
  113. },
  114. logout: function() {
  115. lychee.api("logout", function() {
  116. window.location.reload();
  117. });
  118. },
  119. goto: function(url) {
  120. if (url===undefined) url = "#";
  121. else url = "#" + url;
  122. history.pushState(null, null, url);
  123. lychee.load();
  124. },
  125. load: function() {
  126. var albumID = "",
  127. photoID = "",
  128. hash = document.location.hash.replace("#", "").split("/");
  129. $(".no_content").remove();
  130. contextMenu.close();
  131. multiselect.close();
  132. if (hash[0]!==undefined) albumID = hash[0];
  133. if (hash[1]!==undefined) photoID = hash[1];
  134. if (albumID&&photoID) {
  135. // Trash data
  136. photo.json = null;
  137. // Show Photo
  138. if (lychee.content.html()===""||($("#search").length&&$("#search").val().length!==0)) {
  139. lychee.content.hide();
  140. album.load(albumID, true);
  141. }
  142. photo.load(photoID, albumID);
  143. } else if (albumID) {
  144. // Trash data
  145. photo.json = null;
  146. // Show Album
  147. if (visible.photo()) view.photo.hide();
  148. if (album.json&&albumID==album.json.id) view.album.title();
  149. else album.load(albumID);
  150. } else {
  151. // Trash data
  152. album.json = null;
  153. photo.json = null;
  154. search.code = "";
  155. // Show Albums
  156. if (visible.album()) view.album.hide();
  157. if (visible.photo()) view.photo.hide();
  158. albums.load();
  159. }
  160. },
  161. getUpdate: function() {
  162. $.ajax({
  163. url: lychee.update_path,
  164. success: function(data) { if (parseInt(data)>parseInt(lychee.version_code)) $("#version span").show(); }
  165. });
  166. },
  167. setTitle: function(title, editable) {
  168. if (lychee.title==="") lychee.title = document.title;
  169. if (title==="Albums") document.title = lychee.title;
  170. else document.title = lychee.title + " - " + title;
  171. if (editable) $("#title").addClass("editable");
  172. else $("#title").removeClass("editable");
  173. $("#title").html(title);
  174. },
  175. setMode: function(mode) {
  176. $("#button_settings, #button_settings, #button_search, #search, #button_trash_album, #button_share_album, .button_add, .button_divider").remove();
  177. $("#button_trash, #button_move, #button_share, #button_star").remove();
  178. $(document)
  179. .on("mouseenter", "#title.editable", function() { $(this).removeClass("editable") })
  180. .off("click", "#title.editable")
  181. .off("touchend", "#title.editable")
  182. .off("contextmenu", ".photo")
  183. .off("contextmenu", ".album")
  184. .off("drop");
  185. Mousetrap
  186. .unbind(['u', 'ctrl+u'])
  187. .unbind(['s', 'ctrl+s'])
  188. .unbind(['r', 'ctrl+r'])
  189. .unbind(['d', 'ctrl+d'])
  190. .unbind(['t', 'ctrl+t'])
  191. .unbind(['command+backspace', 'ctrl+backspace']);
  192. if (mode==="public") {
  193. $("header #button_signin, header #hostedwith").show();
  194. lychee.publicMode = true;
  195. } else if (mode==="view") {
  196. Mousetrap.unbind('esc');
  197. $("#button_back, a#next, a#previous").remove();
  198. $(".no_content").remove();
  199. lychee.publicMode = true;
  200. lychee.viewMode = true;
  201. }
  202. },
  203. animate: function(obj, animation) {
  204. var animations = [
  205. ["fadeIn", "fadeOut"],
  206. ["contentZoomIn", "contentZoomOut"]
  207. ];
  208. if (!obj.jQuery) obj = $(obj);
  209. for (var i = 0; i < animations.length; i++) {
  210. for (var x = 0; x < animations[i].length; x++) {
  211. if (animations[i][x]==animation) {
  212. obj.removeClass(animations[i][0] + " " + animations[i][1]).addClass(animation);
  213. return true;
  214. }
  215. }
  216. }
  217. return false;
  218. },
  219. escapeHTML: function(s) {
  220. return s.replace(/&/g, '&amp;')
  221. .replace(/"/g, '&quot;')
  222. .replace(/</g, '&lt;')
  223. .replace(/>/g, '&gt;');
  224. },
  225. loadDropbox: function(callback) {
  226. if (!lychee.dropbox&&lychee.dropboxKey) {
  227. loadingBar.show();
  228. var g = document.createElement("script"),
  229. s = document.getElementsByTagName("script")[0];
  230. g.src = "https://www.dropbox.com/static/api/1/dropins.js";
  231. g.id = "dropboxjs";
  232. g.type = "text/javascript";
  233. g.async = "true";
  234. g.setAttribute("data-app-key", lychee.dropboxKey);
  235. g.onload = g.onreadystatechange = function() {
  236. var rs = this.readyState;
  237. if (rs&&rs!=="complete"&&rs!=="loaded") return;
  238. lychee.dropbox = true;
  239. loadingBar.hide();
  240. callback();
  241. };
  242. s.parentNode.insertBefore(g, s);
  243. } else if (lychee.dropbox&&lychee.dropboxKey) {
  244. callback();
  245. } else {
  246. settings.setDropboxKey(callback);
  247. }
  248. },
  249. removeHTML: function(html) {
  250. var tmp = document.createElement("DIV");
  251. tmp.innerHTML = html;
  252. return tmp.textContent || tmp.innerText;
  253. },
  254. error: function(errorThrown, params, data) {
  255. console.error({
  256. description: errorThrown,
  257. params: params,
  258. response: data
  259. });
  260. loadingBar.show("error", errorThrown);
  261. }
  262. };