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