album.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 = null;
  10. var isID = function(id) {
  11. if (id==='0'||id==='f'||id==='s'||id==='r') return true;
  12. return $.isNumeric(id);
  13. }
  14. if (photo.json) id = photo.json.album;
  15. else if (album.json) id = album.json.id;
  16. // Search
  17. if (isID(id)===false) id = $('.album:hover, .album.active').attr('data-id');
  18. if (isID(id)===false) id = $('.photo:hover, .photo.active').attr('data-album-id');
  19. if (isID(id)===true) return id;
  20. else return false;
  21. }
  22. album.load = function(albumID, refresh) {
  23. var startTime,
  24. params,
  25. durationTime,
  26. waitTime;
  27. password.get(albumID, function() {
  28. if (!refresh) lychee.animate('#content', 'contentZoomOut');
  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('#content', '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='50' 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. albums.deleteByID(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.getByID(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.getByID(albumIDs).title;
  176. if (!oldTitle) oldTitle = '';
  177. oldTitle = oldTitle.replace(/'/g, '&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.getByID(id).title = newTitle;
  193. }
  194. } else if (visible.albums()) {
  195. albumIDs.forEach(function(id) {
  196. albums.getByID(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='50' 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(/'/g, '&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, modal, e) {
  261. var params,
  262. password = '';
  263. albums.refresh();
  264. if (modal===true) {
  265. let msg = '',
  266. text = '',
  267. action = {};
  268. action.fn = function() {
  269. // Current function without showing the modal
  270. album.setPublic(album.getID(), false, e);
  271. };
  272. // Album public = Editing a shared album
  273. if (album.json.public==='1') {
  274. action.title = 'Edit Sharing';
  275. text = 'The sharing-properties of this album will be changed to the following:';
  276. } else {
  277. action.title = 'Share Album';
  278. text = 'This album will be shared with the following properties:';
  279. }
  280. msg = `
  281. <p class='less'>${ text }</p>
  282. <form>
  283. <div class='choice'>
  284. <label>
  285. <input type='checkbox' name='visible'>
  286. <span class='checkbox'>${ build.iconic('check') }</span>
  287. <span class='label'>Visible</span>
  288. </label>
  289. <p>Listed to visitors of your Lychee.</p>
  290. </div>
  291. <div class='choice'>
  292. <label>
  293. <input type='checkbox' name='downloadable'>
  294. <span class='checkbox'>${ build.iconic('check') }</span>
  295. <span class='label'>Downloadable</span>
  296. </label>
  297. <p>Visitors of your Lychee can download this album.</p>
  298. </div>
  299. <div class='choice'>
  300. <label>
  301. <input type='checkbox' name='password'>
  302. <span class='checkbox'>${ build.iconic('check') }</span>
  303. <span class='label'>Password protected</span>
  304. </label>
  305. <p>Only accessible with a valid password.</p>
  306. <input class='text' data-name='password' type='password' placeholder='password' value=''>
  307. </div>
  308. </form>
  309. `
  310. basicModal.show({
  311. body: msg,
  312. buttons: {
  313. action: {
  314. title: action.title,
  315. fn: action.fn
  316. },
  317. cancel: {
  318. title: 'Cancel',
  319. fn: basicModal.close
  320. }
  321. }
  322. });
  323. // Active visible by default (public = 0)
  324. if ((album.json.public==='1'&&album.json.visible==='1')||
  325. (album.json.public==='0')) $('.basicModal .choice input[name="visible"]').click();
  326. if (album.json.downloadable==='1') $('.basicModal .choice input[name="downloadable"]').click();
  327. $('.basicModal .choice input[name="password"]').on('change', function() {
  328. if ($(this).prop('checked')===true) $('.basicModal .choice input[data-name="password"]').show().focus();
  329. else $('.basicModal .choice input[data-name="password"]').hide();
  330. });
  331. return true;
  332. }
  333. // Set data
  334. if (basicModal.visible()) {
  335. // Visible modal => Set album public
  336. album.json.public = '1';
  337. // Set visible
  338. if ($('.basicModal .choice input[name="visible"]:checked').length===1) album.json.visible = '1';
  339. else album.json.visible = '0';
  340. // Set downloadable
  341. if ($('.basicModal .choice input[name="downloadable"]:checked').length===1) album.json.downloadable = '1';
  342. else album.json.downloadable = '0';
  343. // Set password
  344. if ($('.basicModal .choice input[name="password"]:checked').length===1) {
  345. password = $('.basicModal .choice input[data-name="password"]').val();
  346. album.json.password = '1';
  347. } else {
  348. password = '';
  349. album.json.password = '0';
  350. }
  351. // Modal input has been processed, now it can be closed
  352. basicModal.close();
  353. } else {
  354. // Modal not visible => Set album private
  355. album.json.public = '0';
  356. }
  357. // Set data and refresh view
  358. if (visible.album()) {
  359. album.json.visible = (album.json.public==='0') ? '0' : album.json.visible;
  360. album.json.downloadable = (album.json.public==='0') ? '0' : album.json.downloadable;
  361. album.json.password = (album.json.public==='0') ? '0' : album.json.password;
  362. view.album.public();
  363. view.album.visible();
  364. view.album.downloadable();
  365. view.album.password();
  366. if (album.json.public==='1') contextMenu.shareAlbum(albumID, e);
  367. }
  368. params = {
  369. albumID,
  370. public: album.json.public,
  371. password: password,
  372. visible: album.json.visible,
  373. downloadable: album.json.downloadable
  374. }
  375. api.post('Album::setPublic', params, function(data) {
  376. if (data!==true) lychee.error(null, params, data);
  377. });
  378. }
  379. album.share = function(service) {
  380. var link = '',
  381. url = location.href;
  382. switch (service) {
  383. case 0:
  384. link = 'https://twitter.com/share?url=' + encodeURI(url);
  385. break;
  386. case 1:
  387. link = 'http://www.facebook.com/sharer.php?u=' + encodeURI(url) + '&t=' + encodeURI(album.json.title);
  388. break;
  389. case 2:
  390. link = 'mailto:?subject=' + encodeURI(album.json.title) + '&body=' + encodeURI(url);
  391. break;
  392. default:
  393. link = '';
  394. break;
  395. }
  396. if (link.length>5) location.href = link;
  397. }
  398. album.getArchive = function(albumID) {
  399. var link,
  400. url = api.path + '?function=Album::getArchive&albumID=' + albumID;
  401. if (location.href.indexOf('index.html')>0) link = location.href.replace(location.hash, '').replace('index.html', url);
  402. else link = location.href.replace(location.hash, '') + url;
  403. if (lychee.publicMode===true) link += '&password=' + password.value;
  404. location.href = link;
  405. }
  406. album.merge = function(albumIDs) {
  407. var action,
  408. title = '',
  409. sTitle = '',
  410. msg = '';
  411. if (!albumIDs) return false;
  412. if (albumIDs instanceof Array===false) albumIDs = [albumIDs];
  413. // Get title of first album
  414. if (albums.json) title = albums.getByID(albumIDs[0]).title;
  415. if (!title) title = '';
  416. title = title.replace(/'/g, '&apos;');
  417. if (albumIDs.length===2) {
  418. // Get title of second album
  419. if (albums.json) sTitle = albums.getByID(albumIDs[1]).title;
  420. if (!sTitle) sTitle = '';
  421. sTitle = sTitle.replace(/'/g, '&apos;');
  422. msg = "<p>Are you sure you want to merge the album '" + sTitle + "' into the album '" + title + "'?</p>";
  423. } else {
  424. msg = "<p>Are you sure you want to merge all selected albums into the album '" + title + "'?</p>";
  425. }
  426. action = function() {
  427. var params;
  428. basicModal.close();
  429. params = {
  430. albumIDs: albumIDs.join()
  431. }
  432. api.post('Album::merge', params, function(data) {
  433. if (data!==true) {
  434. lychee.error(null, params, data);
  435. } else {
  436. albums.refresh();
  437. albums.load();
  438. }
  439. });
  440. }
  441. basicModal.show({
  442. body: msg,
  443. buttons: {
  444. action: {
  445. title: 'Merge Albums',
  446. fn: action,
  447. class: 'red'
  448. },
  449. cancel: {
  450. title: "Don't Merge",
  451. fn: basicModal.close
  452. }
  453. }
  454. });
  455. }