album.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /**
  2. * @description Takes care of every action an album can handle and execute.
  3. * @copyright 2015 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, .photo', '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, .photo', 'contentZoomIn');
  60. header.setMode('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 action;
  71. action = function(data) {
  72. var params,
  73. isNumber = function(n) { return !isNaN(parseFloat(n)) && isFinite(n) };
  74. basicModal.close();
  75. if (data.title.length===0) data.title = 'Untitled';
  76. params = 'addAlbum&title=' + escape(encodeURI(data.title));
  77. lychee.api(params, function(data) {
  78. // Avoid first album to be true
  79. if (data===true) data = 1;
  80. if (data!==false&&isNumber(data)) {
  81. albums.refresh();
  82. lychee.goto(data);
  83. } else {
  84. lychee.error(null, params, data);
  85. }
  86. });
  87. }
  88. basicModal.show({
  89. body: "<p>Enter a title for the new album: <input class='text' data-name='title' type='text' maxlength='30' placeholder='Title' value='Untitled'></p>",
  90. buttons: {
  91. action: {
  92. title: 'Create Album',
  93. fn: action
  94. },
  95. cancel: {
  96. title: 'Cancel',
  97. fn: basicModal.close
  98. }
  99. }
  100. });
  101. }
  102. album.delete = function(albumIDs) {
  103. var action = {},
  104. cancel = {},
  105. msg = '',
  106. albumTitle = '';
  107. if (!albumIDs) return false;
  108. if (albumIDs instanceof Array===false) albumIDs = [albumIDs];
  109. action.fn = function() {
  110. var params;
  111. basicModal.close();
  112. params = 'deleteAlbum&albumIDs=' + albumIDs;
  113. lychee.api(params, function(data) {
  114. if (visible.albums()) {
  115. albumIDs.forEach(function(id) {
  116. albums.json.num--;
  117. view.albums.content.delete(id);
  118. delete albums.json.content[id];
  119. });
  120. } else {
  121. albums.refresh();
  122. lychee.goto('');
  123. }
  124. if (data!==true) lychee.error(null, params, data);
  125. });
  126. }
  127. if (albumIDs.toString()==='0') {
  128. action.title = 'Clear Unsorted';
  129. cancel.title = 'Keep Unsorted';
  130. msg = "<p>Are you sure you want to delete all photos from 'Unsorted'?<br>This action can't be undone!</p>";
  131. } else if (albumIDs.length===1) {
  132. action.title = 'Delete Album and Photos';
  133. cancel.title = 'Keep Album';
  134. // Get title
  135. if (album.json) albumTitle = album.json.title;
  136. else if (albums.json) albumTitle = albums.json.content[albumIDs].title;
  137. msg = "<p>Are you sure you want to delete the album '" + albumTitle + "' and all of the photos it contains? This action can't be undone!</p>";
  138. } else {
  139. action.title = 'Delete Albums and Photos';
  140. cancel.title = 'Keep Albums';
  141. msg = "<p>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!</p>";
  142. }
  143. basicModal.show({
  144. body: msg,
  145. buttons: {
  146. action: {
  147. title: action.title,
  148. fn: action.fn,
  149. class: 'red'
  150. },
  151. cancel: {
  152. title: cancel.title,
  153. fn: basicModal.close
  154. }
  155. }
  156. });
  157. }
  158. album.setTitle = function(albumIDs) {
  159. var oldTitle = '',
  160. input = '',
  161. msg = '',
  162. action;
  163. if (!albumIDs) return false;
  164. if (albumIDs instanceof Array===false) albumIDs = [albumIDs];
  165. if (albumIDs.length===1) {
  166. // Get old title if only one album is selected
  167. if (album.json) oldTitle = album.json.title;
  168. else if (albums.json) oldTitle = albums.json.content[albumIDs].title;
  169. if (!oldTitle) oldTitle = '';
  170. oldTitle = oldTitle.replace("'", '&apos;');
  171. }
  172. action = function(data) {
  173. var params,
  174. newTitle = data.title;
  175. basicModal.close();
  176. // Remove html from input
  177. newTitle = lychee.removeHTML(newTitle);
  178. // Set to Untitled when empty
  179. newTitle = (newTitle==='') ? 'Untitled' : newTitle;
  180. if (visible.album()) {
  181. album.json.title = newTitle;
  182. view.album.title();
  183. if (albums.json) {
  184. var id = albumIDs[0];
  185. albums.json.content[id].title = newTitle;
  186. }
  187. } else if (visible.albums()) {
  188. albumIDs.forEach(function(id) {
  189. albums.json.content[id].title = newTitle;
  190. view.albums.content.title(id);
  191. });
  192. }
  193. params = 'setAlbumTitle&albumIDs=' + albumIDs + '&title=' + escape(encodeURI(newTitle));
  194. lychee.api(params, function(data) {
  195. if (data!==true) lychee.error(null, params, data);
  196. });
  197. }
  198. input = "<input class='text' data-name='title' type='text' maxlength='30' placeholder='Title' value='" + oldTitle + "'>";
  199. if (albumIDs.length===1) msg = "<p>Enter a new title for this album: " + input + "</p>";
  200. else msg = "<p>Enter a title for all " + albumIDs.length + " selected albums: " + input +"</p>";
  201. basicModal.show({
  202. body: msg,
  203. buttons: {
  204. action: {
  205. title: 'Set Title',
  206. fn: action
  207. },
  208. cancel: {
  209. title: 'Cancel',
  210. fn: basicModal.close
  211. }
  212. }
  213. });
  214. }
  215. album.setDescription = function(photoID) {
  216. var oldDescription = album.json.description.replace("'", '&apos;'),
  217. action;
  218. action = function(data) {
  219. var params,
  220. description = data.description;
  221. basicModal.close();
  222. // Remove html from input
  223. description = lychee.removeHTML(description);
  224. if (visible.album()) {
  225. album.json.description = description;
  226. view.album.description();
  227. }
  228. params = 'setAlbumDescription&albumID=' + photoID + '&description=' + escape(encodeURI(description));
  229. lychee.api(params, function(data) {
  230. if (data!==true) lychee.error(null, params, data);
  231. });
  232. }
  233. basicModal.show({
  234. body: "<p>Please enter a description for this album: <input class='text' data-name='description' type='text' maxlength='800' placeholder='Description' value='" + oldDescription + "'></p>",
  235. buttons: {
  236. action: {
  237. title: 'Set Description',
  238. fn: action
  239. },
  240. cancel: {
  241. title: 'Cancel',
  242. fn: basicModal.close
  243. }
  244. }
  245. });
  246. }
  247. album.setPublic = function(albumID, e) {
  248. var params,
  249. password = '',
  250. listed = false,
  251. downloadable = false;
  252. albums.refresh();
  253. if (!basicModal.visible()&&album.json.public==0) {
  254. var msg = '',
  255. action;
  256. action = function() {
  257. basicModal.close();
  258. album.setPublic(album.getID(), e);
  259. };
  260. msg = `
  261. <p class='less'>This album will be shared with the following properties:</p>
  262. <form>
  263. <div class='choice'>
  264. <label>
  265. <input type='checkbox' name='listed' checked>
  266. <span class='checkbox'>${ build.iconic('check') }</span>
  267. <span class='label'>Visible</span>
  268. </label>
  269. <p>Listed to visitors of your Lychee.</p>
  270. </div>
  271. <div class='choice'>
  272. <label>
  273. <input type='checkbox' name='downloadable'>
  274. <span class='checkbox'>${ build.iconic('check') }</span>
  275. <span class='label'>Downloadable</span>
  276. </label>
  277. <p>Visitors of your Lychee can download this album.</p>
  278. </div>
  279. <div class='choice'>
  280. <label>
  281. <input type='checkbox' name='password'>
  282. <span class='checkbox'>${ build.iconic('check') }</span>
  283. <span class='label'>Password protected</span>
  284. </label>
  285. <p>Only accessible with a valid password.</p>
  286. <input class='text' data-name='password' type='password' placeholder='password' value=''>
  287. </div>
  288. </form>
  289. `
  290. basicModal.show({
  291. body: msg,
  292. buttons: {
  293. action: {
  294. title: 'Share Album',
  295. fn: action
  296. },
  297. cancel: {
  298. title: 'Cancel',
  299. fn: basicModal.close
  300. }
  301. }
  302. });
  303. $('.basicModal .choice input[name="password"]').on('change', function() {
  304. if ($(this).prop('checked')===true) $('.basicModal .choice input[data-name="password"]').show().focus();
  305. else $('.basicModal .choice input[data-name="password"]').hide();
  306. });
  307. return true;
  308. }
  309. if (basicModal.visible()) {
  310. if ($('.basicModal .choice input[name="password"]:checked').length===1) {
  311. password = md5($('.basicModal .choice input[data-name="password"]').val());
  312. album.json.password = 1;
  313. } else {
  314. password = '';
  315. album.json.password = 0;
  316. }
  317. if ($('.basicModal .choice input[name="listed"]:checked').length===1) listed = true;
  318. if ($('.basicModal .choice input[name="downloadable"]:checked').length===1) downloadable = true;
  319. }
  320. if (visible.album()) {
  321. album.json.public = (album.json.public==0) ? 1 : 0;
  322. album.json.password = (album.json.public==0) ? 0 : album.json.password;
  323. view.album.public();
  324. view.album.password();
  325. if (album.json.public==1) contextMenu.shareAlbum(albumID, e);
  326. }
  327. params = 'setAlbumPublic&albumID=' + albumID + '&password=' + password + '&visible=' + listed + '&downloadable=' + downloadable;
  328. lychee.api(params, function(data) {
  329. if (data!==true) lychee.error(null, params, data);
  330. });
  331. }
  332. album.share = function(service) {
  333. var link = '',
  334. url = location.href;
  335. switch (service) {
  336. case 0:
  337. link = 'https://twitter.com/share?url=' + encodeURI(url);
  338. break;
  339. case 1:
  340. link = 'http://www.facebook.com/sharer.php?u=' + encodeURI(url) + '&t=' + encodeURI(album.json.title);
  341. break;
  342. case 2:
  343. link = 'mailto:?subject=' + encodeURI(album.json.title) + '&body=' + encodeURI(url);
  344. break;
  345. default:
  346. link = '';
  347. break;
  348. }
  349. if (link.length>5) location.href = link;
  350. }
  351. album.getArchive = function(albumID) {
  352. var link,
  353. url = 'php/api.php?function=getAlbumArchive&albumID=' + albumID;
  354. if (location.href.indexOf('index.html')>0) link = location.href.replace(location.hash, '').replace('index.html', url);
  355. else link = location.href.replace(location.hash, '') + url;
  356. if (lychee.publicMode) link += '&password=' + password.value;
  357. location.href = link;
  358. }