main.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * @description Used to view single photos with view.php
  3. * @copyright 2015 by Tobias Reich
  4. */
  5. let lychee = { content: $('#content') },
  6. loadingBar = { show() {}, hide() {} }
  7. imageview = $('#imageview')
  8. $(document).ready(function() {
  9. // Event Name
  10. let touchendSupport = (/Android|iPhone|iPad|iPod/i).test(navigator.userAgent || navigator.vendor || window.opera) && ('ontouchend' in document.documentElement),
  11. eventName = (touchendSupport===true ? 'touchend' : 'click')
  12. // Set API error handler
  13. api.onError = error
  14. // Infobox
  15. header.dom('#button_info').on(eventName, sidebar.toggle)
  16. // Direct Link
  17. header.dom('#button_direct').on(eventName, function() {
  18. let link = $('#imageview #image').css('background-image').replace(/"/g,'').replace(/url\(|\)$/ig, '')
  19. window.open(link, '_newtab')
  20. })
  21. loadPhotoInfo(gup('p'))
  22. })
  23. const getPhotoSize = function(photo) {
  24. // Size can be 'big', 'medium' or 'small'
  25. // Default is big
  26. // Small is centered in the middle of the screen
  27. let size = 'big',
  28. scaled = false,
  29. hasMedium = photo.medium!=='',
  30. pixelRatio = window.devicePixelRatio,
  31. view = {
  32. width: $(window).width() - 60,
  33. height: $(window).height() - 100
  34. }
  35. // Detect if the photo will be shown scaled,
  36. // because the screen size is smaller than the photo
  37. if (photo.json.width>view.width || photo.json.height>view.height) scaled = true
  38. // Calculate pixel ratio of screen
  39. if (pixelRatio!==undefined && pixelRatio>1) {
  40. view.width = view.width * pixelRatio
  41. view.height = view.height * pixelRatio
  42. }
  43. // Medium available and
  44. // Medium still bigger than screen
  45. if (hasMedium===true && (1920>view.width && 1080>view.height)) size = 'medium'
  46. // Photo not scaled
  47. // Photo smaller then screen
  48. if (scaled===false && (photo.json.width<view.width&& photo.json.width<view.height)) size = 'small'
  49. return size
  50. }
  51. const loadPhotoInfo = function(photoID) {
  52. let params = {
  53. photoID,
  54. albumID: 0,
  55. password: ''
  56. }
  57. api.post('Photo::get', params, function(data) {
  58. if (data==='Warning: Photo private!' || data==='Warning: Wrong password!') {
  59. $('body').append(build.no_content('question-mark'))
  60. $('body').removeClass('view')
  61. header.dom().remove()
  62. return false
  63. }
  64. // Set title
  65. if (!data.title) data.title = 'Untitled'
  66. document.title = 'Lychee - ' + data.title
  67. header.dom('#title').html(data.title)
  68. let size = getPhotoSize(data)
  69. // Render HTML
  70. imageview.html(build.imageview(data, size, true))
  71. imageview.find('.arrow_wrapper').remove()
  72. imageview.addClass('fadeIn').show()
  73. // Render Sidebar
  74. let structure = sidebar.createStructure.photo(data),
  75. html = sidebar.render(structure)
  76. sidebar.dom('.wrapper').html(html)
  77. sidebar.bind()
  78. })
  79. }
  80. const error = function(errorThrown, params, data) {
  81. console.error({
  82. description : errorThrown,
  83. params : params,
  84. response : data
  85. })
  86. loadingBar.show('error', errorThrown)
  87. }