2020-07-31 12:18:17 -04:00
|
|
|
// eslint-disable-next-line import/no-unresolved
|
2022-02-08 12:56:39 -05:00
|
|
|
import urlBuilderMap from "consts:urlBuilderMap";
|
2020-03-25 16:47:00 -04:00
|
|
|
|
2020-07-31 12:18:17 -04:00
|
|
|
const initializeShareon = () => {
|
2022-02-08 12:56:39 -05:00
|
|
|
const shareonContainers = document.getElementsByClassName("shareon");
|
2020-03-25 16:47:00 -04:00
|
|
|
|
2020-07-30 06:40:00 -04:00
|
|
|
// iterate over <div class="shareon">
|
2020-03-25 16:47:00 -04:00
|
|
|
for (let i = 0; i < shareonContainers.length; i += 1) {
|
2020-07-31 12:18:17 -04:00
|
|
|
/** @type Element */
|
|
|
|
const container = shareonContainers[i];
|
2020-03-25 16:47:00 -04:00
|
|
|
|
2020-07-30 06:40:00 -04:00
|
|
|
// iterate over children of <div class="shareon">
|
2020-03-25 16:47:00 -04:00
|
|
|
for (let j = 0; j < container.children.length; j += 1) {
|
2020-07-31 12:18:17 -04:00
|
|
|
/** @type Element */
|
|
|
|
const child = container.children[j];
|
2020-03-25 16:47:00 -04:00
|
|
|
|
2020-07-30 06:40:00 -04:00
|
|
|
if (child) {
|
|
|
|
const classListLength = child.classList.length;
|
2020-03-25 16:47:00 -04:00
|
|
|
|
2020-07-30 06:40:00 -04:00
|
|
|
// 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
|
2020-07-31 12:16:59 -04:00
|
|
|
if (Object.prototype.hasOwnProperty.call(urlBuilderMap, cls)) {
|
2020-07-30 06:40:00 -04:00
|
|
|
const preset = {
|
|
|
|
url: encodeURIComponent(
|
2022-02-08 12:56:39 -05:00
|
|
|
child.dataset.url ||
|
|
|
|
container.dataset.url ||
|
|
|
|
window.location.href
|
2020-07-30 06:40:00 -04:00
|
|
|
),
|
|
|
|
title: encodeURIComponent(
|
2022-02-08 12:56:39 -05:00
|
|
|
child.dataset.title || container.dataset.title || document.title
|
2020-07-30 06:40:00 -04:00
|
|
|
),
|
|
|
|
media: encodeURIComponent(
|
2022-02-08 12:56:39 -05:00
|
|
|
child.dataset.media || container.dataset.media || ""
|
2020-07-30 06:40:00 -04:00
|
|
|
),
|
|
|
|
text: encodeURIComponent(
|
2022-02-08 12:56:39 -05:00
|
|
|
child.dataset.text || container.dataset.text || ""
|
2020-07-30 06:40:00 -04:00
|
|
|
),
|
|
|
|
via: encodeURIComponent(
|
2022-02-08 12:56:39 -05:00
|
|
|
child.dataset.via || container.dataset.via || ""
|
2020-07-30 06:40:00 -04:00
|
|
|
),
|
2020-09-28 07:33:36 -04:00
|
|
|
fbAppId: encodeURIComponent(
|
2022-02-08 12:56:39 -05:00
|
|
|
child.dataset.fbAppId || container.dataset.fbAppId || ""
|
2020-09-28 07:33:36 -04:00
|
|
|
),
|
2020-07-30 06:40:00 -04:00
|
|
|
};
|
2020-07-31 12:16:59 -04:00
|
|
|
const url = urlBuilderMap[cls](preset);
|
2020-07-30 06:40:00 -04:00
|
|
|
|
2022-02-08 12:56:39 -05:00
|
|
|
if (child.tagName.toLowerCase() === "a") {
|
|
|
|
child.setAttribute("href", url);
|
|
|
|
child.setAttribute("rel", "noopener noreferrer");
|
|
|
|
child.setAttribute("target", "_blank");
|
2020-07-30 06:40:00 -04:00
|
|
|
} else {
|
2020-09-18 14:24:22 -04:00
|
|
|
const getButtonListener = (buttonUrl) => () => {
|
2022-02-08 12:56:39 -05:00
|
|
|
window.open(buttonUrl, "_blank", "noopener,noreferrer");
|
2020-09-18 14:24:22 -04:00
|
|
|
};
|
|
|
|
|
2022-02-08 12:56:39 -05:00
|
|
|
child.addEventListener("click", getButtonListener(url));
|
2020-07-30 06:40:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
break; // once a network is detected we don't want to check further
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-25 16:47:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-07-14 11:47:27 -04:00
|
|
|
|
|
|
|
export default initializeShareon;
|