3 lines
13 KiB
JavaScript
3 lines
13 KiB
JavaScript
function fcn_addJSTrap(){const e=document.querySelector(".comment-form");e&&e.appendChild(fcn_html`
|
|
<input type="hidden" id="fictioneer-comment-validator" 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}).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.overflow="hidden",n.style.height="0",n.style.margin="0",n.style.opacity="0"}else o.classList="fa-solid fa-triangle-exclamation mod-menu-toggle-icon",o.style.color="var(--warning)",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(--warning)",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")}).then((e=>{e.success?(n.classList.toggle("on",e.data.flagged),n.classList.remove("_dubious"),e.data.resync&&fcn_showNotification(e.data.resync)):e.data?.error&&fcn_showNotification(e.data.error,5,"warning")})).catch((e=>{e.status&&e.statusText&&fcn_showNotification(`${e.status}: ${e.statusText}`,5,"warning")})).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(){_$$$("comment")?.addEventListener("focus",(e=>{fcn_revealCommentFormInputs(e.currentTarget)}),{once:!0})}function fcn_textareaAdjust(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"':"",c=n.shortcode?["[","]"]:["<",">"],a=e.selectionStart,i=e.selectionEnd,s=c[0]+t+o+c[1],r=c[0]+"/"+t+c[1],m=s+e.value.substring(a,i)+r;e.value=e.value.substring(0,a)+m+e.value.substring(i,e.value.length),e.setSelectionRange(a+s.length,i+s.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=_$$$("submit"),o=_$$$("comment"),c=_$$$("author"),a=_$$$("email"),i=_$$$("wp-comment-cookies-consent"),s=_$$$("fictioneer-privacy-policy-consent"),r=_$$$("fictioneer-comment-validator"),m=_$$$("comment_parent").value,d=_$$$(`comment-${m}`),l=_$$$("fictioneer-private-comment-toggle"),f=_$$$("fictioneer-comment-notification-toggle");let _=!0,u=!0,p=!0;if(u=o.value.length>1,o.classList.toggle("_error",!u),s&&(p=s.checked,s.classList.toggle("_error",!p)),a&&a.value.length>0&&(_=/\S+@\S+\.\S+/.test(a.value),a.classList.toggle("_error",!_)),!u||!p||!_)return!1;t.classList.add("ajax-in-progress"),n.disabled=!0,n.value=n.dataset.disabled;const g={action:"fictioneer_ajax_submit_comment",post_id:_$$$("comment_post_ID").value,content:o.value,private_comment:l?.checked??0,notification:f?.checked??0,cookie_consent:i?.checked??0,privacy_consent:s?.checked??0,unfiltered_html:_$$$("_wp_unfiltered_html_comment_disabled")?.value??"",depth:d?parseInt(d.dataset.depth)+1:1,fictioneer_comment_validator:r?.value??0};m&&(g.parent_id=m),a?.value&&(g.email=a?.value),c?.value&&(g.author=c?.value),fcn_ajaxPost(g).then((e=>{if(_$$$("comment-submit-error-notice")?.remove(),e.success&&e.data?.comment){let t=_$(".commentlist"),n="insertBefore";if(t&&!d&&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"),d&&(t=d.querySelector(".children"),n="append",!t)){const e=document.createElement("ol");d.appendChild(e),t=e}let c=document.createElement("div");switch(c.innerHTML=e.data.comment,c=c.firstChild,n){case"append":t.appendChild(c);break;case"insertBefore":t.insertBefore(c,t.firstChild);break;case"insertAfter":t.nextSibling?t.parentNode.insertBefore(c,t.nextSibling):t.parentNode.appendChild(c)}fcn_addCommentMouseleaveEvents(),"0"!=_$$$("comment_parent").value&&_$$$("cancel-comment-reply-link").click(),o.value="",o.style.height="";const a=window.location.protocol+"//"+window.location.host+window.location.pathname;let i="";e.data.commentcode&&(i+=`?commentcode=${e.data.commentcode}`),history.pushState({path:a},"",a+i+`#comment-${e.data.comment_id}`),c.scrollIntoView({behavior:"smooth"})}else t.insertBefore(fcn_buildErrorNotice(e.data?.error??__("Error","fictioneer"),"comment-submit-error-notice",!1),t.firstChild)})).catch((e=>{_$$$("comment-submit-error-notice")?.remove(),t.insertBefore(fcn_buildErrorNotice(`${e.status}: ${e.statusText}`,"comment-submit-error-notice"),t.firstChild)})).then((()=>{t.classList.remove("ajax-in-progress"),n.disabled=!1,n.value=n.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(_$$$("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 c=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}).then((e=>{if(e.success){const n=t.querySelector(".fictioneer-comment__content");n.innerHTML=e.data.content,fcn_restoreComment(t,!1,e.data.raw),c||(c=document.createElement("div")),c.classList.add("fictioneer-comment__edit-note"),c.innerHTML=e.data.edited,n.parentNode.appendChild(c)}else fcn_restoreComment(t,!0),e.data?.error&&fcn_showNotification(e.data.error,5,"warning")})).catch((e=>{fcn_restoreComment(t,!0),e.status&&e.statusText&&fcn_showNotification(`${e.status}: ${e.statusText}`,5,"warning")})).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}).then((e=>{e.success?(n.classList.add("_deleted"),n.querySelector(".fictioneer-comment__container").innerHTML=e.data.html):e.data.error&&fcn_showNotification(e.data.error,5,"warning")})).catch((e=>{e.status&&e.statusText&&fcn_showNotification(`${e.status}: ${e.statusText}`,5,"warning")})).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(){fct_commentFormObserver=new IntersectionObserver((([e])=>{e.isIntersecting&&(fcn_getCommentForm(),fct_commentFormObserver.disconnect())}),{rootMargin:"450px",threshold:1}),fct_commentFormObserver.observe(fcn_ajaxCommentForm)}function fcn_getCommentForm(){let e;fcn_ajaxGet({action:"fictioneer_ajax_get_comment_form",post_id:_$$$("comments").dataset.postId}).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"),c=e.querySelector(".logout-link");n&&(n.value=t.data.postId),o&&(o.href="#respond"),c&&(c.href=_$$$("comments").dataset.logoutUrl),fcn_ajaxCommentForm.innerHTML=e.innerHTML,e.remove(),fcn_applyCommentStack(),fcn_addCommentFormEvents(),fcn_theRoot.dataset.ajaxSubmit&&fcn_bindAJAXCommentSubmit(),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??_$$$("comment"))&&(fcn_commentStack.forEach((t=>{e.value+=t})),fcn_commentStack=[],fcn_textareaAdjust(e))}_$(".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("#fictioneer-private-comment-toggle")&&_$$$("respond")?.classList.toggle("_private",e.currentTarget.checked)})); |