55 lines
1.2 KiB
TypeScript
Raw Normal View History

2018-04-02 05:34:16 +00:00
import AnimeNotifier from "../AnimeNotifier"
import Diff from "../Diff"
2017-10-17 09:27:15 +00:00
// Load more
2018-03-29 10:14:46 +00:00
export async function loadMore(arn: AnimeNotifier, button: HTMLButtonElement) {
2017-10-17 09:27:15 +00:00
// Prevent firing this event multiple times
if(arn.isLoading || button.disabled || button.classList.contains("hidden")) {
2017-10-17 09:27:15 +00:00
return
}
const target = document.getElementById("load-more-target")
if(!target) {
return
}
2017-10-17 09:27:15 +00:00
arn.loading(true)
button.disabled = true
let index = button.dataset.index
2018-03-29 10:14:46 +00:00
try {
let response = await fetch("/_" + arn.app.currentPath + "/from/" + index, {
credentials: "same-origin"
})
if(!response.ok) {
throw response.statusText
}
2017-10-17 09:27:15 +00:00
let newIndex = response.headers.get("X-LoadMore-Index")
// End of data?
if(!newIndex || newIndex === "-1") {
button.disabled = true
2017-10-17 09:27:15 +00:00
button.classList.add("hidden")
} else {
button.dataset.index = newIndex
}
// Get the HTML response
let html = await response.text()
2017-10-17 09:27:15 +00:00
// Add the HTML to the existing target
2018-03-29 10:14:46 +00:00
Diff.mutations.queue(() => {
target.insertAdjacentHTML("beforeend", html)
2017-10-17 09:27:15 +00:00
arn.app.emit("DOMContentLoaded")
})
2018-03-29 10:14:46 +00:00
} catch(err) {
arn.statusMessage.showError(err)
} finally {
2017-10-17 09:27:15 +00:00
arn.loading(false)
button.disabled = false
2018-03-29 10:14:46 +00:00
}
2017-10-17 09:27:15 +00:00
}