album.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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==="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. }
  137. buttons = [
  138. ["Set Title", function() {
  139. newTitle = ($(".message input.text").val()==="") ? "Untitled" : $(".message input.text").val();
  140. if (visible.album()) {
  141. album.json.title = newTitle;
  142. view.album.title();
  143. } else if (visible.albums()) {
  144. albumIDs.forEach(function(id, index, array) {
  145. albums.json.content[id].title = newTitle;
  146. view.albums.content.title(id);
  147. });
  148. }
  149. params = "setAlbumTitle&albumIDs=" + albumIDs + "&title=" + escape(encodeURI(newTitle));
  150. lychee.api(params, function(data) {
  151. if (data!==true) lychee.error(null, params, data);
  152. });
  153. }],
  154. ["Cancel", function() {}]
  155. ];
  156. 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);
  157. 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);
  158. },
  159. setDescription: function(photoID) {
  160. var oldDescription = album.json.description,
  161. description,
  162. params,
  163. buttons;
  164. buttons = [
  165. ["Set Description", function() {
  166. description = $(".message input.text").val();
  167. if (visible.album()) {
  168. album.json.description = description;
  169. view.album.description();
  170. }
  171. params = "setAlbumDescription&albumID=" + photoID + "&description=" + escape(description);
  172. lychee.api(params, function(data) {
  173. if (data!==true) lychee.error(null, params, data);
  174. });
  175. }],
  176. ["Cancel", function() {}]
  177. ];
  178. modal.show("Set Description", "Please enter a description for this album: <input class='text' type='text' maxlength='800' placeholder='Description' value='" + oldDescription + "'>", buttons);
  179. },
  180. setPublic: function(albumID, e) {
  181. var params;
  182. if ($(".message input.text").length>0&&$(".message input.text").val().length>0) {
  183. params = "setAlbumPublic&albumID=" + albumID + "&password=" + hex_md5($(".message input.text").val());
  184. album.json.password = true;
  185. } else {
  186. params = "setAlbumPublic&albumID=" + albumID;
  187. album.json.password = false;
  188. }
  189. if (visible.album()) {
  190. album.json.public = (album.json.public==0) ? 1 : 0;
  191. view.album.public();
  192. view.album.password();
  193. if (album.json.public==1) contextMenu.shareAlbum(albumID, e);
  194. }
  195. lychee.api(params, function(data) {
  196. if (data!==true) lychee.error(null, params, data);
  197. });
  198. },
  199. share: function(service) {
  200. var link = "",
  201. url = location.href;
  202. switch (service) {
  203. case 0:
  204. link = "https://twitter.com/share?url=" + encodeURI(url);
  205. break;
  206. case 1:
  207. link = "http://www.facebook.com/sharer.php?u=" + encodeURI(url) + "&t=" + encodeURI(album.json.title);
  208. break;
  209. case 2:
  210. link = "mailto:?subject=" + encodeURI(album.json.title) + "&body=" + encodeURI("Hi! Check this out: " + url);
  211. break;
  212. default:
  213. link = "";
  214. break;
  215. }
  216. if (link.length>5) location.href = link;
  217. },
  218. getArchive: function(albumID) {
  219. var link;
  220. if (location.href.indexOf("index.html")>0) link = location.href.replace(location.hash, "").replace("index.html", "php/api.php?function=getAlbumArchive&albumID=" + albumID);
  221. else link = location.href.replace(location.hash, "") + "php/api.php?function=getAlbumArchive&albumID=" + albumID;
  222. if (lychee.publicMode) link += "&password=" + password.value;
  223. location.href = link;
  224. }
  225. }