lychee.js 7.8 KB

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