UNPKG

digitalmarketplace-frontend

Version:

Digital Marketplace Frontend contains assets and components used in Digital Marketplace projects

277 lines (271 loc) 9.83 kB
function assign(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { target[key] = source[key]; } } return target; } var defaultConverter = { read: function read(value) { if (value[0] === '"') { value = value.slice(1, -1); } return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent); }, write: function write(value) { return encodeURIComponent(value).replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g, decodeURIComponent); } }; function init(converter, defaultAttributes) { function set(name, value, attributes) { if (typeof document === 'undefined') { return; } attributes = assign({}, defaultAttributes, attributes); if (typeof attributes.expires === 'number') { attributes.expires = new Date(Date.now() + attributes.expires * 864e5); } if (attributes.expires) { attributes.expires = attributes.expires.toUTCString(); } name = encodeURIComponent(name).replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent).replace(/[()]/g, escape); var stringifiedAttributes = ''; for (var attributeName in attributes) { if (!attributes[attributeName]) { continue; } stringifiedAttributes += '; ' + attributeName; if (attributes[attributeName] === true) { continue; } stringifiedAttributes += '=' + attributes[attributeName].split(';')[0]; } return document.cookie = name + '=' + converter.write(value, name) + stringifiedAttributes; } function get(name) { if (typeof document === 'undefined' || arguments.length && !name) { return; } var cookies = document.cookie ? document.cookie.split('; ') : []; var jar = {}; for (var i = 0; i < cookies.length; i++) { var parts = cookies[i].split('='); var value = parts.slice(1).join('='); try { var found = decodeURIComponent(parts[0]); jar[found] = converter.read(value, found); if (name === found) { break; } } catch (e) {} } return name ? jar[name] : jar; } return Object.create({ set: set, get: get, remove: function remove(name, attributes) { set(name, '', assign({}, attributes, { expires: -1 })); }, withAttributes: function withAttributes(attributes) { return init(this.converter, assign({}, this.attributes, attributes)); }, withConverter: function withConverter(converter) { return init(assign({}, this.converter, converter), this.attributes); } }, { attributes: { value: Object.freeze(defaultAttributes) }, converter: { value: Object.freeze(converter) } }); } var api = init(defaultConverter, { path: '/' }); var GrantType; (function (GrantType) { GrantType["GRANTED"] = "granted"; GrantType["NOT_GRANTED"] = "not granted"; })(GrantType || (GrantType = {})); const getGrantedText = (state) => state ? GrantType.GRANTED : GrantType.NOT_GRANTED; const updateDataLayer = (cookiePreferences) => { window.dataLayer.push({ event: 'gtm_consent_update', usage_consent: getGrantedText(cookiePreferences.usage), glassbox_consent: getGrantedText(cookiePreferences.glassbox), marketing_consent: GrantType.NOT_GRANTED }); }; const cookieUpdateOptions = [ { cookieName: 'usage', cookiePrefixes: ['_ga', '_gi'] }, { cookieName: 'glassbox', cookiePrefixes: ['_cls'] } ]; const DOMAINS = [ '.marketplace.team', '.digitalmarketplace.service.gov.uk', 'www.applytosupply.digitalmarketplace.service.gov.uk' ]; // When not in production we want to delete cookies in localhost too const getDomains = () => { if (window.location.hostname === 'localhost') { return DOMAINS.concat(['localhost']); } return DOMAINS; }; const getCookiePreferences = () => { const defaultCookieSettings = '{"usage":false,"glassbox":false}'; return JSON.parse(api.get('cookie_preferences_dmp') ?? defaultCookieSettings); }; const setCookiePreferences = (usage) => { const cookiePreferences = getCookiePreferences(); cookiePreferences.usage = usage; cookiePreferences.settings_viewed = true; api.set('cookie_preferences_dmp', JSON.stringify(cookiePreferences), { expires: 365 }); updateDataLayer(cookiePreferences); removeUnwantedCookies(); }; const removeUnwantedCookies = () => { const cookieList = Object.keys(api.get()); const cookiesToRemove = ['digitalmarketplace_cookie_settings_viewed', 'digitalmarketplace_google_analytics_enabled', 'digitalmarketplace_cookie_options_v1', 'dm_cookies_policy']; const cookiePreferences = getCookiePreferences(); const cookiePrefixes = []; cookieUpdateOptions.forEach((cookieUpdateOption) => { if (!cookiePreferences[cookieUpdateOption.cookieName]) cookiePrefixes.push(...cookieUpdateOption.cookiePrefixes); }); for (let i = 0; i < cookieList.length; i++) { const cookieName = cookieList[i]; if (cookiePrefixes.some((cookiePrefix) => cookieName.startsWith(cookiePrefix))) cookiesToRemove.push(cookieName); } getDomains().forEach((domain) => { cookiesToRemove.forEach((cookieName) => { api.remove(cookieName, { path: '/', domain: domain }); }); }); }; class CookieSettings { static moduleName = 'dm-cookie-settings'; $moudle; $radioButtonAnalyticsYes; $radioButtonAnalyticsNo; $confirmationMessage; $previousPageLink; $errorSummary; $formGroup; $warningMessage; $cookieBanner; constructor($module) { this.$moudle = $module; this.$radioButtonAnalyticsYes = this.$moudle.find('input[name=cookies-analytics][value=On]'); this.$radioButtonAnalyticsNo = this.$moudle.find('input[name=cookies-analytics][value=Off]'); this.$confirmationMessage = $('#dm-cookie-settings-confirmation'); this.$previousPageLink = $('.dm-cookie-settings__prev-page'); this.$errorSummary = $('#dm-cookie-settings-error'); this.$formGroup = this.$moudle.find('.govuk-form-group'); this.$warningMessage = $('#dm-cookie-settings-warning'); this.$cookieBanner = $('.dm-cookie-banner'); } init() { this.$moudle.on('submit', (event) => { this.submitSettingsForm(event); }); // Ensure there aren't two forms for setting cookie preferences on the same page this.hideCookieBanner(); this.setInitialFormValues(); } setInitialFormValues() { const currentCookiePreferences = getCookiePreferences(); if (!currentCookiePreferences.settings_viewed) return; this.hideWarningMessage(); // Populate the form with the existing choice let $radioButton; if (currentCookiePreferences.usage) { $radioButton = this.$radioButtonAnalyticsYes; } else { $radioButton = this.$radioButtonAnalyticsNo; } $radioButton.prop('checked', true); } submitSettingsForm(event) { event.preventDefault(); let usage; for (const formDataItem of this.$moudle.serializeArray()) { if (formDataItem.name === 'cookies-analytics') { usage = formDataItem.value === 'On'; break; } } // the cookie choice must be set when form is submitted if (usage === undefined) { this.showError(); return; } // Set the analytics cookie preferences // If 'Off' option not checked, this function will also delete any existing Google Analytics cookies setCookiePreferences(usage); this.hideWarningMessage(); this.hideError(); this.showConfirmationMessage(); } showConfirmationMessage() { const referrer = CookieSettings.getReferrerLink(); document.body.scrollTop = document.documentElement.scrollTop = 0; if (referrer && referrer !== document.location.pathname) { this.$previousPageLink.attr('href', referrer); this.$previousPageLink.css('display', 'block'); } else { this.$previousPageLink.css('display', 'none'); } this.$confirmationMessage.css('display', 'block'); } showError() { if (this.$errorSummary.length) { this.$errorSummary.css('display', 'block'); document.body.scrollTop = document.documentElement.scrollTop = 0; } this.showErrorMessage(); } showErrorMessage() { this.$formGroup.addClass('govuk-form-group--error'); const $errorMessageSpan = $(document.createElement('p')); $errorMessageSpan.attr('id', 'dm-cookie-settings-error-message'); $errorMessageSpan.addClass('govuk-error-message'); $errorMessageSpan.html('<span class="govuk-visually-hidden">Error:</span> Select yes to accept analytics cookies'); $errorMessageSpan.insertBefore('#cookie-settings-1, .govuk-radios--inline'); } hideError() { this.$errorSummary.css('display', 'none'); this.hideErrorMessage(); } hideErrorMessage() { const $errorMessage = this.$moudle.find('#dm-cookie-settings-error-message'); $errorMessage.css('display', 'none'); this.$formGroup.removeClass('govuk-form-group--error'); } hideWarningMessage() { this.$warningMessage.css('display', 'none'); } hideCookieBanner() { this.$cookieBanner.css('display', 'none'); } static getReferrerLink() { return document.referrer ? new URL(document.referrer).pathname : false; } } export { CookieSettings }; //# sourceMappingURL=settings.bundle.mjs.map