album.js 14 KB

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