lychee.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * @name lychee.js
  3. * @author Philipp Maurer
  4. * @author Tobias Reich
  5. * @copyright 2013 by Philipp Maurer, Tobias Reich
  6. *
  7. * Lychee Module
  8. * This module provides the basic functions of Lychee.
  9. */
  10. var lychee = {
  11. init: function() {
  12. this.version = "1.3.1";
  13. this.api_path = "php/api.php";
  14. this.update_path = "http://lychee.electerious.com/version/index.php";
  15. this.updateURL = "https://github.com/electerious/Lychee";
  16. this.upload_path_thumb = "uploads/thumb/";
  17. this.upload_path_big = "uploads/big/";
  18. this.publicMode = false;
  19. this.viewMode = false;
  20. this.checkForUpdates = false;
  21. this.dropbox = false;
  22. this.loadingBar = $("#loading");
  23. this.header = $("header");
  24. this.content = $("#content");
  25. this.imageview = $("#imageview");
  26. this.infobox = $("#infobox");
  27. },
  28. run: function() {
  29. lychee.api("init", "json", function(data) {
  30. lychee.checkForUpdates = data.config.checkForUpdates;
  31. if (!data.loggedIn) lychee.setMode("public");
  32. $(window).bind("popstate", lychee.load);
  33. lychee.load();
  34. });
  35. },
  36. api: function(params, type, callback, loading) {
  37. if (loading==undefined) loadingBar.show();
  38. $.ajax({
  39. type: "POST",
  40. url: lychee.api_path,
  41. data: "function=" + params,
  42. dataType: type,
  43. success:
  44. function(data) {
  45. setTimeout(function() { loadingBar.hide() }, 100);
  46. callback(data);
  47. },
  48. error: lychee.error
  49. });
  50. },
  51. login: function() {
  52. var user = $("input#username").val(),
  53. password = hex_md5($("input#password").val()),
  54. params;
  55. params = "login&user=" + user + "&password=" + password;
  56. lychee.api(params, "text", function(data) {
  57. if (data) {
  58. localStorage.setItem("username", user);
  59. window.location.reload();
  60. } else {
  61. $("#password").val("").addClass("error");
  62. $(".message .button.active").removeClass("pressed");
  63. }
  64. });
  65. },
  66. loginDialog: function() {
  67. $("body").append(build.signInModal());
  68. $("#username").focus();
  69. if (localStorage) {
  70. local_username = localStorage.getItem("username");
  71. if (local_username!=null) {
  72. if (local_username.length>0) $("#username").val(local_username);
  73. $("#password").focus();
  74. }
  75. }
  76. if (lychee.checkForUpdates) lychee.getUpdate();
  77. },
  78. logout: function() {
  79. lychee.api("logout", "text", function(data) {
  80. window.location.reload();
  81. });
  82. },
  83. goto: function(url) {
  84. if (url==undefined) url = "";
  85. document.location.hash = url;
  86. },
  87. load: function() {
  88. var albumID = "",
  89. photoID = "",
  90. hash = document.location.hash.replace("#", "");
  91. contextMenu.close();
  92. if (hash.indexOf("a")!=-1) albumID = hash.split("p")[0].replace("a", "");
  93. if (hash.indexOf("p")!=-1) photoID = hash.split("p")[1];
  94. if (albumID&&photoID) {
  95. // Trash data
  96. albums.json = null;
  97. photo.json = null;
  98. // Show Photo
  99. if (lychee.content.html()==""||($("#search").length&&$("#search").val().length!=0)) {
  100. lychee.content.hide();
  101. album.load(albumID, true);
  102. }
  103. if (!visible.photo()) view.photo.show();
  104. photo.load(photoID, albumID);
  105. } else if (albumID) {
  106. // Trash data
  107. albums.json = null;
  108. photo.json = null;
  109. // Show Album
  110. if (visible.photo()) view.photo.hide();
  111. if (album.json&&albumID==album.json.id) view.album.title();
  112. else album.load(albumID);
  113. } else {
  114. // Trash data
  115. albums.json = null;
  116. album.json = null;
  117. photo.json = null;
  118. search.code = "";
  119. // Show Albums
  120. if (visible.photo()) view.photo.hide();
  121. albums.load();
  122. }
  123. },
  124. getUpdate: function() {
  125. $.ajax({
  126. url: lychee.update_path,
  127. success: function(data) { if (data!=lychee.version) $("#version span").show(); }
  128. });
  129. },
  130. setTitle: function(title, count, editable) {
  131. if (title=="Albums") document.title = "Lychee";
  132. else document.title = "Lychee - " + title;
  133. if (count) title += "<span> - " + count + " photos</span>";
  134. if (editable) $("#title").addClass("editable");
  135. else $("#title").removeClass("editable");
  136. $("#title").html(title);
  137. },
  138. setMode: function(mode) {
  139. $("#button_signout, #search, #button_trash_album, #button_share_album, #button_edit_album, .button_add, #button_archive, .button_divider").remove();
  140. $("#button_trash, #button_move, #button_edit, #button_share, #button_star").remove();
  141. $(document)
  142. .on("mouseenter", "#title.editable", function() { $(this).removeClass("editable") })
  143. .off("click", "#title.editable")
  144. .off("touchend", "#title.editable")
  145. .off("contextmenu", ".photo")
  146. .off("contextmenu", ".album")
  147. .off("drop");
  148. Mousetrap
  149. .unbind('n')
  150. .unbind('u')
  151. .unbind('s')
  152. .unbind('backspace');
  153. if (mode=="public") {
  154. $("#button_signin").show();
  155. lychee.publicMode = true;
  156. } else if (mode=="view") {
  157. Mousetrap.unbind('esc');
  158. $("#button_back, a#next, a#previous").remove();
  159. lychee.publicMode = true;
  160. lychee.viewMode = true;
  161. }
  162. },
  163. animate: function(obj, animation) {
  164. var animations = [
  165. ["fadeIn", "fadeOut"],
  166. ["contentZoomIn", "contentZoomOut"]
  167. ];
  168. if (!obj.jQuery) obj = $(obj);
  169. for (var i = 0; i < animations.length; i++) {
  170. for (var x = 0; x < animations[i].length; x++) {
  171. if (animations[i][x]==animation) {
  172. obj.removeClass(animations[i][0] + " " + animations[i][1]).addClass(animation);
  173. return true;
  174. }
  175. }
  176. }
  177. return false;
  178. },
  179. loadDropbox: function(callback) {
  180. if (!lychee.dropbox) {
  181. loadingBar.show();
  182. var g = document.createElement("script"),
  183. s = document.getElementsByTagName("script")[0];
  184. g.src = "https://www.dropbox.com/static/api/1/dropins.js";
  185. g.id = "dropboxjs";
  186. g.type = "text/javascript";
  187. g.async = "true";
  188. g.setAttribute("data-app-key", "iq7lioj9wu0ieqs");
  189. g.onload = g.onreadystatechange = function() {
  190. var rs = this.readyState;
  191. if (rs&&rs!="complete"&&rs!="loaded") return;
  192. lychee.dropbox = true;
  193. loadingBar.hide();
  194. callback();
  195. };
  196. s.parentNode.insertBefore(g, s);
  197. } else callback();
  198. },
  199. error: function(jqXHR, textStatus, errorThrown) {
  200. console.log(jqXHR);
  201. console.log(textStatus);
  202. console.log(errorThrown);
  203. loadingBar.show("error", textStatus, errorThrown);
  204. }
  205. }