main.js 3.0 KB

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