obrazki.js 2.7 KB

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