lychee.js 7.8 KB

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