2 // Has the user previously consented, establish our
5 let existingConsent = '';
6 // The presence of a 'cookieConsent' local storage item indicates
7 // that previous consent has been set. If the value is empty, then
8 // no consent to non-essential cookies was given
9 const hasStoredConsent = localStorage.getItem('cookieConsent');
12 // The consent bar may not be in the DOM if it is not being used
13 const consentBar = $('#cookieConsentBar');
17 if (hasStoredConsent === null) {
22 // When the modal is opened, populate our state based on currently
24 $('#cookieConsentModal').on('shown.bs.modal', function () {
28 // Initialise existing consent based on local storage
29 function getExistingConsent() {
30 existingConsent = localStorage.getItem('cookieConsent') ?
31 localStorage.getItem('cookieConsent') :
35 function showConsentBar() {
36 const consentBar = $('#cookieConsentBar');
37 const langmenu = $('#changelanguage');
39 const height = langmenu.height();
40 consentBar.css('bottom', height);
42 consentBar.attr('aria-hidden', 'false');
46 function hideConsentBar() {
47 const consentBar = $('#cookieConsentBar');
48 consentBar.attr('aria-hidden', 'true');
52 // Hides the appropriate consent container, depending on what
53 // is currently visible
54 function hideContainer() {
55 if ($('#cookieConsentModal').hasClass('in')) {
56 $('#cookieConsentModal').modal('hide');
62 // Initialise our state of selected item and enable/disable
63 // the "Accept selected" button appropriately
64 function initialiseSelected() {
66 $('.consentCheckbox').each(function () {
67 const val = $(this).val();
68 if (existingConsent.indexOf(val) > -1 ) {
69 $(this).prop('checked', true);
72 $(this).prop('checked', false);
75 enableDisableSelectedButton();
78 // Maintain our state of selected items and enable/disable
79 // the "Accept selected" button appropriately
80 function maintainSelected() {
82 $('.consentCheckbox:checked').each(function () {
83 selected.push($(this).val());
85 enableDisableSelectedButton();
88 // Set the enabled / disabled state of the
89 // "Accept selected button based on the checkbox
91 function enableDisableSelectedButton() {
92 $('#consentAcceptSelected').prop(
98 function runConsentedCode() {
100 $('.consentCode').each(function () {
101 // The user has explicitly consented to this code, or the
102 // code doesn't require consent in the staff view
104 existingConsent.indexOf($(this).data('consent-id')) > -1 ||
105 !$(this).data('requires-consent')
107 const code = atob($(this).data('consent-code'));
108 const func = Function(code);
111 // This code doesn't have consent to run, we may need to remove
112 // any cookies it has previously set
113 const matchPattern = $(this).data('consent-match-pattern');
114 const cookieDomain = $(this).data('consent-cookie-domain');
115 const cookiePath = $(this).data('consent-cookie-path');
116 if (matchPattern.length > 0) {
117 const regex = new RegExp(matchPattern);
118 const allCookies = document.cookie.split('; ');
119 allCookies.forEach(function (cookie) {
120 const name = cookie.split('=')[0];
121 if (regex.test(name)) {
122 document.cookie = name + '=; expires=Thu, 01 Jan 1970 00: 00: 01 GMT; domain=' + cookieDomain + '; path=' + cookiePath;
131 function addButtonHandlers() {
132 // "Accept all" handler
133 $('.consentAcceptAll').on('click', function(e) {
136 $('.consentCheckbox').each(function () {
137 const val = $(this).val();
140 localStorage.setItem('cookieConsent', toSave);
145 // "Accept essential" handler
146 $('.consentAcceptEssential').on('click', function(e) {
148 localStorage.setItem('cookieConsent', []);
153 // "Accept selected" handler
154 $('#consentAcceptSelected').on('click', function(e) {
156 const toSave = selected.length > 0 ? selected : [];
157 localStorage.setItem('cookieConsent', toSave);
163 $('.consentCloseModal').on('click', function(e) {
169 // "More information" handler
170 $('#consentMoreInfo, #viewCookieConsents').on(
175 // Ensure we're up to date with the existing consent
176 getExistingConsent();
177 // Prevent the modal from being closed with anything
179 $('#cookieConsentModal')
188 $('.consentCheckbox').on('click', function () {
193 // On page load, run any code that has been given consent