All files / src/sections/Actions/Share script.js

0% Statements 0/107
0% Branches 0/40
0% Functions 0/17
0% Lines 0/107

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219                                                                                                                                                                                                                                                                                                                                                                                                                                                     
var GIVEPAYMENTS = (function () {
  function stringifyStyleObject(obj) {
    return Object.entries(obj).reduce((acc, [k, v]) => {
      return `${acc}${k}:${v};`;
    }, "");
  }
 
  const givePaymentsStyle = {
    position: "absolute",
    top: "0px",
    bottom: "0px",
    left: "0px",
    right: "0px",
    border: "none",
    "white-space": "nowrap",
    "z-index": 2147483647,
    "text-align": "center",
    width: "100%",
    height: "100%",
    "overflow-y": "scroll",
    "overflow-x": "hidden",
    display: "none",
  };
 
  const closeButtonStyle = {
    position: "absolute",
    top: "20px",
    cursor: "pointer",
    right: "20px",
    background: "none",
    border: "none",
    color: "white",
    "font-size": "20px",
    "z-index": 21474836474,
  };
 
  var iframeId = "givepayments-embed";
 
  function createWidget(p) {
    const url = makeURL(p);
    const auto = p.auto;
    if (!document.getElementById(iframeId)) {
      const iframe = document.createElement("iframe");
      iframe.setAttribute("id", iframeId);
      iframe.setAttribute("allowTransparency", "true");
      iframe.setAttribute("style", stringifyStyleObject(givePaymentsStyle));
      iframe.setAttribute("data-url", url);
 
      iframe.style.visibility = "hidden";
      document.body.appendChild(iframe);
    }
    if (auto && !deviceOS()) {
      loadWidget(url);
    }
  }
 
  function makeCloseButton() {
    const closeButton = document.createElement("button");
    closeButton.textContent = "X";
    closeButton.setAttribute("style", stringifyStyleObject(closeButtonStyle));
 
    return closeButton;
  }
 
  function loadDesktopWidget(url) {
    const iframe = document.getElementById(iframeId);
 
    const closeButton = makeCloseButton();
 
    document.body.appendChild(iframe);
    document.body.appendChild(closeButton);
 
    closeButton.addEventListener("click", () => {
      if (document.body.contains(closeButton)) {
        document.body.removeChild(closeButton);
      }
      if (document.body.contains(iframe)) {
        document.body.removeChild(iframe);
      }
    });
 
    loader();
    iframe.style.width = "100%";
    iframe.style.height = "100%";
    iframe.style.margin = "auto";
    iframe.style.visibility = "visible";
    iframe.style.display = "block";
    iframe.src = url;
    iframe.onload = function () {
      removeLoader();
      iframe.style.background = "rgba(0,0,0,0.5)";
      window.scrollTo(0, 0);
    };
  }
 
  function loadWidget(url) {
    // Check if mobile device
    // If mobile device redirect
    if (deviceOS()) {
      window.location.replace(`${url}&redirect=true`);
    } else {
      loadDesktopWidget(url);
    }
  }
 
  function deviceOS() {
    const useragent = navigator.userAgent;
 
    if (useragent.match(/Android/i)) {
      return "android";
    } else if (useragent.match(/webOS/i)) {
      return "webos";
    } else if (useragent.match(/iPhone/i)) {
      return "iphone";
    } else if (useragent.match(/iPod/i)) {
      return "ipod";
    } else if (useragent.match(/iPad/i)) {
      return "ipad";
    } else if (useragent.match(/Windows Phone/i)) {
      return "windows phone";
    } else if (useragent.match(/SymbianOS/i)) {
      return "symbian";
    } else if (useragent.match(/RIM/i) || useragent.match(/BB/i)) {
      return "blackberry";
    } else {
      return false;
    }
  }
 
  // Loader
  function loader() {
    const loaderDiv = document.createElement("div");
    loaderDiv.setAttribute("id", "givepayments-loader");
 
    const outerStyle =
      "position:fixed;display:table;top:0%;left:50%;transform:translate(-50%,0);border:none;white-space:nowrap;background-color:rgba(0,0,0,0.5);z-index:10000009;width:100%;height:100%;margin:0 auto;overflow:auto;min-height:50%;";
    const outerDiv = document.createElement("div");
    outerDiv.setAttribute("allowTransparency", "true");
    outerDiv.setAttribute("style", outerStyle);
 
    const innerDivStyle =
      "display:table-cell;width:100%;height:100%;vertical-align:middle;text-align:center;";
    const innerDiv = document.createElement("div");
    innerDiv.setAttribute("style", innerDivStyle);
 
    // SVG Loader
    const loaderGraphic = document.createElement("img");
    loaderGraphic.setAttribute("class", "loaderSVG");
    loaderGraphic.setAttribute("type", "image/svg+xml");
    loaderGraphic.src = "https://cdn.givepayments.com/logo.svg";
    innerDiv.appendChild(loaderGraphic);
    outerDiv.appendChild(innerDiv);
    loaderDiv.appendChild(outerDiv);
    document.body.appendChild(loaderDiv);
  }
 
  function removeLoader() {
    const loader = document.getElementById("givepayments-loader");
    if (loader) {
      loader.parentNode.removeChild(loader);
    }
  }
 
  function injectLoaderCSS() {
    const css = `.loaderSVG {animation: pulse 2s infinite;} @keyframes pulse {
  0% {
      transform: scale(1);
  }
  50% {
      transform: scale(1.4);
  }
  100% {
      transform: scale(1);
  }
}`;
 
    if (!document.getElementById("givePaymentsLoaderStyle")) {
      const head = document.getElementsByTagName("head")[0];
      const style = document.createElement("style");
      style.type = "text/css";
      style.setAttribute("id", "givePaymentsLoaderStyle");
      if (style.styleSheet) {
        style.styleSheet.cssText = css;
      } else {
        style.appendChild(document.createTextNode(css));
      }
      head.appendChild(style);
    }
  }
 
  function makeURL(p) {
    const host = p.domainName;
    const id = p.auto || p.id;
    let url = host + `/${id}?widget=true`;
 
    return url;
  }
 
  return {
    init: function (params) {
      const p = params[0];
 
      if (document.readyState === "complete") {
        injectLoaderCSS();
        createWidget(p);
      } else {
        window.onload = function () {
          injectLoaderCSS();
          createWidget(p);
        };
      }
    },
    load: function (params) {
      const p = params[0];
      loadWidget(makeURL(p));
    },
  };
})();