fictioneer/js/ajax-comments.min.js
2024-12-06 17:38:32 +01:00

1 line
3.4 KiB
JavaScript

const fcn_commentSection=_$("#comments[data-ajax-comments]");function fcn_getCommentSection(e=null,t=null,n=null,o=!1){if(!fcn_commentSection)return;let c,a="",r=_$(FcnGlobals.commentFormSelector);if(r&&(a=r.value),fcn_commentSection.classList.contains("ajax-in-progress"))return;if(fcn_commentSection.classList.add("ajax-in-progress"),t||(t=FcnGlobals.urlParams.pg??1),n||(n=n??fcn_commentSection.dataset.order??"desc"),!fcn_commentSection)return;const m={action:"fictioneer_ajax_get_comment_section",post_id:e??fcn_commentSection.dataset.postId,page:parseInt(t),corder:n,fcn_fast_comment_ajax:1};FcnGlobals.urlParams.commentcode&&(m.commentcode=FcnGlobals.urlParams.commentcode),FcnUtils.aGet(m).then((e=>{if(e.success){t=e.data.page,fcn_commentSection.dataset.page=t;const c=document.createElement("div");if(c.innerHTML=e.data.html,c.querySelector("#comment_post_ID")){c.querySelector("#comment_post_ID").value=e.data.postId,c.querySelector("#cancel-comment-reply-link").href="#respond";const t=c.querySelector(".logout-link");t&&(t.href=fcn_commentSection.dataset.logoutUrl)}fcn_commentSection.innerHTML=c.innerHTML,c.remove(),r=_$(FcnGlobals.commentFormSelector),r&&!e.data.disabled&&(r.value=a,fcn_applyCommentStack(r));const m=location.hash.includes("#comment")?location.hash:".respond",s=document.querySelector(m)??_$$$("respond");o&&s.scrollIntoView({behavior:"smooth"});const l=window.location.protocol+"//"+window.location.host+window.location.pathname;(t>1||FcnGlobals.urlParams.pg)&&(FcnGlobals.urlParams.pg=t),("desc"!=n||FcnGlobals.urlParams.corder)&&(FcnGlobals.urlParams.corder=n);let i=Object.entries(FcnGlobals.urlParams).map((([e,t])=>`${e}=${t}`)).join("&");""!==i&&(i=`?${i}`),window.history.pushState({path:l},"",l+i+location.hash)}else c=FcnUtils.buildErrorNotice(e.data.error)})).catch((e=>{c=FcnUtils.buildErrorNotice(e)})).then((()=>{fcn_commentSection.classList.remove("ajax-in-progress"),c&&(fcn_commentSection.innerHTML="",fcn_commentSection.appendChild(c))}))}function fcn_reloadCommentsPage(e=null){fcn_getCommentSection(null,e,null,!0)}function fcn_jumpToCommentPage(){const e=parseInt(window.prompt(fictioneer_tl.notification.enterPageNumber));e>0&&fcn_reloadCommentsPage(e)}var fct_commentSectionObserver;function fcn_setupCommentSectionObserver(){fct_commentSectionObserver=new IntersectionObserver((([e])=>{e.isIntersecting&&(fcn_getCommentSection(),fct_commentSectionObserver.disconnect())}),{rootMargin:"450px",threshold:1}),fcn_commentSection&&fct_commentSectionObserver.observe(fcn_commentSection)}function fcn_loadCommentEarly(){fcn_commentSection&&location.hash.includes("#comment")&&(_$(FcnGlobals.commentFormSelector)||(fct_commentSectionObserver.disconnect(),fcn_reloadCommentsPage()))}function fcn_toggleCommentOrder(e){const t=e.closest(".fictioneer-comments"),n="desc"==t.dataset.order;t.dataset.order=n?"asc":"desc",e.classList.toggle("_on",!n),e.classList.toggle("_off",n),fcn_reloadCommentsPage(t.dataset.page)}document.addEventListener("fcnUserDataReady",(()=>{fcn_setupCommentSectionObserver()})),document.addEventListener("fcnUserDataReady",(()=>{fcn_loadCommentEarly()})),_$(".fictioneer-comments")?.addEventListener("click",(e=>{if(e.target.closest("button[data-page-jump]"))return void fcn_jumpToCommentPage();const t=e.target.closest("button[data-page]");if(t)return void fcn_reloadCommentsPage(t.dataset.page);const n=e.target.closest("button[data-toggle-order]");n&&fcn_toggleCommentOrder(n)}));