From 4841bc4abe66608a8ce71f64de2dcddc4a6af8f8 Mon Sep 17 00:00:00 2001 From: ThaUnknown Date: Tue, 5 Jan 2021 09:26:15 +0100 Subject: [PATCH] is this good code?... --- app/js/interface.js | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/app/js/interface.js b/app/js/interface.js index eac9775..95beda8 100644 --- a/app/js/interface.js +++ b/app/js/interface.js @@ -2,20 +2,20 @@ async function loadHomePage() { let homeLoadElements = [homeContinueMore, homeReleasesMore, homePlanningMore, homeTrendingMore], homePreviewElements = [homeContinue, homeReleases, homePlanning, homeTrending], homeLoadFunctions = { - continue: async function () { - let res = await alRequest({ method: "UserLists", status_in: "CURRENT", id: alID }) - galleryAppend({ media: res.data.Page.mediaList.map(i => i.media), gallery: document.querySelector(".browse") }) + continue: async function (page) { + let res = await alRequest({ method: "UserLists", status_in: "CURRENT", id: alID, page: page || 1 }) + galleryAppend({ media: res.data.Page.mediaList.map(i => i.media), gallery: document.querySelector(".browse"), method: "continue", page: page || 1 }) }, releases: async function () { }, - planning: async function () { - let res = await alRequest({ method: "UserLists", status_in: "PLANNING", id: alID }) - galleryAppend({ media: res.data.Page.mediaList.map(i => i.media), gallery: document.querySelector(".browse") }) + planning: async function (page) { + let res = await alRequest({ method: "UserLists", status_in: "PLANNING", id: alID, page: page || 1 }) + galleryAppend({ media: res.data.Page.mediaList.map(i => i.media), gallery: document.querySelector(".browse"), method: "planning", page: page || 1 }) }, - trending: async function () { - let res = await alRequest({ method: "Trending", id: alID }) - galleryAppend({ media: res.data.Page.media, gallery: document.querySelector(".browse") }) + trending: async function (page) { + let res = await alRequest({ method: "Trending", id: alID, page: page || 1 }) + galleryAppend({ media: res.data.Page.media, gallery: document.querySelector(".browse"), method: "trending", page: page || 1 }) } }, homePreviewFunctions = { @@ -34,18 +34,28 @@ async function loadHomePage() { let res = await alRequest({ method: "Trending", id: alID, perPage: 4 }) galleryAppend({ media: res.data.Page.media, gallery: homeTrending }) } - } + }, + loadTimeout function galleryAppend(opts) { //TODO: add skeleton loading - let frag = document.createDocumentFragment() - opts.media.forEach(media => { - let template = cardCreator(media) - template.onclick = () => viewAnime(media) - frag.appendChild(template) + function appendFrag(media) { + let frag = document.createDocumentFragment() + media.forEach(media => { + let template = cardCreator(media) + template.onclick = () => viewAnime(media) + frag.appendChild(template) + }) + opts.gallery.appendChild(frag) + } + if (opts.method) opts.gallery.addEventListener("scroll", function () { + if (this.scrollTop + this.clientHeight > this.scrollHeight - 800 && !loadTimeout) { + loadTimeout = setTimeout(function () { loadTimeout = undefined }, 1000) + homeLoadFunctions[opts.method](opts.page + 1) + } }) - opts.gallery.textContent = ''; - opts.gallery.appendChild(frag) + if (!opts.page || opts.page == 1) opts.gallery.textContent = ''; + appendFrag(opts.media) } for (let item of homePreviewElements) {