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. if (title.length===0) title = 'Untitled'
  75. api.post('Album::add', { title }, function(data) {
  76. // Avoid first album to be true
  77. if (data===true) data = 1
  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. let params
  108. basicModal.close()
  109. params = {
  110. albumIDs: albumIDs.join()
  111. }
  112. api.post('Album::delete', params, function(data) {
  113. if (visible.albums()) {
  114. albumIDs.forEach(function(id) {
  115. albums.json.num--
  116. view.albums.content.delete(id)
  117. albums.deleteByID(id)
  118. })
  119. } else {
  120. albums.refresh()
  121. lychee.goto('')
  122. }
  123. if (data!==true) lychee.error(null, params, data)
  124. })
  125. }
  126. if (albumIDs.toString()==='0') {
  127. action.title = 'Clear Unsorted'
  128. cancel.title = 'Keep Unsorted'
  129. msg = `<p>Are you sure you want to delete all photos from 'Unsorted'?<br>This action can't be undone!</p>`
  130. } else if (albumIDs.length===1) {
  131. let albumTitle = ''
  132. action.title = 'Delete Album and Photos'
  133. cancel.title = 'Keep Album'
  134. // Get title
  135. if (album.json) albumTitle = album.json.title
  136. else if (albums.json) albumTitle = albums.getByID(albumIDs).title
  137. 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>`
  138. } else {
  139. action.title = 'Delete Albums and Photos'
  140. cancel.title = 'Keep Albums'
  141. 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>`
  142. }
  143. basicModal.show({
  144. body: msg,
  145. buttons: {
  146. action: {
  147. title: action.title,
  148. fn: action.fn,
  149. class: 'red'
  150. },
  151. cancel: {
  152. title: cancel.title,
  153. fn: basicModal.close
  154. }
  155. }
  156. })
  157. }
  158. album.setTitle = function(albumIDs) {
  159. let oldTitle = ''
  160. let msg = ''
  161. if (!albumIDs) return false
  162. if (albumIDs instanceof Array===false) albumIDs = [ albumIDs ]
  163. if (albumIDs.length===1) {
  164. // Get old title if only one album is selected
  165. if (album.json) oldTitle = album.json.title
  166. else if (albums.json) oldTitle = albums.getByID(albumIDs).title
  167. if (!oldTitle) oldTitle = ''
  168. }
  169. const action = function(data) {
  170. let newTitle = data.title
  171. basicModal.close()
  172. // Set title to Untitled when empty
  173. newTitle = (newTitle==='') ? 'Untitled' : newTitle
  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.replace(/'/g, '&apos;')
  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='visible'>
  266. <span class='checkbox'>${ build.iconic('check') }</span>
  267. <span class='label'>Visible</span>
  268. </label>
  269. <p>Listed to visitors of your Lychee.</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>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. // Active visible by default (public = 0)
  304. if ((album.json.public==='1' && album.json.visible==='1') || (album.json.public==='0')) $('.basicModal .choice input[name="visible"]').click()
  305. if (album.json.downloadable==='1') $('.basicModal .choice input[name="downloadable"]').click()
  306. $('.basicModal .choice input[name="password"]').on('change', function() {
  307. if ($(this).prop('checked')===true) $('.basicModal .choice input[name="passwordtext"]').show().focus()
  308. else $('.basicModal .choice input[name="passwordtext"]').hide()
  309. })
  310. return true
  311. }
  312. // Set data
  313. if (basicModal.visible()) {
  314. // Visible modal => Set album public
  315. album.json.public = '1'
  316. // Set visible
  317. if ($('.basicModal .choice input[name="visible"]:checked').length===1) album.json.visible = '1'
  318. else album.json.visible = '0'
  319. // Set downloadable
  320. if ($('.basicModal .choice input[name="downloadable"]:checked').length===1) album.json.downloadable = '1'
  321. else album.json.downloadable = '0'
  322. // Set password
  323. if ($('.basicModal .choice input[name="password"]:checked').length===1) {
  324. password = $('.basicModal .choice input[name="passwordtext"]').val()
  325. album.json.password = '1'
  326. } else {
  327. password = ''
  328. album.json.password = '0'
  329. }
  330. // Modal input has been processed, now it can be closed
  331. basicModal.close()
  332. } else {
  333. // Modal not visible => Set album private
  334. album.json.public = '0'
  335. }
  336. // Set data and refresh view
  337. if (visible.album()) {
  338. album.json.visible = (album.json.public==='0') ? '0' : album.json.visible
  339. album.json.downloadable = (album.json.public==='0') ? '0' : album.json.downloadable
  340. album.json.password = (album.json.public==='0') ? '0' : album.json.password
  341. view.album.public()
  342. view.album.visible()
  343. view.album.downloadable()
  344. view.album.password()
  345. if (album.json.public==='1') contextMenu.shareAlbum(albumID, e)
  346. }
  347. let params = {
  348. albumID,
  349. public : album.json.public,
  350. password : password,
  351. visible : album.json.visible,
  352. downloadable : album.json.downloadable
  353. }
  354. api.post('Album::setPublic', params, function(data) {
  355. if (data!==true) lychee.error(null, params, data)
  356. })
  357. }
  358. album.share = function(service) {
  359. let link = ''
  360. let url = location.href
  361. switch (service) {
  362. case 'twitter':
  363. link = `https://twitter.com/share?url=${ encodeURI(url) }`
  364. break
  365. case 'facebook':
  366. link = `http://www.facebook.com/sharer.php?u=${ encodeURI(url) }&t=${ encodeURI(album.json.title) }`
  367. break
  368. case 'mail':
  369. link = `mailto:?subject=${ encodeURI(album.json.title) }&body=${ encodeURI(url) }`
  370. break
  371. default:
  372. link = ''
  373. break
  374. }
  375. if (link!=='') location.href = link
  376. }
  377. album.getArchive = function(albumID) {
  378. let link = ''
  379. let url = `${ api.path }?function=Album::getArchive&albumID=${ albumID }`
  380. if (location.href.indexOf('index.html')>0) link = location.href.replace(location.hash, '').replace('index.html', url)
  381. else link = location.href.replace(location.hash, '') + url
  382. if (lychee.publicMode===true) link += `&password=${ encodeURIComponent(password.value) }`
  383. location.href = link
  384. }
  385. album.merge = function(albumIDs) {
  386. let title = ''
  387. let sTitle = ''
  388. let msg = ''
  389. if (!albumIDs) return false
  390. if (albumIDs instanceof Array===false) albumIDs = [ albumIDs ]
  391. // Get title of first album
  392. if (albums.json) title = albums.getByID(albumIDs[0]).title
  393. if (!title) title = ''
  394. title = title.replace(/'/g, '&apos;')
  395. if (albumIDs.length===2) {
  396. // Get title of second album
  397. if (albums.json) sTitle = albums.getByID(albumIDs[1]).title
  398. if (!sTitle) sTitle = ''
  399. sTitle = sTitle.replace(/'/g, '&apos;')
  400. msg = lychee.html`<p>Are you sure you want to merge the album '$${ sTitle }' into the album '$${ title }'?</p>`
  401. } else {
  402. msg = lychee.html`<p>Are you sure you want to merge all selected albums into the album '$${ title }'?</p>`
  403. }
  404. const action = function() {
  405. basicModal.close()
  406. let params = {
  407. albumIDs: albumIDs.join()
  408. }
  409. api.post('Album::merge', params, function(data) {
  410. if (data!==true) {
  411. lychee.error(null, params, data)
  412. } else {
  413. albums.refresh()
  414. albums.load()
  415. }
  416. })
  417. }
  418. basicModal.show({
  419. body: msg,
  420. buttons: {
  421. action: {
  422. title: 'Merge Albums',
  423. fn: action,
  424. class: 'red'
  425. },
  426. cancel: {
  427. title: "Don't Merge",
  428. fn: basicModal.close
  429. }
  430. }
  431. })
  432. }