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-04 01:35:37 +02:00
|
|
|
const backElem = createElement(
|
|
|
|
'i',
|
|
|
|
undefined,
|
|
|
|
['fa-solid', 'fa-arrow-left', 'tertiaryButton'],
|
|
|
|
)
|
|
|
|
|
|
|
|
const forwardElem = createElement(
|
|
|
|
'i',
|
|
|
|
undefined,
|
|
|
|
['fa-solid', 'fa-arrow-right', 'tertiaryButton'],
|
|
|
|
)
|
|
|
|
|
2023-03-15 02:43:49 +01:00
|
|
|
const zoomElem = createElement(
|
|
|
|
'i',
|
|
|
|
undefined,
|
|
|
|
['fa-solid', 'tertiaryButton'],
|
|
|
|
)
|
|
|
|
|
|
|
|
const closeElem = createElement(
|
|
|
|
'i',
|
|
|
|
undefined,
|
|
|
|
['fa-solid', 'fa-xmark', 'tertiaryButton'],
|
|
|
|
)
|
|
|
|
|
2023-04-04 01:35:37 +02:00
|
|
|
const menuBarElem = createElement('div', undefined, 'menu-bar', [backElem, forwardElem, zoomElem, closeElem])
|
2023-03-15 02:43:49 +01:00
|
|
|
|
|
|
|
const imageContainer = createElement('div', undefined, 'image-wrapper')
|
|
|
|
|
|
|
|
const backdrop = createElement('div', undefined, 'backdrop')
|
|
|
|
|
|
|
|
const modalContainer = createElement('div', undefined, 'content', [menuBarElem, imageContainer])
|
|
|
|
|
|
|
|
const modalElem = createElement(
|
|
|
|
'div',
|
|
|
|
{ id: 'viewFullSizeImgModal' },
|
|
|
|
['popup'],
|
|
|
|
[backdrop, modalContainer],
|
|
|
|
)
|
|
|
|
document.body.appendChild(modalElem)
|
|
|
|
|
|
|
|
const setZoomLevel = (value) => {
|
|
|
|
const img = imageContainer.querySelector('img')
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
zoomElem.classList.remove('fa-magnifying-glass-plus')
|
|
|
|
zoomElem.classList.add('fa-magnifying-glass-minus')
|
|
|
|
if (img) {
|
|
|
|
img.classList.remove('natural-zoom')
|
|
|
|
|
|
|
|
let zoomLevel = typeof value === 'number' ? value : img.dataset.zoomLevel
|
|
|
|
if (!zoomLevel) {
|
|
|
|
zoomLevel = 100
|
|
|
|
}
|
|
|
|
|
|
|
|
img.dataset.zoomLevel = zoomLevel
|
|
|
|
img.width = img.naturalWidth * (+zoomLevel / 100)
|
|
|
|
img.height = img.naturalHeight * (+zoomLevel / 100)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
zoomElem.classList.remove('fa-magnifying-glass-minus')
|
|
|
|
zoomElem.classList.add('fa-magnifying-glass-plus')
|
|
|
|
if (img) {
|
|
|
|
img.classList.add('natural-zoom')
|
|
|
|
img.removeAttribute('width')
|
|
|
|
img.removeAttribute('height')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
zoomElem.addEventListener(
|
|
|
|
'click',
|
|
|
|
() => setZoomLevel(imageContainer.querySelector('img')?.classList?.contains('natural-zoom')),
|
|
|
|
)
|
|
|
|
|
2023-04-04 01:35:37 +02:00
|
|
|
const state = {
|
|
|
|
previous: undefined,
|
|
|
|
next: undefined,
|
|
|
|
}
|
|
|
|
|
|
|
|
const clear = () => {
|
2023-03-15 02:43:49 +01:00
|
|
|
imageContainer.innerHTML = ''
|
2023-04-04 01:35:37 +02:00
|
|
|
|
|
|
|
Object.keys(state).forEach(key => delete state[key])
|
|
|
|
}
|
|
|
|
|
|
|
|
const close = () => {
|
|
|
|
clear()
|
2023-03-15 02:43:49 +01:00
|
|
|
modalElem.classList.remove('active')
|
|
|
|
document.body.style.overflow = 'initial'
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
const options = typeof optionsFactory === 'function' ? optionsFactory() : optionsFactory
|
|
|
|
const src = typeof options === 'string' ? options : options.src
|
|
|
|
|
|
|
|
const imgElem = createElement('img', { src }, 'natural-zoom')
|
|
|
|
imageContainer.appendChild(imgElem)
|
|
|
|
modalElem.classList.add('active')
|
|
|
|
document.body.style.overflow = 'hidden'
|
|
|
|
setZoomLevel(false)
|
|
|
|
|
|
|
|
if (typeof options === 'object' && options.previous) {
|
|
|
|
state.previous = options.previous
|
|
|
|
backElem.style.display = 'unset'
|
|
|
|
} else {
|
|
|
|
backElem.style.display = 'none'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof options === 'object' && options.next) {
|
|
|
|
state.next = options.next
|
|
|
|
forwardElem.style.display = 'unset'
|
|
|
|
} else {
|
|
|
|
forwardElem.style.display = 'none'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const back = () => {
|
|
|
|
if (state.previous) {
|
|
|
|
init(state.previous)
|
|
|
|
} else {
|
|
|
|
backElem.style.display = 'none'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const forward = () => {
|
|
|
|
if (state.next) {
|
|
|
|
init(state.next)
|
|
|
|
} else {
|
|
|
|
forwardElem.style.display = 'none'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('keydown', (e) => {
|
|
|
|
if (modalElem.classList.contains('active')) {
|
|
|
|
switch (e.key) {
|
|
|
|
case 'Escape':
|
|
|
|
close()
|
|
|
|
break
|
|
|
|
case 'ArrowLeft':
|
|
|
|
back()
|
|
|
|
break
|
|
|
|
case 'ArrowRight':
|
|
|
|
forward()
|
|
|
|
break
|
|
|
|
}
|
2023-03-15 02:43:49 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
window.addEventListener('click', (e) => {
|
|
|
|
if (modalElem.classList.contains('active')) {
|
|
|
|
if (e.target === backdrop || e.target === closeElem) {
|
|
|
|
close()
|
|
|
|
}
|
|
|
|
|
|
|
|
e.stopPropagation()
|
|
|
|
e.stopImmediatePropagation()
|
|
|
|
e.preventDefault()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-04-04 01:35:37 +02:00
|
|
|
backElem.addEventListener('click', back)
|
2023-03-15 02:43:49 +01:00
|
|
|
|
2023-04-04 01:35:37 +02:00
|
|
|
forwardElem.addEventListener('click', forward)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {() => (string | ImageModalRequest) | string | ImageModalRequest} optionsFactory
|
|
|
|
*/
|
|
|
|
return (optionsFactory) => init(optionsFactory)
|
2023-03-15 02:43:49 +01:00
|
|
|
})()
|