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
|
|
|
|
|
2019-11-17 09:25:14 +00:00
|
|
|
const index = button.dataset.index
|
2017-11-20 09:21:16 +00:00
|
|
|
|
2018-03-29 10:14:46 +00:00
|
|
|
try {
|
2019-11-17 09:25:14 +00:00
|
|
|
const response = await fetch("/_" + arn.app.currentPath + "/from/" + index, {
|
2018-03-29 10:14:46 +00:00
|
|
|
credentials: "same-origin"
|
|
|
|
})
|
|
|
|
|
|
|
|
if(!response.ok) {
|
|
|
|
throw response.statusText
|
|
|
|
}
|
|
|
|
|
2019-11-17 09:25:14 +00:00
|
|
|
const newIndex = response.headers.get("X-LoadMore-Index")
|
2017-10-17 09:27:15 +00:00
|
|
|
|
|
|
|
// 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
|
2019-11-17 09:25:14 +00:00
|
|
|
const 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
|
|
|
}
|
2019-11-17 09:44:30 +00:00
|
|
|
}
|