main.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * @description Used to view single photos with view.php
  3. * @copyright 2015 by Tobias Reich
  4. */
  5. // Sub-implementation of Lychee -------------------------------------------------------------- //
  6. let lychee = {}
  7. lychee.content = $('#content')
  8. lychee.getEventName = function() {
  9. let touchendSupport = (/Android|iPhone|iPad|iPod/i).test(navigator.userAgent || navigator.vendor || window.opera) && ('ontouchend' in document.documentElement),
  10. eventName = (touchendSupport===true ? 'touchend' : 'click')
  11. return eventName
  12. }
  13. lychee.escapeHTML = function(html = '') {
  14. // Ensure that html is a string
  15. html += ''
  16. // Escape all critical characters
  17. html = html.replace(/&/g, '&')
  18. .replace(/</g, '&lt;')
  19. .replace(/>/g, '&gt;')
  20. .replace(/"/g, '&quot;')
  21. .replace(/'/g, '&#039;')
  22. .replace(/`/g, '&#96;')
  23. return html
  24. }
  25. lychee.html = function(literalSections, ...substs) {
  26. // Use raw literal sections: we don’t want
  27. // backslashes (\n etc.) to be interpreted
  28. let raw = literalSections.raw,
  29. result = ''
  30. substs.forEach((subst, i) => {
  31. // Retrieve the literal section preceding
  32. // the current substitution
  33. let lit = raw[i]
  34. // If the substitution is preceded by a dollar sign,
  35. // we escape special characters in it
  36. if (lit.slice(-1)==='$') {
  37. subst = lychee.escapeHTML(subst)
  38. lit = lit.slice(0, -1)
  39. }
  40. result += lit
  41. result += subst
  42. })
  43. // Take care of last literal section
  44. // (Never fails, because an empty template string
  45. // produces one literal section, an empty string)
  46. result += raw[raw.length-1]
  47. return result
  48. }
  49. // Main -------------------------------------------------------------- //
  50. let loadingBar = { show() {}, hide() {} },
  51. imageview = $('#imageview')
  52. $(document).ready(function() {
  53. // Event Name
  54. let eventName = lychee.getEventName()
  55. // Set API error handler
  56. api.onError = error
  57. // Infobox
  58. header.dom('#button_info').on(eventName, sidebar.toggle)
  59. // Direct Link
  60. header.dom('#button_direct').on(eventName, function() {
  61. let link = $('#imageview #image').css('background-image').replace(/"/g,'').replace(/url\(|\)$/ig, '')
  62. window.open(link, '_newtab')
  63. })
  64. loadPhotoInfo(gup('p'))
  65. })
  66. const getPhotoSize = function(photo) {
  67. // Size can be 'big', 'medium' or 'small'
  68. // Default is big
  69. // Small is centered in the middle of the screen
  70. let size = 'big',
  71. scaled = false,
  72. hasMedium = photo.medium!=='',
  73. pixelRatio = window.devicePixelRatio,
  74. view = {
  75. width : $(window).width() - 60,
  76. height : $(window).height() - 100
  77. }
  78. // Detect if the photo will be shown scaled,
  79. // because the screen size is smaller than the photo
  80. if (photo.width>view.width || photo.height>view.height) scaled = true
  81. // Calculate pixel ratio of screen
  82. if (pixelRatio!=null && pixelRatio>1) {
  83. view.width = view.width * pixelRatio
  84. view.height = view.height * pixelRatio
  85. }
  86. // Medium available and
  87. // Medium still bigger than screen
  88. if (hasMedium===true && (1920>view.width && 1080>view.height)) size = 'medium'
  89. // Photo not scaled
  90. // Photo smaller then screen
  91. if (scaled===false && (photo.width<view.width&& photo.width<view.height)) size = 'small'
  92. return size
  93. }
  94. const loadPhotoInfo = function(photoID) {
  95. let params = {
  96. photoID,
  97. albumID : 0,
  98. password : ''
  99. }
  100. api.post('Photo::get', params, function(data) {
  101. if (data==='Warning: Photo private!' || data==='Warning: Wrong password!') {
  102. $('body').append(build.no_content('question-mark'))
  103. $('body').removeClass('view')
  104. header.dom().remove()
  105. return false
  106. }
  107. // Set title
  108. if (!data.title) data.title = 'Untitled'
  109. document.title = 'Lychee - ' + data.title
  110. header.dom('#title').html(lychee.escapeHTML(data.title))
  111. let size = getPhotoSize(data)
  112. // Render HTML
  113. imageview.html(build.imageview(data, size, true))
  114. imageview.find('.arrow_wrapper').remove()
  115. imageview.addClass('fadeIn').show()
  116. // Render Sidebar
  117. let structure = sidebar.createStructure.photo(data),
  118. html = sidebar.render(structure)
  119. sidebar.dom('.wrapper').html(html)
  120. sidebar.bind()
  121. })
  122. }
  123. const error = function(errorThrown, params, data) {
  124. console.error({
  125. description : errorThrown,
  126. params : params,
  127. response : data
  128. })
  129. loadingBar.show('error', errorThrown)
  130. }