album.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. if (data!==false && isNumber(data)) {
  79. albums.refresh()
  80. lychee.goto(data)
  81. } else {
  82. lychee.error(null, params, data)
  83. }
  84. })
  85. }
  86. basicModal.show({
  87. body: `<p>Enter a title for the new album: <input class='text' name='title' type='text' maxlength='50' placeholder='Title' value='Untitled'></p>`,
  88. buttons: {
  89. action: {
  90. title: 'Create Album',
  91. fn: action
  92. },
  93. cancel: {
  94. title: 'Cancel',
  95. fn: basicModal.close
  96. }
  97. }
  98. })
  99. }
  100. album.delete = function(albumIDs) {
  101. let action = {}
  102. let cancel = {}
  103. let msg = ''
  104. if (!albumIDs) return false
  105. if (albumIDs instanceof Array===false) albumIDs = [ albumIDs ]
  106. action.fn = function() {
  107. basicModal.close()
  108. let params = {
  109. albumIDs: albumIDs.join()
  110. }
  111. api.post('Album::delete', params, function(data) {
  112. if (visible.albums()) {
  113. albumIDs.forEach(function(id) {
  114. albums.json.num--
  115. view.albums.content.delete(id)
  116. albums.deleteByID(id)
  117. })
  118. } else {
  119. albums.refresh()
  120. lychee.goto()
  121. }
  122. if (data!==true) lychee.error(null, params, data)
  123. })
  124. }
  125. if (albumIDs.toString()==='0') {
  126. action.title = 'Clear Unsorted'
  127. cancel.title = 'Keep Unsorted'
  128. msg = `<p>Are you sure you want to delete all photos from 'Unsorted'?<br>This action can't be undone!</p>`
  129. } else if (albumIDs.length===1) {
  130. let albumTitle = ''
  131. action.title = 'Delete Album and Photos'
  132. cancel.title = 'Keep Album'
  133. // Get title
  134. if (album.json) albumTitle = album.json.title
  135. else if (albums.json) albumTitle = albums.getByID(albumIDs).title
  136. // Fallback for album without a title
  137. if (albumTitle==='') albumTitle = 'Untitled'
  138. 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>`
  139. } else {
  140. action.title = 'Delete Albums and Photos'
  141. cancel.title = 'Keep Albums'
  142. 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>`
  143. }
  144. basicModal.show({
  145. body: msg,
  146. buttons: {
  147. action: {
  148. title: action.title,
  149. fn: action.fn,
  150. class: 'red'
  151. },
  152. cancel: {
  153. title: cancel.title,
  154. fn: basicModal.close
  155. }
  156. }
  157. })
  158. }
  159. album.setTitle = function(albumIDs) {
  160. let oldTitle = ''
  161. let msg = ''
  162. if (!albumIDs) return false
  163. if (albumIDs instanceof Array===false) albumIDs = [ albumIDs ]
  164. if (albumIDs.length===1) {
  165. // Get old title if only one album is selected
  166. if (album.json) oldTitle = album.json.title
  167. else if (albums.json) oldTitle = albums.getByID(albumIDs).title
  168. }
  169. const action = function(data) {
  170. basicModal.close()
  171. let newTitle = data.title
  172. if (visible.album()) {
  173. // Rename only one album
  174. album.json.title = newTitle
  175. view.album.title()
  176. if (albums.json) albums.getByID(albumIDs[0]).title = newTitle
  177. } else if (visible.albums()) {
  178. // Rename all albums
  179. albumIDs.forEach(function(id) {
  180. albums.getByID(id).title = newTitle
  181. view.albums.content.title(id)
  182. })
  183. }
  184. let params = {
  185. albumIDs : albumIDs.join(),
  186. title : newTitle
  187. }
  188. api.post('Album::setTitle', params, function(data) {
  189. if (data!==true) lychee.error(null, params, data)
  190. })
  191. }
  192. let input = lychee.html`<input class='text' name='title' type='text' maxlength='50' placeholder='Title' value='$${ oldTitle }'>`
  193. if (albumIDs.length===1) msg = lychee.html`<p>Enter a new title for this album: ${ input }</p>`
  194. else msg = lychee.html`<p>Enter a title for all $${ albumIDs.length } selected albums: ${ input }</p>`
  195. basicModal.show({
  196. body: msg,
  197. buttons: {
  198. action: {
  199. title: 'Set Title',
  200. fn: action
  201. },
  202. cancel: {
  203. title: 'Cancel',
  204. fn: basicModal.close
  205. }
  206. }
  207. })
  208. }
  209. album.setDescription = function(albumID) {
  210. let oldDescription = album.json.description
  211. const action = function(data) {
  212. let description = data.description
  213. basicModal.close()
  214. if (visible.album()) {
  215. album.json.description = description
  216. view.album.description()
  217. }
  218. let params = {
  219. albumID,
  220. description
  221. }
  222. api.post('Album::setDescription', params, function(data) {
  223. if (data!==true) lychee.error(null, params, data)
  224. })
  225. }
  226. basicModal.show({
  227. 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>`,
  228. buttons: {
  229. action: {
  230. title: 'Set Description',
  231. fn: action
  232. },
  233. cancel: {
  234. title: 'Cancel',
  235. fn: basicModal.close
  236. }
  237. }
  238. })
  239. }
  240. album.setPublic = function(albumID, modal, e) {
  241. let password = ''
  242. albums.refresh()
  243. if (modal===true) {
  244. let text = ''
  245. let action = {}
  246. action.fn = () => {
  247. // Call setPublic function without showing the modal
  248. album.setPublic(album.getID(), false, e)
  249. }
  250. // Album public = Editing a shared album
  251. if (album.json.public==='1') {
  252. action.title = 'Edit Sharing'
  253. text = 'The sharing-properties of this album will be changed to the following:'
  254. } else {
  255. action.title = 'Share Album'
  256. text = 'This album will be shared with the following properties:'
  257. }
  258. let msg = `
  259. <p class='less'>${ text }</p>
  260. <form>
  261. <div class='choice'>
  262. <label>
  263. <input type='checkbox' name='hidden'>
  264. <span class='checkbox'>${ build.iconic('check') }</span>
  265. <span class='label'>Hidden</span>
  266. </label>
  267. <p>Only people with the direct link can view this album.</p>
  268. </div>
  269. <div class='choice'>
  270. <label>
  271. <input type='checkbox' name='downloadable'>
  272. <span class='checkbox'>${ build.iconic('check') }</span>
  273. <span class='label'>Downloadable</span>
  274. </label>
  275. <p>Visitors of your Lychee can download this album.</p>
  276. </div>
  277. <div class='choice'>
  278. <label>
  279. <input type='checkbox' name='password'>
  280. <span class='checkbox'>${ build.iconic('check') }</span>
  281. <span class='label'>Password protected</span>
  282. </label>
  283. <p>Album only accessible with a valid password.</p>
  284. <input class='text' name='passwordtext' type='password' placeholder='password' value=''>
  285. </div>
  286. </form>
  287. `
  288. basicModal.show({
  289. body: msg,
  290. buttons: {
  291. action: {
  292. title: action.title,
  293. fn: action.fn
  294. },
  295. cancel: {
  296. title: 'Cancel',
  297. fn: basicModal.close
  298. }
  299. }
  300. })
  301. if (album.json.public==='1' && album.json.visible==='0') $('.basicModal .choice input[name="hidden"]').click()
  302. if (album.json.downloadable==='1') $('.basicModal .choice input[name="downloadable"]').click()
  303. $('.basicModal .choice input[name="password"]').on('change', function() {
  304. if ($(this).prop('checked')===true) $('.basicModal .choice input[name="passwordtext"]').show().focus()
  305. else $('.basicModal .choice input[name="passwordtext"]').hide()
  306. })
  307. return true
  308. }
  309. // Set data
  310. if (basicModal.visible()) {
  311. // Visible modal => Set album public
  312. album.json.public = '1'
  313. // Set visible
  314. if ($('.basicModal .choice input[name="hidden"]:checked').length===1) album.json.visible = '0'
  315. else album.json.visible = '1'
  316. // Set downloadable
  317. if ($('.basicModal .choice input[name="downloadable"]:checked').length===1) album.json.downloadable = '1'
  318. else album.json.downloadable = '0'
  319. // Set password
  320. if ($('.basicModal .choice input[name="password"]:checked').length===1) {
  321. password = $('.basicModal .choice input[name="passwordtext"]').val()
  322. album.json.password = '1'
  323. } else {
  324. password = ''
  325. album.json.password = '0'
  326. }
  327. // Modal input has been processed, now it can be closed
  328. basicModal.close()
  329. } else {
  330. // Modal not visible => Set album private
  331. album.json.public = '0'
  332. }
  333. // Set data and refresh view
  334. if (visible.album()) {
  335. album.json.visible = (album.json.public==='0') ? '1' : album.json.visible
  336. album.json.downloadable = (album.json.public==='0') ? '0' : album.json.downloadable
  337. album.json.password = (album.json.public==='0') ? '0' : album.json.password
  338. view.album.public()
  339. view.album.hidden()
  340. view.album.downloadable()
  341. view.album.password()
  342. if (album.json.public==='1') contextMenu.shareAlbum(albumID, e)
  343. }
  344. let params = {
  345. albumID,
  346. public : album.json.public,
  347. password : password,
  348. visible : album.json.visible,
  349. downloadable : album.json.downloadable
  350. }
  351. api.post('Album::setPublic', params, function(data) {
  352. if (data!==true) lychee.error(null, params, data)
  353. })
  354. }
  355. album.share = function(service) {
  356. let url = location.href
  357. switch (service) {
  358. case 'twitter':
  359. window.open(`https://twitter.com/share?url=${ encodeURI(url) }`)
  360. break
  361. case 'facebook':
  362. window.open(`http://www.facebook.com/sharer.php?u=${ encodeURI(url) }&t=${ encodeURI(album.json.title) }`)
  363. break
  364. case 'mail':
  365. location.href = `mailto:?subject=${ encodeURI(album.json.title) }&body=${ encodeURI(url) }`
  366. break
  367. }
  368. }
  369. album.getArchive = function(albumID) {
  370. let link = ''
  371. let 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===true) link += `&password=${ encodeURIComponent(password.value) }`
  375. location.href = link
  376. }
  377. album.merge = function(albumIDs) {
  378. let title = ''
  379. let sTitle = ''
  380. let msg = ''
  381. if (!albumIDs) return false
  382. if (albumIDs instanceof Array===false) albumIDs = [ albumIDs ]
  383. // Get title of first album
  384. if (albums.json) title = albums.getByID(albumIDs[0]).title
  385. // Fallback for first album without a title
  386. if (title==='') title = 'Untitled'
  387. if (albumIDs.length===2) {
  388. // Get title of second album
  389. if (albums.json) sTitle = albums.getByID(albumIDs[1]).title
  390. // Fallback for second album without a title
  391. if (sTitle==='') sTitle = 'Untitled'
  392. msg = lychee.html`<p>Are you sure you want to merge the album '$${ sTitle }' into the album '$${ title }'?</p>`
  393. } else {
  394. msg = lychee.html`<p>Are you sure you want to merge all selected albums into the album '$${ title }'?</p>`
  395. }
  396. const action = function() {
  397. basicModal.close()
  398. let params = {
  399. albumIDs: albumIDs.join()
  400. }
  401. api.post('Album::merge', params, function(data) {
  402. if (data!==true) {
  403. lychee.error(null, params, data)
  404. } else {
  405. albums.refresh()
  406. lychee.goto()
  407. }
  408. })
  409. }
  410. basicModal.show({
  411. body: msg,
  412. buttons: {
  413. action: {
  414. title: 'Merge Albums',
  415. fn: action,
  416. class: 'red'
  417. },
  418. cancel: {
  419. title: "Don't Merge",
  420. fn: basicModal.close
  421. }
  422. }
  423. })
  424. }