combine handle next/prev click code into one function

This commit is contained in:
Spedon
2023-03-28 14:14:41 +08:00
parent 42a8bb8bc8
commit de692352f2

View File

@@ -103,60 +103,42 @@ async function handleCloseClick(): Promise<void> {
emptyTransformCache() emptyTransformCache()
} }
const handlePrevClick = (): void => { const handleSideClick = (CLD: boolean): void => {
// get last displayed image's index // get last displayed image's index
const imgIndex: number = calcImageIndex(globalIndex, imagesArrayLen) const imgIndex: number = calcImageIndex(globalIndex, imagesArrayLen)
// change global index and get current displayed image's index // change global index and get current displayed image's index
globalIndexDec() CLD ? globalIndexInc() : globalIndexDec()
const prevImgIndex: number = calcImageIndex(globalIndex, imagesArrayLen) const currImgIndex: number = calcImageIndex(globalIndex, imagesArrayLen)
// store current displayed image's index // store current displayed image's index
pushIndex( CLD
prevImgIndex, ? pushIndex(
trailingImageIndexes, currImgIndex,
stackDepth, trailingImageIndexes,
images, stackDepth,
imagesArrayLen, images,
true, imagesArrayLen,
false false,
) false
)
: pushIndex(
currImgIndex,
trailingImageIndexes,
stackDepth,
images,
imagesArrayLen,
true,
false
)
// hide last displayed image // hide last displayed image
images[imgIndex].style.visibility = 'hidden' images[imgIndex].style.visibility = 'hidden'
images[imgIndex].dataset.status = 'trail' images[imgIndex].dataset.status = 'trail'
// process the image going to display // process the image going to display
center(images[prevImgIndex]) center(images[currImgIndex])
images[prevImgIndex].dataset.status = 'overlay' images[currImgIndex].dataset.status = 'overlay'
// process complete, show the image // process complete, show the image
images[prevImgIndex].style.visibility = 'visible' images[currImgIndex].style.visibility = 'visible'
// change index display // change index display
imgIndexSpanUpdate(prevImgIndex + 1, imagesArrayLen) imgIndexSpanUpdate(currImgIndex + 1, imagesArrayLen)
}
const handleNextClick = (): void => {
// get last displayed image's index
const imgIndex: number = calcImageIndex(globalIndex, imagesArrayLen)
// change global index and get current displayed image's index
globalIndexInc()
const nextImgIndex: number = calcImageIndex(globalIndex, imagesArrayLen)
// store current displayed image's index
pushIndex(
nextImgIndex,
trailingImageIndexes,
stackDepth,
images,
imagesArrayLen,
false,
false
)
// hide last displayed image
images[imgIndex].style.visibility = 'hidden'
images[imgIndex].dataset.status = 'trail'
// process the image going to display
center(images[nextImgIndex])
images[nextImgIndex].dataset.status = 'overlay'
// process complete, show the image
images[nextImgIndex].style.visibility = 'visible'
// change index display
imgIndexSpanUpdate(nextImgIndex + 1, imagesArrayLen)
} }
// change text and position of overlay cursor // change text and position of overlay cursor
@@ -179,13 +161,13 @@ const handleOverlayMouseMove = (e: MouseEvent): void => {
const handleOverlayClick = (): void => { const handleOverlayClick = (): void => {
switch (overlayCursor.dataset.status) { switch (overlayCursor.dataset.status) {
case 'PREV': case 'PREV':
handlePrevClick() handleSideClick(false)
break break
case 'CLOSE': case 'CLOSE':
void handleCloseClick() void handleCloseClick()
break break
case 'NEXT': case 'NEXT':
handleNextClick() handleSideClick(true)
break break
} }
} }