album.js 14 KB

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