/* @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0 @license-end */ import { BaseController } from "./base_controller.js"; export class PaginationController extends BaseController { constructor(host, context, path, replies_controller) { super(); this.replies_controller = replies_controller; this.endpoint = `${host}${context}?path=${path}`; this.comments_node = document.getElementById("comments-thread"); } populatePageAnchors() { const pagination_widget = document.querySelector(".pagination"); var anchors = pagination_widget.querySelectorAll('a'); for (let anchor of anchors) { var page = /\d/.exec(anchor.id)[0]; anchor.setAttribute("href", `${this.endpoint}&page=${page}`); } this.listenForPageChange(anchors); } listenForPageChange(page_selectors) { for (let anchor of page_selectors) { anchor.addEventListener("click", this.changeCommentsPage.bind(this)) } } changeCommentsPage(event) { event.preventDefault(); fetch(event.target.getAttribute("href")) .then((response) => { return response.text(); }) .then((data) => { this.loadComments(this.htmlFromResponse(data)); }); } loadComments(body) { const previous_comments = document.getElementById("comment-section"); const comments = body?.getElementById("comment-section"); if(comments) { previous_comments.remove(); this.comments_node.appendChild(comments); this.replies_controller.init(); this.populatePageAnchors(); } } }