image.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var imglist = null;
  2. var dirname = null;
  3. var filename = null;
  4. var currId = 0;
  5. function setLinks(e) {
  6. var doPush = false;
  7. /* browser navigation handling */
  8. if (e === undefined || e.type != 'popstate') {
  9. doPush = true;
  10. } else {
  11. currId = e.state;
  12. }
  13. /* replace the content */
  14. filename = imglist[currId].name;
  15. if (currId > 0) {
  16. $('#poprz').attr('href', scriptUrlPrefix + 'image.php?dir=' + dirname + '&file=' + imglist[currId-1].name);
  17. $('#poprz').show();
  18. } else {
  19. $('#poprz').hide();
  20. }
  21. if (currId < imglist.length - 1) {
  22. var url = scriptUrlPrefix + 'image.php?dir=' + dirname + '&file=' + imglist[currId+1].name;
  23. $('#nast').attr('href', url);
  24. $('#imagelink').attr('href', url);
  25. $('#nast').show();
  26. } else {
  27. $('#nast').hide();
  28. $('#imagelink').removeAttr('href');
  29. }
  30. var image = $("#image");
  31. image.css('opacity', '0.2');
  32. image.attr('src', imageUrlPrefix + dirname + '/' + filename);
  33. image.on('load', function() {
  34. image.css('opacity', '1');
  35. });
  36. if (doPush)
  37. window.history.pushState(currId, '', scriptUrlPrefix + 'image.php?dir=' + dirname + '&file=' + filename);
  38. commentsImageReplace(imglist[currId]);
  39. refreshComments();
  40. }
  41. function nextImage(e) {
  42. if (e)
  43. e.preventDefault();
  44. if (currId >= imglist.length - 1)
  45. return;
  46. currId++;
  47. setLinks();
  48. }
  49. function prevImage(e) {
  50. if (e)
  51. e.preventDefault();
  52. if (currId == 0)
  53. return;
  54. currId--;
  55. setLinks();
  56. }
  57. function imglistReceived(data) {
  58. imglist = data;
  59. /* find current id by url */
  60. var found = false;
  61. for (var i=0; i<data.length; i++) {
  62. file = data[i];
  63. if(file.name == filename) {
  64. currId = file.ord;
  65. found = true;
  66. break;
  67. }
  68. }
  69. if (!found) /* don't know where i am */
  70. return;
  71. $('#poprz').click(prevImage);
  72. $('#imagelink').click(nextImage);
  73. $('#nast').click(nextImage);
  74. window.onpopstate = setLinks;
  75. window.history.replaceState(currId, '', scriptUrlPrefix + 'image.php?dir=' + dirname + '&file=' + filename);
  76. }
  77. function getImgList() {
  78. var queryString = window.location.search;
  79. if (URLSearchParams === undefined)
  80. return; /* browser not supported */
  81. var urlParams = new URLSearchParams(queryString);
  82. dirname = urlParams.get('dir');
  83. filename = urlParams.get('file');
  84. if (dirname === null || filename === null) /* invalid params */
  85. return;
  86. var args = 'dir=' + dirname;
  87. $.ajax({
  88. dataType: 'json',
  89. url: scriptUrlPrefix+'dirlist.php?'+args,
  90. success: imglistReceived
  91. });
  92. }
  93. $(document).keydown(function(e) {
  94. switch (e.which) {
  95. case 37:
  96. prevImage();
  97. return false;
  98. case 39:
  99. nextImage();
  100. return false;
  101. }
  102. });
  103. $(document).ready(function() {
  104. getImgList();
  105. $('body').on('swipeleft', nextImage);
  106. $('body').on('swiperight', prevImage);
  107. });