image.js 2.7 KB

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