image.js 2.8 KB

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