Untitled

 avatar
unknown
plain_text
a year ago
5.5 kB
8
Indexable
(function(){
    var style=document.createElement('style');style.type='text/css';style.appendChild(document.createTextNode('html body .product-variations .attribute__variants:not([data-selected="all"])+div.product-options {    display: none !important;}html body .product-variations .attribute__variants>li:nth-child(n+2):not(.eg-some-selected) {    display: none !important;}'));document.head.appendChild(style);
(function (w) {
  "use strict";

  var tag = "cv-2",
    debug = document.cookie.indexOf("cfQA") > -1,
    window = typeof unsafeWindow !== "undefined" ? unsafeWindow : w;
  var preSelected = false;

  window[tag] = {

    log: function (msg) {
      if (debug) console.log("[EXPO]", tag, "-->", msg);
    },

    waitForElement: function (cssSelector, callback) {
      var stop,
        elementCached,
        timeout,
        check = function () {
          try {
            elementCached = document.querySelector(cssSelector);

            if (stop) return;

            if (elementCached) {
              callback(elementCached);
              clearTimeout(timeout);

              window[tag].log(cssSelector + " found");
            } else {
              window.requestAnimationFrame(check);
            }
          } catch (err) {
            window[tag].log(err.message);
          }
        };

      window.requestAnimationFrame(check);

      timeout = setTimeout(function () {
        stop = true;
        window[tag].log(cssSelector + " not found");
      }, 5000);
    },

    testStart: function () {
      try {
        this.waitForElement("body", function (docBody) {
          docBody.classList.add(tag);
        });

        // INITIALIZE THE TEST
        initVariation();

        window[tag].log("test running");
      } catch (err) {
        window[tag].log(err.message);
      }
    },
  };

  // INITIALIZE VARIATION
  window[tag].testStart();

  function initVariation() {
    window[tag].waitForElement(".product-variations .attribute__variants> li:last-child", () => {
      const selectedEls = document.querySelectorAll('html body .product-variations .attribute__variants> li .attribute__swatch--selected');
      selectedEls.forEach((selectedEl) => {
        // for crr selected el
        const liel = selectedEl.closest(".attribute__variants-group");
        const prevSib = liel.previousElementSibling;
        if (prevSib && !prevSib.querySelector(".attribute__swatch--selected.selected")) {
          // deselect this and all its below options
          deseletOption(liel);
          deseletAllBellowOpts(liel);
        } else if (!prevSib || (prevSib && prevSib.querySelector(".attribute__swatch--selected.selected"))) {
          isLastElement(liel);
          managePreselect(liel);
          // for next el
          var nextEl = liel.nextElementSibling;
          if (nextEl) {
            nextEl.classList.add("eg-some-selected");
            nextEl.scrollIntoView({ behavior: "smooth" });
            isLastElement(nextEl);
            managePreselect(nextEl);
          }
        }
      });
    });
  }

  const send = XMLHttpRequest.prototype.send
  XMLHttpRequest.prototype.send = function () {
    this.addEventListener('load', function () {
      if (this.responseURL.includes("/Product-Variation")) {
        initVariation();
        changeToRecommended();
      }
    })
    return send.apply(this, arguments)
  }

  function isLastElement(element) {
    let isLastEl = false;
    if (element && element.parentNode) {
      const parent = element.parentNode;
      const children = Array.from(parent.children);
      const lastChild = children[children.length - 1];
      isLastEl = lastChild === element;
    }
    if (isLastEl) {
      document.querySelector("html body .product-variations .attribute__variants").setAttribute('data-selected', "all");
    }
  }

  const preseletOpt = function (el) {
    const isSeleveLength = el.querySelector("#sleeveLength");
    if (isSeleveLength) {
      const seleveCta = el.querySelector(".attribute--recommended div");
      if (!seleveCta.parentElement.classList.contains("selected")) {
        seleveCta.click();
        preSelected = true;
      }
    }
  }

  const deseletAllBellowOpts = (el) => {
    const startElement = el
    if (startElement) {
      const nextSiblings = [];
      let nextSibling = startElement.nextElementSibling;
      while (nextSibling) {
        nextSiblings.push(nextSibling);
        nextSibling = nextSibling.nextElementSibling;
      }
    }
    nextSiblings.forEach(sib => {
      deseletOption(sib);
    });
  }

  const deseletOption = function (el) {
    preSelected = false;
    const selectedOpt = el.querySelector(".attribute__swatch--selected.selected");
    if (selectedOpt) {
      selectedOpt.querySelector("div").click();
    }
  }


  const managePreselect = (el) => {
    if (!preSelected) {
      preseletOpt(el);
    }
  }

  const changeToRecommended = () => {
    window[tag].waitForElement("html body .product-variations .attribute__variants> li #sleeveLength", function () {
      const isSeleveLength = document.querySelector("html body .product-variations .attribute__variants> li #sleeveLength");
      if (isSeleveLength) {
        const el = isSeleveLength.closest(".attribute__variants-group");
        const toolTip = el.querySelector(".attribute--recommended .attribute__box-tip");
        if (toolTip) {
          toolTip.innerText = "RECOMMENDED";
        }
      }
    });
  }
})(window);
})();
Editor is loading...