main.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @description Used to view single photos with view.php
  3. * @copyright 2015 by Tobias Reich
  4. */
  5. var header = $('header'),
  6. headerTitle = $('#title'),
  7. imageview = $('#imageview'),
  8. api_path = 'php/api.php',
  9. infobox = $('#infobox');
  10. $(document).ready(function(){
  11. /* Event Name */
  12. if ('ontouchend' in document.documentElement) eventName = 'touchend';
  13. else eventName = 'click';
  14. /* Window */
  15. $(window).keydown(key);
  16. /* Infobox */
  17. infobox.find('.header .close').on(eventName, hideInfobox);
  18. $(document) .on(eventName, '#infobox_overlay', hideInfobox);
  19. $('#button_info') .on(eventName, showInfobox);
  20. /* Direct Link */
  21. $('#button_direct').on(eventName, function() {
  22. var link = $('#imageview #image').css('background-image').replace(/"/g,'').replace(/url\(|\)$/ig, '');
  23. window.open(link, '_newtab');
  24. });
  25. loadPhotoInfo(gup('p'));
  26. });
  27. key = function(e) {
  28. var code = (e.keyCode ? e.keyCode : e.which);
  29. if (code===27) {
  30. hideInfobox();
  31. e.preventDefault();
  32. }
  33. }
  34. getPhotoSize = function(photo) {
  35. // Size can be 'big', 'medium' or 'small'
  36. // Default is big
  37. // Small is centered in the middle of the screen
  38. var size = 'big',
  39. scaled = false,
  40. hasMedium = photo.medium!=='',
  41. pixelRatio = window.devicePixelRatio,
  42. view = {
  43. width: $(window).width()-60,
  44. height: $(window).height()-100
  45. };
  46. // Detect if the photo will be shown scaled,
  47. // because the screen size is smaller than the photo
  48. if (photo.width>view.width||
  49. photo.width>view.height) scaled = true;
  50. // Calculate pixel ratio of screen
  51. if (pixelRatio!==undefined&&pixelRatio>1) {
  52. view.width = view.width * pixelRatio;
  53. view.height = view.height * pixelRatio;
  54. }
  55. // Medium available and
  56. // Medium still bigger than screen
  57. if (hasMedium===true&&
  58. (1920>view.width&&1080>view.height)) size = 'medium';
  59. // Photo not scaled
  60. // Photo smaller then screen
  61. if (scaled===false&&
  62. (photo.width<view.width&&
  63. photo.width<view.height)) size = 'small';
  64. return size;
  65. }
  66. showInfobox = function() {
  67. $('body').append("<div id='infobox_overlay' class='fadeIn'></div>");
  68. infobox.addClass('active');
  69. }
  70. hideInfobox = function() {
  71. $('#infobox_overlay').removeClass('fadeIn').addClass('fadeOut');
  72. setTimeout(function() { $('#infobox_overlay').remove() }, 300);
  73. infobox.removeClass('active');
  74. }
  75. loadPhotoInfo = function(photoID) {
  76. var params = 'function=getPhoto&photoID=' + photoID + '&albumID=0&password=""';
  77. $.ajax({type: 'POST', url: api_path, data: params, dataType: 'json', success: function(data) {
  78. var size = getPhotoSize(data);
  79. if (!data.title) data.title = 'Untitled';
  80. document.title = 'Lychee - ' + data.title;
  81. headerTitle.html(data.title);
  82. imageview.attr('data-id', photoID);
  83. if (size==='big') imageview.html("<div id='image' style='background-image: url(" + data.url + ");'></div>");
  84. else if (size==='medium') imageview.html("<div id='image' style='background-image: url(" + data.medium + ");'></div>");
  85. else imageview.html("<div id='image' class='small' style='background-image: url(" + data.url + "); width: " + data.width + "px; height: " + data.height + "px; margin-top: -" + parseInt((data.height/2)-20) + "px; margin-left: -" + data.width/2 + "px;'></div>");
  86. imageview
  87. .removeClass('fadeOut')
  88. .addClass('fadeIn')
  89. .show();
  90. infobox.find('.wrapper').html(build.infoboxPhoto(data, true));
  91. }, error: ajaxError });
  92. }
  93. ajaxError = function(errorThrown, params, data) {
  94. console.error({
  95. description: errorThrown,
  96. params: params,
  97. response: data
  98. });
  99. }