imageviewer.js 9.8 KB
Newer Older
A
AUTOMATIC 已提交
1
// A full size 'lightbox' preview modal shown when left clicking on gallery previews
D
DepFA 已提交
2
function closeModal() {
3
    gradioApp().getElementById("lightboxModal").style.display = "none";
D
DepFA 已提交
4 5
}

6
function showModal(event) {
7 8 9 10 11 12 13 14 15
    const source = event.target || event.srcElement;
    const modalImage = gradioApp().getElementById("modalImage")
    const lb = gradioApp().getElementById("lightboxModal")
    modalImage.src = source.src
    if (modalImage.style.display === 'none') {
        lb.style.setProperty('background-image', 'url(' + source.src + ')');
    }
    lb.style.display = "block";
    lb.focus()
16 17 18 19 20 21 22 23 24

    const tabTxt2Img = gradioApp().getElementById("tab_txt2img")
    const tabImg2Img = gradioApp().getElementById("tab_img2img")
    // show the save button in modal only on txt2img or img2img tabs
    if (tabTxt2Img.style.display != "none" || tabImg2Img.style.display != "none") {
        gradioApp().getElementById("modal_save").style.display = "inline"
    } else {
        gradioApp().getElementById("modal_save").style.display = "none"
    }
25
    event.stopPropagation()
D
DepFA 已提交
26 27
}

28
function negmod(n, m) {
29
    return ((n % m) + m) % m;
30 31
}

32 33 34 35 36 37 38 39 40 41 42
function updateOnBackgroundChange() {
    const modalImage = gradioApp().getElementById("modalImage")
    if (modalImage && modalImage.offsetParent) {
        let allcurrentButtons = gradioApp().querySelectorAll(".gallery-item.transition-all.\\!ring-2")
        let currentButton = null
        allcurrentButtons.forEach(function(elem) {
            if (elem.parentElement.offsetParent) {
                currentButton = elem;
            }
        })

43
        if (currentButton?.children?.length > 0 && modalImage.src != currentButton.children[0].src) {
44 45 46 47 48
            modalImage.src = currentButton.children[0].src;
            if (modalImage.style.display === 'none') {
                modal.style.setProperty('background-image', `url(${modalImage.src})`)
            }
        }
49
    }
50 51 52 53 54 55 56 57
}

function modalImageSwitch(offset) {
    var allgalleryButtons = gradioApp().querySelectorAll(".gallery-item.transition-all")
    var galleryButtons = []
    allgalleryButtons.forEach(function(elem) {
        if (elem.parentElement.offsetParent) {
            galleryButtons.push(elem);
58
        }
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    })

    if (galleryButtons.length > 1) {
        var allcurrentButtons = gradioApp().querySelectorAll(".gallery-item.transition-all.\\!ring-2")
        var currentButton = null
        allcurrentButtons.forEach(function(elem) {
            if (elem.parentElement.offsetParent) {
                currentButton = elem;
            }
        })

        var result = -1
        galleryButtons.forEach(function(v, i) {
            if (v == currentButton) {
                result = i
            }
        })

        if (result != -1) {
            nextButton = galleryButtons[negmod((result + offset), galleryButtons.length)]
            nextButton.click()
            const modalImage = gradioApp().getElementById("modalImage");
            const modal = gradioApp().getElementById("lightboxModal");
            modalImage.src = nextButton.children[0].src;
            if (modalImage.style.display === 'none') {
                modal.style.setProperty('background-image', `url(${modalImage.src})`)
            }
            setTimeout(function() {
                modal.focus()
            }, 10)
89
        }
90
    }
91
}
D
DepFA 已提交
92

93 94 95 96 97
function saveImage(){
    const tabTxt2Img = gradioApp().getElementById("tab_txt2img")
    const tabImg2Img = gradioApp().getElementById("tab_img2img")
    const saveTxt2Img = "save_txt2img"
    const saveImg2Img = "save_img2img"
98
    if (tabTxt2Img.style.display != "none") {
99
        gradioApp().getElementById(saveTxt2Img).click()
100
    } else if (tabImg2Img.style.display != "none") {
101 102 103 104 105 106 107 108 109 110 111
        gradioApp().getElementById(saveImg2Img).click()
    } else {
        console.error("missing implementation for saving modal of this type")
    }
}

