imageviewer.js 4.0 KB
Newer Older
A
AUTOMATIC 已提交
1
// A full size 'lightbox' preview modal shown when left clicking on gallery previews
2

D
DepFA 已提交
3 4 5 6
function closeModal() {
  gradioApp().getElementById("lightboxModal").style.display = "none";
}

7 8 9
function showModal(event) {
  var source = event.target || event.srcElement;
  gradioApp().getElementById("modalImage").src = source.src
10 11 12
  var lb = gradioApp().getElementById("lightboxModal")
  lb.style.display = "block";
  lb.focus()
13
  event.stopPropagation()
D
DepFA 已提交
14 15
}

16 17 18 19 20 21
function negmod(n, m) {
  return ((n % m) + m) % m;
}

function modalImageSwitch(offset){
  var galleryButtons = gradioApp().querySelectorAll(".gallery-item.transition-all")
22

23 24
  if(galleryButtons.length>1){
      var currentButton  = gradioApp().querySelector(".gallery-item.transition-all.\\!ring-2")
25

26 27
      var result = -1
      galleryButtons.forEach(function(v, i){ if(v==currentButton) { result = i } })
D
DepFA 已提交
28

29 30 31 32 33 34 35 36
      if(result != -1){
        nextButton = galleryButtons[negmod((result+offset),galleryButtons.length)]
        nextButton.click()
        gradioApp().getElementById("modalImage").src = nextButton.children[0].src
        setTimeout( function(){gradioApp().getElementById("lightboxModal").focus()},10)
      }
  }
}
D
DepFA 已提交
37

38 39 40
function modalNextImage(event){
  modalImageSwitch(1)
  event.stopPropagation()
D
DepFA 已提交
41 42
}

43 44 45 46 47 48 49 50 51 52 53 54 55
function modalPrevImage(event){
  modalImageSwitch(-1)  
  event.stopPropagation()
}

function modalKeyHandler(event){
    switch (event.key) {
        case "ArrowLeft":
            modalPrevImage(event)
            break;
        case "ArrowRight":
            modalNextImage(event)
            break;
A
Adam Snodgrass 已提交
56 57 58
        case "Escape":
            closeModal();
            break;
59 60 61
    }
}

D
typo  
DepFA 已提交
62 63 64 65 66 67 68 69 70 71 72
function showGalleryImage(){
    setTimeout(function() {
        fullImg_preview = gradioApp().querySelectorAll('img.w-full.object-contain')
        
        if(fullImg_preview != null){
            fullImg_preview.forEach(function function_name(e) {
                if(e && e.parentElement.tagName == 'DIV'){

                    e.style.cursor='pointer'

                    e.addEventListener('click', function (evt) {
73 74
                        if(!opts.js_modal_lightbox) return;
                        showModal(evt)
D
typo  
DepFA 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88
                    },true);
                }
            });
        }

    }, 100);
}

function galleryImageHandler(e){
    if(e && e.parentElement.tagName == 'BUTTON'){
        e.onclick = showGalleryImage;
    }
}

A
AUTOMATIC 已提交
89
onUiUpdate(function(){
90 91 92 93
	fullImg_preview = gradioApp().querySelectorAll('img.w-full')
	    if(fullImg_preview != null){
		fullImg_preview.forEach(galleryImageHandler);
	}
A
AUTOMATIC 已提交
94
})
95 96

document.addEventListener("DOMContentLoaded", function() {
D
DepFA 已提交
97 98 99 100 101 102 103 104 105
    const modalFragment = document.createDocumentFragment();
    const modal = document.createElement('div')
    modal.onclick = closeModal;
    
    const modalClose = document.createElement('span')
    modalClose.className = 'modalClose cursor';
    modalClose.innerHTML = '×'
    modalClose.onclick = closeModal;
    modal.id = "lightboxModal";
106 107
    modal.tabIndex=0
    modal.addEventListener('keydown', modalKeyHandler, true)
D
DepFA 已提交
108 109 110 111 112
    modal.appendChild(modalClose)

    const modalImage = document.createElement('img')
    modalImage.id = 'modalImage';
    modalImage.onclick = closeModal;
113 114
    modalImage.tabIndex=0
    modalImage.addEventListener('keydown', modalKeyHandler, true)
D
DepFA 已提交
115 116
    modal.appendChild(modalImage)

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    const modalPrev = document.createElement('a')
    modalPrev.className = 'modalPrev';
    modalPrev.innerHTML = '❮'
    modalPrev.tabIndex=0
    modalPrev.addEventListener('click',modalPrevImage,true);
    modalPrev.addEventListener('keydown', modalKeyHandler, true)
    modal.appendChild(modalPrev)

    const modalNext = document.createElement('a')
    modalNext.className = 'modalNext';
    modalNext.innerHTML = '❯'
    modalNext.tabIndex=0
    modalNext.addEventListener('click',modalNextImage,true);
    modalNext.addEventListener('keydown', modalKeyHandler, true)

    modal.appendChild(modalNext)


D
DepFA 已提交
135 136 137 138
    gradioApp().getRootNode().appendChild(modal)
    
    document.body.appendChild(modalFragment);
	
139
});