mirror of
https://github.com/Sped0n/bridget.git
synced 2026-04-17 03:29:31 -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
107 lines
2.5 KiB
TypeScript
107 lines
2.5 KiB
TypeScript
import { container } from '../container'
|
|
import { setIndex } from '../globalState'
|
|
import { type ImageJSON } from '../resources'
|
|
|
|
import { slideUp } from './gallery'
|
|
import { mounted } from './state'
|
|
// eslint-disable-next-line sort-imports
|
|
import { getRandom, onIntersection, type MobileImage } from './utils'
|
|
|
|
/**
|
|
* variables
|
|
*/
|
|
|
|
export let imgs: MobileImage[] = []
|
|
|
|
/**
|
|
* main functions
|
|
*/
|
|
|
|
function handleClick(i: number): void {
|
|
setIndex(i)
|
|
slideUp()
|
|
}
|
|
|
|
/**
|
|
* init
|
|
*/
|
|
|
|
export function initCollection(ijs: ImageJSON[]): void {
|
|
createCollection(ijs)
|
|
// get container
|
|
const collection = document
|
|
.getElementsByClassName('collection')
|
|
.item(0) as HTMLDivElement
|
|
// add watcher
|
|
mounted.addWatcher((o) => {
|
|
if (o) {
|
|
collection.classList.remove('hidden')
|
|
} else {
|
|
collection.classList.add('hidden')
|
|
}
|
|
})
|
|
// get image elements
|
|
imgs = Array.from(collection.getElementsByTagName('img')) as MobileImage[]
|
|
// add event listeners
|
|
imgs.forEach((img, i) => {
|
|
// preload first 5 images on page load
|
|
if (i < 5) {
|
|
img.src = img.dataset.src
|
|
}
|
|
// event listeners
|
|
img.addEventListener(
|
|
'click',
|
|
() => {
|
|
handleClick(i)
|
|
},
|
|
{ passive: true }
|
|
)
|
|
img.addEventListener(
|
|
'keydown',
|
|
() => {
|
|
handleClick(i)
|
|
},
|
|
{ passive: true }
|
|
)
|
|
// preload
|
|
onIntersection(img, (entries, observer) => {
|
|
entries.every((entry) => {
|
|
// no intersection, skip
|
|
if (entry.intersectionRatio <= 0) return true
|
|
// preload the i + 5th image
|
|
if (i + 5 < imgs.length) {
|
|
imgs[i + 5].src = imgs[i + 5].dataset.src
|
|
}
|
|
// disconnect observer and return false to break the loop
|
|
observer.disconnect()
|
|
return false
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
/**
|
|
* helper
|
|
*/
|
|
|
|
function createCollection(ijs: ImageJSON[]): void {
|
|
// create container for images
|
|
const _collection: HTMLDivElement = document.createElement('div')
|
|
_collection.className = 'collection'
|
|
// append images to container
|
|
for (const [i, ij] of ijs.entries()) {
|
|
// random x and y
|
|
const x = i !== 0 ? getRandom(-25, 25) : 0
|
|
const y = i !== 0 ? getRandom(-30, 30) : 0
|
|
// element
|
|
const e = document.createElement('img') as MobileImage
|
|
e.dataset.src = ij.loUrl
|
|
e.height = ij.loImgH
|
|
e.width = ij.loImgW
|
|
e.alt = ij.alt
|
|
e.style.transform = `translate3d(${x}%, ${y - 50}%, 0)`
|
|
_collection.append(e)
|
|
}
|
|
container.append(_collection)
|
|
}
|