function modalSaveImage(event) {
    saveImage()
    event.stopPropagation()
}

112 113 114
function modalNextImage(event) {
    modalImageSwitch(1)
    event.stopPropagation()
D
DepFA 已提交
115 116
}

117 118 119
function modalPrevImage(event) {
    modalImageSwitch(-1)
    event.stopPropagation()
120 121
}

122
function modalKeyHandler(event) {
123
    switch (event.key) {
124 125 126
        case "s":
            saveImage()
            break;
127 128 129 130 131 132
        case "ArrowLeft":
            modalPrevImage(event)
            break;
        case "ArrowRight":
            modalNextImage(event)
            break;
A
Adam Snodgrass 已提交
133 134 135
        case "Escape":
            closeModal();
            break;
136 137 138
    }
}

139
function showGalleryImage() {
D
typo  
DepFA 已提交
140 141
    setTimeout(function() {
        fullImg_preview = gradioApp().querySelectorAll('img.w-full.object-contain')
142 143

        if (fullImg_preview != null) {
D
typo  
DepFA 已提交
144
            fullImg_preview.forEach(function function_name(e) {
G
guaneec 已提交
145 146 147
                if (e.dataset.modded)
                    return;
                e.dataset.modded = true;
D
typo  
DepFA 已提交
148 149
                if(e && e.parentElement.tagName == 'DIV'){
                    e.style.cursor='pointer'
150
                    e.style.userSelect='none'
151 152 153 154 155 156 157 158 159

                    var isFirefox = isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1

                    // For Firefox, listening on click first switched to next image then shows the lightbox.
                    // If you know how to fix this without switching to mousedown event, please.
                    // For other browsers the event is click to make it possiblr to drag picture.
                    var event = isFirefox ? 'mousedown' : 'click'

                    e.addEventListener(event, function (evt) {
160
                        if(!opts.js_modal_lightbox || evt.button != 0) return;
A
Aidan Holland 已提交
161
                        modalZoomSet(gradioApp().getElementById('modalImage'), opts.js_modal_lightbox_initially_zoomed)
162
                        evt.preventDefault()
163
                        showModal(evt)
164
                    }, true);
D
typo  
DepFA 已提交
165 166 167 168 169 170 171
                }
            });
        }

    }, 100);
}

172 173
function modalZoomSet(modalImage, enable) {
    if (enable) {
D
DepFA 已提交
174
        modalImage.classList.add('modalImageFullscreen');
175
    } else {
D
DepFA 已提交
176 177
        modalImage.classList.remove('modalImageFullscreen');
    }
178 179
}

180
function modalZoomToggle(event) {
181 182
    modalImage = gradioApp().getElementById("modalImage");
    modalZoomSet(modalImage, !modalImage.classList.contains('modalImageFullscreen'))
D
DepFA 已提交
183 184 185
    event.stopPropagation()
}

186
function modalTileImageToggle(event) {
187 188 189 190 191 192 193 194 195 196 197 198 199 200
    const modalImage = gradioApp().getElementById("modalImage");
    const modal = gradioApp().getElementById("lightboxModal");
    const isTiling = modalImage.style.display === 'none';
    if (isTiling) {
        modalImage.style.display = 'block';
        modal.style.setProperty('background-image', 'none')
    } else {
        modalImage.style.display = 'none';
        modal.style.setProperty('background-image', `url(${modalImage.src})`)
    }

    event.stopPropagation()
}

201 202
function galleryImageHandler(e) {
    if (e && e.parentElement.tagName == 'BUTTON') {
D
typo  
DepFA 已提交
203 204 205 206
        e.onclick = showGalleryImage;
    }
}

