2 lines
4.1 KiB
JavaScript
2 lines
4.1 KiB
JavaScript
function t_prod__init(t){var e=document.querySelector("#rec"+t);if(e){var r=e.querySelectorAll(".js-product");Array.prototype.forEach.call(r,(function(t){t.querySelector(".js-product-option, .js-product-multioption")&&t_prod__initProduct(t)}))}}function t_prod__initProduct(t,e){var r=t;"string"==typeof r&&(r=e),t_prod__initPrice(r),t_prod__addEvents__options(r),t_prod__updatePrice(r,e)}function t_prod__initPrice(t){var e=t;if(e instanceof Element||(e=e[0]),e){var r=e.querySelector(".js-product-price");r||(e.insertAdjacentHTML("beforeend",'<div style="display:none;" class="js-product-price"></div>'),r=e.querySelector(".js-product-price"));var c=r.textContent,n=r.getAttribute("data-product-price-def"),i=r.getAttribute("data-product-price-def-str");if(c){if(!n&&!i){var o=t_prod__cleanPrice(c);r.setAttribute("data-product-price-def",o),r.setAttribute("data-product-price-def-str",c.replace(/"/g,""))}}else r.setAttribute("data-product-price-def",0)}}function t_prod__updatePrice(t,e){var r=t;if(r instanceof Element||(r=r[0]),r){var c=r.querySelector(".js-product-price"),n=r.querySelectorAll(".js-product-option-variants"),i,o=0;if(c){var a=c.getAttribute("data-product-price-def");a*=1;var d=0,p=function t(e){if(e){var r=t_prod__cleanPrice(e);-1!==e.indexOf("+")?o+=r:0!==parseFloat(r)&&(a=r)}else d+=1};Array.prototype.forEach.call(n,(function(t){var e=t.querySelector("option:checked").getAttribute("data-product-variant-price");p(e)}));var u=r.querySelectorAll('.js-product-multioption input[type="checkbox"]:checked'),s;if(Array.prototype.forEach.call(u,(function(t){var e=t.getAttribute("data-product-variant-price");p(e)})),i=a+o,c.classList.contains("js-store-prod-price-range-val"))c.setAttribute("data-product-price-range-val",t_prod__roundPrice(i));else if(i>0){var l=t_prod__showPrice(t_prod__roundPrice(i),e),_=c.getAttribute("data-product-price-def-str"),f=d===n.length,v=r.classList.contains("js-store-product");if(f&&!v){var y=t_prod__saveUserInputInPrice(i,l,_);c.innerHTML=y}else c.innerHTML=l}else c.innerHTML=c.getAttribute("data-product-price-def-str")}}}function t_prod__saveUserInputInPrice(t,e,r){if(!r)return e;if(-1!==r.indexOf(e))return r;var c=t.toString();if(e!==r)if(window.tcart&&"00"===window.tcart.currency_dec){var n=window.tcart.currency_sep,i=e.replace(n+"00","");if(-1!==r.indexOf(i))return r.replace(i,e);if(-1!==r.indexOf(c))return r.replace(c,e)}else if(-1!==r.indexOf(c))return r.replace(c,e);return e}function t_prod__addEvents__options(t){var e=t;if(e instanceof Element||(e=e[0]),e){var r=e.querySelectorAll(".js-product-option-variants");Array.prototype.forEach.call(r,(function(t){var e=t.closest(".js-product");t.addEventListener("change",(function(){t_prod__updatePrice(e),t_triggerEvent(document.body,"twishlist_addbtn")}))}));var c=e.querySelectorAll('.js-product-multioption input[type="checkbox"]');Array.prototype.forEach.call(c,(function(t){var e=t.closest(".js-product");t.addEventListener("change",(function(){t_prod__updatePrice(e),t_triggerEvent(document.body,"twishlist_addbtn")}))}))}}function t_prod__cleanPrice(t){var e=t;return e?(e=(e=e.toString()).replace(",",".").replace(/[^0-9.]/g,""),e=parseFloat(e).toFixed(2),isNaN(e)&&(e=0),e=parseFloat(e),(e*=1)<0&&(e=0)):e=0,e}function t_prod__roundPrice(t){var e=t;return e?(e=parseFloat(e).toFixed(2),e=parseFloat(e),(e*=1)<0&&(e=0)):e=0,e}function t_prod__showPrice(t,e){var r=t;if(r){var c;if(r=r.toString(),window.tcart&&window.tcart.currency_dec&&"00"===window.tcart.currency_dec||e&&e.currencyDecimal&&"00"===e.currencyDecimal)if(-1===r.indexOf(".")&&-1===r.indexOf(","))r+=".00";else 1===r.substr(r.indexOf(".")+1).length&&(r+="0");r=r.replace(/\B(?=(\d{3})+(?!\d))/g," "),r=window.tcart&&window.tcart.currency_sep&&"."===window.tcart.currency_sep||e&&e.currencySeparator&&"."===e.currencySeparator?r.replace(",","."):r.replace(".",",")}else r="";return r}function t_triggerEvent(t,e){var r;document.createEvent?(r=document.createEvent("HTMLEvents")).initEvent(e,!0,!1):document.createEventObject&&((r=document.createEventObject()).eventType=e),r.eventName=e,t.dispatchEvent?t.dispatchEvent(r):t.fireEvent?t.fireEvent("on"+r.eventType,r):t[e]?t[e]():t["on"+e]&&t["on"+e]()}
|