album.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. }
  8. album.getID = function() {
  9. var id;
  10. if (photo.json) id = photo.json.album;
  11. else if (album.json) id = album.json.id;
  12. else id = $('.album:hover, .album.active').attr('data-id');
  13. // Search
  14. if (!id) id = $('.album:hover, .album.active').attr('data-id');
  15. if (!id) id = $('.photo:hover, .photo.active').attr('data-album-id');
  16. if (id) return id;
  17. else return false;
  18. }
  19. album.load = function(albumID, refresh) {
  20. var startTime,
  21. params,
  22. durationTime,
  23. waitTime;
  24. password.get(albumID, function() {
  25. if (!refresh) {
  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. album.parse = function() {
  67. if (!album.json.title) album.json.title = 'Untitled';
  68. }
  69. album.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. // Avoid first album to be true
  82. if (data===true) data = 1;
  83. if (data!==false&&isNumber(data)) {
  84. albums.refresh();
  85. lychee.goto(data);
  86. } else {
  87. lychee.error(null, params, data);
  88. }
  89. });
  90. }],
  91. ['Cancel', function() {}]
  92. ];
  93. modal.show('New Album', "Enter a title for this album: <input class='text' type='text' maxlength='30' placeholder='Title' value='Untitled'>", buttons);
  94. }
  95. album.delete = function(albumIDs) {
  96. var params,
  97. buttons,
  98. albumTitle;
  99. if (!albumIDs) return false;
  100. if (albumIDs instanceof Array===false) albumIDs = [albumIDs];
  101. buttons = [
  102. ['', function() {
  103. params = 'deleteAlbum&albumIDs=' + albumIDs;
  104. lychee.api(params, function(data) {
  105. if (visible.albums()) {
  106. albumIDs.forEach(function(id) {
  107. albums.json.num--;
  108. view.albums.content.delete(id);
  109. delete albums.json.content[id];
  110. });
  111. } else {
  112. albums.refresh();
  113. lychee.goto('');
  114. }
  115. if (data!==true) lychee.error(null, params, data);
  116. });
  117. }],
  118. ['', function() {}]
  119. ];
  120. if (albumIDs.toString()==='0') {
  121. buttons[0][0] = 'Clear Unsorted';
  122. buttons[1][0] = 'Keep Unsorted';
  123. modal.show('Clear Unsorted', "Are you sure you want to delete all photos from 'Unsorted'?<br>This action can't be undone!", buttons);
  124. } else if (albumIDs.length===1) {
  125. buttons[0][0] = 'Delete Album and Photos';
  126. buttons[1][0] = 'Keep Album';
  127. // Get title
  128. if (album.json) albumTitle = album.json.title;
  129. else if (albums.json) albumTitle = albums.json.content[albumIDs].title;
  130. 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);
  131. } else {
  132. buttons[0][0] = 'Delete Albums and Photos';
  133. buttons[1][0] = 'Keep Albums';
  134. 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);
  135. }
  136. }
  137. album.setTitle = function(albumIDs) {
  138. var oldTitle = '',
  139. newTitle,
  140. params,
  141. buttons;
  142. if (!albumIDs) return false;
  143. if (albumIDs instanceof Array===false) albumIDs = [albumIDs];
  144. if (albumIDs.length===1) {
  145. // Get old title if only one album is selected
  146. if (album.json) oldTitle = album.json.title;
  147. else if (albums.json) oldTitle = albums.json.content[albumIDs].title;
  148. if (!oldTitle) oldTitle = '';
  149. oldTitle = oldTitle.replace("'", '&apos;');
  150. }
  151. buttons = [
  152. ['Set Title', function() {
  153. // Get input
  154. newTitle = $('.message input.text').val();
  155. // Remove html from input
  156. newTitle = lychee.removeHTML(newTitle);
  157. // Set to Untitled when empty
  158. newTitle = (newTitle==='') ? 'Untitled' : newTitle;
  159. if (visible.album()) {
  160. album.json.title = newTitle;
  161. view.album.title();
  162. if (albums.json) {
  163. var id = albumIDs[0];
  164. albums.json.content[id].title = newTitle;
  165. }
  166. } else if (visible.albums()) {
  167. albumIDs.forEach(function(id) {
  168. albums.json.content[id].title = newTitle;
  169. view.albums.content.title(id);
  170. });
  171. }
  172. params = 'setAlbumTitle&albumIDs=' + albumIDs + '&title=' + escape(encodeURI(newTitle));
  173. lychee.api(params, function(data) {
  174. if (data!==true) lychee.error(null, params, data);
  175. });
  176. }],
  177. ['Cancel', function() {}]
  178. ];
  179. 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);
  180. 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);
  181. }
  182. album.setDescription = function(photoID) {
  183. var oldDescription = album.json.description.replace("'", '&apos;'),
  184. description,
  185. params,
  186. buttons;
  187. buttons = [
  188. ['Set Description', function() {
  189. // Get input
  190. description = $('.message input.text').val();
  191. // Remove html from input
  192. description = lychee.removeHTML(description);
  193. if (visible.album()) {
  194. album.json.description = description;
  195. view.album.description();
  196. }
  197. params = 'setAlbumDescription&albumID=' + photoID + '&description=' + escape(encodeURI(description));
  198. lychee.api(params, function(data) {
  199. if (data!==true) lychee.error(null, params, data);
  200. });
  201. }],
  202. ['Cancel', function() {}]
  203. ];
  204. modal.show('Set Description', "Please enter a description for this album: <input class='text' type='text' maxlength='800' placeholder='Description' value='" + oldDescription + "'>", buttons);
  205. }
  206. album.setPublic = function(albumID, e) {
  207. var params,
  208. password = '',
  209. listed = false,
  210. downloadable = false;
  211. albums.refresh();
  212. if (!visible.message()&&album.json.public==0) {
  213. 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);
  214. $('.message .choice input[name="password"]').on('change', function() {
  215. if ($(this).prop('checked')===true) $('.message .choice input.text').show();
  216. else $('.message .choice input.text').hide();
  217. });
  218. return true;
  219. }
  220. if (visible.message()) {
  221. if ($('.message .choice input[name="password"]:checked').val()==='password') {
  222. password = md5($('.message input.text').val());
  223. album.json.password = 1;
  224. } else {
  225. password = '';
  226. album.json.password = 0;
  227. }
  228. if ($('.message .choice input[name="listed"]:checked').val()==='listed') listed = true;
  229. if ($('.message .choice input[name="downloadable"]:checked').val()==='downloadable') downloadable = true;
  230. }
  231. params = 'setAlbumPublic&albumID=' + albumID + '&password=' + password + '&visible=' + listed + '&downloadable=' + downloadable;
  232. if (visible.album()) {
  233. album.json.public = (album.json.public==0) ? 1 : 0;
  234. album.json.password = (album.json.public==0) ? 0 : album.json.password;
  235. view.album.public();
  236. view.album.password();
  237. if (album.json.public==1) contextMenu.shareAlbum(albumID, e);
  238. }
  239. lychee.api(params, function(data) {
  240. if (data!==true) lychee.error(null, params, data);
  241. });
  242. }
  243. album.share = function(service) {
  244. var link = '',
  245. url = location.href;
  246. switch (service) {
  247. case 0:
  248. link = 'https://twitter.com/share?url=' + encodeURI(url);
  249. break;
  250. case 1:
  251. link = 'http://www.facebook.com/sharer.php?u=' + encodeURI(url) + '&t=' + encodeURI(album.json.title);
  252. break;
  253. case 2:
  254. link = 'mailto:?subject=' + encodeURI(album.json.title) + '&body=' + encodeURI(url);
  255. break;
  256. default:
  257. link = '';
  258. break;
  259. }
  260. if (link.length>5) location.href = link;
  261. }
  262. album.getArchive = function(albumID) {
  263. var link,
  264. url = 'php/api.php?function=getAlbumArchive&albumID=' + albumID;
  265. if (location.href.indexOf('index.html')>0) link = location.href.replace(location.hash, '').replace('index.html', url);
  266. else link = location.href.replace(location.hash, '') + url;
  267. if (lychee.publicMode) link += '&password=' + password.value;
  268. location.href = link;
  269. }