1 line
No EOL
5.1 KiB
JavaScript
1 line
No EOL
5.1 KiB
JavaScript
window.addEventListener("load",function(t){astrawpWooQuantityButtons(),quantityInput()});const astraminiCarttargetNode=document.getElementById("ast-site-header-cart");if(null!=astraminiCarttargetNode){const b={attributes:!1,childList:!0,subtree:!0},c=()=>{astrawpWooQuantityButtons(),quantityInput()},d=new MutationObserver(c);d.observe(astraminiCarttargetNode,b)}function astrawpWooQuantityButtons(c){document.querySelector(".woocommerce div.product form.cart");c=c||".qty",$quantityBoxesWrap=document.querySelectorAll("div.quantity:not(.elementor-widget-woocommerce-cart .quantity):not(.buttons_added), td.quantity:not(.elementor-widget-woocommerce-cart .quantity):not(.buttons_added)");for(var t=0;t<$quantityBoxesWrap.length;t++){var e=$quantityBoxesWrap[t],a=e.querySelector(c);if(a&&"date"!==a.getAttribute("type")&&"hidden"!==a.getAttribute("type")){switch(($qty_parent=a.parentElement).classList.add("buttons_added"),astra_qty_btn.style_type){case"no-internal-border":a.classList.add("ast-no-internal-border"),$qty_parent.insertAdjacentHTML("afterbegin",'<label class="screen-reader-text" for="minus_qty">'+astra_qty_btn.minus_qty+'</label><a href="javascript:void(0)" id ="minus_qty" class="minus no-internal-border">-</a>'),$qty_parent.insertAdjacentHTML("beforeend",'<label class="screen-reader-text" for="plus_qty"> '+astra_qty_btn.plus_qty+'</label><a href="javascript:void(0)" id ="plus_qty" class="plus no-internal-border">+</a> ');break;case"vertical-icon":$qty_parent.classList.add("ast-vertical-style-applied"),a.classList.add("vertical-icons-applied"),$qty_parent.insertAdjacentHTML("beforeend",'<label class="screen-reader-text" for="plus_qty"> '+astra_qty_btn.plus_qty+'</label><a href="javascript:void(0)" id ="plus_qty" class="plus ast-vertical-icon">+</a><label class="screen-reader-text" for="minus_qty">'+astra_qty_btn.minus_qty+'</label><a href="javascript:void(0)" id ="minus_qty" class="minus ast-vertical-icon">-</a>');break;default:$qty_parent.insertAdjacentHTML("afterbegin",'<label class="screen-reader-text" for="minus_qty">'+astra_qty_btn.minus_qty+'</label><a href="javascript:void(0)" id ="minus_qty" class="minus">-</a>'),$qty_parent.insertAdjacentHTML("beforeend",'<label class="screen-reader-text" for="plus_qty"> '+astra_qty_btn.plus_qty+'</label><a href="javascript:void(0)" id ="plus_qty" class="plus">+</a>')}$quantityEach=document.querySelectorAll("input"+c+":not(.product-quantity)");for(var n=0;n<$quantityEach.length;n++){var r=$quantityEach[n],s=r.getAttribute("min");s&&0<s&&parseFloat(r.value)<s&&(r.value=s)}var i=document.getElementsByTagName("BODY")[0],o=document.getElementsByClassName("cart")[0];if(i.classList.contains("single-product")&&!o.classList.contains("grouped_form")){let e=document.querySelector(".woocommerce input[type=number].qty");e&&e.addEventListener("keyup",function(){var t=e.value;e.value=t})}for(var u=e.querySelectorAll(".plus, .minus"),l=0;l<u.length;l++)u[l].addEventListener("click",function(t){var e,a=t.target.parentElement.querySelector(c),n=parseFloat(a.value),r=parseFloat(a.getAttribute("max")),s=parseFloat(a.getAttribute("min")),i=parseFloat(a.getAttribute("step")),o=Number.isInteger(i),r=(n&&""!==n&&"NaN"!==n||(n=0),""!==r&&"NaN"!==r||(r=""),""!==s&&"NaN"!==s||(s=0),"any"!==i&&""!==i&&void 0!==i&&"NaN"!==i||(i=1),t.target.classList.contains("plus")?r&&(r==n||r<n)?a.value=r:(e=n+parseFloat(i),a.value=o?e:e.toFixed(1)):s&&(s==n||n<s)?a.value=s:0<n&&(e=n-parseFloat(i),a.value=o?e:e.toFixed(1)),new Event("change",{bubbles:!0})),u=(a.dispatchEvent(r),document.getElementsByName("update_cart"));if(0<u.length)for(var l=0;l<u.length;l++)u[l].disabled=!1,u[l].click();s=a.value,n=a.getAttribute("name").replace(/cart\[([\w]+)\]\[qty\]/g,"$1");sendAjaxQuantityRequest(t.currentTarget,s,n)},!1)}}}function sendAjaxQuantityRequest(t,a,n){const r=t.closest(".woocommerce-mini-cart");if(r&&astra&&astra.single_product_qty_ajax_nonce&&astra.ajax_url){t=astra.single_product_qty_ajax_nonce;r.classList.add("ajax-mini-cart-qty-loading");let e=new XMLHttpRequest;e.open("POST",astra.ajax_url,!0),e.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),e.send("action=astra_add_cart_single_product_quantity&hash="+n+"&quantity="+a+"&qtyNonce="+t),e.onload=function(){var t;e.readyState==XMLHttpRequest.DONE&&(200<=e.status||400<=e.status)&&((t=document.createEvent("HTMLEvents")).initEvent("wc_fragment_refresh",!0,!1),document.body.dispatchEvent(t),setTimeout(()=>{r.classList.remove("ajax-mini-cart-qty-loading")},500))}}}jQuery(function(t){t(document.body).on("wc_fragments_refreshed",function(){astrawpWooQuantityButtons(),quantityInput()})}),function(){var t=XMLHttpRequest.prototype.send;XMLHttpRequest.prototype.send=function(){return this.addEventListener("load",function(){astrawpWooQuantityButtons()}),t.apply(this,arguments)}}();let typingTimer,doneTypingInterval=500;function quantityInput(){document.querySelector(".woocommerce-mini-cart")&&document.querySelectorAll(".input-text.qty").forEach(t=>{t.addEventListener("keyup",a=>{clearTimeout(typingTimer),t.value&&(typingTimer=setTimeout(()=>{var t=a.target.value,e=a.target.getAttribute("name").replace(/cart\[([\w]+)\]\[qty\]/g,"$1");t&&sendAjaxQuantityRequest(a.target,t,e)},doneTypingInterval))})})} |