album.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 = $(".album:hover, .album.active").attr("data-id");
  16. if (!id) id = $(".photo:hover, .photo.active").attr("data-album-id");
  17. if (id) return id;
  18. else return false;
  19. },
  20. load: function(albumID, refresh) {
  21. var startTime,
  22. params,
  23. durationTime,
  24. waitTime;
  25. password.get(albumID, function() {
  26. if (!refresh) {
  27. loadingBar.show();
  28. lychee.animate(".album:nth-child(-n+50), .photo:nth-child(-n+50)", "contentZoomOut");
  29. lychee.animate(".divider", "fadeOut");
  30. }
  31. startTime = new Date().getTime();
  32. params = "getAlbum&albumID=" + albumID + "&password=" + password.value;
  33. lychee.api(params, function(data) {
  34. if (data==="Warning: Album private!") {
  35. if (document.location.hash.replace("#", "").split("/")[1]!=undefined) {
  36. // Display photo only
  37. lychee.setMode("view");
  38. } else {
  39. // Album not public
  40. lychee.content.show();
  41. lychee.goto("");
  42. }
  43. return false;
  44. }
  45. if (data==="Warning: Wrong password!") {
  46. album.load(albumID, refresh);
  47. return false;
  48. }
  49. album.json = data;
  50. durationTime = (new Date().getTime() - startTime);
  51. if (durationTime>300) waitTime = 0; else if (refresh) waitTime = 0; else waitTime = 300 - durationTime;
  52. if (!visible.albums()&&!visible.photo()&&!visible.album()) waitTime = 0;
  53. setTimeout(function() {
  54. view.album.init();
  55. if (!refresh) {
  56. lychee.animate(".album:nth-child(-n+50), .photo:nth-child(-n+50)", "contentZoomIn");
  57. view.header.mode("album");
  58. }
  59. }, waitTime);
  60. });
  61. });
  62. },
  63. parse: function(photo) {
  64. if (!album.json.title) album.json.title = "Untitled";
  65. },
  66. add: function() {
  67. var title,
  68. params,
  69. buttons,
  70. isNumber = function(n) { return !isNaN(parseFloat(n)) && isFinite(n) };
  71. buttons = [
  72. ["Create Album", function() {
  73. title = $(".message input.text").val();
  74. if (title.length===0) title = "Untitled";
  75. modal.close();
  76. params = "addAlbum&title=" + escape(encodeURI(title));
  77. lychee.api(params, function(data) {
  78. if (data===true) data = 1; // Avoid first album to be true
  79. if (data!==false&&isNumber(data)) 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. if (!oldTitle) oldTitle = "";
  137. oldTitle = oldTitle.replace("'", "&apos;");
  138. }
  139. buttons = [
  140. ["Set Title", function() {
  141. // Get input
  142. newTitle = $(".message input.text").val();
  143. // Remove html from input
  144. newTitle = lychee.removeHTML(newTitle);
  145. // Set to Untitled when empty
  146. newTitle = (newTitle==="") ? "Untitled" : newTitle;
  147. if (visible.album()) {
  148. album.json.title = newTitle;
  149. view.album.title();
  150. } else if (visible.albums()) {
  151. albumIDs.forEach(function(id, index, array) {
  152. albums.json.content[id].title = newTitle;
  153. view.albums.content.title(id);
  154. });
  155. }
  156. params = "setAlbumTitle&albumIDs=" + albumIDs + "&title=" + escape(encodeURI(newTitle));
  157. lychee.api(params, function(data) {
  158. if (data!==true) lychee.error(null, params, data);
  159. });
  160. }],
  161. ["Cancel", function() {}]
  162. ];
  163. 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);
  164. 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);
  165. },
  166. setDescription: function(photoID) {
  167. var oldDescription = album.json.description.replace("'", "&apos;"),
  168. description,
  169. params,
  170. buttons;
  171. buttons = [
  172. ["Set Description", function() {
  173. // Get input
  174. description = $(".message input.text").val();
  175. // Remove html from input
  176. description = lychee.removeHTML(description);
  177. if (visible.album()) {
  178. album.json.description = description;
  179. view.album.description();
  180. }
  181. params = "setAlbumDescription&albumID=" + photoID + "&description=" + escape(encodeURI(description));
  182. lychee.api(params, function(data) {
  183. if (data!==true) lychee.error(null, params, data);
  184. });
  185. }],
  186. ["Cancel", function() {}]
  187. ];
  188. modal.show("Set Description", "Please enter a description for this album: <input class='text' type='text' maxlength='800' placeholder='Description' value='" + oldDescription + "'>", buttons);
  189. },
  190. setPublic: function(albumID, e) {
  191. var params,
  192. password = "",
  193. listed = false,
  194. downloadable = false;
  195. if (!visible.message()&&album.json.public==0) {
  196. modal.show("Share Album", "This album will be shared with the following properties:</p><form><div class='choice'><input type='checkbox' name='listed' value='listed' checked><h2>Visible</h2><p>Listed to visitors of your Lychee.</p></div><div class='choice'><input type='checkbox' name='downloadable' value='downloadable'><h2>Downloadable</h2><p>Visitors of your Lychee can download this album.</p></div><div class='choice'><input type='checkbox' name='password' value='password'><h2>Password protected</h2><p>Only accessible with a valid password.<input class='text' type='password' placeholder='password' value='' style='display: none;'></p></div></form><p style='display: none;'>", [["Share Album", function() { album.setPublic(album.getID(), e) }], ["Cancel", function() {}]], -170);
  197. $(".message .choice input[name='password']").on("change", function() {
  198. if ($(this).prop('checked')===true) $(".message .choice input.text").show();
  199. else $(".message .choice input.text").hide();
  200. });
  201. return true;
  202. }
  203. if (visible.message()) {
  204. if ($(".message .choice input[name='password']:checked").val()==="password") {
  205. password = md5($(".message input.text").val());
  206. album.json.password = 1;
  207. } else {
  208. password = "";
  209. album.json.password = 0;
  210. }
  211. if ($(".message .choice input[name='listed']:checked").val()==="listed") listed = true;
  212. if ($(".message .choice input[name='downloadable']:checked").val()==="downloadable") downloadable = true;
  213. }
  214. params = "setAlbumPublic&albumID=" + albumID + "&password=" + password + "&visible=" + listed + "&downloadable=" + downloadable;
  215. if (visible.album()) {
  216. album.json.public = (album.json.public==0) ? 1 : 0;
  217. album.json.password = (album.json.public==0) ? 0 : album.json.password;
  218. view.album.public();
  219. view.album.password();
  220. if (album.json.public==1) contextMenu.shareAlbum(albumID, e);
  221. }
  222. lychee.api(params, function(data) {
  223. if (data!==true) lychee.error(null, params, data);
  224. });
  225. },
  226. share: function(service) {
  227. var link = "",
  228. url = location.href;
  229. switch (service) {
  230. case 0:
  231. link = "https://twitter.com/share?url=" + encodeURI(url);
  232. break;
  233. case 1:
  234. link = "http://www.facebook.com/sharer.php?u=" + encodeURI(url) + "&t=" + encodeURI(album.json.title);
  235. break;
  236. case 2:
  237. link = "mailto:?subject=" + encodeURI(album.json.title) + "&body=" + encodeURI(url);
  238. break;
  239. default:
  240. link = "";
  241. break;
  242. }
  243. if (link.length>5) location.href = link;
  244. },
  245. getArchive: function(albumID) {
  246. var link;
  247. if (location.href.indexOf("index.html")>0) link = location.href.replace(location.hash, "").replace("index.html", "php/api.php?function=getAlbumArchive&albumID=" + albumID);
  248. else link = location.href.replace(location.hash, "") + "php/api.php?function=getAlbumArchive&albumID=" + albumID;
  249. if (lychee.publicMode) link += "&password=" + password.value;
  250. location.href = link;
  251. }
  252. };