1 line
5.0 KiB
JavaScript
1 line
5.0 KiB
JavaScript
!function(){"use strict";var e="woocommerce-review-order__item-rating-error";function r(e){var r=Array.prototype.slice.call(e.querySelectorAll(".woocommerce-star-rating__input")),t=e.getAttribute("aria-describedby"),o=t?document.getElementById(t):null;function n(){if(o){var e=r.filter(function(e){return e.checked})[0];o.textContent=e&&e.getAttribute("data-label")||""}}r.forEach(function(e,t){e.addEventListener("change",n),e.addEventListener("keydown",function(e){var o,n=null;switch(e.key){case"ArrowRight":case"ArrowDown":n=(t-1+r.length)%r.length;break;case"ArrowLeft":case"ArrowUp":n=(t+1)%r.length;break;case"Home":n=r.length-1;break;case"End":n=0;break;default:return}e.preventDefault(),(o=r[n]).focus(),o.checked=!0,o.dispatchEvent(new window.Event("change",{bubbles:!0}))})}),n()}function t(e){var r=e.querySelector(".woocommerce-star-rating__input:checked");return r&&parseInt(r.value,10)||0}function o(e){var r=e.querySelector(".woocommerce-review-order__item-review-textarea");return r?(r.value||"").trim():""}function n(e){var r=parseInt(e.getAttribute("data-initial-rating")||"0",10)||0,n=(e.getAttribute("data-initial-text")||"").trim();return t(e)!==r||o(e)!==n}function i(r,t){var o=r.querySelector(".woocommerce-review-order__item-rating");if(o){var n=o.querySelector("."+e);if(t){if(!n){var i=(window.wcOrderReview&&window.wcOrderReview.i18n||{}).rating_required||"Please rate this product before submitting your review.",c=document.createElement("p");c.className=e,c.setAttribute("role","alert"),c.textContent=i,o.appendChild(c)}}else n&&n.parentNode.removeChild(n)}}function c(e,r,t){var o=e.querySelector(".woocommerce-review-order__item-status");o&&o.parentNode.removeChild(o);var n=window.wcOrderReview&&window.wcOrderReview.i18n||{},i={ok:n.ok||"Thanks, your review is live.",pending_moderation:n.pending_moderation||"Thanks, your review is pending approval.",error:n.error||"Something went wrong, please try again."},c=document.createElement("p");c.className="woocommerce-review-order__item-status woocommerce-review-order__item-status--"+r,c.setAttribute("role","status"),c.textContent=t||i[r]||i.error,e.appendChild(c)}function a(e){var r=e.querySelector(".woocommerce-review-order__notice-dismiss");r&&r.addEventListener("click",function(){e.classList.add("woocommerce-review-order__notice--hidden")})}function u(){var u=document.querySelectorAll(".woocommerce-star-rating");Array.prototype.forEach.call(u,r);var d=document.querySelectorAll(".woocommerce-review-order__form");Array.prototype.forEach.call(d,function(r){!function(e){var r=e.querySelector(".woocommerce-review-order__submit");if(r){var t=Array.prototype.slice.call(e.querySelectorAll(".woocommerce-review-order__item"));e.syncReviewOrderSubmitGate=o,e.addEventListener("change",o),e.addEventListener("input",o),o()}else window.console&&window.console.warn&&window.console.warn("Review Order form is missing its submit button (.woocommerce-review-order__submit); the dirty gate will not run.");function o(){r.disabled=!t.some(n)}}(r),function(r,t){var o=r.getAttribute("data-ajax-url");o&&r.addEventListener("submit",function(n){if(n.preventDefault(),t()){var i=r.querySelector(".woocommerce-review-order__submit");i&&(i.disabled=!0),window.fetch(o,{method:"POST",credentials:"same-origin",body:new window.FormData(r)}).then(function(e){return e.json()["catch"](function(){return{success:!1}})}).then(function(e){if(e&&e.success&&e.data){var t=e.data.results||{},o=!1,n=!1;if(Object.keys(t).forEach(function(e){var i=t[e],a=r.querySelector('.woocommerce-review-order__item[data-row-index="'+e+'"]');a&&i&&i.status&&c(a,i.status),i&&i.status&&("ok"===i.status||"pending_moderation"===i.status)?o=!0:n=!0}),o&&!n){var i=r.closest(".woocommerce-review-order");if(i){i.classList.add("is-success");var a=i.querySelector(".woocommerce-review-order__success");a&&(a.hidden=!1),"function"==typeof i.scrollIntoView&&i.scrollIntoView({behavior:"smooth",block:"start"})}}}else Array.prototype.forEach.call(r.querySelectorAll(".woocommerce-review-order__item"),function(e){e.querySelector(".woocommerce-star-rating__input:checked")&&c(e,"error")})})["catch"](function(){Array.prototype.forEach.call(r.querySelectorAll(".woocommerce-review-order__item"),function(e){e.querySelector(".woocommerce-star-rating__input:checked")&&c(e,"error")})}).then(function(){"function"==typeof r.syncReviewOrderSubmitGate?r.syncReviewOrderSubmitGate():i&&(i.disabled=!1)})}else{var a=r.querySelector("."+e);a&&"function"==typeof a.scrollIntoView&&a.scrollIntoView({behavior:"smooth",block:"center"})}})}(r,function(e){var r=Array.prototype.slice.call(e.querySelectorAll(".woocommerce-review-order__item"));return r.forEach(function(e){e.addEventListener("change",function(){(0===o(e).length||t(e)>0)&&i(e,!1)}),e.addEventListener("input",function(){(0===o(e).length||t(e)>0)&&i(e,!1)})}),function(){var e=!0;return r.forEach(function(r){var n=o(r).length>0&&0===t(r);i(r,n),n&&(e=!1)}),e}}(r))});var l=document.querySelectorAll(".woocommerce-review-order__notice");Array.prototype.forEach.call(l,a)}"loading"===document.readyState?document.addEventListener("DOMContentLoaded",u):u()}(); |