Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
2.5 kB
2
Indexable
Never
(function () {
  try {
    /* main variables */
    var debug = 0;
    var variation_name = "";
    var $;

    /* 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 live(selector, event, callback, context) {
      /* Helper Functions */
      // 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
      if (!Element.prototype.matches) {
        Element.prototype.matches =
          Element.prototype.matchesSelector ||
          Element.prototype.webkitMatchesSelector ||
          Element.prototype.msMatchesSelector;
      }

      // 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 (el && found) callback.call(el, e);
        });
      }
      live(selector, event, callback, context);
    }

    /* Variation Init */
    function init() {
      var cartSubtotal = document.querySelector('html body .cart-subtotal');
      var woocommerceInfo = document.querySelector('html body .woocommerce-info');
      var checkoutCoupon = document.querySelector('html body .checkout_coupon');

      if (cartSubtotal && woocommerceInfo && checkoutCoupon) {
        cartSubtotal.insertAdjacentElement("beforebegin", woocommerceInfo);
        woocommerceInfo.insertAdjacentElement("afterend", checkoutCoupon);
        live('html body .showcoupon', 'click', function () {
          checkoutCoupon.classList.toggle('eg-active');
        });
      }
    }

    /* Initialize variation */
    waitForElement('html body .cart-subtotal', init, 50, 15000);
  } catch (e) {
    if (debug) console.log(e, "error in Test" + variation_name);
  }
})();
Leave a Comment