Populating array with new items

This commit is contained in:
Alan Grainger
2025-01-09 21:16:16 +08:00
parent b9d6c53499
commit 695cd64c94
2 changed files with 66 additions and 48 deletions
+64 -46
View File
@@ -1,58 +1,76 @@
function initLightGallery (params = {}) { class LGallery {
// Create the lightGallery instance items
const lgEl = document.getElementById('lightgallery') lightGallery
const lg = lightGallery(lgEl, Object.assign({ element
plugins: [lgZoom, lgThumbnail, lgVideo, lgFullscreen], index = 0
speed: 500,
/*
This license key was graciously provided by LightGallery under their
GPLv3 open-source project license:
*/
licenseKey: '8FFA6495-676C4D30-8BFC54B6-4D0A6CEC'
/*
Please do not take it and use it for other projects, as it was provided
specifically for Immich Public Proxy.
For your own projects you can use the default license key of init (params = {}) {
0000-0000-000-0000 as per their docs: // Create the lightGallery instance
this.element = document.getElementById('lightgallery')
this.lightGallery = lightGallery(this.element, Object.assign({
plugins: [lgZoom, lgThumbnail, lgVideo, lgFullscreen],
speed: 500,
/*
This license key was graciously provided by LightGallery under their
GPLv3 open-source project license:
*/
licenseKey: '8FFA6495-676C4D30-8BFC54B6-4D0A6CEC'
/*
Please do not take it and use it for other projects, as it was provided
specifically for Immich Public Proxy.
https://www.lightgalleryjs.com/docs/settings/#licenseKey For your own projects you can use the default license key of
*/ 0000-0000-000-0000 as per their docs:
}, params.lgConfig))
// repeat the array for testing https://www.lightgalleryjs.com/docs/settings/#licenseKey
let items = [] */
for (let i = 0; i < 50; i++) { }, params.lgConfig))
items = items.concat(params.items)
// repeat the array for testing
let items = []
for (let i = 0; i < 50; i++) {
items = items.concat(params.items)
}
this.items = items
let timeout
window.addEventListener('scroll', () => {
if (timeout) clearTimeout(timeout)
timeout = setTimeout(lgallery.handleScroll, 100)
})
lgallery.handleScroll()
} }
// Append thumbnails handleScroll () {
items.forEach(item => { const rect = lgallery.element.getBoundingClientRect()
if (item.video) { const scrollPosition = Math.max(0, rect.bottom - window.innerHeight)
lgEl.insertAdjacentHTML('beforeend', `<a data-video='${item.video}' const buffer = 200 // pixels before bottom to trigger load
if (scrollPosition <= buffer) {
lgallery.loadMoreItems()
}
}
loadMoreItems () {
const numberOfItems = 20
if (this.index < this.items.length) {
// Append thumbnails
this.items
.slice(this.index, this.index + numberOfItems)
.forEach(item => {
if (item.video) {
this.element.insertAdjacentHTML('beforeend', `<a data-video='${item.video}'
${item.downloadUrl ? 'data-download-url="' + item.downloadUrl + '"' : ''}> ${item.downloadUrl ? 'data-download-url="' + item.downloadUrl + '"' : ''}>
<img alt="" src="${item.thumbnailUrl}"/><div class="play-icon"></div></a>`) <img alt="" src="${item.thumbnailUrl}"/><div class="play-icon"></div></a>`)
} else { } else {
lgEl.insertAdjacentHTML('beforeend', `<a href="${item.previewUrl}" this.element.insertAdjacentHTML('beforeend', `<a href="${item.previewUrl}"
${item.downloadUrl ? 'data-download-url="' + item.downloadUrl + '"' : ''}> ${item.downloadUrl ? 'data-download-url="' + item.downloadUrl + '"' : ''}>
<img alt="" src="${item.thumbnailUrl}"/></a>`) <img alt="" src="${item.thumbnailUrl}"/></a>`)
}
})
this.lightGallery.refresh()
this.index += numberOfItems
} }
})
lg.refresh()
let timeout
window.addEventListener('scroll', () => {
if (timeout) clearTimeout(timeout)
timeout = setTimeout(handleScroll, 100)
})
}
function handleScroll () {
const scrollPosition = window.innerHeight + window.scrollY
const pageHeight = document.documentElement.scrollHeight
const buffer = 100 // pixels before bottom to trigger load
if (pageHeight - scrollPosition <= buffer) {
loadMoreItems()
} }
} }
const lgallery = new LGallery()
+2 -2
View File
@@ -32,8 +32,8 @@
<script src="/share/static/lg/lg-video.min.js"></script> <script src="/share/static/lg/lg-video.min.js"></script>
<script src="/share/static/lg/lg-zoom.min.js"></script> <script src="/share/static/lg/lg-zoom.min.js"></script>
<script type="text/javascript"> <script type="text/javascript">
initLightGallery(<%- JSON.stringify({lgConfig, items, openItem}) %>) // initLightGallery imported from web.js lgallery.init(<%- JSON.stringify({lgConfig, items, openItem}) %>) // initLightGallery imported from web.js
<% if (false && openItem) { %> <% if (false && openItem) { %>
const openItem = <%- openItem %> const openItem = <%- openItem %>
const thumbs = document.querySelectorAll('#lightgallery a') const thumbs = document.querySelectorAll('#lightgallery a')
if (thumbs.length >= openItem) { if (thumbs.length >= openItem) {