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
|
2018-01-25 15:52:09 +00:00
|
|
|
if(arn.isLoading || button.disabled || button.classList.contains("hidden")) {
|
2017-10-17 09:27:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-19 14:34:49 +00:00
|
|
|
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
|
2017-11-20 09:21:16 +00:00
|
|
|
|
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?
|
2019-04-19 14:34:49 +00:00
|
|
|
if(!newIndex || newIndex === "-1") {
|
2017-11-20 09:21:16 +00:00
|
|
|
button.disabled = true
|
2017-10-17 09:27:15 +00:00
|
|
|
button.classList.add("hidden")
|
|
|
|
} else {
|
|
|
|
button.dataset.index = newIndex
|
|
|
|
}
|
2017-11-20 09:21:16 +00:00
|
|
|
|
2019-04-19 14:34:49 +00:00
|
|
|
// Get the HTML response
|
|
|
|
let html = await response.text()
|
2017-10-17 09:27:15 +00:00
|
|
|
|
2019-04-19 14:34:49 +00:00
|
|
|
// Add the HTML to the existing target
|
2018-03-29 10:14:46 +00:00
|
|
|
Diff.mutations.queue(() => {
|
2019-04-19 14:34:49 +00:00
|
|
|
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
|
|
|
}
|