var CONFIGURATOR_URL = "https://directsamenstellen.nl"; var RP_CONFIGURATOR_ID_TAG_NAME = "data-rp-configurator-id"; var RP_WIDGET_CONFIGURATOR_TAG_NAME = "data-rp-is-widget" var RP_ID_DATA_TAG_NAME= "data-rp-unique-id"; var RP_CUSTOM_ID_DATA_TAG_NAME= "data-rp-id"; var RP_BUTTON_ID_TAG_NAME = "data-rp-button-id"; var RP_INTERNAL_EVENT_NAME= "message"; var RP_EXTERNAL_EVENT_NAME= "rp-event"; var reuzenpandaSnippet = {}; reuzenpandaSnippet.RP_CONFIGURATORS = []; reuzenpandaSnippet.RP_BUTTONS = []; var extraExecutors = []; if(window.extraExecutors) { extraExecutors = window.extraExecutors; } reuzenpandaSnippet.RP_MESSAGE_EXECUTORS = [ ...extraExecutors, { id: "gtm-push", executor: (data) => { window.dataLayer?.push(data[0]); window.rpDataLayer?.push(data[0]); } }, { id: "set-widget-open-state", executor: (data) => { if (data.isOpen) { reuzenpandaSnippet.open(data.configuratorId); } else { reuzenpandaSnippet.removeAnchorsFromUrl(); reuzenpandaSnippet.close(data.configuratorId); } for (var button of reuzenpandaSnippet.filterButtonsByValue(data.configuratorId)) { button.setOpen(!data.isOpen); } } }, { id: "route", executor: (data) => { if (!data.url) { return; } if (!data.url.startsWith("http://") && !data.url.startsWith("https://")) { data.url = "https://" + data.url; } window.location.href = data.url; } } ]; reuzenpandaSnippet.RP_ANCHOR_EXECUTORS = [ { regex: new RegExp("reuzenpanda-open-all"), executor: () => { reuzenpandaSnippet.open() } }, { regex: new RegExp("reuzenpanda-open-(\\w{8}(-\\w{4}){3}-\\w{12}?)"), executor: (anchor) => { var id = new RegExp("reuzenpanda-open-(\\w{8}(-\\w{4}){3}-\\w{12}?)").exec(anchor)[1]; reuzenpandaSnippet.open(id) } } ]; reuzenpandaSnippet.removeAnchorsFromUrl = () => { var url = window.location.href; var [baseUrl, ...hashParts] = url.split('#'); var reuzenpandaHashParts = hashParts.filter(part => { var matchingExecutors = reuzenpandaSnippet.RP_ANCHOR_EXECUTORS.some(ex => ex.regex.test(part)); if (matchingExecutors) { return true; } return false; }); if (reuzenpandaHashParts.length === 0) { return; } var newUrl = `${baseUrl}`; window.history.replaceState(null, document.title, newUrl); }; (() => { var configurators = [{"id":"cb683379-2be2-49bd-b2b9-bae1da223fe1","displaySettings":{"showBranding":false,"showActionButton":true,"actionButtonText":"Logo aanvragen"}},{"id":"360eb786-04b3-48d8-8d0b-ec058f0fc430","displaySettings":{"showBranding":false,"showActionButton":false,"actionButtonText":null}},{"id":"e9f78e1c-6d5c-4372-bd6a-d21325f7f124","displaySettings":{"showBranding":false,"showActionButton":false,"actionButtonText":null}}]; var showActionButtons = true; var showWidget = true; if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", async function(event) { setup(); load(); }); } else { setup(); load(); } function setup() { configurators.forEach((configurator) => { if (showWidget) { loadWidgetContainer(configurator.id); } if (configurator.displaySettings?.showActionButton && showActionButtons) { loadButtonContainer(configurator.id); } }); loadStyleSheet("https://snippet.reuzenpanda.nl/api/snippet/v1/css"); window.addEventListener(RP_INTERNAL_EVENT_NAME, convertMessageToExecutorEvent); window.addEventListener(RP_EXTERNAL_EVENT_NAME, handleExecutorEvent); window.addEventListener('hashchange', handleHashChange); } function load() { configurators.forEach((configurator) => { loadConfigurator(configurator.id); if (configurator.displaySettings?.showActionButton && showActionButtons) { loadButton(configurator.id); } }); setTimeout(() => { handleAnchor(window.location.href); }, 2000); } function loadWidgetContainer(configuratorId) { let div = document.createElement("div"); div.setAttribute(RP_CONFIGURATOR_ID_TAG_NAME, configuratorId); div.setAttribute(RP_WIDGET_CONFIGURATOR_TAG_NAME, "true"); div.classList.add("reuzenpanda-configurator"); div.classList.add("reuzenpanda-widget"); document.body.appendChild(div); } function loadButtonContainer(configuratorId) { var existingButton = document.querySelector(`[${RP_BUTTON_ID_TAG_NAME}="${configuratorId}"]`); if (existingButton) { return; } let div = document.createElement("div"); div.classList.add("reuzenpanda-configurator-button"); div.setAttribute(RP_BUTTON_ID_TAG_NAME, configuratorId); document.body.appendChild(div); } function loadStyleSheet(url) { let styleSheet = document.createElement("link"); styleSheet.rel = "stylesheet"; styleSheet.type = "text/css" styleSheet.href = url; document.getElementsByTagName("head")[0].appendChild(styleSheet); } function loadConfigurator(configuratorId) { let containers = document.querySelectorAll(`[${RP_CONFIGURATOR_ID_TAG_NAME}]`); for (var container of containers) { if (container.getAttribute(RP_CONFIGURATOR_ID_TAG_NAME) === configuratorId) { var isWidget = container.getAttribute(RP_WIDGET_CONFIGURATOR_TAG_NAME); let id = uuidv4(); container.setAttribute(RP_ID_DATA_TAG_NAME, id); container.appendChild(getIframe(id, configuratorId, isWidget ? "reuzenpanda-widget" : "reuzenpanda-embedded")); let configurator = generateConfigurator(id, configuratorId, container); reuzenpandaSnippet.RP_CONFIGURATORS.push(configurator); } } function getIframe(id, configuratorId, type) { let iframe = document.createElement("iframe"); iframe.src = CONFIGURATOR_URL + `/configurator/${configuratorId}/${type}/1?messageId=${id}`; iframe.classList.add("reuzenpanda-iframe"); return iframe; } function generateConfigurator(id, configuratorId, element) { return { id: id, configuratorId: configuratorId, element: element, setOpen: (open) => { if (open) { element.classList.add("--visible"); } else { element.classList.remove("--visible"); } } }; } } function loadButton(configuratorId) { let containers = document.querySelectorAll(`[${RP_BUTTON_ID_TAG_NAME}]`); for (var container of containers) { if (container.getAttribute(RP_BUTTON_ID_TAG_NAME) === configuratorId) { let id = uuidv4(); container.setAttribute(RP_ID_DATA_TAG_NAME, id); container.appendChild(getButtonIframe(id, configuratorId)); let button = generateButton(id, configuratorId, container); reuzenpandaSnippet.RP_BUTTONS.push(button); } } function generateButton(id, configuratorId, element) { return { id: id, configuratorId: configuratorId, element: element, setOpen: (open) => { if (open) { element.classList.remove("--invisible"); } else { element.classList.add("--invisible"); } } }; } function getButtonIframe(id, configuratorId) { let iframe = document.createElement("iframe"); iframe.src = CONFIGURATOR_URL + `/configurator/${configuratorId}/button?messageId=${id}`; iframe.classList.add("reuzenpanda-iframe"); return iframe; } } function convertMessageToExecutorEvent(message) { if (!message || !message.data) { return; } var configuratorElements = document.querySelectorAll(`[${RP_CONFIGURATOR_ID_TAG_NAME}]`); let customId = ""; for (var c of configuratorElements) { if (c.getAttribute(RP_ID_DATA_TAG_NAME) === message.data.messageId) { customId = c.getAttribute(RP_CUSTOM_ID_DATA_TAG_NAME); } } var myEvent = new CustomEvent(RP_EXTERNAL_EVENT_NAME, { detail: { id: message.data.id, data: message.data.data, messageId: message.data.messageId, customId: customId } }); window.dispatchEvent(myEvent); } function handleExecutorEvent(e) { var executors = reuzenpandaSnippet.RP_MESSAGE_EXECUTORS.filter((ex) => ex.id === e.detail.id); for (var executor of executors) { executor.executor(e.detail.data); } } function handleHashChange(e) { if (e.newURL === undefined || e.newURL === null) { return; } if (!e.newURL.includes("#")) { return; } handleAnchor(e.newURL); } function handleAnchor(url) { let anchor = url.split("#")[1]; var executors = reuzenpandaSnippet.RP_ANCHOR_EXECUTORS.filter((ex) => ex.regex.test(anchor)); for (var executor of executors) { executor.executor(anchor); } } })(); function uuidv4() { return "000000000".replace(/[018]/g, c => (+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16) ); } reuzenpandaSnippet.filterConfiguratorsByValue = (value) => { return reuzenpandaSnippet.RP_CONFIGURATORS.filter((c) => c.id === value || c.configuratorId === value || c.name === value || c.customid === value ) ?? []; } reuzenpandaSnippet.filterButtonsByValue = (value) => { return reuzenpandaSnippet.RP_BUTTONS.filter((c) => c.id === value || c.configuratorId === value || c.name === value || c.customid === value ) ?? []; } reuzenpandaSnippet.open = (key) => { for (var configurator of key ? reuzenpandaSnippet.filterConfiguratorsByValue(key) : reuzenpandaSnippet.RP_CONFIGURATORS) { if (!configurator) { continue; } configurator.setOpen(true); for (var button of reuzenpandaSnippet.filterButtonsByValue(configurator.configuratorId)) { button.setOpen(false); } } } reuzenpandaSnippet.close = (key) => { for (var configurator of key ? reuzenpandaSnippet.filterConfiguratorsByValue(key) : reuzenpandaSnippet.RP_CONFIGURATORS) { if (!configurator) { continue; } configurator.setOpen(false); for (var button of reuzenpandaSnippet.filterButtonsByValue(configurator.configuratorId)) { button.setOpen(true); } } } reuzenpandaSnippet.loadConfigurator = (configuratorId) => { let containers = document.querySelectorAll(`[${RP_CONFIGURATOR_ID_TAG_NAME}]`); for (var container of containers) { if (container.getAttribute(RP_CONFIGURATOR_ID_TAG_NAME) !== configuratorId) { continue; } var isWidget = container.getAttribute(RP_WIDGET_CONFIGURATOR_TAG_NAME); let id = uuidv4(); container.setAttribute(RP_ID_DATA_TAG_NAME, id); container.appendChild(getIframe(id, configuratorId, isWidget ? "reuzenpanda-widget" : "reuzenpanda-embedded")); let configurator = generateConfigurator(id, configuratorId, container); reuzenpandaSnippet.RP_CONFIGURATORS.push(configurator); } function getIframe(id, configuratorId, type) { let iframe = document.createElement("iframe"); iframe.src = CONFIGURATOR_URL + `/configurator/${configuratorId}/${type}/1?messageId=${id}`; iframe.classList.add("reuzenpanda-iframe"); return iframe; } function generateConfigurator(id, configuratorId, element) { return { id: id, configuratorId: configuratorId, element: element, setOpen: (open) => { if (open) { element.classList.add("--visible"); } else { element.classList.remove("--visible"); } } }; } }