album.js 8.1 KB

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