fictioneer/js/comments.min.js
2023-02-16 22:31:31 +01:00

1 line
12 KiB
JavaScript

function fcn_addJSTrap(){let e=document.querySelector(".comment-form");if(e){let t=document.createElement("input");t.setAttribute("type","hidden"),t.id="fictioneer-comment-validator",t.setAttribute("name","fictioneer_comment_validator"),t.setAttribute("value","299792458"),e.appendChild(t)}}function fcn_moderateComment(e,t){let n=_$$$(`comment-${e}`),o=n.querySelector(".fictioneer-mod-menu-toggle");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":o.onclick="",n.style.overflow="hidden",n.style.height="0",n.style.margin="0",n.style.opacity="0"}else o.innerHTML='<i class="fa-solid fa-triangle-exclamation"></i>',o.style.color="var(--warning)",o.style.opacity="1",o.onclick="",e.data.error&&fcn_showNotification(e.data.error,5,"warning")})).catch((e=>{o.innerHTML='<i class="fa-solid fa-triangle-exclamation"></i>',o.style.color="var(--warning)",o.style.opacity="1",o.onclick="",e.status&&e.statusText&&fcn_showNotification(`${e.status}: ${e.statusText}`,5,"warning")})).then((()=>{n.classList.remove("ajax-in-progress")})))}function fcn_addModerationMenuEvents(e=document){e.querySelectorAll(".toggle-last-clicked").forEach((e=>{e.addEventListener("click",(e=>{fcn_toggleLastClicked(e.currentTarget),e.stopPropagation()}))}))}function fcn_addModerationEvents(){_$$(".button-ajax-moderate-comment").forEach((e=>{e.addEventListener("click",(e=>{fcn_moderateComment(e.currentTarget.dataset.id,e.currentTarget.dataset.action)}))}))}function fcn_addCommentMouseleaveEvents(){_$$(".fictioneer-comment__container").forEach((e=>{e.addEventListener("mouseleave",(e=>{fcn_lastClicked&&fcn_lastClicked.classList.remove("last-clicked"),fcn_lastClicked=null,e.stopPropagation()}))}))}function fcn_flagComment(e){if(!fcn_isLoggedIn)return;let t=e.closest(".fictioneer-comment"),n=t.querySelector(".fictioneer-report-comment-button");if(t.classList.contains("ajax-in-progress"))return;t.classList.add("ajax-in-progress");let o={action:"fictioneer_ajax_report_comment",id:t.dataset.id,dubious:n.classList.contains("_dubious")};fcn_ajaxPost(o).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")})),e.removeAttribute("onclick")}function fcn_textareaAdjust(e){e.style.height="auto",e.style.height=e.scrollHeight+"px"}function fcn_addTextareaEvents(){_$$(".adaptive-textarea").forEach((e=>{e.addEventListener("input",(e=>{fcn_textareaAdjust(e.currentTarget)}))}))}function fcn_addPrivateToggleEvents(){_$$$("fictioneer-private-comment-toggle")?.addEventListener("change",(e=>{_$$$("respond")?.classList.toggle("_private",e.currentTarget.checked)}))}function fcn_wrapInTag(e,t,n={}){let o=n.href?' href="'+n.href+'" target="_blank" rel="nofollow noreferrer noopener"':"",a=n.shortcode?["[","]"]:["<",">"],c=e.selectionStart,i=e.selectionEnd,r=a[0]+t+o+a[1],s=a[0]+"/"+t+a[1],m=r+e.value.substring(c,i)+s;e.value=e.value.substring(0,c)+m+e.value.substring(i,e.value.length),e.setSelectionRange(c+r.length,i+r.length),e.focus()}function fcn_commentMakeBold(){fcn_wrapInTag(_$$$("comment"),"b",{shortcode:!0})}function fcn_commentMakeItalic(){fcn_wrapInTag(_$$$("comment"),"i",{shortcode:!0})}function fcn_commentMakeStrike(){fcn_wrapInTag(_$$$("comment"),"s",{shortcode:!0})}function fcn_commentMakeLink(){fcn_wrapInTag(_$$$("comment"),"link",{shortcode:!0})}function fcn_commentMakeQuote(){fcn_wrapInTag(_$$$("comment"),"quote",{shortcode:!0})}function fcn_commentMakeSpoiler(){fcn_wrapInTag(_$$$("comment"),"spoiler",{shortcode:!0})}function fcn_commentMakeImage(){fcn_wrapInTag(_$$$("comment"),"img",{shortcode:!0})}function fcn_bindAJAXCommentSubmit(){fcn_theRoot.dataset.ajaxSubmit&&"true"==fcn_theRoot.dataset.ajaxSubmit&&_$$$("commentform")?.addEventListener("submit",(e=>{if(e.preventDefault(),Date.now()<fcn_pageLoadTimestamp+3e3)return;let t=e.currentTarget,n=_$$$("submit"),o=_$$$("comment"),a=_$$$("author"),c=_$$$("email"),i=_$$$("wp-comment-cookies-consent"),r=_$$$("fictioneer-privacy-policy-consent"),s=_$$$("fictioneer-comment-validator"),m=_$$$("comment_parent").value,l=_$$$(`comment-${m}`),d=_$$$("fictioneer-private-comment-toggle"),f=_$$$("fictioneer-comment-notification-toggle"),_=!0,u=!0,g=!0;if(u=o.value.length>1,o.classList.toggle("_error",!u),r&&(g=r.checked),r?.classList.toggle("_error",!g),c&&c.value.length>0&&(_=/\S+@\S+\.\S+/.test(c.value)),c?.classList.toggle("_error",!_),!u||!g||!_)return!1;t.classList.add("ajax-in-progress"),n.disabled=!0,n.value=n.dataset.disabled;let p={action:"fictioneer_ajax_submit_comment",post_id:_$$$("comment_post_ID").value,content:o.value,private_comment:d?.checked??0,notification:f?.checked??0,cookie_consent:i?.checked??0,privacy_consent:r?.checked??0,unfiltered_html:_$$$("_wp_unfiltered_html_comment_disabled")?.value??"",depth:l?parseInt(l.dataset.depth)+1:1,fictioneer_comment_validator:s?.value??0};m&&(p.parent_id=m),c?.value&&(p.email=c?.value),a?.value&&(p.author=a?.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){let e=document.createElement("ol");e.classList="fictioneer-comments__list commentlist",_$$$("comments").appendChild(e),n="append",t=e}if(l&&(t=l.querySelector(".children"),n="append",!t)){let e=document.createElement("ol");l.appendChild(e),t=e}let a=document.createElement("div");switch(a.innerHTML=e.data.comment,a=a.firstChild,n){case"append":t.appendChild(a);break;case"insertBefore":t.insertBefore(a,t.firstChild);break;case"insertAfter":t.nextSibling?t.parentNode.insertBefore(a,t.nextSibling):t.parentNode.appendChild(a)}fcn_addModerationMenuEvents(a),fcn_addModerationEvents(),fcn_addCommentMouseleaveEvents(),"0"!=_$$$("comment_parent").value&&_$$$("cancel-comment-reply-link").click(),o.value="",o.style.height="";let c=window.location.protocol+"//"+window.location.host+window.location.pathname,i="";e.data.commentcode&&(i+=`?commentcode=${e.data.commentcode}`),history.pushState({path:c},"",c+i+`#comment-${e.data.comment_id}`),a.scrollIntoView({behavior:"smooth"})}else{let n=e.data?.error??__("Error","fictioneer");t.insertBefore(fcn_buildErrorNotice(n,"comment-submit-error-notice"),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}))}))}fcn_addJSTrap(),fcn_addModerationEvents(),fcn_addCommentMouseleaveEvents(),fcn_addTextareaEvents(),fcn_addPrivateToggleEvents(),fcn_bindAJAXCommentSubmit();var fcn_commentEditUndos={};function fcn_triggerInlineCommentEdit(e){let t=e.closest(".fictioneer-comment");if(t){let e=t.querySelector(".fictioneer-comment__content"),n=t.querySelector(".fictioneer-comment__edit"),o=t.querySelector(".comment-inline-edit-content");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){let t=e.closest(".fictioneer-comment"),n=t.querySelector(".fictioneer-comment__edit"),o=t.querySelector(".fictioneer-comment__edit-note"),a=t.querySelector(".comment-inline-edit-content").value;if(a!=fcn_commentEditUndos[t.id]){if(t){let c={action:"fictioneer_ajax_edit_comment",comment_id:t.id.replace("comment-",""),content:a};n.classList.add("ajax-in-progress"),e.innerHTML=e.dataset.disabled,e.disabled=!0,fcn_ajaxPost(c).then((e=>{if(e.success){let n=t.querySelector(".fictioneer-comment__content");n.innerHTML=e.data.content,fcn_restoreComment(t,!1,e.data.raw),o||(o=document.createElement("div")),o.classList.add("fictioneer-comment__edit-note"),o.innerHTML=e.data.edited,n.parentNode.appendChild(o)}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}))}}else fcn_restoreComment(t,!0)}function fcn_cancelInlineCommentEdit(e){let 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.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;let 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)){let t=e.querySelector(".fictioneer-comment__delete");t&&(t.hidden=!1)}}))}function fcn_deleteMyComment(e){if(!fcn_isLoggedIn)return;let t=prompt(e.dataset.dialogMessage);if(!t||t.toLowerCase()!=e.dataset.dialogConfirm.toLowerCase())return;let n=e.closest(".fictioneer-comment");if(n.classList.contains("ajax-in-progress"))return;n.classList.add("ajax-in-progress");let o={action:"fictioneer_ajax_delete_my_comment",comment_id:n.dataset.id};fcn_ajaxPost(o).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")}))}fcn_isLoggedIn&&fcn_revealEditButton(),fcn_isLoggedIn&&fcn_revealDeleteButton();const fcn_ajaxCommentForm=_$$$("ajax-comment-form-target");function fcn_getCommentForm(){let e,t={action:"fictioneer_ajax_get_comment_form",post_id:_$$$("comments").dataset.postId};fcn_ajaxGet(t).then((t=>{if(t.success){let e=document.createElement("div");e.innerHTML=t.data.html;let 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_addTextareaEvents(),fcn_addPrivateToggleEvents(),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.ajaxNonce&&!_$$$("fictioneer-ajax-nonce")?fcn_theRoot.addEventListener("nonceReady",(()=>{fcn_getCommentForm()})):fcn_getCommentForm());