main.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. let 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. let 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 loadPhotoInfo = function(photoID) {
  67. let params = {
  68. photoID,
  69. albumID : 0,
  70. password : ''
  71. }
  72. api.post('Photo::get', params, function(data) {
  73. if (data==='Warning: Photo private!' || data==='Warning: Wrong password!') {
  74. $('body').append(build.no_content('question-mark'))
  75. $('body').removeClass('view')
  76. header.dom().remove()
  77. return false
  78. }
  79. // Set title
  80. if (!data.title) data.title = 'Untitled'
  81. document.title = 'Lychee - ' + data.title
  82. header.dom('.header__title').html(lychee.escapeHTML(data.title))
  83. // Render HTML
  84. imageview.html(build.imageview(data, true))
  85. imageview.find('.arrow_wrapper').remove()
  86. imageview.addClass('fadeIn').show()
  87. // Render Sidebar
  88. let structure = sidebar.createStructure.photo(data)
  89. let html = sidebar.render(structure)
  90. sidebar.dom('.sidebar__wrapper').html(html)
  91. sidebar.bind()
  92. })
  93. }
  94. const error = function(errorThrown, params, data) {
  95. console.error({
  96. description : errorThrown,
  97. params : params,
  98. response : data
  99. })
  100. loadingBar.show('error', errorThrown)
  101. }