export class farallonComment { loading: boolean = false; post_id: any; total: any = 0; total_paged: any = 1; paged: any = 1; constructor() { if (!document.querySelector(".post--ingle__comments")) return; this.post_id = ( document.querySelector(".post--ingle__comments") as HTMLElement ).dataset.id; this.fetchComments(); this.init(); } fetchComments() { fetch( // @ts-ignore window.commentDomain + "/post/" + this.post_id + "/comments?paged=" + this.paged ).then((res) => { res.json().then((data) => { const comments = data.payload.comments; this.total = data.payload.total; this.total_paged = data.payload.total_paged; if (this.total_paged > 1) { this.randerNav(); } document.querySelector(".comments--title .count")!.innerHTML = this.total; const html = comments .map((item: any) => { let children = ""; if (item.children) { children = `
    ${item.children .map((i) => { return `
  1. ${i.avatar}
    ${i.comment_text}
  2. `; }) .join("")}
`; } return `
  • ${item.avatar}
    ${item.comment_text}
    ${children}
  • `; }) .join(""); document.querySelector(".commentlist ")!.innerHTML = html; }); }); } randerNav() { const nextDisabled = this.paged == 1 ? "disabled" : ""; const preDisabled = this.paged == this.total_paged ? "disabled" : ""; const html = ` Older CommentsNewer Comments `; document.querySelector(".commentnav")!.innerHTML = html; document.querySelectorAll(".cnav-item").forEach((item) => { item.addEventListener("click", (e) => { if (item.classList.contains("disabled")) return; console.log(item); const action = item.attributes["data-action"].value; console.log(action); if (action == "pre") { this.paged += 1; } else { this.paged -= 1; } this.fetchComments(); }); }); } private init() { if (document.querySelector(".comment-form")) { document .querySelector(".comment-form") ?.addEventListener("submit", (e) => { e.preventDefault(); if (this.loading) return; const form = document.querySelector( ".comment-form" ) as HTMLFormElement; // @ts-ignore const formData = new FormData(form); // @ts-ignore const formDataObj: { [index: string]: any } = {}; formData.forEach( (value, key: any) => (formDataObj[key] = value) ); this.loading = true; fetch( // @ts-ignore window.commentDomain + "/post/" + this.post_id + "/comment", { method: "POST", body: JSON.stringify(formDataObj), headers: { // @ts-ignore // "X-WP-Nonce": obvInit.nonce, "Content-Type": "application/json", }, } ) .then((response) => { return response.json(); }) .then((data) => { this.loading = false; if (data.status != 200) { return; //this.showNotice(data.message, "error"); } let a = document.getElementById( "cancel-comment-reply-link" ), i = document.getElementById("respond"), n = document.getElementById("wp-temp-form-div"); const comment = data.data; const html = `
  • ${comment.avatar}
    ${comment.comment_author}
    ${comment.comment_text}
  • `; // @ts-ignore const parent_id = ( document.querySelector( "#comment_parent" ) as HTMLInputElement )?.value; // @ts-ignore (a.style.display = "none"), // @ts-ignore (a.onclick = null), // @ts-ignore (( document.getElementById( "comment_parent" ) as HTMLInputElement ).value = "0"), n && // @ts-ignore i && // @ts-ignore n.parentNode && n.parentNode.removeChild(n); if (document.querySelector(".comment-body__fresh")) document .querySelector(".comment-body__fresh") ?.classList.remove("comment-body__fresh"); // @ts-ignore document.getElementById("comment").value = ""; // @ts-ignore if (parent_id != "0") { document .querySelector( // @ts-ignore "#comment-" + parent_id ) ?.insertAdjacentHTML( "beforeend", '
      ' + html + "
    " ); console.log(parent_id); } else { if (document.querySelector(".no--comment")) { document .querySelector(".no--comment") ?.remove(); } document .querySelector(".commentlist") ?.insertAdjacentHTML("beforeend", html); } const newComment = document.querySelector( `#comment-${comment.comment_ID}` ) as HTMLElement; if (newComment) { newComment.scrollIntoView({ behavior: "smooth", }); } // this.showNotice("评论成功"); }); }); } } }