mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-18 13:39:57 -04:00
Update cookies page
Includes: - new content - added option to turn analytics on/off - non-js version for the on/off switch - a banner to confirm user's choice was saved, shown when they click the save button - the cookie banner that appears on all other pages removed from this page
This commit is contained in:
84
app/assets/javascripts/cookieSettings.js
Normal file
84
app/assets/javascripts/cookieSettings.js
Normal file
@@ -0,0 +1,84 @@
|
||||
window.GOVUK = window.GOVUK || {};
|
||||
window.GOVUK.Modules = window.GOVUK.Modules || {};
|
||||
|
||||
(function (Modules) {
|
||||
function CookieSettings () {}
|
||||
|
||||
CookieSettings.prototype.start = function ($module) {
|
||||
this.$module = $module[0];
|
||||
|
||||
this.$module.submitSettingsForm = this.submitSettingsForm.bind(this);
|
||||
|
||||
document.querySelector('form[data-module=cookie-settings]')
|
||||
.addEventListener('submit', this.$module.submitSettingsForm);
|
||||
|
||||
this.setInitialFormValues();
|
||||
};
|
||||
|
||||
CookieSettings.prototype.setInitialFormValues = function () {
|
||||
var currentConsentCookie = window.GOVUK.getConsentCookie('consent');
|
||||
|
||||
if (!currentConsentCookie) { return; }
|
||||
|
||||
var radioButton;
|
||||
|
||||
if (currentConsentCookie.analytics) {
|
||||
radioButton = document.querySelector('input[name=cookies-analytics][value=on]');
|
||||
} else {
|
||||
radioButton = document.querySelector('input[name=cookies-analytics][value=off]');
|
||||
}
|
||||
|
||||
radioButton.checked = true;
|
||||
};
|
||||
|
||||
CookieSettings.prototype.submitSettingsForm = function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
var formInputs = event.target.querySelectorAll("input[name=cookies-analytics]");
|
||||
var options = {};
|
||||
|
||||
for ( var i = 0; i < formInputs.length; i++ ) {
|
||||
var input = formInputs[i];
|
||||
if (input.checked) {
|
||||
var value = input.value === "on" ? true : false;
|
||||
|
||||
options.analytics = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
window.GOVUK.setConsentCookie(options);
|
||||
|
||||
this.showConfirmationMessage();
|
||||
|
||||
if(window.GOVUK.hasConsentFor('analytics')) {
|
||||
window.GOVUK.initAnalytics();
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
CookieSettings.prototype.showConfirmationMessage = function () {
|
||||
var confirmationMessage = document.querySelector('div[data-cookie-confirmation]');
|
||||
var previousPageLink = document.querySelector('.cookie-settings__prev-page');
|
||||
var referrer = CookieSettings.prototype.getReferrerLink();
|
||||
|
||||
document.body.scrollTop = document.documentElement.scrollTop = 0;
|
||||
|
||||
if (referrer && referrer !== document.location.pathname) {
|
||||
previousPageLink.href = referrer;
|
||||
previousPageLink.style.display = "block";
|
||||
} else {
|
||||
previousPageLink.style.display = "none";
|
||||
}
|
||||
|
||||
confirmationMessage.style.display = "block";
|
||||
};
|
||||
|
||||
CookieSettings.prototype.getReferrerLink = function () {
|
||||
return document.referrer ? new URL(document.referrer).pathname : false;
|
||||
};
|
||||
|
||||
Modules.CookieSettings = CookieSettings;
|
||||
})(window.GOVUK.Modules);
|
||||
|
||||
Reference in New Issue
Block a user