56 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
}
arn.loading(true)
button.disabled = true
2018-04-02 05:44:11 +00:00
let target = document.getElementById("load-more-target")
2017-10-17 09:27:15 +00:00
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 === "-1") {
button.disabled = true
2017-10-17 09:27:15 +00:00
button.classList.add("hidden")
} else {
button.dataset.index = newIndex
}
2018-03-29 10:14:46 +00:00
let body = await response.text()
2017-10-17 09:27:15 +00:00
let tmp = document.createElement(target.tagName)
tmp.innerHTML = body
let children = [...tmp.childNodes]
2018-03-29 10:14:46 +00:00
Diff.mutations.queue(() => {
2017-10-17 09:27:15 +00:00
for(let child of children) {
target.appendChild(child)
}
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
}