fictioneer/js/ajax-comments.min.js
Tetrakern 64bb7653fc Use global handler for some click events
Gets around the problem with AJAX content that needs events to be rebound.
2023-06-09 11:30:31 +02:00

1 line
2.9 KiB
JavaScript

const fcn_ajaxCommentsSection=_$$$("comments");var fct_commentsObserver,fcn_commentStack=[];function fcn_getCommentSection(e=null,n=null,t=!1){if(!fcn_ajaxCommentsSection)return;let o,c=_$$$("comments"),a="",m=_$$$("comment");if(m&&(a=m.value),fcn_ajaxCommentsSection.classList.contains("ajax-in-progress"))return;if(fcn_ajaxCommentsSection.classList.add("ajax-in-progress"),n||(n=fcn_urlParams.pg??1),!fcn_ajaxCommentsSection)return;let r={action:"fictioneer_ajax_get_comment_section",post_id:e??fcn_ajaxCommentsSection.dataset.postId,page:n};fcn_urlParams.commentcode&&(r.commentcode=fcn_urlParams.commentcode),fcn_ajaxGet(r).then((e=>{if(e.success){n=e.data.page;let o=document.createElement("div");if(o.innerHTML=e.data.html,o.querySelector("#comment_post_ID")){o.querySelector("#comment_post_ID").value=e.data.postId,o.querySelector("#cancel-comment-reply-link").href="#respond";let n=o.querySelector(".logout-link");n&&(n.href=_$$$("comments").dataset.logoutUrl)}c.innerHTML=o.innerHTML,o.remove(),m=_$$$("comment"),m&&(m.value=a),m&&fcn_commentStack.forEach((e=>{m.value+=e})),fcn_commentStack=[],m&&fcn_textareaAdjust(m),fcn_addModerationEvents(),fcn_addCommentMouseleaveEvents(),fcn_addTextareaEvents(),fcn_addPrivateToggleEvents(),fcn_bindAJAXCommentSubmit(),fcn_addJSTrap(),fcn_revealEditButton();let r=location.hash.includes("#comment")?location.hash:".respond",s=document.querySelector(r)??_$$$("respond");t&&s.scrollIntoView({behavior:"smooth"});let i=window.location.protocol+"//"+window.location.host+window.location.pathname,l="";fcn_urlParams.commentcode&&(l+=`?commentcode=${fcn_urlParams.commentcode}`),n>1&&(l+=l.length>1?`&pg=${n}`:`?pg=${n}`),window.history.pushState({path:i},"",i+l+location.hash)}else o=fcn_buildErrorNotice(e.data.error)})).catch((e=>{o=fcn_buildErrorNotice(e)})).then((()=>{fcn_ajaxCommentsSection.classList.remove("ajax-in-progress"),o&&(c.innerHTML="",c.appendChild(o))}))}function fcn_reloadCommentsPage(e=null){fcn_getCommentSection(null,e,!0)}function fcn_jumpToCommentPage(){let e=parseInt(window.prompt(_x("Enter page number:","Pagination jump prompt.","fictioneer")));e>0&&fcn_reloadCommentsPage(e)}function fcn_setupCommentObserver(){fct_commentsObserver=new IntersectionObserver((([e])=>{e.isIntersecting&&(fcn_getCommentSection(),fct_commentsObserver.disconnect())}),{rootMargin:"400px",threshold:1}),fcn_ajaxCommentsSection&&fct_commentsObserver.observe(fcn_ajaxCommentsSection)}function fcn_loadCommentEarly(){_$$$("comments")&&location.hash.includes("#comment")&&(_$$$("comment")||(fct_commentsObserver.disconnect(),fcn_reloadCommentsPage()))}fcn_theRoot.dataset.ajaxNonce&&!_$$$("fictioneer-ajax-nonce")?fcn_theRoot.addEventListener("nonceReady",(()=>{fcn_setupCommentObserver()})):fcn_setupCommentObserver(),fcn_theRoot.dataset.ajaxNonce&&!_$$$("fictioneer-ajax-nonce")?fcn_theRoot.addEventListener("nonceReady",(()=>{fcn_loadCommentEarly()})):fcn_loadCommentEarly();