2023-03-15 02:43:49 +01:00
|
|
|
"use strict"
|
|
|
|
|
2023-04-04 01:35:37 +02:00
|
|
|
/**
|
|
|
|
* @typedef {object} ImageModalRequest
|
|
|
|
* @property {string} src
|
|
|
|
* @property {ImageModalRequest | () => ImageModalRequest | undefined} previous
|
|
|
|
* @property {ImageModalRequest | () => ImageModalRequest | undefined} next
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {(() => (string | ImageModalRequest) | string | ImageModalRequest) => {}}
|
|
|
|
*/
|
2023-03-15 02:43:49 +01:00
|
|
|
const imageModal = (function() {
|
2023-04-27 19:56:56 +02:00
|
|
|
const backElem = createElement("i", undefined, ["fa-solid", "fa-arrow-left", "tertiaryButton"])
|
2023-04-04 01:35:37 +02:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const forwardElem = createElement("i", undefined, ["fa-solid", "fa-arrow-right", "tertiaryButton"])
|
2023-04-04 01:35:37 +02:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const zoomElem = createElement("i", undefined, ["fa-solid", "tertiaryButton"])
|
2023-03-15 02:43:49 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const closeElem = createElement("i", undefined, ["fa-solid", "fa-xmark", "tertiaryButton"])
|
2023-03-15 02:43:49 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const menuBarElem = createElement("div", undefined, "menu-bar", [backElem, forwardElem, zoomElem, closeElem])
|
2023-03-15 02:43:49 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const imageContainer = createElement("div", undefined, "image-wrapper")
|
2023-03-15 02:43:49 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const backdrop = createElement("div", undefined, "backdrop")
|
2023-03-15 02:43:49 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const modalContainer = createElement("div", undefined, "content", [menuBarElem, imageContainer])
|
2023-03-15 02:43:49 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const modalElem = createElement("div", { id: "viewFullSizeImgModal" }, ["popup"], [backdrop, modalContainer])
|
2023-03-15 02:43:49 +01:00
|
|
|
document.body.appendChild(modalElem)
|
|
|
|
|
|
|
|
const setZoomLevel = (value) => {
|
2023-04-27 19:56:56 +02:00
|
|
|
const img = imageContainer.querySelector("img")
|
2023-03-15 02:43:49 +01:00
|
|
|
|
|
|
|
if (value) {
|
2023-04-27 19:56:56 +02:00
|
|
|
zoomElem.classList.remove("fa-magnifying-glass-plus")
|
|
|
|
zoomElem.classList.add("fa-magnifying-glass-minus")
|
2023-03-15 02:43:49 +01:00
|
|
|
if (img) {
|
2023-04-27 19:56:56 +02:00
|
|
|
img.classList.remove("natural-zoom")
|
2023-03-15 02:43:49 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
let zoomLevel = typeof value === "number" ? value : img.dataset.zoomLevel
|
2023-03-15 02:43:49 +01:00
|
|
|
if (!zoomLevel) {
|
|
|
|
zoomLevel = 100
|
|
|
|
}
|
|
|
|
|
|
|
|
img.dataset.zoomLevel = zoomLevel
|
|
|
|
img.width = img.naturalWidth * (+zoomLevel / 100)
|
|
|
|
img.height = img.naturalHeight * (+zoomLevel / 100)
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-27 19:56:56 +02:00
|
|
|
zoomElem.classList.remove("fa-magnifying-glass-minus")
|
|
|
|
zoomElem.classList.add("fa-magnifying-glass-plus")
|
2023-03-15 02:43:49 +01:00
|
|
|
if (img) {
|
2023-04-27 19:56:56 +02:00
|
|
|
img.classList.add("natural-zoom")
|
|
|
|
img.removeAttribute("width")
|
|
|
|
img.removeAttribute("height")
|
2023-03-15 02:43:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
zoomElem.addEventListener("click", () =>
|
|
|
|
setZoomLevel(imageContainer.querySelector("img")?.classList?.contains("natural-zoom"))
|
2023-03-15 02:43:49 +01:00
|
|
|
)
|
|
|
|
|
2023-05-05 01:36:10 +02:00
|
|
|
const initialState = () => ({
|
2023-04-04 01:35:37 +02:00
|
|
|
previous: undefined,
|
2023-04-28 12:20:44 +02:00
|
|
|
next: undefined,
|
2023-05-05 01:36:10 +02:00
|
|
|
|
|
|
|
start: {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
},
|
|
|
|
|
|
|
|
scroll: {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const state = initialState()
|
|
|
|
|
|
|
|
// Allow grabbing the image to scroll
|
|
|
|
const stopGrabbing = (e) => {
|
|
|
|
if(imageContainer.classList.contains("grabbing")) {
|
|
|
|
imageContainer.classList.remove("grabbing")
|
|
|
|
e?.preventDefault()
|
|
|
|
console.log(`stopGrabbing()`, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const addImageGrabbing = (image) => {
|
|
|
|
image?.addEventListener('mousedown', (e) => {
|
|
|
|
if (!image.classList.contains("natural-zoom")) {
|
|
|
|
e.stopPropagation()
|
|
|
|
e.stopImmediatePropagation()
|
|
|
|
e.preventDefault()
|
|
|
|
|
|
|
|
imageContainer.classList.add("grabbing")
|
|
|
|
state.start.x = e.pageX - imageContainer.offsetLeft
|
|
|
|
state.scroll.x = imageContainer.scrollLeft
|
|
|
|
state.start.y = e.pageY - imageContainer.offsetTop
|
|
|
|
state.scroll.y = imageContainer.scrollTop
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
image?.addEventListener('mouseup', stopGrabbing)
|
|
|
|
image?.addEventListener('mouseleave', stopGrabbing)
|
|
|
|
image?.addEventListener('mousemove', (e) => {
|
|
|
|
if(imageContainer.classList.contains("grabbing")) {
|
|
|
|
e.stopPropagation()
|
|
|
|
e.stopImmediatePropagation()
|
|
|
|
e.preventDefault()
|
|
|
|
|
|
|
|
// Might need to increase this multiplier based on the image size to window size ratio
|
|
|
|
// The default 1:1 is pretty slow
|
|
|
|
const multiplier = 1.0
|
|
|
|
|
|
|
|
const deltaX = e.pageX - imageContainer.offsetLeft - state.start.x
|
|
|
|
imageContainer.scrollLeft = state.scroll.x - (deltaX * multiplier)
|
|
|
|
const deltaY = e.pageY - imageContainer.offsetTop - state.start.y
|
|
|
|
imageContainer.scrollTop = state.scroll.y - (deltaY * multiplier)
|
|
|
|
}
|
|
|
|
})
|
2023-04-04 01:35:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const clear = () => {
|
2023-04-27 19:56:56 +02:00
|
|
|
imageContainer.innerHTML = ""
|
2023-04-04 01:35:37 +02:00
|
|
|
|
2023-05-05 01:36:10 +02:00
|
|
|
Object.entries(initialState()).forEach(([key, value]) => state[key] = value)
|
|
|
|
|
|
|
|
stopGrabbing()
|
2023-04-04 01:35:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const close = () => {
|
|
|
|
clear()
|
2023-04-27 19:56:56 +02:00
|
|
|
modalElem.classList.remove("active")
|
|
|
|
document.body.style.overflow = "initial"
|
2023-03-15 02:43:49 +01:00
|
|
|
}
|
|
|
|
|
2023-04-04 01:35:37 +02:00
|
|
|
/**
|
|
|
|
* @param {() => (string | ImageModalRequest) | string | ImageModalRequest} optionsFactory
|
|
|
|
*/
|
|
|
|
function init(optionsFactory) {
|
|
|
|
if (!optionsFactory) {
|
2023-03-15 02:43:49 +01:00
|
|
|
close()
|
2023-04-04 01:35:37 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
clear()
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const options = typeof optionsFactory === "function" ? optionsFactory() : optionsFactory
|
|
|
|
const src = typeof options === "string" ? options : options.src
|
2023-04-04 01:35:37 +02:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const imgElem = createElement("img", { src }, "natural-zoom")
|
2023-05-05 01:36:10 +02:00
|
|
|
addImageGrabbing(imgElem)
|
2023-04-04 01:35:37 +02:00
|
|
|
imageContainer.appendChild(imgElem)
|
2023-04-27 19:56:56 +02:00
|
|
|
modalElem.classList.add("active")
|
|
|
|
document.body.style.overflow = "hidden"
|
2023-04-04 01:35:37 +02:00
|
|
|
setZoomLevel(false)
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
if (typeof options === "object" && options.previous) {
|
2023-04-04 01:35:37 +02:00
|
|
|
state.previous = options.previous
|
2023-04-27 19:56:56 +02:00
|
|
|
backElem.style.display = "unset"
|
2023-04-04 01:35:37 +02:00
|
|
|
} else {
|
2023-04-27 19:56:56 +02:00
|
|
|
backElem.style.display = "none"
|
2023-04-04 01:35:37 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
if (typeof options === "object" && options.next) {
|
2023-04-04 01:35:37 +02:00
|
|
|
state.next = options.next
|
2023-04-27 19:56:56 +02:00
|
|
|
forwardElem.style.display = "unset"
|
2023-04-04 01:35:37 +02:00
|
|
|
} else {
|
2023-04-27 19:56:56 +02:00
|
|
|
forwardElem.style.display = "none"
|
2023-04-04 01:35:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const back = () => {
|
|
|
|
if (state.previous) {
|
|
|
|
init(state.previous)
|
|
|
|
} else {
|
2023-04-27 19:56:56 +02:00
|
|
|
backElem.style.display = "none"
|
2023-04-04 01:35:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const forward = () => {
|
|
|
|
if (state.next) {
|
|
|
|
init(state.next)
|
|
|
|
} else {
|
2023-04-27 19:56:56 +02:00
|
|
|
forwardElem.style.display = "none"
|
2023-04-04 01:35:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
window.addEventListener("keydown", (e) => {
|
|
|
|
if (modalElem.classList.contains("active")) {
|
2023-04-04 01:35:37 +02:00
|
|
|
switch (e.key) {
|
2023-04-27 19:56:56 +02:00
|
|
|
case "Escape":
|
2023-04-04 01:35:37 +02:00
|
|
|
close()
|
|
|
|
break
|
2023-04-27 19:56:56 +02:00
|
|
|
case "ArrowLeft":
|
2023-04-04 01:35:37 +02:00
|
|
|
back()
|
|
|
|
break
|
2023-04-27 19:56:56 +02:00
|
|
|
case "ArrowRight":
|
2023-04-04 01:35:37 +02:00
|
|
|
forward()
|
|
|
|
break
|
|
|
|
}
|
2023-03-15 02:43:49 +01:00
|
|
|
}
|
|
|
|
})
|
2023-04-27 19:56:56 +02:00
|
|
|
window.addEventListener("click", (e) => {
|
|
|
|
if (modalElem.classList.contains("active")) {
|
2023-03-15 02:43:49 +01:00
|
|
|
if (e.target === backdrop || e.target === closeElem) {
|
|
|
|
close()
|
|
|
|
}
|
|
|
|
|
|
|
|
e.stopPropagation()
|
|
|
|
e.stopImmediatePropagation()
|
|
|
|
e.preventDefault()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
backElem.addEventListener("click", back)
|
2023-03-15 02:43:49 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
forwardElem.addEventListener("click", forward)
|
2023-04-04 01:35:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {() => (string | ImageModalRequest) | string | ImageModalRequest} optionsFactory
|
|
|
|
*/
|
|
|
|
return (optionsFactory) => init(optionsFactory)
|
2023-03-15 02:43:49 +01:00
|
|
|
})()
|