js

mail@pastecode.io avatar
unknown
ejs
2 years ago
11 kB
4
Indexable
Never
(function () {
    try {
        /* main variables */
        var debug=0;
        var variation_name="";
 
        /* all Pure helper functions */
        function waitForElement(selector, trigger, delayInterval, delayTimeout) {
            var interval=setInterval(function () {
                    if (document && document.querySelector(selector) && document.querySelectorAll(selector).length > 0) {
                        clearInterval(interval);
                        trigger();
                    }
                }
 
                , delayInterval);
 
            setTimeout(function () {
                    clearInterval(interval);
                }
 
                , delayTimeout);
        }
 
        function waitForTrust(trigger, delayInterval, delayTimeout) {
            var interval=setInterval(function () {
                    if (window.Trustpilot && window.Trustpilot.loadFromElement) {
                        clearInterval(interval);
                        trigger();
                    }
                }
 
                , delayInterval);
 
            setTimeout(function () {
                    clearInterval(interval);
                }
 
                , delayTimeout);
        }
        function live(selector, event, callback, context) {
            // helper for enabling IE 8 event bindings
            function addEvent(el, type, handler) {
                if (el.attachEvent) el.attachEvent("on" + type, handler);
                else el.addEventListener(type, handler);
            }
            // matches polyfill
            this.Element &&
                (function(ElementPrototype) {
                    ElementPrototype.matches =
                        ElementPrototype.matches ||
                        ElementPrototype.matchesSelector ||
                        ElementPrototype.webkitMatchesSelector ||
                        ElementPrototype.msMatchesSelector ||
                        function(selector) {
                            var node = this,
                                nodes = (node.parentNode || node.document).querySelectorAll(
                                    selector
                                ),
                                i = -1;
                            while (nodes[++i] && nodes[i] != node);
                            return !!nodes[i];
                        };
                })(Element.prototype);
            // live binding helper using matchesSelector
            function live(selector, event, callback, context) {
                addEvent(context || document, event, function(e) {
                    var found,
                        el = e.target || e.srcElement;
                    while (
                        el &&
                        el.matches &&
                        el !== context &&
                        !(found = el.matches(selector))
                    )
                        el = el.parentElement;
                    if (found) callback.call(el, e);
                });
            }
            live(selector, event, callback, context);
        }
    
var popup=`<div id="eg-popup" class="eg-overlay"></div>
<div class="eg-popup">
    <a class="eg-close" >×</a>
    <p class="eg-popup-title">Please Select Your Size</p>
    <div class="eg-content">
        <button class="eg-add-to-cart" >Add to Cart</button>
    </div>
</div>`;
 
        function init() {
        var Products=document.querySelectorAll(".findify-components-common--grid >div");
        Products.forEach(function (e){
            // console.log(e.parentElement)
            if(!e.classList.contains("eg-cta-added")){
                e.classList.add("eg-cta-added");
                e.insertAdjacentHTML("beforeend","<button class='eg-quick-atc-cta'>Add to Cart</button >")
            }
             
        });
 
        live(".options a:not(.sold-out)", "click", function ($event) {
            $event.preventDefault();
            // event.stopImmediatePropagation();
            if(document.querySelector(".options a.active")){
                document.querySelector(".options a.active").classList.remove("active");
            
                
            }
            document.querySelector(".eg-add-to-cart").classList.add("active")
                    this.classList.add("active");     
          });
 
          live(".options a.sold-out", "click", function () {
            document.querySelector(".eg-add-to-cart").classList.remove("active");
          });
 
       
document.querySelectorAll('.eg-quick-atc-cta').forEach(item => {
    item.addEventListener('click', event => {
        // console.log("clicked")
        
       
                    var Url=item.parentElement.querySelector(".findify-components--cards--product").getAttribute("href");
 ajaxCall(Url)
    })
  })
  live('.eg-close, .eg-overlay', 'click', event => {
    document.querySelector(".eg-overlay").remove();
    document.querySelector(".eg-popup").remove();
 })
 
 live('.eg-add-to-cart', 'click', event => {
    if(document.querySelector(".pdp-fieldset")){
        // console.log("ATC clicked");
    var variant = $(".options a.active").attr("data-variant-id")
    Shopify.addItem(variant,'1',function(){
        // console.log('heelo'+ variant);
    });
    }else{
        var variantID= document.querySelector("#pdp--form > input[name=id]:nth-child(3)").value;
        Shopify.addItem(variantID,'1',function(){
            // console.log('heelo'+ variant);
        });
        
    }
    document.querySelector(".eg-close").click();
    setTimeout(function(){
        sideCartValue();
    },1000)
    
    document.querySelector(".cart__container a").click();
 })
 
           
        }
        function ajaxCall(url){
    
                var xmlHttp = new XMLHttpRequest();
                xmlHttp.onreadystatechange = function () {
                    if (xmlHttp.readyState == 4) {
                        if (xmlHttp.status == 200) {
                            var parser = new DOMParser();
                            var response = parser.parseFromString(xmlHttp.responseText, 'text/html');
                            var productSize = response.querySelector("#pdp--form-container").innerHTML;
                            if(!document.querySelector(".eg-overlay")){
                                document.querySelector("body").insertAdjacentHTML("beforeend", popup);
                            }
                            if(!response.querySelector(".options a")){
                                document.querySelector(".eg-add-to-cart").classList.add("eg-one-size")
                            }
                            if(!document.querySelector(".eg-content").classList.contains("eg-size-added")){
                                document.querySelector(".eg-content").classList.add("eg-size-added");
                                document.querySelector(".eg-add-to-cart").insertAdjacentHTML("beforebegin", productSize);
                            }
                                  
                        }
                    }
                };
                xmlHttp.open("GET", url, true);
                xmlHttp.send(null);
        }
 
        function sideCartValue(){
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.onreadystatechange = function () {
                if (xmlHttp.readyState == 4) {
                    if (xmlHttp.status == 200) {
                        var parser = new DOMParser();
                        var response = parser.parseFromString(xmlHttp.responseText, 'text/html');
                        var cartproduces= response.querySelector(".inline-cart__content-container").innerHTML;
                        var Itemsqnty =response.querySelector(".inline-cart__header").innerHTML;
                        var ItemSubTotal =response.querySelector(".inline-cart__total-amount-container").innerHTML;
                        var MainNavQty =response.querySelector(".inline-cart__header .inline-cart__total-count").innerText;
                        document.querySelector(".inline-cart__content-container").innerHTML= cartproduces;
                        document.querySelector(".inline-cart__header").innerHTML= Itemsqnty;
                        document.querySelector(".inline-cart__total-amount-container").innerHTML= ItemSubTotal;
                        document.querySelector(".cart__container > a > span > span").innerText= MainNavQty;
                        setTimeout(function(){
                            if(document.querySelector(".checkout-cta-section .inline-cart__btn").classList.contains("is-disabled")){
                                document.querySelector(".checkout-cta-section .inline-cart__btn").classList.remove("is-disabled")
                            }
                        },500)
                    } 
                }
            };
            xmlHttp.open("GET", "https://www.wickedclothes.com/cart?view=contents", true);
            xmlHttp.send(null);
        }
 
        function checkAjax(){
            // console.log("fired")
            const send = XMLHttpRequest.prototype.send;
            XMLHttpRequest.prototype.send = function() { 
                this.addEventListener('load', function() {
                    if(this.responseURL.indexOf('/cart.js?r') != -1){
                        waitForElement(".findify-components-common--grid >div", init, 50, 30000);    
                    }
                });
                return send.apply(this, arguments);
            };
          
     }
   
     live('.findify-components--button.findify-components--checkbox-facet__item, .findify-components--category-facet button, .findify-components-search--mobile-sorting__body button, .findify-components-search--desktop-facets__facet button, .findify-components--dropdown__open button', 'click', event => {
        checkAjax()
     })
     listener();
 
     function listener() {
         /* These are the modifications: */
         window.addEventListener("locationchange", function() {
             if (window.location.href.indexOf('offset=') > -1) {
                waitForElement(".findify-components-common--grid >div", init, 50, 40000);
             }
 
         });
         history.pushState = ((f) =>
             function pushState() {
                 var ret = f.apply(this, arguments);
                 window.dispatchEvent(new Event("pushstate"));
                 window.dispatchEvent(new Event("locationchange"));
                 return ret;
             })(history.pushState);
         history.replaceState = ((f) =>
             function replaceState() {
                 var ret = f.apply(this, arguments);
                 window.dispatchEvent(new Event("replacestate"));
                 window.dispatchEvent(new Event("locationchange"));
                 return ret;
             })(history.replaceState);
         window.addEventListener("popstate", () => {
             window.dispatchEvent(new Event("locationchange"));
         });
     }
 
        waitForElement(".findify-components-common--grid >div", init, 50, 40000);
    }
    catch (e) {
        if (debug) console.log(e, "error in Test" + variation_name);
    }
})();