album.js 14 KB

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