mirror of
https://github.com/Sped0n/bridget.git
synced 2026-04-17 11:39:29 -07:00
* feat: refactor file structure and imports in mobile and desktop components - Removed the import of `scrollable` from `assets/ts/mobile/scroll.ts` - Renamed `assets/ts/mobile/mounted.ts` to `assets/ts/mobile/state.ts` - Changed the import of `active` from `./stage` to `./state` in `assets/ts/desktop/customCursor.ts` - Changed the import of `active` from `../state` to `../globalState` in `assets/ts/desktop/stage.ts` - Changed the import of `active` from `./stage` to `./state` in `assets/ts/desktop/stageNav.ts` - Renamed `assets/ts/state.ts` to `assets/ts/globalState.ts` - Created a new file `assets/ts/desktop/state.ts` - Added the interface `HistoryItem` to `assets/ts/desktop/state.ts` - Added the variables `cordHist`, `isOpen`, `active`, and `isLoading` to `assets/ts/desktop/state.ts` - Deleted the function `loader` from `assets/ts/desktop/stage.ts` and replaced it with `setLoaderForImage` - Deleted the import of `./stage` from `assets/ts/desktop/stageNav.ts` - Added the import of `minimizeImage` from `./stage` in `assets/ts/desktop/stageNav.ts` - Deleted the import of `./mounted` from `assets/ts/mobile/collection.ts` - Changed the import of `mounted` from `./mounted` to `./state` in ` * refactor: refactor the `onVisible` function to improve performance - Modify the type of the `onVisible` function parameter `T` to extend `HTMLElement` - Change the `entries.forEach` loop in the `onVisible` function to `entries.every` * feat: add new function for detecting opacity changes in element - Add a new function `onOpacityOne` in `assets/ts/utils.ts` - The function uses a `MutationObserver` to check for opacity changes on an element - When the element's opacity reaches `1`, the provided callback function is called - The `MutationObserver` is disconnected after the callback is executed * refactor: refactor function names and parameters in intersection and mutation observers - Change the function name `onVisible` to `onIntersection` - Modify the `callback` parameter in the `onIntersection` function to accept `IntersectionObserverEntry[]` and `IntersectionObserver` parameters - Remove the code block that checks for intersection ratio and immediately calls the `callback` function in the `onIntersection` function - Modify the function name `onOpacityOne` to `onMutation` - Modify the `callback` parameter in the `onMutation` function to accept `MutationRecord[]` and `MutationObserver` parameters - Add a default value for the `observeOptions` parameter in the `onMutation` function * refactor: refine preload logic on both mobile and desktop * refactor: refactor import statements and add new files - The import statement for `Watchable` in `assets/ts/globalState.ts` has been changed from `../utils` to `../globalUtils` - The import statement for `Watchable` in `assets/ts/desktop/state.ts` has been changed from `../utils` to `../globalUtils` - The import statement for `decrement` and `increment` in `assets/ts/desktop/stageNav.ts` has been changed from `../utils` to `../globalUtils` - A new file `utils.ts` has been added in the `assets/ts/desktop` directory - The import statements for `getRandom`, `onIntersection`, and `type MobileImage` in `assets/ts/mobile/collection.ts` have been changed from `../utils` to `./utils` - The `imgs` array in `assets/ts/mobile/collection.ts` has been changed from an array of `HTMLImageElement` to an array of `MobileImage` - The import statements for `expand`, `loadGsap`, `loadSwiper`, and `removeDuplicates` in `assets/ts/mobile/gallery.ts` have been changed from `../utils` to `../globalUtils` - The import statement for `type MobileImage` in `assets/ts/mobile/gallery.ts` has been changed from `./utils` to `../mobile/utils` - The `galleryLoadImages` function in `assets/ts/mobile/gallery.ts` has been removed - A new file `utils.ts` * refactor: refactor swiper import and functions in mobile and global utils * refactor: refactor navigation and image loading logic in desktop and mobile * refactor: remove print function and optimize removeDuplicates return * refactor: update text variable assignments and attributes * refactor: update variable types in galleryImages and collectionImages in mobile/gallery.ts * refactor: refactor variable types for consistency with naming conventions * refactor: update animation durations and types in gallery functions * refactor: refactor image loading logic and add console logs * refactor: refactor sass hierarchy * refactor: remove console logs in multiple files
312 lines
7.4 KiB
TypeScript
312 lines
7.4 KiB
TypeScript
import { type Power3, type gsap } from 'gsap'
|
|
import { type Swiper } from 'swiper'
|
|
|
|
import { container, scrollable } from '../container'
|
|
import { isAnimating, navigateVector, setIndex, state } from '../globalState'
|
|
import { expand, loadGsap, removeDuplicates } from '../globalUtils'
|
|
import { type ImageJSON } from '../resources'
|
|
|
|
import { mounted } from './state'
|
|
// eslint-disable-next-line sort-imports
|
|
import { capitalizeFirstLetter, loadSwiper, type MobileImage } from './utils'
|
|
|
|
/**
|
|
* variables
|
|
*/
|
|
|
|
let swiperNode: HTMLDivElement
|
|
let gallery: HTMLDivElement
|
|
let curtain: HTMLDivElement
|
|
let swiper: Swiper
|
|
let lastIndex = -1
|
|
let indexDispNums: HTMLSpanElement[] = []
|
|
let galleryImages: MobileImage[] = []
|
|
let collectionImages: MobileImage[] = []
|
|
|
|
let _Swiper: typeof Swiper
|
|
let _gsap: typeof gsap
|
|
let _Power3: typeof Power3
|
|
|
|
let libLoaded = false
|
|
|
|
/**
|
|
* main functions
|
|
*/
|
|
|
|
export function slideUp(): void {
|
|
if (isAnimating.get() || !libLoaded) return
|
|
isAnimating.set(true)
|
|
|
|
// load active image
|
|
galleryLoadImages()
|
|
|
|
_gsap.to(curtain, {
|
|
opacity: 1,
|
|
duration: 1
|
|
})
|
|
|
|
_gsap.to(gallery, {
|
|
y: 0,
|
|
ease: _Power3.easeInOut,
|
|
duration: 1,
|
|
delay: 0.4
|
|
})
|
|
|
|
setTimeout(() => {
|
|
scrollable.set(false)
|
|
isAnimating.set(false)
|
|
}, 1400)
|
|
}
|
|
|
|
function slideDown(): void {
|
|
if (isAnimating.get()) return
|
|
isAnimating.set(true)
|
|
scrollToActive()
|
|
|
|
_gsap.to(gallery, {
|
|
y: '100%',
|
|
ease: _Power3.easeInOut,
|
|
duration: 1
|
|
})
|
|
|
|
_gsap.to(curtain, {
|
|
opacity: 0,
|
|
duration: 1.2,
|
|
delay: 0.4
|
|
})
|
|
|
|
setTimeout(() => {
|
|
scrollable.set(true)
|
|
isAnimating.set(false)
|
|
}, 1600)
|
|
}
|
|
|
|
/**
|
|
* init
|
|
*/
|
|
|
|
export function initGallery(ijs: ImageJSON[]): void {
|
|
// create gallery
|
|
createGallery(ijs)
|
|
// get elements
|
|
indexDispNums = Array.from(
|
|
document.getElementsByClassName('nav').item(0)?.getElementsByClassName('num') ?? []
|
|
) as HTMLSpanElement[]
|
|
swiperNode = document.getElementsByClassName('galleryInner').item(0) as HTMLDivElement
|
|
gallery = document.getElementsByClassName('gallery').item(0) as HTMLDivElement
|
|
curtain = document.getElementsByClassName('curtain').item(0) as HTMLDivElement
|
|
galleryImages = Array.from(gallery.getElementsByTagName('img')) as MobileImage[]
|
|
collectionImages = Array.from(
|
|
document
|
|
.getElementsByClassName('collection')
|
|
.item(0)
|
|
?.getElementsByTagName('img') ?? []
|
|
) as MobileImage[]
|
|
// state watcher
|
|
state.addWatcher(() => {
|
|
const s = state.get()
|
|
// change slide only when index is changed
|
|
if (s.index === lastIndex) return
|
|
else if (lastIndex === -1)
|
|
navigateVector.set('none') // lastIndex before first set
|
|
else if (s.index < lastIndex) navigateVector.set('prev')
|
|
else navigateVector.set('next')
|
|
changeSlide(s.index)
|
|
updateIndexText()
|
|
lastIndex = s.index
|
|
})
|
|
// mounted watcher
|
|
mounted.addWatcher((o) => {
|
|
if (!o) return
|
|
scrollable.set(true)
|
|
})
|
|
// dynamic import
|
|
window.addEventListener(
|
|
'touchstart',
|
|
() => {
|
|
loadGsap()
|
|
.then((g) => {
|
|
_gsap = g[0]
|
|
_Power3 = g[1]
|
|
})
|
|
.catch((e) => {
|
|
console.log(e)
|
|
})
|
|
loadSwiper()
|
|
.then((s) => {
|
|
_Swiper = s
|
|
swiper = new _Swiper(swiperNode, { spaceBetween: 20 })
|
|
swiper.on('slideChange', ({ realIndex }) => {
|
|
setIndex(realIndex)
|
|
})
|
|
})
|
|
.catch((e) => {
|
|
console.log(e)
|
|
})
|
|
libLoaded = true
|
|
},
|
|
{ once: true, passive: true }
|
|
)
|
|
// mounted
|
|
mounted.set(true)
|
|
}
|
|
|
|
/**
|
|
* helper
|
|
*/
|
|
|
|
function changeSlide(slide: number): void {
|
|
galleryLoadImages()
|
|
swiper.slideTo(slide, 0)
|
|
}
|
|
|
|
function scrollToActive(): void {
|
|
collectionImages[state.get().index].scrollIntoView({
|
|
block: 'center',
|
|
behavior: 'auto'
|
|
})
|
|
}
|
|
|
|
function updateIndexText(): void {
|
|
const indexValue: string = expand(state.get().index + 1)
|
|
const indexLength: string = expand(state.get().length)
|
|
indexDispNums.forEach((e: HTMLSpanElement, i: number) => {
|
|
if (i < 4) {
|
|
e.innerText = indexValue[i]
|
|
} else {
|
|
e.innerText = indexLength[i - 4]
|
|
}
|
|
})
|
|
}
|
|
|
|
function galleryLoadImages(): void {
|
|
let activeImagesIndex: number[] = []
|
|
const currentIndex = state.get().index
|
|
const nextIndex = Math.min(currentIndex + 1, state.get().length - 1)
|
|
const prevIndex = Math.max(currentIndex - 1, 0)
|
|
switch (navigateVector.get()) {
|
|
case 'next':
|
|
activeImagesIndex = [nextIndex]
|
|
break
|
|
case 'prev':
|
|
activeImagesIndex = [prevIndex]
|
|
break
|
|
case 'none':
|
|
activeImagesIndex = [currentIndex, nextIndex, prevIndex]
|
|
break
|
|
}
|
|
removeDuplicates(activeImagesIndex).forEach((i) => {
|
|
const e = galleryImages[i]
|
|
if (e.src === e.dataset.src) return // already loaded
|
|
e.src = e.dataset.src
|
|
})
|
|
}
|
|
|
|
function createGallery(ijs: ImageJSON[]): void {
|
|
/**
|
|
* gallery
|
|
* |- galleryInner
|
|
* |- swiper-wrapper
|
|
* |- swiper-slide
|
|
* |- img
|
|
* |- swiper-slide
|
|
* |- img
|
|
* |- ...
|
|
* |- nav
|
|
* |- index
|
|
* |- close
|
|
*/
|
|
// swiper wrapper
|
|
const _swiperWrapper = document.createElement('div')
|
|
_swiperWrapper.className = 'swiper-wrapper'
|
|
// loading text
|
|
const loadingText = container.dataset.loading
|
|
for (const ij of ijs) {
|
|
// swiper slide
|
|
const _swiperSlide = document.createElement('div')
|
|
_swiperSlide.className = 'swiper-slide'
|
|
// loading indicator
|
|
const l = document.createElement('div')
|
|
l.className = 'loadingText'
|
|
l.innerText = loadingText
|
|
// img
|
|
const e = document.createElement('img') as MobileImage
|
|
e.dataset.src = ij.hiUrl
|
|
e.height = ij.hiImgH
|
|
e.width = ij.hiImgW
|
|
e.alt = ij.alt
|
|
e.classList.add('hide')
|
|
// load event
|
|
e.addEventListener(
|
|
'load',
|
|
() => {
|
|
e.classList.remove('hide')
|
|
l.classList.add('hide')
|
|
},
|
|
{ once: true, passive: true }
|
|
)
|
|
// parent container
|
|
const p = document.createElement('div')
|
|
p.className = 'slideContainer'
|
|
// append
|
|
p.append(e)
|
|
p.append(l)
|
|
_swiperSlide.append(p)
|
|
_swiperWrapper.append(_swiperSlide)
|
|
}
|
|
// swiper node
|
|
const _swiperNode = document.createElement('div')
|
|
_swiperNode.className = 'galleryInner'
|
|
_swiperNode.append(_swiperWrapper)
|
|
// index
|
|
const _index = document.createElement('div')
|
|
_index.insertAdjacentHTML(
|
|
'afterbegin',
|
|
`<span class="num"></span><span class="num"></span><span class="num"></span><span class="num"></span>
|
|
<span>/</span>
|
|
<span class="num"></span><span class="num"></span><span class="num"></span><span class="num"></span>`
|
|
)
|
|
// close
|
|
const _close = document.createElement('div')
|
|
const str: string = document
|
|
.getElementById('main')
|
|
?.getAttribute('closeText') as string
|
|
_close.innerText = capitalizeFirstLetter(str)
|
|
_close.addEventListener(
|
|
'click',
|
|
() => {
|
|
slideDown()
|
|
},
|
|
{ passive: true }
|
|
)
|
|
_close.addEventListener(
|
|
'keydown',
|
|
() => {
|
|
slideDown()
|
|
},
|
|
{ passive: true }
|
|
)
|
|
// nav
|
|
const _navDiv = document.createElement('div')
|
|
_navDiv.className = 'nav'
|
|
_navDiv.append(_index, _close)
|
|
// gallery
|
|
const _gallery = document.createElement('div')
|
|
_gallery.className = 'gallery'
|
|
_gallery.append(_swiperNode)
|
|
_gallery.append(_navDiv)
|
|
|
|
/**
|
|
* curtain
|
|
*/
|
|
const _curtain = document.createElement('div')
|
|
_curtain.className = 'curtain'
|
|
|
|
/**
|
|
* container
|
|
* |- gallery
|
|
* |- curtain
|
|
*/
|
|
container.append(_gallery, _curtain)
|
|
}
|