diff --git a/src/openforms/js/public.js b/src/openforms/js/public.js index 51162751b8..0f607628cd 100644 --- a/src/openforms/js/public.js +++ b/src/openforms/js/public.js @@ -1,3 +1,5 @@ +import {showCookieBar} from 'django-cookie-consent'; + import 'components/auth-mode'; import {onLoaded} from 'utils/dom'; @@ -12,8 +14,33 @@ const registerPrintHandler = () => { }); }; +const initCookieBar = () => { + const templateNode = document.getElementById('cookie-consent__cookie-bar'); + if (!templateNode) return; + + const {varName, statusUrl} = templateNode.dataset; + showCookieBar({ + statusUrl, + templateSelector: '#cookie-consent__cookie-bar', + cookieGroupsSelector: '#cookie-consent__cookie-groups', + acceptSelector: '.cookie-notice__accept', + declineSelector: '.cookie-notice__decline', + insertBefore: '#cookie-consent__cookie-bar', + onAccept: groups => { + const isAnalyticsEnabled = groups.find(group => group.varname === varName); + if (!isAnalyticsEnabled) return; + const analyticsTemplateNodes = document.querySelectorAll('.analytics-scripts'); + analyticsTemplateNodes.forEach(templateNode => { + const clone = templateNode.content.cloneNode(true); + templateNode.parentNode.insertBefore(clone, templateNode); + }); + }, + }); +}; + const init = () => { registerPrintHandler(); + initCookieBar(); }; onLoaded(init); diff --git a/src/openforms/templates/includes/cookie-notice.html b/src/openforms/templates/includes/cookie-notice.html index e07a12ce74..026cb9ba87 100644 --- a/src/openforms/templates/includes/cookie-notice.html +++ b/src/openforms/templates/includes/cookie-notice.html @@ -1,6 +1,5 @@ -{% load i18n static cookie_consent_tags %} +{% load i18n cookie_consent_tags %} -{% static "cookie_consent/cookiebar.module.js" as cookiebar_src %} {% url 'cookie_consent_cookie_group_list' as manage_url %} {% url 'cookie_consent_status' as status_url %} @@ -8,7 +7,11 @@ {% all_cookie_groups 'cookie-consent__cookie-groups' %} -