fictioneer/js/comments.min.js
2024-10-13 00:36:43 +02:00

3 lines
14 KiB
JavaScript

function fcn_addJSTrap(){const e=document.querySelector(".comment-form");e&&e.appendChild(fcn_html`
<input type="hidden" name="fictioneer_comment_validator" value="299792458">
`)}function fcn_moderateComment(e,t){const n=_$$$(`comment-${e}`),o=n.querySelector(".mod-menu-toggle-icon");n.classList.contains("ajax-in-progress")||(n.classList.add("ajax-in-progress"),"trash"!=t&&"spam"!=t||(n.style.height=n.clientHeight+"px"),fcn_ajaxPost({action:"fictioneer_ajax_moderate_comment",operation:t,id:e,fcn_fast_comment_ajax:1}).then((e=>{if(e.success)switch(e.data.operation){case"sticky":n.classList.add("_sticky");break;case"unsticky":n.classList.remove("_sticky");break;case"approve":n.classList.remove("_unapproved");break;case"unapprove":n.classList.add("_unapproved");break;case"open":n.classList.remove("_closed");break;case"close":n.classList.add("_closed");break;case"trash":case"spam":n.style.cssText="overflow: hidden; height: 0; margin: 0; opacity: 0;"}else o.classList="fa-solid fa-triangle-exclamation mod-menu-toggle-icon",o.style.color="var(--notice-warning-background)",n.querySelector(".popup-menu-toggle").style.opacity="1",e.data.error&&fcn_showNotification(e.data.error,5,"warning")})).catch((e=>{o.classList="fa-solid fa-triangle-exclamation mod-menu-toggle-icon",o.style.color="var(--notice-warning-background)",n.querySelector(".popup-menu-toggle").style.opacity="1",e.status&&e.statusText?fcn_showNotification(`${e.status}: ${e.statusText}`,5,"warning"):e&&fcn_showNotification(e,5,"warning")})).then((()=>{n.classList.remove("ajax-in-progress"),fcn_lastClicked?.classList.remove("last-clicked"),fcn_lastClicked=null})))}function fcn_addCommentMouseleaveEvents(){_$$(".fictioneer-comment__container").forEach((e=>{e.addEventListener("mouseleave",(e=>{fcn_lastClicked?.classList.remove("last-clicked"),fcn_lastClicked=null,e.stopPropagation()}))}))}function fcn_flagComment(e){if(!fcn_isLoggedIn)return;const t=e.closest(".fictioneer-comment"),n=t.querySelector(".fictioneer-report-comment-button");t.classList.contains("ajax-in-progress")||(t.classList.add("ajax-in-progress"),fcn_ajaxPost({action:"fictioneer_ajax_report_comment",id:t.dataset.id,dubious:n.classList.contains("_dubious"),fcn_fast_comment_ajax:1}).then((e=>{e.success?(n.classList.toggle("on",e.data.flagged),n.classList.remove("_dubious"),e.data.resync&&fcn_showNotification(e.data.resync)):(fcn_showNotification(e.data.failure??e.data.error??fictioneer_tl.notification.error,5,"warning"),(e.data.error||e.data.failure)&&console.error("Error:",e.data.error??e.data.failure))})).catch((e=>{e.status&&e.statusText&&fcn_showNotification(`${e.status}: ${e.statusText}`,5,"warning"),console.error(e)})).then((()=>{t.classList.remove("ajax-in-progress")})))}function fcn_revealCommentFormInputs(e){e.closest("form").querySelectorAll(".fictioneer-respond__form-actions, .fictioneer-respond__form-bottom").forEach((e=>{e.classList.remove("hidden")}))}function fcn_addCommentFormEvents(){_$(fictioneer_comments.form_selector??"#comment")?.addEventListener("focus",(e=>{fcn_revealCommentFormInputs(e.currentTarget)}),{once:!0})}function fcn_textareaAdjust(e){e&&(e.style.height="auto",e.style.height=`${e.scrollHeight}px`)}function fcn_wrapInTag(e,t,n={}){const o=n.href?' href="'+n.href+'" target="_blank" rel="nofollow noreferrer noopener"':"",a=n.shortcode?["[","]"]:["<",">"],c=e.selectionStart,r=e.selectionEnd,i=a[0]+t+o+a[1],s=a[0]+"/"+t+a[1],m=i+e.value.substring(c,r)+s;e.value=e.value.substring(0,c)+m+e.value.substring(r,e.value.length),e.setSelectionRange(c+i.length,r+i.length),e.focus()}function fcn_bindAJAXCommentSubmit(){fcn_theRoot.dataset.ajaxSubmit&&_$$$("commentform")?.addEventListener("submit",(e=>{if(e.preventDefault(),Date.now()<fcn_pageLoadTimestamp+3e3)return;const t=e.currentTarget,n=new FormData(t),o=Object.fromEntries(n.entries()),a=t.querySelector('[name="submit"]'),c=_$(fictioneer_comments.form_selector??"#comment"),r=t.querySelector('[name="author"]'),i=t.querySelector('[name="email"]'),s=t.querySelector('[name="fictioneer-privacy-policy-consent"]'),m=o.comment_parent??0,l=_$$$(`comment-${m}`),d=t.closest(".fictioneer-comments")?.dataset.order??"desc";let f=!0,_=!0,u=!0;if(_=c.value.length>1,c.classList.toggle("_error",!_),s&&(u=s.checked,s.classList.toggle("_error",!u)),i&&i.value.length>0&&(f=/\S+@\S+\.\S+/.test(i.value),i.classList.toggle("_error",!f)),!_||!u||!f)return!1;t.classList.add("ajax-in-progress"),a.disabled=!0,a.value=a.dataset.disabled;const p={action:"fictioneer_ajax_submit_comment",content:c.value,depth:l?parseInt(l.dataset.depth)+1:1,fcn_fast_comment_ajax:1,...o};i?.value&&(p.email=i?.value),r?.value&&(p.author=r?.value),fcn_ajaxPost(p).then((e=>{if(_$$$("comment-submit-error-notice")?.remove(),e.success&&e.data?.comment){let t=_$(".commentlist"),n="insertBefore";if(t&&!l&&t.firstElementChild){let e=null;if(t.firstElementChild.classList.contains("_sticky"))for(e=t.firstElementChild,t=e,n="insertAfter";e.nextElementSibling&&e.nextElementSibling.classList.contains("_sticky");)e=t.nextElementSibling,t=e}if(t||(t=document.createElement("ol"),t.classList="fictioneer-comments__list commentlist",_$$$("comments").appendChild(t),n="append"),l&&(t=l.querySelector(".children"),n="append",!t)){const e=document.createElement("ol");l.appendChild(e),t=e}let o=document.createElement("div");switch(o.innerHTML=e.data.comment,o=o.firstChild,n){case"append":t.appendChild(o);break;case"insertBefore":t.insertBefore(o,t.firstChild);break;case"insertAfter":t.nextSibling?t.parentNode.insertBefore(o,t.nextSibling):t.parentNode.appendChild(o)}fcn_addCommentMouseleaveEvents(),"0"!=_$$$("comment_parent").value&&_$$$("cancel-comment-reply-link").click(),c.value="",c.style.height="";const a=window.location.protocol+"//"+window.location.host+window.location.pathname;("desc"!=d||fcn_urlParams.corder)&&(fcn_urlParams.corder=d),e.data.commentcode&&(fcn_urlParams.commentcode=e.data.commentcode);let r=Object.entries(fcn_urlParams).map((([e,t])=>`${e}=${t}`)).join("&");""!==r&&(r=`?${r}`),history.replaceState({path:a},"",a+r+`#comment-${e.data.comment_id}`),o.scrollIntoView({behavior:"smooth"})}else t.insertBefore(fcn_buildErrorNotice(e.data.failure??e.data.error??fictioneer_tl.notification.error,"comment-submit-error-notice",!1),t.firstChild),e.data.failure&&e.data.error&&console.error("Error:",e.data.error)})).catch((e=>{_$$$("comment-submit-error-notice")?.remove(),t.insertBefore(fcn_buildErrorNotice(`${e.statusText} (${e.status})`,"comment-submit-error-notice"),t.firstChild)})).then((()=>{t.classList.remove("ajax-in-progress"),a.disabled=!1,a.value=a.dataset.enabled}))}))}document.addEventListener("fcnUserDataReady",(()=>{fcn_getUserData().fingerprint==fcn_theRoot.dataset.authorFingerprint&&fcn_theBody.classList.add("is-post-author")})),fcn_addJSTrap(),fcn_addCommentMouseleaveEvents(),fcn_addCommentFormEvents(),_$(".comment-section")?.addEventListener("click",(e=>{const t=e.target.closest("[data-bbcode]");t&&fcn_wrapInTag(_$(fictioneer_comments.form_selector??"#comment"),t.dataset.bbcode,{shortcode:!0})})),_$(".comment-section")?.addEventListener("keydown",(e=>{if(_$(".fictioneer-comment-toolbar")&&"TEXTAREA"===document.activeElement.tagName&&(e.ctrlKey||e.metaKey)){const t=e.key.toLowerCase();if(["b","i","s","q","h","l"].includes(t)){e.preventDefault();const n={q:"quote",h:"spoiler",l:"link"};fcn_wrapInTag(document.activeElement,n[t]||t,{shortcode:!0})}}})),fcn_bindAJAXCommentSubmit();const fcn_commentEditActionsTemplate=_$(".comment-edit-actions-template");var fcn_commentEditUndos={};function fcn_triggerInlineCommentEdit(e){const t=e.closest(".fictioneer-comment");if(t){const e=t.querySelector(".fictioneer-comment__content"),n=t.querySelector(".fictioneer-comment__edit"),o=t.querySelector(".comment-inline-edit-content");n.appendChild(fcn_commentEditActionsTemplate.content.cloneNode(!0)),fcn_commentEditUndos[t.id]=o.value,t.classList.add("_editing"),e.hidden=!0,n.hidden=!1,o.style.height=`${o.scrollHeight}px`}}function fcn_submitInlineCommentEdit(e){const t=e.closest(".fictioneer-comment"),n=t.querySelector(".fictioneer-comment__edit"),o=t.querySelector(".comment-inline-edit-content").value;let a=t.querySelector(".fictioneer-comment__edit-note");o!=fcn_commentEditUndos[t.id]?t&&(n.classList.add("ajax-in-progress"),e.innerHTML=e.dataset.disabled,e.disabled=!0,fcn_ajaxPost({action:"fictioneer_ajax_edit_comment",comment_id:t.id.replace("comment-",""),content:o,fcn_fast_comment_ajax:1}).then((e=>{if(e.success){const n=t.querySelector(".fictioneer-comment__content");n.innerHTML=e.data.content,fcn_restoreComment(t,!1,e.data.raw),a||(a=document.createElement("div")),a.classList.add("fictioneer-comment__edit-note"),a.innerHTML=e.data.edited,n.parentNode.appendChild(a)}else fcn_restoreComment(t,!0),fcn_showNotification(e.data.failure??e.data.error??fictioneer_tl.notification.error,5,"warning"),(e.data.failure||e.data.error)&&console.error("Error:",e.data.error??e.data.failure)})).catch((e=>{fcn_restoreComment(t,!0),e.status&&e.statusText&&fcn_showNotification(`${e.statusText} (${e.status})`,5,"warning"),console.error(e)})).then((()=>{n.classList.remove("ajax-in-progress"),e.innerHTML=e.dataset.enabled,e.disabled=!1}))):fcn_restoreComment(t,!0)}function fcn_cancelInlineCommentEdit(e){const t=e.closest(".fictioneer-comment");t&&fcn_restoreComment(t,!0)}function fcn_restoreComment(e,t=!1,n=null){e.querySelector(".fictioneer-comment__content").hidden=!1,e.querySelector(".fictioneer-comment__edit").hidden=!0,e.querySelector(".fictioneer-comment__edit-actions")?.remove(),e.classList.remove("_editing"),t&&fcn_commentEditUndos[e.id]?e.querySelector(".comment-inline-edit-content").value=fcn_commentEditUndos[e.id]:n&&(e.querySelector(".comment-inline-edit-content").value=n)}function fcn_revealEditButton(){let e=parseInt(fcn_theRoot.dataset.editTime);e&&(e=e>0?6e4*e:e,_$$(".fictioneer-comment[data-fingerprint]").forEach((t=>{if(fcn_matchFingerprint(t.dataset.fingerprint)){if(e>0&&parseInt(t.dataset.timestamp)+e<Date.now())return;const n=t.querySelector(".fictioneer-comment__edit-toggle");n&&(n.hidden=!1)}})))}function fcn_revealDeleteButton(){_$$(".fictioneer-comment[data-fingerprint]").forEach((e=>{if(fcn_matchFingerprint(e.dataset.fingerprint)){const t=e.querySelector(".fictioneer-comment__delete");t&&(t.hidden=!1)}}))}function fcn_deleteMyComment(e){if(!fcn_isLoggedIn)return;const t=prompt(e.dataset.dialogMessage);if(!t||t.toLowerCase()!=e.dataset.dialogConfirm.toLowerCase())return;const n=e.closest(".fictioneer-comment");n.classList.contains("ajax-in-progress")||(n.classList.add("ajax-in-progress"),fcn_ajaxPost({action:"fictioneer_ajax_delete_my_comment",comment_id:n.dataset.id,fcn_fast_comment_ajax:1}).then((e=>{e.success?(n.classList.add("_deleted"),n.querySelector(".fictioneer-comment__container").innerHTML=e.data.html):(fcn_showNotification(e.data.failure??e.data.error??fictioneer_tl.notification.error,5,"warning"),(e.data.error||e.data.failure)&&console.error("Error:",e.data.error??e.data.failure))})).catch((e=>{e.status&&e.statusText&&fcn_showNotification(`${e.status}: ${e.statusText}`,5,"warning"),console.error(e)})).then((()=>{n.classList.remove("ajax-in-progress")})))}document.addEventListener("fcnUserDataReady",(()=>{fcn_revealEditButton()})),document.addEventListener("fcnUserDataReady",(()=>{fcn_revealDeleteButton()}));const fcn_ajaxCommentForm=_$$$("ajax-comment-form-target");function fcn_setupCommentFormObserver(){const e=new IntersectionObserver((([t])=>{t.isIntersecting&&(fcn_getCommentForm(),e.disconnect())}),{rootMargin:"450px",threshold:1});e.observe(fcn_ajaxCommentForm)}function fcn_getCommentForm(){let e;fcn_ajaxGet({action:"fictioneer_ajax_get_comment_form",post_id:_$$$("comments").dataset.postId,fcn_fast_comment_ajax:1}).then((t=>{if(t.success){const e=document.createElement("div");e.innerHTML=t.data.html;const n=e.querySelector("#comment_post_ID"),o=e.querySelector("#cancel-comment-reply-link"),a=e.querySelector(".logout-link");n&&(n.value=t.data.postId),o&&(o.href="#respond"),a&&(a.href=_$$$("comments").dataset.logoutUrl),fcn_ajaxCommentForm.innerHTML=e.innerHTML,e.remove(),fcn_applyCommentStack(),fcn_addCommentFormEvents(),fcn_theRoot.dataset.ajaxSubmit&&fcn_bindAJAXCommentSubmit(),fcn_addNonceHTML(t.data.nonceHtml),fcn_addJSTrap()}else e=fcn_buildErrorNotice(t.data.error)})).catch((t=>{e=fcn_buildErrorNotice(t)})).then((()=>{fcn_ajaxCommentForm.classList.remove("comments-skeleton"),e&&(fcn_ajaxCommentForm.innerHTML="",fcn_ajaxCommentForm.appendChild(e))}))}fcn_ajaxCommentForm&&(fcn_theRoot.dataset.ajaxAuth?document.addEventListener("fcnAuthReady",(()=>{fcn_setupCommentFormObserver()})):fcn_setupCommentFormObserver());var fcn_commentStack=[];function fcn_applyCommentStack(e=null){(e=e??_$(fictioneer_comments.form_selector??"#comment"))&&("TEXTAREA"==e.tagName?(fcn_commentStack.forEach((t=>{e.value+=t})),fcn_textareaAdjust(e)):"DIV"==e.tagName&&fcn_commentStack.forEach((t=>{e.innerHTML+=t})),fcn_commentStack=[])}_$(".fictioneer-comments")?.addEventListener("click",(e=>{const t=e.target.closest("[data-click]");if(t)switch(t?.dataset.click){case"submit-inline-comment-edit":fcn_submitInlineCommentEdit(t);break;case"cancel-inline-comment-edit":fcn_cancelInlineCommentEdit(t);break;case"trigger-inline-comment-edit":fcn_triggerInlineCommentEdit(t);break;case"delete-my-comment":fcn_deleteMyComment(t);break;case"flag-comment":fcn_flagComment(t);break;case"ajax-mod-action":fcn_moderateComment(t.dataset.id,t.dataset.action)}})),_$(".fictioneer-comments")?.addEventListener("input",(e=>{e.target.matches(".adaptive-textarea")&&fcn_textareaAdjust(e.target),e.target.closest('[name="fictioneer-private-comment-toggle"]')&&_$$$("respond")?.classList.toggle("_private",e.currentTarget.checked)}));