// eslint-disable-next-line import/no-unresolved import urlBuilderMap from "consts:urlBuilderMap"; const initializeShareon = () => { const shareonContainers = document.getElementsByClassName("shareon"); // iterate over
for (let i = 0; i < shareonContainers.length; i += 1) { /** @type Element */ const container = shareonContainers[i]; // iterate over children of
for (let j = 0; j < container.children.length; j += 1) { /** @type Element */ const child = container.children[j]; if (child) { const classListLength = child.classList.length; // iterate over classes of the child element for (let k = 0; k < classListLength; k += 1) { const cls = child.classList.item(k); // if it's one of the networks if (Object.prototype.hasOwnProperty.call(urlBuilderMap, cls)) { const preset = { url: encodeURIComponent( child.dataset.url || container.dataset.url || window.location.href ), title: encodeURIComponent( child.dataset.title || container.dataset.title || document.title ), media: encodeURIComponent( child.dataset.media || container.dataset.media || "" ), text: encodeURIComponent( child.dataset.text || container.dataset.text || "" ), via: encodeURIComponent( child.dataset.via || container.dataset.via || "" ), fbAppId: encodeURIComponent( child.dataset.fbAppId || container.dataset.fbAppId || "" ), }; const url = urlBuilderMap[cls](preset); if (child.tagName.toLowerCase() === "a") { child.setAttribute("href", url); child.setAttribute("rel", "noopener noreferrer"); child.setAttribute("target", "_blank"); } else { const getButtonListener = (buttonUrl) => () => { window.open(buttonUrl, "_blank", "noopener,noreferrer"); }; child.addEventListener("click", getButtonListener(url)); } break; // once a network is detected we don't want to check further } } } } } }; export default initializeShareon;