fictioneer/js/ajax-comments.min.js
Tetrakern 0ee8bccd01 Fix breaking bug in AJAX comment section
If commenting requires to be registered and logged in, this cause an error and prevented the comment section from loading.
2024-06-26 15:26:27 +02:00

1 line
3.6 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,m="",a=_$(fictioneer_comments.form_selector??"#comment");if(a&&(m=a.value),fcn_commentSection.classList.contains("ajax-in-progress"))return;if(fcn_commentSection.classList.add("ajax-in-progress"),t||(t=fcn_urlParams.pg??1),n||(n=n??fcn_commentSection.dataset.order??"desc"),!fcn_commentSection)return;const r={action:"fictioneer_ajax_get_comment_section",post_id:e??fcn_commentSection.dataset.postId,page:parseInt(t),corder:n,fcn_fast_comment_ajax:1};fcn_urlParams.commentcode&&(r.commentcode=fcn_urlParams.commentcode),fcn_ajaxGet(r).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(),a=_$(fictioneer_comments.form_selector??"#comment"),a&&!e.data.disabled&&(a.value=m,fcn_applyCommentStack(a)),fcn_addCommentMouseleaveEvents(),fcn_addCommentFormEvents(),fcn_bindAJAXCommentSubmit(),fcn_addJSTrap(),fcn_revealEditButton(),fcn_revealDeleteButton();const r=location.hash.includes("#comment")?location.hash:".respond",s=document.querySelector(r)??_$$$("respond");o&&s.scrollIntoView({behavior:"smooth"});const i=window.location.protocol+"//"+window.location.host+window.location.pathname;let d="";fcn_urlParams.commentcode&&(d+=`?commentcode=${fcn_urlParams.commentcode}`),t>1&&(d+=d.length>1?`&pg=${t}`:`?pg=${t}`),"desc"!=n&&(d+=d.length>1?`&corder=${n}`:`?corder=${n}`),window.history.pushState({path:i},"",i+d+location.hash)}else c=fcn_buildErrorNotice(e.data.error)})).catch((e=>{c=fcn_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")&&(_$(fictioneer_comments.form_selector??"#comment")||(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)}fcn_theRoot.dataset.ajaxAuth?document.addEventListener("fcnAuthReady",(()=>{fcn_setupCommentSectionObserver()})):fcn_setupCommentSectionObserver(),fcn_theRoot.dataset.ajaxAuth?document.addEventListener("fcnAuthReady",(()=>{fcn_loadCommentEarly()})):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)}));