imageviewer.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // A full size 'lightbox' preview modal shown when left clicking on gallery previews
  2. function closeModal() {
  3. gradioApp().getElementById("lightboxModal").style.display = "none";
  4. }
  5. function showModal(event) {
  6. var source = event.target || event.srcElement;
  7. gradioApp().getElementById("modalImage").src = source.src
  8. var lb = gradioApp().getElementById("lightboxModal")
  9. lb.style.display = "block";
  10. lb.focus()
  11. event.stopPropagation()
  12. }
  13. function negmod(n, m) {
  14. return ((n % m) + m) % m;
  15. }
  16. function modalImageSwitch(offset){
  17. var galleryButtons = gradioApp().querySelectorAll(".gallery-item.transition-all")
  18. if(galleryButtons.length>1){
  19. var currentButton = gradioApp().querySelector(".gallery-item.transition-all.\\!ring-2")
  20. var result = -1
  21. galleryButtons.forEach(function(v, i){ if(v==currentButton) { result = i } })
  22. if(result != -1){
  23. nextButton = galleryButtons[negmod((result+offset),galleryButtons.length)]
  24. nextButton.click()
  25. gradioApp().getElementById("modalImage").src = nextButton.children[0].src
  26. setTimeout( function(){gradioApp().getElementById("lightboxModal").focus()},10)
  27. }
  28. }
  29. }
  30. function modalNextImage(event){
  31. modalImageSwitch(1)
  32. event.stopPropagation()
  33. }
  34. function modalPrevImage(event){
  35. modalImageSwitch(-1)
  36. event.stopPropagation()
  37. }
  38. function modalKeyHandler(event){
  39. switch (event.key) {
  40. case "ArrowLeft":
  41. modalPrevImage(event)
  42. break;
  43. case "ArrowRight":
  44. modalNextImage(event)
  45. break;
  46. case "Escape":
  47. closeModal();
  48. break;
  49. }
  50. }
  51. function showGalleryImage(){
  52. setTimeout(function() {
  53. fullImg_preview = gradioApp().querySelectorAll('img.w-full.object-contain')
  54. if(fullImg_preview != null){
  55. fullImg_preview.forEach(function function_name(e) {
  56. if(e && e.parentElement.tagName == 'DIV'){
  57. e.style.cursor='pointer'
  58. e.addEventListener('click', function (evt) {
  59. if(!opts.js_modal_lightbox) return;
  60. showModal(evt)
  61. },true);
  62. }
  63. });
  64. }
  65. }, 100);
  66. }
  67. function galleryImageHandler(e){
  68. if(e && e.parentElement.tagName == 'BUTTON'){
  69. e.onclick = showGalleryImage;
  70. }
  71. }
  72. onUiUpdate(function(){
  73. fullImg_preview = gradioApp().querySelectorAll('img.w-full')
  74. if(fullImg_preview != null){
  75. fullImg_preview.forEach(galleryImageHandler);
  76. }
  77. })
  78. document.addEventListener("DOMContentLoaded", function() {
  79. const modalFragment = document.createDocumentFragment();
  80. const modal = document.createElement('div')
  81. modal.onclick = closeModal;
  82. const modalClose = document.createElement('span')
  83. modalClose.className = 'modalClose cursor';
  84. modalClose.innerHTML = '×'
  85. modalClose.onclick = closeModal;
  86. modal.id = "lightboxModal";
  87. modal.tabIndex=0
  88. modal.addEventListener('keydown', modalKeyHandler, true)
  89. modal.appendChild(modalClose)
  90. const modalImage = document.createElement('img')
  91. modalImage.id = 'modalImage';
  92. modalImage.onclick = closeModal;
  93. modalImage.tabIndex=0
  94. modalImage.addEventListener('keydown', modalKeyHandler, true)
  95. modal.appendChild(modalImage)
  96. const modalPrev = document.createElement('a')
  97. modalPrev.className = 'modalPrev';
  98. modalPrev.innerHTML = '❮'
  99. modalPrev.tabIndex=0
  100. modalPrev.addEventListener('click',modalPrevImage,true);
  101. modalPrev.addEventListener('keydown', modalKeyHandler, true)
  102. modal.appendChild(modalPrev)
  103. const modalNext = document.createElement('a')
  104. modalNext.className = 'modalNext';
  105. modalNext.innerHTML = '❯'
  106. modalNext.tabIndex=0
  107. modalNext.addEventListener('click',modalNextImage,true);
  108. modalNext.addEventListener('keydown', modalKeyHandler, true)
  109. modal.appendChild(modalNext)
  110. gradioApp().getRootNode().appendChild(modal)
  111. document.body.appendChild(modalFragment);
  112. });