main.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @description Used to view single photos with view.php
  3. * @copyright 2015 by Tobias Reich
  4. */
  5. var lychee = { content: $('#content') },
  6. loadingBar = { show() {}, hide() {} }
  7. imageview = $('#imageview');
  8. $(document).ready(function() {
  9. // Event Name
  10. var 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. var link = $('#imageview #image').css('background-image').replace(/"/g,'').replace(/url\(|\)$/ig, '');
  19. window.open(link, '_newtab');
  20. });
  21. loadPhotoInfo(gup('p'));
  22. });
  23. 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. var 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.width>view.width||
  38. photo.width>view.height) scaled = true;
  39. // Calculate pixel ratio of screen
  40. if (pixelRatio!==undefined&&pixelRatio>1) {
  41. view.width = view.width * pixelRatio;
  42. view.height = view.height * pixelRatio;
  43. }
  44. // Medium available and
  45. // Medium still bigger than screen
  46. if (hasMedium===true&&
  47. (1920>view.width&&1080>view.height)) size = 'medium';
  48. // Photo not scaled
  49. // Photo smaller then screen
  50. if (scaled===false&&
  51. (photo.width<view.width&&
  52. photo.width<view.height)) size = 'small';
  53. return size;
  54. }
  55. loadPhotoInfo = function(photoID) {
  56. var params = {
  57. photoID,
  58. albumID: 0,
  59. password: ''
  60. }
  61. api.post('Photo::get', params, function(data) {
  62. if (data==='Warning: Photo private!'||
  63. data==='Warning: Wrong password!') {
  64. $('body').append(build.no_content('question-mark'));
  65. $('body').removeClass('view');
  66. header.dom().remove();
  67. return false;
  68. }
  69. /* Set title */
  70. if (!data.title) data.title = 'Untitled';
  71. document.title = 'Lychee - ' + data.title;
  72. header.dom('#title').html(data.title);
  73. /* Render HTML */
  74. var size = getPhotoSize(data);
  75. imageview.html(build.imageview(data, size, true));
  76. imageview.find('.arrow_wrapper').remove();
  77. imageview.addClass('fadeIn').show();
  78. /* Render Sidebar */
  79. var structure = sidebar.createStructure.photo(data),
  80. html = sidebar.render(structure);
  81. sidebar.dom('.wrapper').html(html);
  82. sidebar.bind();
  83. });
  84. }
  85. error = function(errorThrown, params, data) {
  86. console.error({
  87. description: errorThrown,
  88. params: params,
  89. response: data
  90. });
  91. loadingBar.show('error', errorThrown);
  92. }