photo.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /**
  2. * @description Takes care of every action a photo can handle and execute.
  3. */
  4. photo = {
  5. json : null,
  6. cache : null
  7. }
  8. photo.getID = function() {
  9. let id = null
  10. if (photo.json) id = photo.json.id
  11. else id = $('.photo:hover, .photo.active').attr('data-id')
  12. if ($.isNumeric(id)===true) return id
  13. else return false
  14. }
  15. photo.load = function(photoID, albumID) {
  16. const checkContent = function() {
  17. if (album.json!=null) photo.load(photoID, albumID)
  18. else setTimeout(checkContent, 100)
  19. }
  20. const checkPasswd = function() {
  21. if (password.value!=='') photo.load(photoID, albumID)
  22. else setTimeout(checkPasswd, 200)
  23. }
  24. if (album.json==null) {
  25. checkContent()
  26. return false
  27. }
  28. let params = {
  29. photoID,
  30. albumID,
  31. password: password.value
  32. }
  33. api.post('Photo::get', params, function(data) {
  34. if (data==='Warning: Photo private!') {
  35. lychee.content.show()
  36. lychee.goto()
  37. return false
  38. }
  39. if (data==='Warning: Wrong password!') {
  40. checkPasswd()
  41. return false
  42. }
  43. photo.json = data
  44. if (!visible.photo()) view.photo.show()
  45. view.photo.init()
  46. lychee.imageview.show()
  47. setTimeout(() => {
  48. lychee.content.show()
  49. photo.preloadNext(photoID)
  50. }, 300)
  51. })
  52. }
  53. // Preload the next photo for better response time
  54. photo.preloadNext = function(photoID) {
  55. if (album.json &&
  56. album.json.content &&
  57. album.json.content[photoID] &&
  58. album.json.content[photoID].nextPhoto!='') {
  59. let nextPhoto = album.json.content[photoID].nextPhoto
  60. let url = album.json.content[nextPhoto].url
  61. let medium = album.json.content[nextPhoto].medium
  62. let href = (medium!=null && medium!=='' ? medium : url)
  63. $('head [data-prefetch]').remove()
  64. $('head').append(`<link data-prefetch rel="prefetch" href="${ href }">`)
  65. }
  66. }
  67. photo.parse = function() {
  68. if (!photo.json.title) photo.json.title = 'Untitled'
  69. }
  70. photo.previous = function(animate) {
  71. if (photo.getID()!==false &&
  72. album.json &&
  73. album.json.content[photo.getID()] &&
  74. album.json.content[photo.getID()].previousPhoto!=='') {
  75. let delay = 0
  76. if (animate===true) {
  77. delay = 200
  78. $('#imageview #image').css({
  79. WebkitTransform : 'translateX(100%)',
  80. MozTransform : 'translateX(100%)',
  81. transform : 'translateX(100%)',
  82. opacity : 0
  83. })
  84. }
  85. setTimeout(() => {
  86. if (photo.getID()===false) return false
  87. lychee.goto(album.getID() + '/' + album.json.content[photo.getID()].previousPhoto)
  88. }, delay)
  89. }
  90. }
  91. photo.next = function(animate) {
  92. if (photo.getID()!==false &&
  93. album.json &&
  94. album.json.content[photo.getID()] &&
  95. album.json.content[photo.getID()].nextPhoto!=='') {
  96. let delay = 0
  97. if (animate===true) {
  98. delay = 200
  99. $('#imageview #image').css({
  100. WebkitTransform : 'translateX(-100%)',
  101. MozTransform : 'translateX(-100%)',
  102. transform : 'translateX(-100%)',
  103. opacity : 0
  104. })
  105. }
  106. setTimeout(() => {
  107. if (photo.getID()===false) return false
  108. lychee.goto(album.getID() + '/' + album.json.content[photo.getID()].nextPhoto)
  109. }, delay)
  110. }
  111. }
  112. photo.duplicate = function(photoIDs) {
  113. if (!photoIDs) return false
  114. if (photoIDs instanceof Array===false) photoIDs = [ photoIDs ]
  115. albums.refresh()
  116. let params = {
  117. photoIDs: photoIDs.join()
  118. }
  119. api.post('Photo::duplicate', params, function(data) {
  120. if (data!==true) lychee.error(null, params, data)
  121. else album.load(album.getID())
  122. })
  123. }
  124. photo.delete = function(photoIDs) {
  125. let action = {}
  126. let cancel = {}
  127. let msg = ''
  128. let photoTitle = ''
  129. if (!photoIDs) return false
  130. if (photoIDs instanceof Array===false) photoIDs = [ photoIDs ]
  131. if (photoIDs.length===1) {
  132. // Get title if only one photo is selected
  133. if (visible.photo()) photoTitle = photo.json.title
  134. else photoTitle = album.json.content[photoIDs].title
  135. // Fallback for photos without a title
  136. if (photoTitle==='') photoTitle = 'Untitled'
  137. }
  138. action.fn = function() {
  139. let nextPhoto = null
  140. let previousPhoto = null
  141. basicModal.close()
  142. photoIDs.forEach(function(id, index, array) {
  143. // Change reference for the next and previous photo
  144. if (album.json.content[id].nextPhoto!=='' || album.json.content[id].previousPhoto!=='') {
  145. nextPhoto = album.json.content[id].nextPhoto
  146. previousPhoto = album.json.content[id].previousPhoto
  147. album.json.content[previousPhoto].nextPhoto = nextPhoto
  148. album.json.content[nextPhoto].previousPhoto = previousPhoto
  149. }
  150. delete album.json.content[id]
  151. view.album.content.delete(id)
  152. })
  153. albums.refresh()
  154. // Go to next photo if there is a next photo and
  155. // next photo is not the current one. Show album otherwise.
  156. if (visible.photo() && nextPhoto!=null && nextPhoto!==photo.getID()) lychee.goto(album.getID() + '/' + nextPhoto)
  157. else if (!visible.albums()) lychee.goto(album.getID())
  158. let params = {
  159. photoIDs: photoIDs.join()
  160. }
  161. api.post('Photo::delete', params, function(data) {
  162. if (data!==true) lychee.error(null, params, data)
  163. })
  164. }
  165. if (photoIDs.length===1) {
  166. action.title = 'Delete Photo'
  167. cancel.title = 'Keep Photo'
  168. msg = lychee.html`<p>Are you sure you want to delete the photo '$${ photoTitle }'? This action can't be undone!</p>`
  169. } else {
  170. action.title = 'Delete Photo'
  171. cancel.title = 'Keep Photo'
  172. msg = lychee.html`<p>Are you sure you want to delete all $${ photoIDs.length } selected photo? This action can't be undone!</p>`
  173. }
  174. basicModal.show({
  175. body: msg,
  176. buttons: {
  177. action: {
  178. title: action.title,
  179. fn: action.fn,
  180. class: 'red'
  181. },
  182. cancel: {
  183. title: cancel.title,
  184. fn: basicModal.close
  185. }
  186. }
  187. })
  188. }
  189. photo.setTitle = function(photoIDs) {
  190. let oldTitle = ''
  191. let msg = ''
  192. if (!photoIDs) return false
  193. if (photoIDs instanceof Array===false) photoIDs = [ photoIDs ]
  194. if (photoIDs.length===1) {
  195. // Get old title if only one photo is selected
  196. if (photo.json) oldTitle = photo.json.title
  197. else if (album.json) oldTitle = album.json.content[photoIDs].title
  198. }
  199. const action = function(data) {
  200. basicModal.close()
  201. let newTitle = data.title
  202. if (visible.photo()) {
  203. photo.json.title = (newTitle==='' ? 'Untitled' : newTitle)
  204. view.photo.title()
  205. }
  206. photoIDs.forEach(function(id, index, array) {
  207. album.json.content[id].title = newTitle
  208. view.album.content.title(id)
  209. })
  210. let params = {
  211. photoIDs : photoIDs.join(),
  212. title : newTitle
  213. }
  214. api.post('Photo::setTitle', params, function(data) {
  215. if (data!==true) lychee.error(null, params, data)
  216. })
  217. }
  218. let input = lychee.html`<input class='text' name='title' type='text' maxlength='50' placeholder='Title' value='$${ oldTitle }'>`
  219. if (photoIDs.length===1) msg = lychee.html`<p>Enter a new title for this photo: ${ input }</p>`
  220. else msg = lychee.html`<p>Enter a title for all $${ photoIDs.length } selected photos: ${ input }</p>`
  221. basicModal.show({
  222. body: msg,
  223. buttons: {
  224. action: {
  225. title: 'Set title',
  226. fn: action
  227. },
  228. cancel: {
  229. title: 'Cancel',
  230. fn: basicModal.close
  231. }
  232. }
  233. })
  234. }
  235. photo.setAlbum = function(photoIDs, albumID) {
  236. let nextPhoto = null
  237. let previousPhoto = null
  238. if (!photoIDs) return false
  239. if (photoIDs instanceof Array===false) photoIDs = [ photoIDs ]
  240. photoIDs.forEach(function(id, index, array) {
  241. // Change reference for the next and previous photo
  242. if (album.json.content[id].nextPhoto!==''||album.json.content[id].previousPhoto!=='') {
  243. nextPhoto = album.json.content[id].nextPhoto
  244. previousPhoto = album.json.content[id].previousPhoto
  245. album.json.content[previousPhoto].nextPhoto = nextPhoto
  246. album.json.content[nextPhoto].previousPhoto = previousPhoto
  247. }
  248. delete album.json.content[id]
  249. view.album.content.delete(id)
  250. })
  251. albums.refresh()
  252. // Go to next photo if there is a next photo and
  253. // next photo is not the current one. Show album otherwise.
  254. if (visible.photo() && nextPhoto!=null && nextPhoto!==photo.getID()) lychee.goto(album.getID() + '/' + nextPhoto)
  255. else if (!visible.albums()) lychee.goto(album.getID())
  256. let params = {
  257. photoIDs: photoIDs.join(),
  258. albumID
  259. }
  260. api.post('Photo::setAlbum', params, function(data) {
  261. if (data!==true) lychee.error(null, params, data)
  262. })
  263. }
  264. photo.setStar = function(photoIDs) {
  265. if (!photoIDs) return false
  266. if (visible.photo()) {
  267. photo.json.star = (photo.json.star==='0' ? '1' : '0')
  268. view.photo.star()
  269. }
  270. photoIDs.forEach(function(id, index, array) {
  271. album.json.content[id].star = (album.json.content[id].star==='0' ? '1' : '0')
  272. view.album.content.star(id)
  273. })
  274. albums.refresh()
  275. let params = {
  276. photoIDs: photoIDs.join()
  277. }
  278. api.post('Photo::setStar', params, function(data) {
  279. if (data!==true) lychee.error(null, params, data)
  280. })
  281. }
  282. photo.setPublic = function(photoID, e) {
  283. if (photo.json.public==='2') {
  284. const action = function() {
  285. basicModal.close()
  286. lychee.goto(photo.json.original_album)
  287. }
  288. basicModal.show({
  289. body: '<p>This photo is located in a public album. To make this photo private or public, edit the visibility of the associated album.</p>',
  290. buttons: {
  291. action: {
  292. title: 'Show Album',
  293. fn: action
  294. },
  295. cancel: {
  296. title: 'Cancel',
  297. fn: basicModal.close
  298. }
  299. }
  300. })
  301. return false
  302. }
  303. if (visible.photo()) {
  304. photo.json.public = (photo.json.public==='0' ? '1' : '0')
  305. view.photo.public()
  306. if (photo.json.public==='1') contextMenu.sharePhoto(photoID, e)
  307. }
  308. album.json.content[photoID].public = (album.json.content[photoID].public==='0' ? '1' : '0')
  309. view.album.content.public(photoID)
  310. albums.refresh()
  311. api.post('Photo::setPublic', { photoID }, function(data) {
  312. if (data!==true) lychee.error(null, params, data)
  313. })
  314. }
  315. photo.setDescription = function(photoID) {
  316. let oldDescription = photo.json.description
  317. const action = function(data) {
  318. basicModal.close()
  319. let description = data.description
  320. if (visible.photo()) {
  321. photo.json.description = description
  322. view.photo.description()
  323. }
  324. let params = {
  325. photoID,
  326. description
  327. }
  328. api.post('Photo::setDescription', params, function(data) {
  329. if (data!==true) lychee.error(null, params, data)
  330. })
  331. }
  332. basicModal.show({
  333. body: lychee.html`<p>Enter a description for this photo: <input class='text' name='description' type='text' maxlength='800' placeholder='Description' value='$${ oldDescription }'></p>`,
  334. buttons: {
  335. action: {
  336. title: 'Set Description',
  337. fn: action
  338. },
  339. cancel: {
  340. title: 'Cancel',
  341. fn: basicModal.close
  342. }
  343. }
  344. })
  345. }
  346. photo.editTags = function(photoIDs) {
  347. let oldTags = ''
  348. let msg = ''
  349. if (!photoIDs) return false
  350. if (photoIDs instanceof Array===false) photoIDs = [ photoIDs ]
  351. // Get tags
  352. if (visible.photo()) oldTags = photo.json.tags
  353. else if (visible.album() && photoIDs.length===1) oldTags = album.json.content[photoIDs].tags
  354. else if (visible.search() && photoIDs.length===1) oldTags = album.json.content[photoIDs].tags
  355. else if (visible.album() && photoIDs.length>1) {
  356. let same = true
  357. photoIDs.forEach(function(id, index, array) {
  358. if (album.json.content[id].tags===album.json.content[photoIDs[0]].tags && same===true) same = true
  359. else same = false
  360. })
  361. if (same===true) oldTags = album.json.content[photoIDs[0]].tags
  362. }
  363. // Improve tags
  364. oldTags = oldTags.replace(/,/g, ', ')
  365. const action = function(data) {
  366. basicModal.close()
  367. photo.setTags(photoIDs, data.tags)
  368. }
  369. let input = lychee.html`<input class='text' name='tags' type='text' maxlength='800' placeholder='Tags' value='$${ oldTags }'>`
  370. if (photoIDs.length===1) msg = lychee.html`<p>Enter your tags for this photo. You can add multiple tags by separating them with a comma: ${ input }</p>`
  371. else msg = lychee.html`<p>Enter your tags for all $${ photoIDs.length } selected photos. Existing tags will be overwritten. You can add multiple tags by separating them with a comma: ${ input }</p>`
  372. basicModal.show({
  373. body: msg,
  374. buttons: {
  375. action: {
  376. title: 'Set Tags',
  377. fn: action
  378. },
  379. cancel: {
  380. title: 'Cancel',
  381. fn: basicModal.close
  382. }
  383. }
  384. })
  385. }
  386. photo.setTags = function(photoIDs, tags) {
  387. if (!photoIDs) return false
  388. if (photoIDs instanceof Array===false) photoIDs = [ photoIDs ]
  389. // Parse tags
  390. tags = tags.replace(/(\ ,\ )|(\ ,)|(,\ )|(,{1,}\ {0,})|(,$|^,)/g, ',')
  391. tags = tags.replace(/,$|^,|(\ ){0,}$/g, '')
  392. if (visible.photo()) {
  393. photo.json.tags = tags
  394. view.photo.tags()
  395. }
  396. photoIDs.forEach(function(id, index, array) {
  397. album.json.content[id].tags = tags
  398. })
  399. let params = {
  400. photoIDs: photoIDs.join(),
  401. tags
  402. }
  403. api.post('Photo::setTags', params, function(data) {
  404. if (data!==true) lychee.error(null, params, data)
  405. })
  406. }
  407. photo.deleteTag = function(photoID, index) {
  408. let tags
  409. // Remove
  410. tags = photo.json.tags.split(',')
  411. tags.splice(index, 1)
  412. // Save
  413. photo.json.tags = tags.toString()
  414. photo.setTags([ photoID ], photo.json.tags)
  415. }
  416. photo.share = function(photoID, service) {
  417. let url = photo.getViewLink(photoID)
  418. switch (service) {
  419. case 'twitter':
  420. window.open(`https://twitter.com/share?url=${ encodeURI(url) }`)
  421. break
  422. case 'facebook':
  423. window.open(`http://www.facebook.com/sharer.php?u=${ encodeURI(url) }&t=${ encodeURI(photo.json.title) }`)
  424. break
  425. case 'mail':
  426. location.href = `mailto:?subject=${ encodeURI(photo.json.title) }&body=${ encodeURI(url) }`
  427. break
  428. case 'dropbox':
  429. lychee.loadDropbox(function() {
  430. let filename = photo.json.title + '.' + photo.getDirectLink().split('.').pop()
  431. Dropbox.save(photo.getDirectLink(), filename)
  432. })
  433. break
  434. }
  435. }
  436. photo.getArchive = function(photoID) {
  437. let link
  438. let url = `${ api.path }?function=Photo::getArchive&photoID=${ photoID }`
  439. if (location.href.indexOf('index.html')>0) link = location.href.replace(location.hash, '').replace('index.html', url)
  440. else link = location.href.replace(location.hash, '') + url
  441. if (lychee.publicMode===true) link += `&password=${ encodeURIComponent(password.value) }`
  442. location.href = link
  443. }
  444. photo.getDirectLink = function() {
  445. let url = ''
  446. if (photo.json && photo.json.url && photo.json.url!=='') url = photo.json.url
  447. return url
  448. }
  449. photo.getViewLink = function(photoID) {
  450. let url = 'view.php?p=' + photoID
  451. if (location.href.indexOf('index.html')>0) return location.href.replace('index.html' + location.hash, url)
  452. else return location.href.replace(location.hash, url)
  453. }