album.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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) lychee.goto(data);
  72. else lychee.error(null, params, data);
  73. });
  74. } else loadingBar.show("error", "Title too short or too long. Please try again!");
  75. }],
  76. ["Cancel", function() {}]
  77. ];
  78. modal.show("New Album", "Please enter a title for this album: <input class='text' type='text' placeholder='Title' value='Untitled'>", buttons);
  79. },
  80. delete: function(albumID) {
  81. var params,
  82. buttons,
  83. albumTitle;
  84. buttons = [
  85. ["Delete Album and Photos", function() {
  86. params = "deleteAlbum&albumID=" + albumID;
  87. lychee.api(params, function(data) {
  88. if (visible.albums()) {
  89. albums.json.num--;
  90. view.albums.content.delete(albumID);
  91. } else lychee.goto("");
  92. if (data!==true) lychee.error(null, params, data);
  93. });
  94. }],
  95. ["Keep Album", function() {}]
  96. ];
  97. if (albumID==="0") {
  98. buttons[0][0] = "Clear Unsorted";
  99. modal.show("Clear Unsorted", "Are you sure you want to delete all photos from 'Unsorted'?<br>This action can't be undone!", buttons)
  100. } else {
  101. if (album.json) albumTitle = album.json.title;
  102. else if (albums.json) albumTitle = albums.json.content[albumID].title;
  103. 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);
  104. }
  105. },
  106. setTitle: function(albumID) {
  107. var oldTitle = "",
  108. newTitle,
  109. params,
  110. buttons;
  111. if (!albumID) return false;
  112. if (album.json) oldTitle = album.json.title;
  113. else if (albums.json) oldTitle = albums.json.content[albumID].title;
  114. buttons = [
  115. ["Set Title", function() {
  116. newTitle = $(".message input.text").val();
  117. if (newTitle==="") newTitle = "Untitled";
  118. if (albumID!==""&&albumID!=null&&albumID&&newTitle.length<31) {
  119. if (visible.album()) {
  120. album.json.title = newTitle;
  121. view.album.title(oldTitle);
  122. } else if (visible.albums()) {
  123. albums.json.content[albumID].title = newTitle;
  124. view.albums.content.title(albumID);
  125. }
  126. params = "setAlbumTitle&albumID=" + albumID + "&title=" + escape(encodeURI(newTitle));
  127. lychee.api(params, function(data) {
  128. if (data!==true) lychee.error(null, params, data);
  129. });
  130. } else if (newTitle.length>0) loadingBar.show("error", "New title too short or too long. Please try again!");
  131. }],
  132. ["Cancel", function() {}]
  133. ];
  134. modal.show("Set Title", "Please enter a new title for this album: <input class='text' type='text' placeholder='Title' value='" + oldTitle + "'>", buttons);
  135. },
  136. setDescription: function(photoID) {
  137. var oldDescription = album.json.description,
  138. description,
  139. params,
  140. buttons;
  141. buttons = [
  142. ["Set Description", function() {
  143. description = $(".message input.text").val();
  144. if (description.length<800) {
  145. if (visible.album()) {
  146. album.json.description = description;
  147. view.album.description();
  148. }
  149. params = "setAlbumDescription&albumID=" + photoID + "&description=" + escape(description);
  150. lychee.api(params, function(data) {
  151. if (data!==true) lychee.error(null, params, data);
  152. });
  153. } else loadingBar.show("error", "Description too long. Please try again!");
  154. }],
  155. ["Cancel", function() {}]
  156. ];
  157. modal.show("Set Description", "Please enter a description for this album: <input class='text' type='text' placeholder='Description' value='" + oldDescription + "'>", buttons);
  158. },
  159. setPublic: function(albumID, e) {
  160. var params;
  161. if ($(".message input.text").length>0&&$(".message input.text").val().length>0) {
  162. params = "setAlbumPublic&albumID=" + albumID + "&password=" + hex_md5($(".message input.text").val());
  163. album.json.password = true;
  164. } else {
  165. params = "setAlbumPublic&albumID=" + albumID;
  166. album.json.password = false;
  167. }
  168. if (visible.album()) {
  169. album.json.public = (album.json.public==0) ? 1 : 0;
  170. view.album.public();
  171. view.album.password();
  172. if (album.json.public==1) contextMenu.shareAlbum(albumID, e);
  173. }
  174. lychee.api(params, function(data) {
  175. if (data!==true) lychee.error(null, params, data);
  176. });
  177. },
  178. share: function(service) {
  179. var link = "",
  180. url = location.href;
  181. switch (service) {
  182. case 0:
  183. link = "https://twitter.com/share?url=" + encodeURI(url);
  184. break;
  185. case 1:
  186. link = "http://www.facebook.com/sharer.php?u=" + encodeURI(url) + "&t=" + encodeURI(album.json.title);
  187. break;
  188. case 2:
  189. link = "mailto:?subject=" + encodeURI(album.json.title) + "&body=" + encodeURI("Hi! Check this out: " + url);
  190. break;
  191. default:
  192. link = "";
  193. break;
  194. }
  195. if (link.length>5) location.href = link;
  196. },
  197. getArchive: function(albumID) {
  198. var link;
  199. if (location.href.indexOf("index.html")>0) link = location.href.replace(location.hash, "").replace("index.html", "php/api.php?function=getAlbumArchive&albumID=" + albumID);
  200. else link = location.href.replace(location.hash, "") + "php/api.php?function=getAlbumArchive&albumID=" + albumID;
  201. if (lychee.publicMode) link += "&password=" + password.value;
  202. location.href = link;
  203. }
  204. }