mirror of
https://github.com/Sped0n/bridget.git
synced 2026-04-18 20:19:30 -07:00
move pushIndex function to utils.ts
This commit is contained in:
@@ -1,5 +1,12 @@
|
|||||||
import { overlayEnable } from './overlay'
|
import { overlayEnable } from './overlay'
|
||||||
import { calcImageIndex, center, delay, mouseToTransform, type position } from './utils'
|
import {
|
||||||
|
calcImageIndex,
|
||||||
|
center,
|
||||||
|
delay,
|
||||||
|
mouseToTransform,
|
||||||
|
pushIndex,
|
||||||
|
type position
|
||||||
|
} from './utils'
|
||||||
import { thresholdIndex, thresholdSensitivityArray } from './thresholdCtl'
|
import { thresholdIndex, thresholdSensitivityArray } from './thresholdCtl'
|
||||||
import { imgIndexSpanUpdate } from './indexDisp'
|
import { imgIndexSpanUpdate } from './indexDisp'
|
||||||
import { imagesArrayLen } from './dataFetch'
|
import { imagesArrayLen } from './dataFetch'
|
||||||
@@ -19,51 +26,17 @@ let EnterOverlayClickAbCtl = new AbortController()
|
|||||||
|
|
||||||
export const stackDepth: number = 5
|
export const stackDepth: number = 5
|
||||||
|
|
||||||
export const pushIndex = (
|
|
||||||
index: number,
|
|
||||||
invert: boolean = false,
|
|
||||||
autoHide: boolean = true
|
|
||||||
): number => {
|
|
||||||
let indexesNum: number = trailingImageIndexes.length
|
|
||||||
let overflow: number
|
|
||||||
if (!invert) {
|
|
||||||
// push the tail index out and hide the image
|
|
||||||
if (indexesNum === stackDepth) {
|
|
||||||
trailingImageIndexes.push(index)
|
|
||||||
overflow = trailingImageIndexes.shift() as number
|
|
||||||
if (autoHide) {
|
|
||||||
images[overflow].style.display = 'none'
|
|
||||||
images[overflow].dataset.status = 'trail'
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
trailingImageIndexes.push(index)
|
|
||||||
indexesNum += 1
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (indexesNum === stackDepth) {
|
|
||||||
trailingImageIndexes.unshift(
|
|
||||||
calcImageIndex(index - stackDepth + 1, imagesArrayLen)
|
|
||||||
)
|
|
||||||
overflow = trailingImageIndexes.pop() as number
|
|
||||||
if (autoHide) {
|
|
||||||
images[overflow].style.display = 'none'
|
|
||||||
images[overflow].dataset.status = 'trail'
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
trailingImageIndexes.unshift(
|
|
||||||
calcImageIndex(index - indexesNum + 1, imagesArrayLen)
|
|
||||||
)
|
|
||||||
indexesNum += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return indexesNum
|
|
||||||
}
|
|
||||||
|
|
||||||
// activate top image
|
// activate top image
|
||||||
const activate = (index: number, mouseX: number, mouseY: number): void => {
|
const activate = (index: number, mouseX: number, mouseY: number): void => {
|
||||||
EnterOverlayClickAbCtl.abort()
|
EnterOverlayClickAbCtl.abort()
|
||||||
EnterOverlayClickAbCtl = new AbortController()
|
EnterOverlayClickAbCtl = new AbortController()
|
||||||
const indexesNum: number = pushIndex(index)
|
const indexesNum: number = pushIndex(
|
||||||
|
index,
|
||||||
|
trailingImageIndexes,
|
||||||
|
stackDepth,
|
||||||
|
images,
|
||||||
|
imagesArrayLen
|
||||||
|
)
|
||||||
// set img position
|
// set img position
|
||||||
images[index].style.transform = mouseToTransform(mouseX, mouseY, true, true)
|
images[index].style.transform = mouseToTransform(mouseX, mouseY, true, true)
|
||||||
images[index].dataset.status = 'null'
|
images[index].dataset.status = 'null'
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { delay, center, calcImageIndex, mouseToTransform } from './utils'
|
import { delay, center, calcImageIndex, mouseToTransform, pushIndex } from './utils'
|
||||||
import {
|
import {
|
||||||
handleOnMove,
|
handleOnMove,
|
||||||
globalIndex,
|
globalIndex,
|
||||||
@@ -6,7 +6,6 @@ import {
|
|||||||
globalIndexInc,
|
globalIndexInc,
|
||||||
trailingImageIndexes,
|
trailingImageIndexes,
|
||||||
transformCache,
|
transformCache,
|
||||||
pushIndex,
|
|
||||||
emptyTransformCache,
|
emptyTransformCache,
|
||||||
emptyTrailingImageIndexes,
|
emptyTrailingImageIndexes,
|
||||||
stackDepth
|
stackDepth
|
||||||
@@ -78,7 +77,15 @@ const handlePrevClick = (): void => {
|
|||||||
const imgIndex: number = calcImageIndex(globalIndex, imagesArrayLen)
|
const imgIndex: number = calcImageIndex(globalIndex, imagesArrayLen)
|
||||||
globalIndexDec()
|
globalIndexDec()
|
||||||
const prevImgIndex = calcImageIndex(globalIndex, imagesArrayLen)
|
const prevImgIndex = calcImageIndex(globalIndex, imagesArrayLen)
|
||||||
pushIndex(prevImgIndex, true, false)
|
pushIndex(
|
||||||
|
prevImgIndex,
|
||||||
|
trailingImageIndexes,
|
||||||
|
stackDepth,
|
||||||
|
images,
|
||||||
|
imagesArrayLen,
|
||||||
|
true,
|
||||||
|
false
|
||||||
|
)
|
||||||
images[imgIndex].style.display = 'none'
|
images[imgIndex].style.display = 'none'
|
||||||
center(images[prevImgIndex])
|
center(images[prevImgIndex])
|
||||||
images[prevImgIndex].dataset.status = 'top'
|
images[prevImgIndex].dataset.status = 'top'
|
||||||
@@ -90,7 +97,15 @@ const handleNextClick = (): void => {
|
|||||||
const imgIndex: number = calcImageIndex(globalIndex, imagesArrayLen)
|
const imgIndex: number = calcImageIndex(globalIndex, imagesArrayLen)
|
||||||
globalIndexInc()
|
globalIndexInc()
|
||||||
const nextImgIndex = calcImageIndex(globalIndex, imagesArrayLen)
|
const nextImgIndex = calcImageIndex(globalIndex, imagesArrayLen)
|
||||||
pushIndex(nextImgIndex, false, false)
|
pushIndex(
|
||||||
|
nextImgIndex,
|
||||||
|
trailingImageIndexes,
|
||||||
|
stackDepth,
|
||||||
|
images,
|
||||||
|
imagesArrayLen,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
)
|
||||||
images[imgIndex].style.display = 'none'
|
images[imgIndex].style.display = 'none'
|
||||||
center(images[nextImgIndex])
|
center(images[nextImgIndex])
|
||||||
images[nextImgIndex].dataset.status = 'top'
|
images[nextImgIndex].dataset.status = 'top'
|
||||||
|
|||||||
@@ -96,3 +96,43 @@ export const getDeviceType = (): deviceType => {
|
|||||||
} else result.desktop = true
|
} else result.desktop = true
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const pushIndex = (
|
||||||
|
index: number,
|
||||||
|
indexesArray: number[],
|
||||||
|
stackDepth: number,
|
||||||
|
imagesArray: NodeListOf<HTMLImageElement>,
|
||||||
|
imagesArrayLen: number,
|
||||||
|
invert: boolean = false,
|
||||||
|
autoHide: boolean = true
|
||||||
|
): number => {
|
||||||
|
let indexesNum: number = indexesArray.length
|
||||||
|
let overflow: number
|
||||||
|
if (!invert) {
|
||||||
|
// push the tail index out and hide the image
|
||||||
|
if (indexesNum === stackDepth) {
|
||||||
|
indexesArray.push(index)
|
||||||
|
overflow = indexesArray.shift() as number
|
||||||
|
if (autoHide) {
|
||||||
|
imagesArray[overflow].style.display = 'none'
|
||||||
|
imagesArray[overflow].dataset.status = 'trail'
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
indexesArray.push(index)
|
||||||
|
indexesNum += 1
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (indexesNum === stackDepth) {
|
||||||
|
indexesArray.unshift(calcImageIndex(index - stackDepth + 1, imagesArrayLen))
|
||||||
|
overflow = indexesArray.pop() as number
|
||||||
|
if (autoHide) {
|
||||||
|
imagesArray[overflow].style.display = 'none'
|
||||||
|
imagesArray[overflow].dataset.status = 'trail'
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
indexesArray.unshift(calcImageIndex(index - indexesNum + 1, imagesArrayLen))
|
||||||
|
indexesNum += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return indexesNum
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user