lychee.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. lychee = {
  11. init: function(api_path, upload_path) {
  12. this.version = "1.1";
  13. this.api_path = api_path;
  14. this.upload_path = upload_path;
  15. this.loadingBar = $("#loading");
  16. this.header = $("header");
  17. this.headerTitle = $("#title");
  18. this.content = $("#content");
  19. this.image_view = $("#image_view");
  20. this.infobox = $("#infobox");
  21. },
  22. ready: function() {
  23. $("#tools_albums").show();
  24. if (!mobileBrowser()) $(".tools").tipsy({gravity: 'n'});
  25. if (window.webkitNotifications) { window.webkitNotifications.requestPermission() };
  26. lychee.api("loggedIn", "text", function(data) {
  27. if (data!=1) {
  28. $("body").append(build.signInModal());
  29. $("#username").focus();
  30. if (localStorage) {
  31. local_username = localStorage.getItem("username");
  32. if (local_username==null) return false;
  33. if (local_username.length>1) $("#username").val(local_username);
  34. $("#password").focus();
  35. }
  36. } else if (data) {
  37. $(window).bind("popstate", lychee.load);
  38. lychee.load();
  39. }
  40. });
  41. },
  42. api: function(params, type, callback) {
  43. $.ajax({
  44. type: "POST",
  45. url: lychee.api_path,
  46. data: "function=" + params,
  47. dataType: type,
  48. success: callback,
  49. error: lychee.error
  50. });
  51. },
  52. login: function() {
  53. user = $("input#username").val();
  54. password = hex_md5($("input#password").val());
  55. params = "login&user=" + user + "&password=" + password;
  56. lychee.api(params, "text", function(data) {
  57. if (data) {
  58. if (localStorage) { localStorage.setItem("username", user); }
  59. $(window).bind("popstate", lychee.load);
  60. lychee.load();
  61. lychee.closeModal();
  62. } else {
  63. $("#password").val("").addClass("error");
  64. $(".message .button.active").removeClass("pressed");
  65. }
  66. });
  67. },
  68. logout: function() {
  69. lychee.api("logout", "text", function(data) {
  70. window.location.reload();
  71. });
  72. },
  73. upload: function(files) {
  74. pre_progress = 0;
  75. $(".upload_overlay").remove();
  76. $("body").append(build.uploadModal());
  77. var formData = new FormData();
  78. for (var i = 0; i < files.length; i++) formData.append(i, files[i]);
  79. formData.append("function", "upload");
  80. if (lychee.content.attr("data-id")=="") formData.append("albumID", 0);
  81. else formData.append("albumID", lychee.content.attr("data-id"));
  82. var xhr = new XMLHttpRequest();
  83. xhr.open('POST', lychee.api_path);
  84. xhr.onload = function () {
  85. if (xhr.status===200) {
  86. $(".progressbar div").css("width", "100%");
  87. $(".upload_overlay").removeClass("fadeIn").css("opacity", 0);
  88. $.timer(300,function(){ $(".upload_overlay").remove() });
  89. if (window.webkitNotifications&&BrowserDetect.browser=="Safari") {
  90. var popup = window.webkitNotifications.createNotification("", "Upload complete", "You can now manage your new photos.");
  91. popup.show();
  92. }
  93. if (lychee.content.attr("data-id")=="") lychee.goto("a0");
  94. else photos.load(lychee.content.attr("data-id"));
  95. }
  96. };
  97. xhr.upload.onprogress = function (event) {
  98. if (event.lengthComputable) {
  99. var progress = (event.loaded / event.total * 100 | 0);
  100. if (progress>pre_progress) {
  101. $(".progressbar div").css("width", progress + "%");
  102. pre_progress = progress;
  103. }
  104. if (progress>=100) $(".progressbar div").css("opacity", 0.2);
  105. }
  106. };
  107. $("#upload_files").val("");
  108. xhr.send(formData);
  109. },
  110. load: function() {
  111. contextMenu.close();
  112. hash = document.location.hash.replace("#", "");
  113. albumID = "";
  114. photoID = "";
  115. if (hash.indexOf("a")!=-1) albumID = hash.split("p")[0].replace("a", ""); else albumID = "";
  116. if (hash.indexOf("p")!=-1) photoID = hash.split("p")[1]; else photoID = "";
  117. lychee.content.attr("data-id", albumID);
  118. lychee.image_view.attr("data-id", photoID);
  119. if (albumID&&photoID) {
  120. // Show ImageView
  121. if (lychee.content.html()==""||$("#search").val().length!=0) {
  122. lychee.content.hide();
  123. photos.load(albumID, true);
  124. }
  125. photos.loadInfo(photoID);
  126. } else if (albumID) {
  127. // Show Album
  128. if (visible.infobox) photos.hideInfobox();
  129. if (!visible.controls()) lychee.showControls();
  130. if (visible.imageview()) photos.hideView();
  131. else photos.load(albumID, false);
  132. } else {
  133. // Show Albums
  134. if (visible.infobox) photos.hideInfobox();
  135. if (!visible.controls()) lychee.showControls();
  136. if (visible.imageview()) photos.hideView();
  137. albums.load();
  138. }
  139. },
  140. goto: function(url) {
  141. document.location.hash = url;
  142. },
  143. title: function() {
  144. return lychee.headerTitle.html().replace($("#title span").html(), "").replace("<span></span>", "");
  145. },
  146. showControls: function() {
  147. clearTimeout($(window).data("timeout"));
  148. if (visible.imageview()) {
  149. lychee.image_view.removeClass("full");
  150. lychee.loadingBar.css("opacity", 1);
  151. lychee.header.css("margin-Top", "0px");
  152. if ($("#image_view #image.small").length>0) {
  153. $("#image_view #image").css({
  154. marginTop: -1*($("#image_view #image").height()/2)+20
  155. });
  156. } else {
  157. $("#image_view #image").css({
  158. top: 70,
  159. right: 30,
  160. bottom: 30,
  161. left: 30
  162. });
  163. }
  164. }
  165. },
  166. hideControls: function() {
  167. if (visible.imageview()) {
  168. clearTimeout($(window).data("timeout"));
  169. $(window).data("timeout", setTimeout(function() {
  170. lychee.image_view.addClass("full");
  171. lychee.loadingBar.css("opacity", 0);
  172. lychee.header.css("margin-Top", "-45px");
  173. if ($("#image_view #image.small").length>0) {
  174. $("#image_view #image").css({
  175. marginTop: -1*($("#image_view #image").height()/2)
  176. });
  177. } else {
  178. $("#image_view #image").css({
  179. top: 0,
  180. right: 0,
  181. bottom: 0,
  182. left: 0
  183. });
  184. }
  185. }, 500));
  186. }
  187. },
  188. closeModal: function() {
  189. $(".message_overlay").removeClass("fadeIn").css("opacity", 0);
  190. $.timer(300,function(){ $(".message_overlay").remove() });
  191. },
  192. animate: function(obj, animation) {
  193. animations = [
  194. ["fadeIn", "fadeOut"],
  195. ["contentZoomIn", "contentZoomOut"]
  196. ];
  197. if (!obj.jQuery) obj = $(obj);
  198. for (i = 0; i < animations.length; i++) {
  199. for (var x = 0; x < animations[i].length; x++) {
  200. if (animations[i][x]==animation) {
  201. obj.removeClass(animations[i][0] + " " + animations[i][1]).addClass(animation);
  202. return true;
  203. }
  204. }
  205. }
  206. return false;
  207. },
  208. error: function(jqXHR, textStatus, errorThrown) {
  209. console.log(jqXHR);
  210. console.log(textStatus);
  211. console.log(errorThrown);
  212. loadingBar.show("error", textStatus, errorThrown);
  213. }
  214. }