album.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * @name Album Module
  3. * @description Takes care of every action an album can handle and execute.
  4. * @author Tobias Reich
  5. * @copyright 2014 by Tobias Reich
  6. */
  7. album = {
  8. json: null,
  9. getID: function() {
  10. var id;
  11. if (photo.json) id = photo.json.album;
  12. else if (album.json) id = album.json.id;
  13. else id = $(".album:hover, .album.active").attr("data-id");
  14. // Search
  15. if (!id) id = $(".photo:hover, .photo.active").attr("data-album-id");
  16. if (id) return id;
  17. else return false;
  18. },
  19. load: function(albumID, refresh) {
  20. var startTime,
  21. params,
  22. durationTime,
  23. waitTime;
  24. password.get(albumID, function() {
  25. if (!refresh) {
  26. loadingBar.show();
  27. lychee.animate(".album, .photo", "contentZoomOut");
  28. lychee.animate(".divider", "fadeOut");
  29. }
  30. startTime = new Date().getTime();
  31. params = "getAlbum&albumID=" + albumID + "&password=" + password.value;
  32. lychee.api(params, function(data) {
  33. if (data==="Warning: Album private!") {
  34. if (document.location.hash.replace("#", "").split("/")[1]!=undefined) {
  35. // Display photo only
  36. lychee.setMode("view");
  37. } else {
  38. // Album not public
  39. lychee.content.show();
  40. lychee.goto("");
  41. }
  42. return false;
  43. }
  44. if (data==="Warning: Wrong password!") {
  45. album.load(albumID, refresh);
  46. return false;
  47. }
  48. album.json = data;
  49. durationTime = (new Date().getTime() - startTime);
  50. if (durationTime>300) waitTime = 0; else if (refresh) waitTime = 0; else waitTime = 300 - durationTime;
  51. if (!visible.albums()&&!visible.photo()&&!visible.album()) waitTime = 0;
  52. setTimeout(function() {
  53. view.album.init();
  54. if (!refresh) {
  55. lychee.animate(".album, .photo", "contentZoomIn");
  56. view.header.mode("album");
  57. }
  58. }, waitTime);
  59. });
  60. });
  61. },
  62. parse: function(photo) {
  63. if (photo&&photo.thumbUrl) photo.thumbUrl = lychee.upload_path_thumb + photo.thumbUrl;
  64. else if (!album.json.title) album.json.title = "Untitled";
  65. },
  66. add: function() {
  67. var title,
  68. params,
  69. buttons;
  70. buttons = [
  71. ["Create Album", function() {
  72. title = $(".message input.text").val();
  73. if (title.length===0) title = "Untitled";
  74. modal.close();
  75. params = "addAlbum&title=" + escape(encodeURI(title));
  76. lychee.api(params, function(data) {
  77. if (data!==false) {
  78. if (data===true) data = 1; // Avoid first album to be true
  79. lychee.goto(data);
  80. } else lychee.error(null, params, data);
  81. });
  82. }],
  83. ["Cancel", function() {}]
  84. ];
  85. modal.show("New Album", "Enter a title for this album: <input class='text' type='text' maxlength='30' placeholder='Title' value='Untitled'>", buttons);
  86. },
  87. delete: function(albumIDs) {
  88. var params,
  89. buttons,
  90. albumTitle;
  91. if (!albumIDs) return false;
  92. if (albumIDs instanceof Array===false) albumIDs = [albumIDs];
  93. buttons = [
  94. ["", function() {
  95. params = "deleteAlbum&albumIDs=" + albumIDs;
  96. lychee.api(params, function(data) {
  97. if (visible.albums()) {
  98. albumIDs.forEach(function(id, index, array) {
  99. albums.json.num--;
  100. view.albums.content.delete(id);
  101. });
  102. } else lychee.goto("");
  103. if (data!==true) lychee.error(null, params, data);
  104. });
  105. }],
  106. ["", function() {}]
  107. ];
  108. if (albumIDs.toString()==="0") {
  109. buttons[0][0] = "Clear Unsorted";
  110. buttons[1][0] = "Keep Unsorted";
  111. modal.show("Clear Unsorted", "Are you sure you want to delete all photos from 'Unsorted'?<br>This action can't be undone!", buttons);
  112. } else if (albumIDs.length===1) {
  113. buttons[0][0] = "Delete Album and Photos";
  114. buttons[1][0] = "Keep Album";
  115. // Get title
  116. if (album.json) albumTitle = album.json.title;
  117. else if (albums.json) albumTitle = albums.json.content[albumIDs].title;
  118. modal.show("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!", buttons);
  119. } else {
  120. buttons[0][0] = "Delete Albums and Photos";
  121. buttons[1][0] = "Keep Albums";
  122. modal.show("Delete Albums", "Are you sure you want to delete all " + albumIDs.length + " selected albums and all of the photos they contain? This action can't be undone!", buttons);
  123. }
  124. },
  125. setTitle: function(albumIDs) {
  126. var oldTitle = "",
  127. newTitle,
  128. params,
  129. buttons;
  130. if (!albumIDs) return false;
  131. if (albumIDs instanceof Array===false) albumIDs = [albumIDs];
  132. if (albumIDs.length===1) {
  133. // Get old title if only one album is selected
  134. if (album.json) oldTitle = album.json.title;
  135. else if (albums.json) oldTitle = albums.json.content[albumIDs].title;
  136. oldTitle = oldTitle.replace("'", "&apos;");
  137. }
  138. buttons = [
  139. ["Set Title", function() {
  140. newTitle = ($(".message input.text").val()==="") ? "Untitled" : $(".message input.text").val();
  141. if (visible.album()) {
  142. album.json.title = newTitle;
  143. view.album.title();
  144. } else if (visible.albums()) {
  145. albumIDs.forEach(function(id, index, array) {
  146. albums.json.content[id].title = newTitle;
  147. view.albums.content.title(id);
  148. });
  149. }
  150. params = "setAlbumTitle&albumIDs=" + albumIDs + "&title=" + escape(encodeURI(newTitle));
  151. lychee.api(params, function(data) {
  152. if (data!==true) lychee.error(null, params, data);
  153. });
  154. }],
  155. ["Cancel", function() {}]
  156. ];
  157. if (albumIDs.length===1) modal.show("Set Title", "Enter a new title for this album: <input class='text' type='text' maxlength='30' placeholder='Title' value='" + oldTitle + "'>", buttons);
  158. else modal.show("Set Titles", "Enter a title for all " + albumIDs.length + " selected album: <input class='text' type='text' maxlength='30' placeholder='Title' value='" + oldTitle + "'>", buttons);
  159. },
  160. setDescription: function(photoID) {
  161. var oldDescription = album.json.description.replace("'", "&apos;"),
  162. description,
  163. params,
  164. buttons;
  165. buttons = [
  166. ["Set Description", function() {
  167. description = $(".message input.text").val();
  168. if (visible.album()) {
  169. album.json.description = description;
  170. view.album.description();
  171. }
  172. params = "setAlbumDescription&albumID=" + photoID + "&description=" + escape(description);
  173. lychee.api(params, function(data) {
  174. if (data!==true) lychee.error(null, params, data);
  175. });
  176. }],
  177. ["Cancel", function() {}]
  178. ];
  179. modal.show("Set Description", "Please enter a description for this album: <input class='text' type='text' maxlength='800' placeholder='Description' value='" + oldDescription + "'>", buttons);
  180. },
  181. setPublic: function(albumID, e) {
  182. var params;
  183. if ($(".message input.text").length>0&&$(".message input.text").val().length>0) {
  184. params = "setAlbumPublic&albumID=" + albumID + "&password=" + md5($(".message input.text").val());
  185. album.json.password = true;
  186. } else {
  187. params = "setAlbumPublic&albumID=" + albumID;
  188. album.json.password = false;
  189. }
  190. if (visible.album()) {
  191. album.json.public = (album.json.public==0) ? 1 : 0;
  192. view.album.public();
  193. view.album.password();
  194. if (album.json.public==1) contextMenu.shareAlbum(albumID, e);
  195. }
  196. lychee.api(params, function(data) {
  197. if (data!==true) lychee.error(null, params, data);
  198. });
  199. },
  200. share: function(service) {
  201. var link = "",
  202. url = location.href;
  203. switch (service) {
  204. case 0:
  205. link = "https://twitter.com/share?url=" + encodeURI(url);
  206. break;
  207. case 1:
  208. link = "http://www.facebook.com/sharer.php?u=" + encodeURI(url) + "&t=" + encodeURI(album.json.title);
  209. break;
  210. case 2:
  211. link = "mailto:?subject=" + encodeURI(album.json.title) + "&body=" + encodeURI("Hi! Check this out: " + url);
  212. break;
  213. default:
  214. link = "";
  215. break;
  216. }
  217. if (link.length>5) location.href = link;
  218. },
  219. getArchive: function(albumID) {
  220. var link;
  221. if (location.href.indexOf("index.html")>0) link = location.href.replace(location.hash, "").replace("index.html", "php/api.php?function=getAlbumArchive&albumID=" + albumID);
  222. else link = location.href.replace(location.hash, "") + "php/api.php?function=getAlbumArchive&albumID=" + albumID;
  223. if (lychee.publicMode) link += "&password=" + password.value;
  224. location.href = link;
  225. }
  226. };