lychee.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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.2";
  13. this.api_path = api_path;
  14. this.upload_path = upload_path;
  15. this.update_path = "http://lychee.electerious.com/version/index.php";
  16. this.updateURL = "https://github.com/electerious/Lychee";
  17. this.publicMode = false;
  18. this.checkForUpdates = false;
  19. this.bitlyUsername = "";
  20. this.loadingBar = $("#loading");
  21. this.header = $("header");
  22. this.headerTitle = $("#title");
  23. this.content = $("#content");
  24. this.image_view = $("#image_view");
  25. this.infobox = $("#infobox");
  26. },
  27. ready: function() {
  28. if (!mobileBrowser()) $(".tools").tipsy({gravity: 'n'});
  29. if (window.webkitNotifications) window.webkitNotifications.requestPermission();
  30. lychee.api("init", "json", function(data) {
  31. lychee.checkForUpdates = data.config.checkForUpdates;
  32. lychee.bitlyUsername = data.config.bitlyUsername;
  33. if (!data.loggedIn) lychee.setPublicMode();
  34. $(window).bind("popstate", lychee.load);
  35. lychee.load();
  36. });
  37. },
  38. setPublicMode: function() {
  39. this.publicMode = true;
  40. $("#button_signout, #search, #button_trash_album, #button_share_album, #button_edit_album, .button_add, #button_archive, .button_divider").remove();
  41. $("#button_trash, #button_move, #button_edit, #button_share, #button_star").remove();
  42. $(document)
  43. .on("mouseenter", "#title.editable", function() { $(this).removeClass("editable") })
  44. .off(event_name, "#title.editable")
  45. .off("contextmenu", ".photo")
  46. .off("contextmenu", ".album")
  47. .off("drop");
  48. $("#button_signin").show();
  49. },
  50. api: function(params, type, callback, loading) {
  51. if (loading==undefined) loadingBar.show();
  52. $.ajax({
  53. type: "POST",
  54. url: lychee.api_path,
  55. data: "function=" + params,
  56. dataType: type,
  57. success:
  58. function(data) {
  59. $.timer(100, function(){ loadingBar.hide() });
  60. callback(data);
  61. },
  62. error: lychee.error
  63. });
  64. },
  65. showLogin: function() {
  66. $("body").append(build.signInModal());
  67. $("#username").focus();
  68. if (localStorage) {
  69. local_username = localStorage.getItem("username");
  70. if (local_username==null) return false;
  71. if (local_username.length>0) $("#username").val(local_username);
  72. $("#password").focus();
  73. }
  74. if (lychee.checkForUpdates) lychee.update();
  75. },
  76. login: function() {
  77. user = $("input#username").val();
  78. password = hex_md5($("input#password").val());
  79. params = "login&user=" + user + "&password=" + password;
  80. lychee.api(params, "text", function(data) {
  81. if (data) {
  82. localStorage.setItem("username", user);
  83. window.location.reload();
  84. } else {
  85. $("#password").val("").addClass("error");
  86. $(".message .button.active").removeClass("pressed");
  87. }
  88. });
  89. },
  90. logout: function() {
  91. lychee.api("logout", "text", function(data) {
  92. window.location.reload();
  93. });
  94. },
  95. update: function() {
  96. $.ajax({
  97. url: lychee.update_path,
  98. success: function(data) { if (data!=lychee.version) $("#version span").show(); }
  99. });
  100. },
  101. upload: function(files) {
  102. pre_progress = 0;
  103. $(".upload_overlay").remove();
  104. $("body").append(build.uploadModal());
  105. var formData = new FormData();
  106. for (var i = 0; i < files.length; i++) formData.append(i, files[i]);
  107. formData.append("function", "upload");
  108. if (lychee.content.attr("data-id")=="") formData.append("albumID", 0);
  109. else formData.append("albumID", lychee.content.attr("data-id"));
  110. var xhr = new XMLHttpRequest();
  111. xhr.open('POST', lychee.api_path);
  112. xhr.onload = function () {
  113. if (xhr.status===200) {
  114. $(".progressbar div").css("width", "100%");
  115. $(".upload_overlay").removeClass("fadeIn").css("opacity", 0);
  116. $.timer(300,function(){ $(".upload_overlay").remove() });
  117. if (window.webkitNotifications&&BrowserDetect.browser=="Safari") {
  118. var popup = window.webkitNotifications.createNotification("", "Upload complete", "You can now manage your new photos.");
  119. popup.show();
  120. }
  121. if (lychee.content.attr("data-id")=="") lychee.goto("a0");
  122. else photos.load(lychee.content.attr("data-id"));
  123. }
  124. };
  125. xhr.upload.onprogress = function (event) {
  126. if (event.lengthComputable) {
  127. var progress = (event.loaded / event.total * 100 | 0);
  128. if (progress>pre_progress) {
  129. $(".progressbar div").css("width", progress + "%");
  130. pre_progress = progress;
  131. }
  132. if (progress>=100) $(".progressbar div").css("opacity", 0.2);
  133. }
  134. };
  135. $("#upload_files").val("");
  136. xhr.send(formData);
  137. },
  138. importUrl: function() {
  139. link = prompt("Please enter the direct link to a photo to import it:", "");
  140. if (lychee.content.attr("data-id")=="") albumID = 0;
  141. else albumID = lychee.content.attr("data-id");
  142. lychee.closeModal();
  143. if (link.length>3) {
  144. params = "importUrl&url=" + escape(link) + "&albumID=" + albumID;
  145. lychee.api(params, "text", function(data) {
  146. if (data) {
  147. if (lychee.content.attr("data-id")=="") lychee.goto("a0");
  148. else photos.load(lychee.content.attr("data-id"));
  149. } else loadingBar.show("error");
  150. });
  151. } else if (link.length>0) loadingBar.show("error", "Error", "Link to short or too long. Please try another one!");
  152. },
  153. load: function() {
  154. contextMenu.close();
  155. hash = document.location.hash.replace("#", "");
  156. albumID = "";
  157. photoID = "";
  158. if (hash.indexOf("a")!=-1) albumID = hash.split("p")[0].replace("a", ""); else albumID = "";
  159. if (hash.indexOf("p")!=-1) photoID = hash.split("p")[1]; else photoID = "";
  160. lychee.content.attr("data-id", albumID);
  161. lychee.image_view.attr("data-id", photoID);
  162. if (albumID&&photoID) {
  163. // Show ImageView
  164. if (lychee.content.html()==""||($("#search").length&&$("#search").val().length!=0)) {
  165. lychee.content.hide();
  166. photos.load(albumID, true);
  167. }
  168. photos.loadInfo(photoID, albumID);
  169. } else if (albumID) {
  170. // Show Album
  171. if (visible.infobox) photos.hideInfobox();
  172. if (!visible.controls()) lychee.showControls();
  173. if (visible.imageview()) photos.hideView();
  174. else photos.load(albumID, false);
  175. } else {
  176. // Show Albums
  177. if (visible.infobox) photos.hideInfobox();
  178. if (!visible.controls()) lychee.showControls();
  179. if (visible.imageview()) photos.hideView();
  180. albums.load();
  181. }
  182. },
  183. goto: function(url) {
  184. document.location.hash = url;
  185. },
  186. title: function() {
  187. return lychee.headerTitle.html().replace($("#title span").html(), "").replace("<span></span>", "");
  188. },
  189. showControls: function() {
  190. clearTimeout($(window).data("timeout"));
  191. if (visible.imageview()) {
  192. lychee.image_view.removeClass("full");
  193. lychee.loadingBar.css("opacity", 1);
  194. lychee.header.css("margin-Top", "0px");
  195. if ($("#image_view #image.small").length>0) {
  196. $("#image_view #image").css({
  197. marginTop: -1*($("#image_view #image").height()/2)+20
  198. });
  199. } else {
  200. $("#image_view #image").css({
  201. top: 70,
  202. right: 30,
  203. bottom: 30,
  204. left: 30
  205. });
  206. }
  207. }
  208. },
  209. hideControls: function() {
  210. if (visible.imageview()&&!visible.infobox()) {
  211. clearTimeout($(window).data("timeout"));
  212. $(window).data("timeout", setTimeout(function() {
  213. lychee.image_view.addClass("full");
  214. lychee.loadingBar.css("opacity", 0);
  215. lychee.header.css("margin-Top", "-45px");
  216. if ($("#image_view #image.small").length>0) {
  217. $("#image_view #image").css({
  218. marginTop: -1*($("#image_view #image").height()/2)
  219. });
  220. } else {
  221. $("#image_view #image").css({
  222. top: 0,
  223. right: 0,
  224. bottom: 0,
  225. left: 0
  226. });
  227. }
  228. }, 500));
  229. }
  230. },
  231. closeModal: function() {
  232. $(".message_overlay").removeClass("fadeIn").css("opacity", 0);
  233. $.timer(300,function(){ $(".message_overlay").remove() });
  234. },
  235. animate: function(obj, animation) {
  236. animations = [
  237. ["fadeIn", "fadeOut"],
  238. ["contentZoomIn", "contentZoomOut"]
  239. ];
  240. if (!obj.jQuery) obj = $(obj);
  241. for (i = 0; i < animations.length; i++) {
  242. for (var x = 0; x < animations[i].length; x++) {
  243. if (animations[i][x]==animation) {
  244. obj.removeClass(animations[i][0] + " " + animations[i][1]).addClass(animation);
  245. return true;
  246. }
  247. }
  248. }
  249. return false;
  250. },
  251. error: function(jqXHR, textStatus, errorThrown) {
  252. console.log(jqXHR);
  253. console.log(textStatus);
  254. console.log(errorThrown);
  255. loadingBar.show("error", textStatus, errorThrown);
  256. }
  257. }