albums.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /**
  2. * @name albums.js
  3. * @author Philipp Maurer
  4. * @author Tobias Reich
  5. * @copyright 2013 by Philipp Maurer, Tobias Reich
  6. *
  7. * Albums Module
  8. * Takes care of every action albums can handle and execute.
  9. */
  10. albums = {
  11. load: function() {
  12. lychee.animate(".album, .photo", "contentZoomOut");
  13. /* Search */
  14. lychee.content.attr("data-search", "");
  15. lychee.animate(".divider", "fadeOut");
  16. startTime = new Date().getTime();
  17. lychee.api("getAlbums", "json", function(data) {
  18. durationTime = (new Date().getTime() - startTime);
  19. if (durationTime>300) waitTime = 0; else waitTime = 300 - durationTime;
  20. $.timer(waitTime,function(){
  21. $("#tools_album, #tools_photo").hide();
  22. $("#tools_albums").show();
  23. /* Smart Albums */
  24. unsortedAlbum = new Object();
  25. unsortedAlbum.id = 0;
  26. unsortedAlbum.title = "Unsorted";
  27. unsortedAlbum.sysdate = data.unsortNum + " photos";
  28. unsortedAlbum.unsorted = 1;
  29. if (data.unsortThumb0) unsortedAlbum.thumb0 = lychee.upload_path + data.unsortThumb0; else unsortedAlbum.thumb0 = "";
  30. if (data.unsortThumb1) unsortedAlbum.thumb1 = lychee.upload_path + data.unsortThumb1; else unsortedAlbum.thumb1 = "";
  31. if (data.unsortThumb2) unsortedAlbum.thumb2 = lychee.upload_path + data.unsortThumb2; else unsortedAlbum.thumb2 = "";
  32. starredAlbum = new Object();
  33. starredAlbum.id = "f";
  34. starredAlbum.title = "Starred";
  35. starredAlbum.sysdate = data.starredNum + " photos";
  36. starredAlbum.star = 1;
  37. if (data.starredThumb0) starredAlbum.thumb0 = lychee.upload_path + data.starredThumb0; else starredAlbum.thumb0 = "";
  38. if (data.starredThumb1) starredAlbum.thumb1 = lychee.upload_path + data.starredThumb1; else starredAlbum.thumb1 = "";
  39. if (data.starredThumb2) starredAlbum.thumb2 = lychee.upload_path + data.starredThumb2; else starredAlbum.thumb2 = "";
  40. publicAlbum = new Object();
  41. publicAlbum.id = "s";
  42. publicAlbum.title = "Public";
  43. publicAlbum.sysdate = data.publicNum + " photos";
  44. publicAlbum.public = 1;
  45. if (data.publicThumb0) publicAlbum.thumb0 = lychee.upload_path + data.publicThumb0; else publicAlbum.thumb0 = "";
  46. if (data.publicThumb1) publicAlbum.thumb1 = lychee.upload_path + data.publicThumb1; else publicAlbum.thumb1 = "";
  47. if (data.publicThumb2) publicAlbum.thumb2 = lychee.upload_path + data.publicThumb2; else publicAlbum.thumb2 = "";
  48. if (lychee.publicMode) smartData = "";
  49. else smartData = build.divider("Smart Albums") + build.album(unsortedAlbum) + build.album(starredAlbum) + build.album(publicAlbum);
  50. /* Albums */
  51. if (data.albums) {
  52. albumsData = build.divider("Albums");
  53. $.each(data.album, function() { albumsData += build.album(this); });
  54. } else albumsData = "";
  55. if (smartData==""&&albumsData=="") $("body").append(build.no_content("picture"));
  56. else {
  57. lychee.content.html(smartData + albumsData);
  58. lychee.animate(".album, .photo", "contentZoomIn");
  59. }
  60. document.title = "Lychee";
  61. lychee.headerTitle.html("Albums").removeClass("editable");
  62. $("img").retina();
  63. });
  64. })
  65. },
  66. loadInfo: function(albumID, password) {
  67. if (albumID=="f"||albumID=="s"||albumID==0) {
  68. lychee.headerTitle.removeClass("editable");
  69. $("#button_edit_album, #button_trash_album, #button_share_album").hide();
  70. lychee.api("getSmartInfo", "json", function(data) {
  71. switch (albumID) {
  72. case "f":
  73. document.title = "Lychee - Starred";
  74. lychee.headerTitle.html("Starred<span> - " + data.starredNum + " photos</span>");
  75. break;
  76. case "s":
  77. document.title = "Lychee - Public";
  78. lychee.headerTitle.html("Public<span> - " + data.publicNum + " photos</span>");
  79. break;
  80. case "0":
  81. document.title = "Lychee - Unsorted";
  82. lychee.headerTitle.html("Unsorted<span> - " + data.unsortNum + " photos</span>");
  83. $("#button_trash_album").show();
  84. break;
  85. }
  86. });
  87. } else {
  88. /*if (lychee.publicMode&&password==undefined) {
  89. password = localStorage.getItem("album" + albumID);
  90. if (password==null) {
  91. if (lychee.publicMode) password = prompt("Please enter a password for this album:", ""); else password = "";
  92. if (password!="") password = hex_md5(password);
  93. localStorage.setItem("album" + albumID, password);
  94. }
  95. }*/
  96. password = "";
  97. $("#button_edit_album, #button_trash_album, #button_share_album, .button_divider").show();
  98. params = "getAlbumInfo&albumID=" + albumID + "&password=" + password;
  99. lychee.api(params, "json", function(data) {
  100. if (!data.title) data.title = "Untitled";
  101. document.title = "Lychee - " + data.title;
  102. lychee.headerTitle.html(data.title + "<span> - " + data.num + " photos</span>").addClass("editable");
  103. if (data.public=="1") {
  104. $("#button_share_album a").addClass("active");
  105. $("#button_share_album").attr("title", "Share Album");
  106. } else {
  107. $("#button_share_album a").removeClass("active");
  108. $("#button_share_album").attr("title", "Make Public");
  109. }
  110. });
  111. }
  112. },
  113. add: function() {
  114. title = prompt("Please enter a title for this album:", "Untitled");
  115. lychee.closeModal();
  116. if (title.length>0&&title.length<31) {
  117. params = "addAlbum&title=" + escape(title);
  118. lychee.api(params, "text", function(data) {
  119. if (data) lychee.goto("a" + data);
  120. else loadingBar.show("error");
  121. });
  122. } else if (title.length>0) loadingBar.show("error", "Error", "Title to short or too long. Please try another one!");
  123. },
  124. hide: function(albumID) {
  125. $(".album[data-id='" + albumID + "']").css("opacity", 0).animate({
  126. width: 0,
  127. marginLeft: 0
  128. }, 300, function() {
  129. $(this).remove();
  130. });
  131. },
  132. delete: function(albumID, delAll) {
  133. params = "deleteAlbum&albumID=" + albumID + "&delAll=" + delAll;
  134. lychee.api(params, "text", function(data) {
  135. if (data) {
  136. if (visible.albums()) albums.hide(albumID);
  137. else lychee.goto("");
  138. } else loadingBar.show("error");
  139. });
  140. },
  141. deleteDialog: function(albumID) {
  142. if (albumID==0) {
  143. f1 = "albums.delete(0, true);";
  144. f2 = "";
  145. modal = build.modal("Clear Unsorted", "Are you sure you want to delete all photos from 'Unsorted'?<br>This action can't be undone!", ["Clear Unsorted", "Keep Photos"], [f1, f2]);
  146. $("body").append(modal);
  147. } else {
  148. if (visible.albums()) albumTitle = $(".album[data-id='" + albumID + "'] .overlay h1").html();
  149. else albumTitle = lychee.title();
  150. f1 = "albums.delete(" + albumID + ", true);";
  151. f2 = "albums.delete(" + albumID + ", false);";
  152. modal = build.modal("Delete Album", "Are you sure you want to delete the album '" + albumTitle + "' and all of the photos it contains? This action can't be undone!", ["Delete Album and Photos", "Keep Photos"], [f1, f2]);
  153. $("body").append(modal);
  154. }
  155. },
  156. setTitle: function(albumID) {
  157. if (!albumID) oldTitle = lychee.title(); else oldTitle = "";
  158. if (!albumID) albumID = lychee.content.attr("data-id");
  159. newTitle = prompt("Please enter a new title for this album:", oldTitle);
  160. if (albumID!=""&&albumID!=null&&albumID&&newTitle.length>0&&newTitle.length<31) {
  161. params = "setAlbumTitle&albumID=" + albumID + "&title=" + encodeURI(newTitle);
  162. lychee.api(params, "text", function(data) {
  163. if (data) {
  164. if (visible.albums()) $(".album[data-id='" + albumID + "'] .overlay h1").html(newTitle);
  165. else {
  166. lychee.headerTitle.html(newTitle + "<span>" + $("#title span").html() + "</span>");
  167. document.title = "Lychee - " + newTitle;
  168. }
  169. } else loadingBar.show("error");
  170. });
  171. } else if (newTitle.length>0) loadingBar.show("error", "Error", "New title to short or too long. Please try another one!");
  172. },
  173. setPublic: function(e) {
  174. albumID = lychee.content.attr("data-id");
  175. params = "setAlbumPublic&albumID=" + albumID;
  176. lychee.api(params, "text", function(data) {
  177. if (data) {
  178. if ($("#button_share_album a.active").length) {
  179. $("#button_share_album a").removeClass("active");
  180. $("#button_share_album").attr("title", "Make Public");
  181. } else {
  182. $("#button_share_album a").addClass("active");
  183. $("#button_share_album").attr("title", "Share Album");
  184. contextMenu.share_album(albumID, e.pageX, e.pageY);
  185. }
  186. } else loadingBar.show("error");
  187. });
  188. },
  189. setPassword: function(albumID, password) {
  190. if (!albumID) albumID = lychee.content.attr("data-id");
  191. if (!password) password = prompt("Please enter a password for this album:", "");
  192. if (password!="") password = hex_md5(password);
  193. params = "setAlbumPassword&albumID=" + albumID + "&password=" + password;
  194. lychee.api(params, "text", function(data) {
  195. if (!data) loadingBar.show("error");
  196. });
  197. },
  198. /*checkAlbumPassword: function(albumID, password) {
  199. params = "checkAlbumPassword&albumID=" + albumID + "&password=" + hex_md5(password);
  200. lychee.api(params, "text", function(data) {
  201. if (data) {
  202. if (password!="") password = hex_md5(password);
  203. localStorage.setItem("album" + albumID, password);
  204. return true;
  205. } else return false;
  206. });
  207. },*/
  208. share: function(service, albumID) {
  209. link = "";
  210. url = location.href;
  211. switch (service) {
  212. case 0:
  213. link = "https://twitter.com/share?url=" + encodeURI(url);
  214. break;
  215. case 1:
  216. link = "http://www.facebook.com/sharer.php?u=" + encodeURI(url) + "&t=" + encodeURI(lychee.title());
  217. break;
  218. case 2:
  219. link = "mailto:?subject=" + encodeURI(lychee.title()) + "&body=" + encodeURI("Hi! Check this out: " + url);
  220. break;
  221. case 3:
  222. modal = build.modal("Copy Link", "Everyone can view your public albums, but only you can edit them. Use this link to share your album with others: <input class='copylink' value='" + url + "'>", ["Close"], [""]);
  223. $("body").append(modal);
  224. $(".copylink").focus();
  225. break;
  226. default:
  227. link = "";
  228. break;
  229. }
  230. if (link.length>5) location.href = link;
  231. },
  232. getArchive: function() {
  233. albumID = lychee.content.attr("data-id");
  234. if (location.href.indexOf("index.html")>0) link = location.href.replace(location.hash, "").replace("index.html", "php/api.php?function=getAlbumArchive&albumID=" + albumID);
  235. else link = location.href.replace(location.hash, "") + "php/api.php?function=getAlbumArchive&albumID=" + albumID;
  236. location.href = link;
  237. }
  238. }