Line 0
Link Here
|
0 |
- |
1 |
(function () { |
|
|
2 |
// Has the user previously consented, establish our |
3 |
// initial state |
4 |
let selected = []; |
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'); |
10 |
getExistingConsent(); |
11 |
|
12 |
// The consent bar may not be in the DOM if it is not being used |
13 |
const consentBar = $('#cookieConsentBar'); |
14 |
if (!consentBar) { |
15 |
return; |
16 |
} |
17 |
if (hasStoredConsent === null) { |
18 |
showConsentBar(); |
19 |
} |
20 |
addButtonHandlers(); |
21 |
|
22 |
// When the modal is opened, populate our state based on currently |
23 |
// selected values |
24 |
$('#cookieConsentModal').on('shown.bs.modal', function () { |
25 |
initialiseSelected(); |
26 |
}); |
27 |
|
28 |
// Initialise existing consent based on local storage |
29 |
function getExistingConsent() { |
30 |
existingConsent = localStorage.getItem('cookieConsent') ? |
31 |
localStorage.getItem('cookieConsent') : |
32 |
[]; |
33 |
} |
34 |
|
35 |
function showConsentBar() { |
36 |
const consentBar = $('#cookieConsentBar'); |
37 |
consentBar.attr('aria-hidden', 'false'); |
38 |
consentBar.show(); |
39 |
} |
40 |
|
41 |
function hideConsentBar() { |
42 |
const consentBar = $('#cookieConsentBar'); |
43 |
consentBar.attr('aria-hidden', 'true'); |
44 |
consentBar.hide(); |
45 |
} |
46 |
|
47 |
// Hides the appropriate consent container, depending on what |
48 |
// is currently visible |
49 |
function hideContainer() { |
50 |
if ($('#cookieConsentModal').hasClass('in')) { |
51 |
$('#cookieConsentModal').modal('hide'); |
52 |
} else { |
53 |
hideConsentBar(); |
54 |
} |
55 |
} |
56 |
|
57 |
// Initialise our state of selected item and enable/disable |
58 |
// the "Accept selected" button appropriately |
59 |
function initialiseSelected() { |
60 |
selected = []; |
61 |
$('.consentCheckbox').each(function () { |
62 |
const val = $(this).val(); |
63 |
if (existingConsent.indexOf(val) > -1 ) { |
64 |
$(this).prop('checked', true); |
65 |
selected.push(val); |
66 |
} else { |
67 |
$(this).prop('checked', false); |
68 |
} |
69 |
}); |
70 |
enableDisableSelectedButton(); |
71 |
} |
72 |
|
73 |
// Maintain our state of selected items and enable/disable |
74 |
// the "Accept selected" button appropriately |
75 |
function maintainSelected() { |
76 |
selected = []; |
77 |
$('.consentCheckbox:checked').each(function () { |
78 |
selected.push($(this).val()); |
79 |
}); |
80 |
enableDisableSelectedButton(); |
81 |
} |
82 |
|
83 |
// Set the enabled / disabled state of the |
84 |
// "Accept selected button based on the checkbox |
85 |
// states |
86 |
function enableDisableSelectedButton() { |
87 |
$('#consentAcceptSelected').prop( |
88 |
'disabled', |
89 |
selected.length === 0 |
90 |
); |
91 |
} |
92 |
|
93 |
function runConsentedCode() { |
94 |
getExistingConsent(); |
95 |
$('.consentCode').each(function () { |
96 |
// The user has explicitly consented to this code, or the |
97 |
// code doesn't require consent in the staff view |
98 |
if ( |
99 |
existingConsent.indexOf($(this).data('consent-id')) > -1 || |
100 |
!$(this).data('requires-consent') |
101 |
) { |
102 |
const code = atob($(this).data('consent-code')); |
103 |
const func = Function(code); |
104 |
func(); |
105 |
} |
106 |
}); |
107 |
} |
108 |
|
109 |
function addButtonHandlers() { |
110 |
// "Accept all" handler |
111 |
$('.consentAcceptAll').on('click', function(e) { |
112 |
e.preventDefault(); |
113 |
let toSave = []; |
114 |
$('.consentCheckbox').each(function () { |
115 |
const val = $(this).val(); |
116 |
toSave.push(val); |
117 |
}); |
118 |
localStorage.setItem('cookieConsent', toSave); |
119 |
hideContainer(); |
120 |
runConsentedCode(); |
121 |
}); |
122 |
|
123 |
// "Accept essential" handler |
124 |
$('.consentAcceptEssential').on('click', function(e) { |
125 |
e.preventDefault(); |
126 |
localStorage.setItem('cookieConsent', []); |
127 |
hideContainer(); |
128 |
}); |
129 |
|
130 |
// "Accept selected" handler |
131 |
$('#consentAcceptSelected').on('click', function(e) { |
132 |
e.preventDefault(); |
133 |
const toSave = selected.length > 0 ? selected : []; |
134 |
localStorage.setItem('cookieConsent', toSave); |
135 |
hideContainer(); |
136 |
runConsentedCode(); |
137 |
}); |
138 |
|
139 |
// "More information" handler |
140 |
$('#consentMoreInfo').on( |
141 |
'click', |
142 |
function (e) { |
143 |
e.preventDefault(); |
144 |
hideConsentBar(); |
145 |
// Ensure we're up to date with the existing consent |
146 |
getExistingConsent(); |
147 |
// Prevent the modal from being closed with anything |
148 |
// but the buttons |
149 |
$('#cookieConsentModal') |
150 |
.modal({ |
151 |
backdrop: 'static', |
152 |
keyboard: false, |
153 |
focus: true, |
154 |
show: true |
155 |
}); |
156 |
} |
157 |
); |
158 |
$('.consentCheckbox').on('click', function () { |
159 |
maintainSelected(); |
160 |
}); |
161 |
} |
162 |
|
163 |
// On page load, run any code that has been given consent |
164 |
runConsentedCode(); |
165 |
})(); |