album.js 11 KB

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