207
onUiUpdate(function() {
D
DepFA 已提交
208
    fullImg_preview = gradioApp().querySelectorAll('img.w-full')
209 210
    if (fullImg_preview != null) {
        fullImg_preview.forEach(galleryImageHandler);
D
DepFA 已提交
211
    }
212
    updateOnBackgroundChange();
A
AUTOMATIC 已提交
213
})
214 215

document.addEventListener("DOMContentLoaded", function() {
D
DepFA 已提交
216 217 218
    const modalFragment = document.createDocumentFragment();
    const modal = document.createElement('div')
    modal.onclick = closeModal;
D
DepFA 已提交
219
    modal.id = "lightboxModal";
220
    modal.tabIndex = 0
D
DepFA 已提交
221
    modal.addEventListener('keydown', modalKeyHandler, true)
222

U
unknown 已提交
223 224 225 226
    // detect gamepads and enable related functionality
    let gamepadScript = document.createElement('script');
    gamepadScript.src = 'imageviewerGamepad.js';
    document.body.appendChild(gamepadScript);
D
DepFA 已提交
227

228 229 230
    const modalControls = document.createElement('div')
    modalControls.className = 'modalControls gradio-container';
    modal.append(modalControls);
231

D
DepFA 已提交
232 233 234 235
    const modalZoom = document.createElement('span')
    modalZoom.className = 'modalZoom cursor';
    modalZoom.innerHTML = '⤡'
    modalZoom.addEventListener('click', modalZoomToggle, true)
236
    modalZoom.title = "Toggle zoomed view";
237 238
    modalControls.appendChild(modalZoom)

239 240 241
    const modalTileImage = document.createElement('span')
    modalTileImage.className = 'modalTileImage cursor';
    modalTileImage.innerHTML = '⊞'
242
    modalTileImage.addEventListener('click', modalTileImageToggle, true)
243
    modalTileImage.title = "Preview tiling";
244 245
    modalControls.appendChild(modalTileImage)

246 247
    const modalSave = document.createElement("span")
    modalSave.className = "modalSave cursor"
248
    modalSave.id = "modal_save"
249 250 251 252 253
    modalSave.innerHTML = "🖫"
    modalSave.addEventListener("click", modalSaveImage, true)
    modalSave.title = "Save Image(s)"
    modalControls.appendChild(modalSave)

254 255 256 257
    const modalClose = document.createElement('span')
    modalClose.className = 'modalClose cursor';
    modalClose.innerHTML = '×'
    modalClose.onclick = closeModal;
258
    modalClose.title = "Close image viewer";
259
    modalControls.appendChild(modalClose)
D
DepFA 已提交
260

D
DepFA 已提交
261 262 263
    const modalImage = document.createElement('img')
    modalImage.id = 'modalImage';
    modalImage.onclick = closeModal;
264
    modalImage.tabIndex = 0
265
    modalImage.addEventListener('keydown', modalKeyHandler, true)
D
DepFA 已提交
266 267
    modal.appendChild(modalImage)

268 269 270
    const modalPrev = document.createElement('a')
    modalPrev.className = 'modalPrev';
    modalPrev.innerHTML = '❮'
271 272
    modalPrev.tabIndex = 0
    modalPrev.addEventListener('click', modalPrevImage, true);
273 274 275 276 277 278
    modalPrev.addEventListener('keydown', modalKeyHandler, true)
    modal.appendChild(modalPrev)

    const modalNext = document.createElement('a')
    modalNext.className = 'modalNext';
    modalNext.innerHTML = '❯'
279 280
    modalNext.tabIndex = 0
    modalNext.addEventListener('click', modalNextImage, true);
281 282 283 284 285
    modalNext.addEventListener('keydown', modalKeyHandler, true)

    modal.appendChild(modalNext)


D
DepFA 已提交
286
    gradioApp().getRootNode().appendChild(modal)
287

D
DepFA 已提交
288
    document.body.appendChild(modalFragment);
289

290
});