optimize reused code fragments

This commit is contained in:
Spedon
2023-03-28 17:00:14 +08:00
parent 823de500d7
commit 9a6f141533

View File

@@ -97,45 +97,45 @@ export const getDeviceType = (): deviceType => {
return result return result
} }
const autoHide = (e: HTMLImageElement): void => {
e.style.visibility = 'hidden'
e.dataset.status = 'trail'
}
export const pushIndex = ( export const pushIndex = (
index: number, index: number,
indexesArray: number[], indexesArray: number[],
stackDepth: number, stackDepth: number,
imagesArray: NodeListOf<HTMLImageElement>, imagesArray: NodeListOf<HTMLImageElement>,
imagesArrayLen: number, imagesArrayLen: number,
invert: boolean = false, invertFlag: boolean = false,
autoHide: boolean = true autoHideFlag: boolean = true
): number => { ): number => {
let indexesNum: number = indexesArray.length let indexesNum: number = indexesArray.length
// create variable overflow to store the tail index // create variable overflow to store the tail index
let overflow: number let overflow: number
if (!invert) { if (!invertFlag) {
// push the tail index out and hide the image // if stack is full, push the tail index out and hide the image
if (indexesNum === stackDepth) { if (indexesNum === stackDepth) {
// insert // insert
indexesArray.push(index) indexesArray.push(index)
// pop out // pop out
overflow = indexesArray.shift() as number overflow = indexesArray.shift() as number
// auto hide tail image // auto hide tail image
if (autoHide) { if (autoHideFlag) autoHide(imagesArray[overflow])
imagesArray[overflow].style.visibility = 'hidden'
imagesArray[overflow].dataset.status = 'trail'
}
} else { } else {
indexesArray.push(index) indexesArray.push(index)
indexesNum += 1 indexesNum += 1
} }
} else { } else {
// if stack is full, push the tail index out and hide the image
if (indexesNum === stackDepth) { if (indexesNum === stackDepth) {
// insert // insert
indexesArray.unshift(calcImageIndex(index - stackDepth + 1, imagesArrayLen)) indexesArray.unshift(calcImageIndex(index - stackDepth + 1, imagesArrayLen))
// pop out // pop out
overflow = indexesArray.pop() as number overflow = indexesArray.pop() as number
// auto hide tail image // auto hide tail image
if (autoHide) { if (autoHideFlag) autoHide(imagesArray[overflow])
imagesArray[overflow].style.visibility = 'hidden'
imagesArray[overflow].dataset.status = 'trail'
}
} else { } else {
indexesArray.unshift(calcImageIndex(index - indexesNum + 1, imagesArrayLen)) indexesArray.unshift(calcImageIndex(index - indexesNum + 1, imagesArrayLen))
indexesNum += 1 indexesNum += 1