MarseyWorld/files/assets/js/expanded_image_modal.js

68 lines
1.9 KiB
JavaScript
Raw Normal View History

2024-02-18 20:39:01 +00:00
let all_images
let position
let last_img_index
const imgnav_next = document.getElementById('imgnav-next')
const imgnav_prev = document.getElementById('imgnav-prev')
2024-02-19 00:55:31 +00:00
const expandImageModal = document.getElementById('expandImageModal')
2024-02-18 20:39:01 +00:00
document.addEventListener('keydown', (e) => {
if (['ArrowRight', 'd'].includes(e.key) && imgnav_next && !imgnav_next.classList.contains('d-none')) {
imgnav_next.click()
}
else if (['ArrowLeft', 'a'].includes(e.key) && imgnav_prev && !imgnav_prev.classList.contains('d-none')) {
imgnav_prev.click()
}
})
2024-02-19 00:55:31 +00:00
expandImageModal.addEventListener('hide.bs.modal', () => {
imgnav_next.classList.add('d-none')
imgnav_prev.classList.add('d-none')
});
2024-02-18 20:39:01 +00:00
function handle_navigation(delta) {
position += delta
if (position < last_img_index) {
imgnav_next.classList.remove('d-none')
imgnav_next.dataset.href = all_images[position+1].dataset.src
2024-02-18 20:39:01 +00:00
}
if (position > 0) {
imgnav_prev.classList.remove('d-none')
imgnav_prev.dataset.href = all_images[position-1].dataset.src
2024-02-18 20:39:01 +00:00
}
}
function expandImage(url) {
document.getElementById('imgnav-next').classList.add('d-none')
document.getElementById('imgnav-prev').classList.add('d-none')
const e = this.event
if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey)
return;
e.preventDefault();
document.getElementById("expanded-image").src = '';
document.getElementById("expanded-image-wrap-link").href = '';
if (!url)
{
url = e.target.dataset.src
if (!url) url = e.target.src
}
document.getElementById("expanded-image").src = url.replace("200w.webp", "giphy.webp");
document.getElementById("expanded-image-wrap-link").href = url.replace("200w.webp", "giphy.webp");
bootstrap.Modal.getOrCreateInstance(expandImageModal).show();
};
imgnav_next.onclick = () => {
expandImage(imgnav_next.dataset.href)
2024-02-18 20:39:01 +00:00
handle_navigation(1)
}
imgnav_prev.onclick = () => {
expandImage(imgnav_prev.dataset.href)
2024-02-18 20:39:01 +00:00
handle_navigation(-1)
}