album.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. let id = null
  10. let isID = (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 = false) {
  23. password.get(albumID, function() {
  24. if (refresh===false) lychee.animate('.content', 'contentZoomOut')
  25. let startTime = new Date().getTime()
  26. let params = {
  27. albumID,
  28. password: password.value
  29. }
  30. api.post('Album::get', params, function(data) {
  31. let waitTime = 0
  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. let 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(() => {
  57. view.album.init()
  58. if (refresh===false) {
  59. lychee.animate(lychee.content, '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. const action = function(data) {
  71. let title = data.title
  72. const isNumber = (n) => (!isNaN(parseFloat(n)) && isFinite(n))
  73. basicModal.close()
  74. let params = {
  75. title
  76. }
  77. api.post('Album::add', 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' name='title' type='text' maxlength='50' 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. let action = {}
  104. let cancel = {}
  105. let msg = ''
  106. if (!albumIDs) return false
  107. if (albumIDs instanceof Array===false) albumIDs = [ albumIDs ]
  108. action.fn = function() {
  109. basicModal.close()
  110. let params = {
  111. albumIDs: albumIDs.join()
  112. }
  113. api.post('Album::delete', params, function(data) {
  114. if (visible.albums()) {
  115. albumIDs.forEach(function(id) {
  116. albums.json.num--
  117. view.albums.content.delete(id)
  118. albums.deleteByID(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. let albumTitle = ''
  133. action.title = 'Delete Album and Photos'
  134. cancel.title = 'Keep Album'
  135. // Get title
  136. if (album.json) albumTitle = album.json.title
  137. else if (albums.json) albumTitle = albums.getByID(albumIDs).title
  138. // Fallback for album without a title
  139. if (albumTitle==='') albumTitle = 'Untitled'
  140. msg = lychee.html`<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>`
  141. } else {
  142. action.title = 'Delete Albums and Photos'
  143. cancel.title = 'Keep Albums'
  144. msg = lychee.html`<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>`
  145. }
  146. basicModal.show({
  147. body: msg,
  148. buttons: {
  149. action: {
  150. title: action.title,
  151. fn: action.fn,
  152. class: 'red'
  153. },
  154. cancel: {
  155. title: cancel.title,
  156. fn: basicModal.close
  157. }
  158. }
  159. })
  160. }
  161. album.setTitle = function(albumIDs) {
  162. let oldTitle = ''
  163. let msg = ''
  164. if (!albumIDs) return false
  165. if (albumIDs instanceof Array===false) albumIDs = [ albumIDs ]
  166. if (albumIDs.length===1) {
  167. // Get old title if only one album is selected
  168. if (album.json) oldTitle = album.json.title
  169. else if (albums.json) oldTitle = albums.getByID(albumIDs).title
  170. }
  171. const action = function(data) {
  172. basicModal.close()
  173. let newTitle = data.title
  174. if (visible.album()) {
  175. // Rename only one album
  176. album.json.title = newTitle
  177. view.album.title()
  178. if (albums.json) albums.getByID(albumIDs[0]).title = newTitle
  179. } else if (visible.albums()) {
  180. // Rename all albums
  181. albumIDs.forEach(function(id) {
  182. albums.getByID(id).title = newTitle
  183. view.albums.content.title(id)
  184. })
  185. }
  186. let params = {
  187. albumIDs : albumIDs.join(),
  188. title : newTitle
  189. }
  190. api.post('Album::setTitle', params, function(data) {
  191. if (data!==true) lychee.error(null, params, data)
  192. })
  193. }
  194. let input = lychee.html`<input class='text' name='title' type='text' maxlength='50' placeholder='Title' value='$${ oldTitle }'>`
  195. if (albumIDs.length===1) msg = lychee.html`<p>Enter a new title for this album: ${ input }</p>`
  196. else msg = lychee.html`<p>Enter a title for all $${ albumIDs.length } selected albums: ${ input }</p>`
  197. basicModal.show({
  198. body: msg,
  199. buttons: {
  200. action: {
  201. title: 'Set Title',
  202. fn: action
  203. },
  204. cancel: {
  205. title: 'Cancel',
  206. fn: basicModal.close
  207. }
  208. }
  209. })
  210. }
  211. album.setDescription = function(albumID) {
  212. let oldDescription = album.json.description
  213. const action = function(data) {
  214. let description = data.description
  215. basicModal.close()
  216. if (visible.album()) {
  217. album.json.description = description
  218. view.album.description()
  219. }
  220. let params = {
  221. albumID,
  222. description
  223. }
  224. api.post('Album::setDescription', params, function(data) {
  225. if (data!==true) lychee.error(null, params, data)
  226. })
  227. }
  228. basicModal.show({
  229. body: lychee.html`<p>Please enter a description for this album: <input class='text' name='description' type='text' maxlength='800' placeholder='Description' value='$${ oldDescription }'></p>`,
  230. buttons: {
  231. action: {
  232. title: 'Set Description',
  233. fn: action
  234. },
  235. cancel: {
  236. title: 'Cancel',
  237. fn: basicModal.close
  238. }
  239. }
  240. })
  241. }
  242. album.setPublic = function(albumID, modal, e) {
  243. let password = ''
  244. albums.refresh()
  245. if (modal===true) {
  246. let text = ''
  247. let action = {}
  248. action.fn = () => {
  249. // Call setPublic function without showing the modal
  250. album.setPublic(album.getID(), false, e)
  251. }
  252. // Album public = Editing a shared album
  253. if (album.json.public==='1') {
  254. action.title = 'Edit Sharing'
  255. text = 'The sharing-properties of this album will be changed to the following:'
  256. } else {
  257. action.title = 'Share Album'
  258. text = 'This album will be shared with the following properties:'
  259. }
  260. let msg = `
  261. <p class='less'>${ text }</p>
  262. <form>
  263. <div class='choice'>
  264. <label>
  265. <input type='checkbox' name='hidden'>
  266. <span class='checkbox'>${ build.iconic('check') }</span>
  267. <span class='label'>Hidden</span>
  268. </label>
  269. <p>Only people with the direct link can view this album.</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>Album only accessible with a valid password.</p>
  286. <input class='text' name='passwordtext' type='password' placeholder='password' value=''>
  287. </div>
  288. </form>
  289. `
  290. basicModal.show({
  291. body: msg,
  292. buttons: {
  293. action: {
  294. title: action.title,
  295. fn: action.fn
  296. },
  297. cancel: {
  298. title: 'Cancel',
  299. fn: basicModal.close
  300. }
  301. }
  302. })
  303. if (album.json.public==='1' && album.json.visible==='0') $('.basicModal .choice input[name="hidden"]').click()
  304. if (album.json.downloadable==='1') $('.basicModal .choice input[name="downloadable"]').click()
  305. $('.basicModal .choice input[name="password"]').on('change', function() {
  306. if ($(this).prop('checked')===true) $('.basicModal .choice input[name="passwordtext"]').show().focus()
  307. else $('.basicModal .choice input[name="passwordtext"]').hide()
  308. })
  309. return true
  310. }
  311. // Set data
  312. if (basicModal.visible()) {
  313. // Visible modal => Set album public
  314. album.json.public = '1'
  315. // Set visible
  316. if ($('.basicModal .choice input[name="hidden"]:checked').length===1) album.json.visible = '0'
  317. else album.json.visible = '1'
  318. // Set downloadable
  319. if ($('.basicModal .choice input[name="downloadable"]:checked').length===1) album.json.downloadable = '1'
  320. else album.json.downloadable = '0'
  321. // Set password
  322. if ($('.basicModal .choice input[name="password"]:checked').length===1) {
  323. password = $('.basicModal .choice input[name="passwordtext"]').val()
  324. album.json.password = '1'
  325. } else {
  326. password = ''
  327. album.json.password = '0'
  328. }
  329. // Modal input has been processed, now it can be closed
  330. basicModal.close()
  331. } else {
  332. // Modal not visible => Set album private
  333. album.json.public = '0'
  334. }
  335. // Set data and refresh view
  336. if (visible.album()) {
  337. album.json.visible = (album.json.public==='0') ? '1' : album.json.visible
  338. album.json.downloadable = (album.json.public==='0') ? '0' : album.json.downloadable
  339. album.json.password = (album.json.public==='0') ? '0' : album.json.password
  340. view.album.public()
  341. view.album.hidden()
  342. view.album.downloadable()
  343. view.album.password()
  344. if (album.json.public==='1') contextMenu.shareAlbum(albumID, e)
  345. }
  346. let params = {
  347. albumID,
  348. public : album.json.public,
  349. password : password,
  350. visible : album.json.visible,
  351. downloadable : album.json.downloadable
  352. }
  353. api.post('Album::setPublic', params, function(data) {
  354. if (data!==true) lychee.error(null, params, data)
  355. })
  356. }
  357. album.share = function(service) {
  358. let link = ''
  359. let url = location.href
  360. switch (service) {
  361. case 'twitter':
  362. link = `https://twitter.com/share?url=${ encodeURI(url) }`
  363. break
  364. case 'facebook':
  365. link = `http://www.facebook.com/sharer.php?u=${ encodeURI(url) }&t=${ encodeURI(album.json.title) }`
  366. break
  367. case 'mail':
  368. link = `mailto:?subject=${ encodeURI(album.json.title) }&body=${ encodeURI(url) }`
  369. break
  370. default:
  371. link = ''
  372. break
  373. }
  374. if (link!=='') location.href = link
  375. }
  376. album.getArchive = function(albumID) {
  377. let link = ''
  378. let url = `${ api.path }?function=Album::getArchive&albumID=${ albumID }`
  379. if (location.href.indexOf('index.html')>0) link = location.href.replace(location.hash, '').replace('index.html', url)
  380. else link = location.href.replace(location.hash, '') + url
  381. if (lychee.publicMode===true) link += `&password=${ encodeURIComponent(password.value) }`
  382. location.href = link
  383. }
  384. album.merge = function(albumIDs) {
  385. let title = ''
  386. let sTitle = ''
  387. let msg = ''
  388. if (!albumIDs) return false
  389. if (albumIDs instanceof Array===false) albumIDs = [ albumIDs ]
  390. // Get title of first album
  391. if (albums.json) title = albums.getByID(albumIDs[0]).title
  392. // Fallback for first album without a title
  393. if (title==='') title = 'Untitled'
  394. if (albumIDs.length===2) {
  395. // Get title of second album
  396. if (albums.json) sTitle = albums.getByID(albumIDs[1]).title
  397. // Fallback for second album without a title
  398. if (sTitle==='') sTitle = 'Untitled'
  399. msg = lychee.html`<p>Are you sure you want to merge the album '$${ sTitle }' into the album '$${ title }'?</p>`
  400. } else {
  401. msg = lychee.html`<p>Are you sure you want to merge all selected albums into the album '$${ title }'?</p>`
  402. }
  403. const action = function() {
  404. basicModal.close()
  405. let params = {
  406. albumIDs: albumIDs.join()
  407. }
  408. api.post('Album::merge', params, function(data) {
  409. if (data!==true) {
  410. lychee.error(null, params, data)
  411. } else {
  412. albums.refresh()
  413. lychee.goto()
  414. }
  415. })
  416. }
  417. basicModal.show({
  418. body: msg,
  419. buttons: {
  420. action: {
  421. title: 'Merge Albums',
  422. fn: action,
  423. class: 'red'
  424. },
  425. cancel: {
  426. title: "Don't Merge",
  427. fn: basicModal.close
  428. }
  429. }
  430. })
  431. }