image.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.fadeTo('fast', 0.2, function () {
  32. image.css('opacity', '0.2');
  33. image.attr('src', imageUrlPrefix + dirname + '/' + filename);
  34. image.on('load', function() {
  35. // image.fadeTo('fast', 1);
  36. image.css('opacity', '1');
  37. });
  38. if (doPush)
  39. window.history.pushState(currId, '', scriptUrlPrefix + 'image.php?dir=' + dirname + '&file=' + filename);
  40. // });
  41. }
  42. function nextImage(e) {
  43. if (e)
  44. e.preventDefault();
  45. if (currId >= imglist.length - 1)
  46. return;
  47. currId++;
  48. setLinks();
  49. }
  50. function prevImage(e) {
  51. if (e)
  52. e.preventDefault();
  53. if (currId == 0)
  54. return;
  55. currId--;
  56. setLinks();
  57. }
  58. function imglistReceived(data) {
  59. imglist = data;
  60. /* find current id by url */
  61. var found = false;
  62. for (var i=0; i<data.length; i++) {
  63. file = data[i];
  64. if(file.name == filename) {
  65. currId = file.id;
  66. found = true;
  67. break;
  68. }
  69. }
  70. if (!found) /* don't know where i am */
  71. return;
  72. $('#poprz').click(prevImage);
  73. $('#imagelink').click(nextImage);
  74. $('#nast').click(nextImage);
  75. window.onpopstate = setLinks;
  76. window.history.replaceState(currId, '', scriptUrlPrefix + 'image.php?dir=' + dirname + '&file=' + filename);
  77. }
  78. function getImgList() {
  79. var queryString = window.location.search;
  80. if (URLSearchParams === undefined)
  81. return; /* browser not supported */
  82. var urlParams = new URLSearchParams(queryString);
  83. dirname = urlParams.get('dir');
  84. filename = urlParams.get('file');
  85. if (dirname === null || filename === null) /* invalid params */
  86. return;
  87. var args = 'dir=' + dirname;
  88. $.ajax({
  89. dataType: 'json',
  90. url: scriptUrlPrefix+'dirlist.php?'+args,
  91. success: imglistReceived
  92. });
  93. }
  94. $(document).keydown(function(e) {
  95. switch (e.which) {
  96. case 37:
  97. prevImage();
  98. return false;
  99. case 39:
  100. nextImage();
  101. return false;
  102. }
  103. });
  104. $(document).ready(function() {
  105. getImgList();
  106. $('body').on('swipeleft', nextImage);
  107. $('body').on('swiperight', prevImage);
  108. });