photos.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /**
  2. * @name photos.js
  3. * @author Philipp Maurer
  4. * @author Tobias Reich
  5. * @copyright 2013 by Philipp Maurer, Tobias Reich
  6. *
  7. * Photos Module
  8. * Takes care of every action photos can handle and execute.
  9. */
  10. photos = {
  11. load: function(albumID, refresh) {
  12. // If refresh is true the function will only refresh the content and not change the toolbar buttons either the title
  13. if (!refresh) {
  14. loadingBar.show();
  15. if (visible.imageview()) photos.hideView();
  16. lychee.animate(".album, .photo", "contentZoomOut");
  17. lychee.animate(".divider", "fadeOut");
  18. }
  19. startTime = new Date().getTime();
  20. params = "getPhotos&albumID=" + albumID;
  21. lychee.api(params, "json", function(data) {
  22. durationTime = (new Date().getTime() - startTime);
  23. if (durationTime>300) waitTime = 0; else if (refresh) waitTime = 0; else waitTime = 300 - durationTime;
  24. $.timer(waitTime,function(){
  25. photosData = "";
  26. $.each(data, function() { photosData += build.photo(this); });
  27. lychee.content.html(photosData);
  28. if (!refresh) {
  29. lychee.animate(".album, .photo", "contentZoomIn");
  30. $("#tools_albums, #tools_photo").hide();
  31. $("#tools_album").show();
  32. $("img").retina();
  33. albums.loadInfo(albumID);
  34. }
  35. });
  36. });
  37. },
  38. loadInfo: function(photoID) {
  39. photos.showView();
  40. loadingBar.show();
  41. params = "getPhotoInfo&photoID=" + photoID;
  42. lychee.api(params, "json", function(data) {
  43. if (!data.title) data.title = "Untitled";
  44. document.title = "Lychee - " + data.title;
  45. lychee.headerTitle.html(data.title).addClass("editable");
  46. $("#button_star a").removeClass("icon-star-empty icon-star");
  47. if (data.star=="1") {
  48. $("#button_star a").addClass("icon-star");
  49. $("#button_star").attr("title", "Unstar Photo");
  50. } else {
  51. $("#button_star a").addClass("icon-star-empty");
  52. $("#button_star").attr("title", "Star Photo");
  53. }
  54. if (data.public=="1") {
  55. $("#button_share a").addClass("active");
  56. $("#button_share").attr("title", "Make Photo Private");
  57. } else {
  58. $("#button_share a").removeClass("active");
  59. $("#button_share").attr("title", "Share Photo");
  60. }
  61. data.url = lychee.upload_path + data.url;
  62. if (visible.controls()&&photos.isSmall(data)) lychee.image_view.html("<a id='previous' class='icon-caret-left'></a><a id='next' class='icon-caret-right'></a><div id='image' class='small' style='background-image: url(" + data.url + "); width: " + data.width + "px; height: " + data.height + "px; margin-top: -" + parseInt(data.height/2-20) + "px; margin-left: -" + data.width/2 + "px;'></div>");
  63. else if (visible.controls()) lychee.image_view.html("<a id='previous' class='icon-caret-left'></a><a id='next' class='icon-caret-right'></a><div id='image' style='background-image: url(" + data.url + ")'></div>");
  64. else if (photos.isSmall(data)) lychee.image_view.html("<a id='previous' style='left: -50px' class='icon-caret-left'></a><a id='next' style='right: -50px' class='icon-caret-right'></a><div id='image' class='small' style='background-image: url(" + data.url + "); width: " + data.width + "px; height: " + data.height + "px; margin-top: -" + parseInt(data.height/2) + "px; margin-left: -" + data.width/2 + "px;'></div>");
  65. else lychee.image_view.html("<a id='previous' style='left: -50px' class='icon-caret-left'></a><a id='next' style='right: -50px' class='icon-caret-right'></a><div id='image' style='background-image: url(" + data.url + "); top: 0px; right: 0px; bottom: 0px; left: 0px;'></div>");
  66. lychee.animate(image_view, "fadeIn");
  67. lychee.image_view.show();
  68. if (!visible.controls()) lychee.hideControls();
  69. lychee.infobox.html(build.infobox(data)).show();
  70. $.timer(300,function(){ lychee.content.show(); });
  71. loadingBar.hide();
  72. });
  73. },
  74. isSmall: function(photo) {
  75. size = [
  76. ["width", false],
  77. ["height", false]
  78. ];
  79. if (photo.width<$(window).width()-60) size["width"] = true;
  80. if (photo.height<$(window).height()-100) size["height"] = true;
  81. if (size["width"]&&size["height"]) return true;
  82. else return false;
  83. },
  84. showView: function() {
  85. // Change toolbar-buttons
  86. $("#tools_albums, #tools_album").hide();
  87. $("#tools_photo").show();
  88. // Make body not scrollable
  89. $("body").css("overflow", "hidden");
  90. },
  91. hideView: function() {
  92. // Change toolbar-buttons
  93. $("#tools_photo, #tools_albums").hide();
  94. $("#tools_album").show();
  95. // Make body scrollable
  96. $("body").css("overflow", "scroll");
  97. // Change website title and url by using albums.loadInfo
  98. albums.loadInfo(lychee.content.attr("data-id"));
  99. // Hide ImageViewer
  100. lychee.animate(image_view, "fadeOut");
  101. $.timer(300,function(){ lychee.image_view.hide() });
  102. },
  103. showInfobox: function() {
  104. if (!visible.infobox()) $("body").append("<div id='infobox_overlay'></div>");
  105. lychee.infobox.css("right", "0px");
  106. },
  107. hideInfobox: function() {
  108. $("#infobox_overlay").remove();
  109. lychee.infobox.css("right", "-320px");
  110. },
  111. hide: function(photoID) {
  112. $(".photo[data-id='" + photoID + "']").css("opacity", 0).animate({
  113. width: 0,
  114. marginLeft: 0
  115. }, 300, function() {
  116. $(this).remove();
  117. if (!visible.imageview()) {
  118. imgNum = parseInt($("#title span").html().replace("- ", "").replace(" photos", ""))-1;
  119. $("#title span").html(" - " + imgNum + " photos");
  120. }
  121. });
  122. },
  123. delete: function(photoID) {
  124. loadingBar.show();
  125. params = "deletePhoto&photoID=" + photoID;
  126. lychee.api(params, "text", function(data) {
  127. if (data) {
  128. photos.hide(photoID);
  129. lychee.goto("a" + lychee.content.attr("data-id"));
  130. loadingBar.hide();
  131. } else loadingBar.show("error");
  132. });
  133. },
  134. deleteDialog: function(photoID) {
  135. if (!photoID) photoID = lychee.image_view.attr("data-id");
  136. if (visible.imageview()) photoTitle = lychee.title();
  137. else photoTitle = $(".photo[data-id='" + photoID + "'] .overlay h1").html();
  138. if (photoTitle=="") photoTitle = "Untitled";
  139. f1 = "photos.delete(" + photoID + ");";
  140. f2 = "";
  141. modal = build.modal("Delete Photo", "Are you sure you want to delete the photo '" + photoTitle + "'?<br>This action can't be undone!", ["Delete Photo", "Keep Photo"], [f1, f2]);
  142. $("body").append(modal);
  143. },
  144. rename: function(photoID) {
  145. if (!photoID) oldTitle = lychee.title(); else oldTitle = "";
  146. if (!photoID) photoID = lychee.image_view.attr("data-id");
  147. newTitle = prompt("Please enter a new title for this photo:", oldTitle);
  148. if (photoID!=null&&photoID&&newTitle.length<31) {
  149. loadingBar.show();
  150. if (newTitle=="") newTitle = "Untitled";
  151. params = "setPhotoTitle&photoID=" + photoID + "&title=" + encodeURI(newTitle);
  152. lychee.api(params, "text", function(data) {
  153. if (data) {
  154. if (visible.imageview()) {
  155. $("#infobox .attr_name").html($("#infobox .attr_name").html().replace(lychee.title(), newTitle));
  156. lychee.headerTitle.html(newTitle);
  157. document.title = "Lychee - " + newTitle;
  158. }
  159. $(".photo[data-id='" + photoID + "'] .overlay h1").html(newTitle);
  160. loadingBar.hide();
  161. } else loadingBar.show("error");
  162. });
  163. } else if (newTitle.length>0) loadingBar.show("error", "Error", "New title to short or too long. Please try another one!");
  164. },
  165. move: function(photoID, albumID) {
  166. if (albumID>=0) {
  167. loadingBar.show();
  168. params = "movePhoto&photoID=" + photoID + "&albumID=" + albumID;
  169. lychee.api(params, "text", function(data) {
  170. if (data) {
  171. photos.hide(photoID);
  172. lychee.goto("a" + lychee.content.attr("data-id"));
  173. loadingBar.hide();
  174. } else loadingBar.show("error");
  175. });
  176. }
  177. },
  178. setStar: function() {
  179. loadingBar.show();
  180. photoID = lychee.image_view.attr("data-id");
  181. params = "setPhotoStar&photoID=" + photoID;
  182. lychee.api(params, "text", function(data) {
  183. if (data) {
  184. if ($("#button_star a.icon-star-empty").length) {
  185. $("#button_star a").removeClass("icon-star-empty icon-star").addClass("icon-star");
  186. $("#button_star").attr("title", "Unstar Photo");
  187. } else {
  188. $("#button_star a").removeClass("icon-star-empty icon-star").addClass("icon-star-empty");
  189. $("#button_star").attr("title", "Star Photo");
  190. }
  191. photos.load(lychee.content.attr("data-id"), true);
  192. loadingBar.hide();
  193. } else loadingBar.show("error");
  194. });
  195. },
  196. setPublic: function(e) {
  197. loadingBar.show();
  198. photoID = lychee.image_view.attr("data-id");
  199. params = "setPhotoPublic&photoID=" + photoID + "&url=" + photos.getViewLink(photoID);
  200. lychee.api(params, "text", function(data) {
  201. if (data) {
  202. if ($("#button_share a.active").length) {
  203. $("#button_share a").removeClass("active");
  204. $("#button_share").attr("title", "Make Private");
  205. } else {
  206. $("#button_share a").addClass("active");
  207. $("#button_share").attr("title", "Share Photo");
  208. contextMenu.share(photoID, e.pageX, e.pageY);
  209. }
  210. photos.load(lychee.content.attr("data-id"), true);
  211. loadingBar.hide();
  212. } else loadingBar.show("error");
  213. });
  214. },
  215. setDescription: function() {
  216. description = prompt("Please enter a description for this photo:", "");
  217. photoID = lychee.image_view.attr("data-id");
  218. if (description.length>0&&description.length<160) {
  219. loadingBar.show();
  220. params = "setPhotoDescription&photoID=" + photoID + "&description=" + escape(description);
  221. lychee.api(params, "text", function(data) {
  222. if (data) photos.loadInfo(photoID);
  223. else loadingBar.show("error");
  224. });
  225. } else if (description.length>0) loadingBar.show("error", "Error", "Description to short or too long. Please try another one!");
  226. },
  227. share: function(service, photoID) {
  228. loadingBar.show();
  229. params = "sharePhoto&photoID=" + photoID + "&url=" + photos.getViewLink(photoID);
  230. lychee.api(params, "json", function(data) {
  231. switch (service) {
  232. case 0:
  233. link = data.twitter;
  234. break;
  235. case 1:
  236. link = data.facebook;
  237. break;
  238. case 2:
  239. link = data.mail;
  240. break;
  241. case 3:
  242. link = "copy";
  243. modal = build.modal("Copy Link", "You can use this link to share your image with other people: <input class='copylink' value='" + photos.getViewLink(photoID) + "'>", ["Close"], [""]);
  244. $("body").append(modal);
  245. $(".copylink").focus();
  246. break;
  247. case 4:
  248. link = "copy";
  249. modal = build.modal("Copy Shortlink", "You can use this link to share your image with other people: <input class='copylink' value='" + data.shortlink + "'>", ["Close"], [""]);
  250. $("body").append(modal);
  251. $(".copylink").focus();
  252. break;
  253. default:
  254. link = "";
  255. break;
  256. }
  257. if (link=="copy") loadingBar.hide();
  258. else if (link.length>5) {
  259. location.href = link;
  260. loadingBar.hide();
  261. } else loadingBar.show("error");
  262. });
  263. },
  264. getViewLink: function(photoID) {
  265. if (location.href.indexOf("index.html")>0) return location.href.replace("index.html" + location.hash, "view.php?p=" + photoID);
  266. else return location.href.replace(location.hash, "view.php?p=" + photoID);
  267. },
  268. previous: function() {
  269. albumID = lychee.content.attr("data-id");
  270. photoID = lychee.image_view.attr("data-id");
  271. params = "previousPhoto&photoID=" + photoID + "&albumID=" + albumID;
  272. lychee.api(params, "json", function(data) {
  273. if (data!=false) lychee.goto("a" + albumID + "p" + data.id);
  274. });
  275. },
  276. next: function() {
  277. albumID = lychee.content.attr("data-id");
  278. photoID = lychee.image_view.attr("data-id");
  279. params = "nextPhoto&photoID=" + photoID + "&albumID=" + albumID;
  280. lychee.api(params, "json", function(data) {
  281. if (data) lychee.goto("a" + albumID + "p" + data.id);
  282. });
  283. }
  284. }