album.js 10 KB

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