Sha256: 7cbf3055a2a379cbe5561147119a7270e876d13c7041ed83ab4f16a2277975b7

Contents?: true

Size: 1.8 KB

Versions: 66

Compression:

Stored size: 1.8 KB

Contents

/* global GovSingleConsent */
// = require govuk-single-consent/dist/singleconsent.iife.js

(function (root) {
  'use strict'
  window.GOVUK = window.GOVUK || {}

  window.GOVUK.singleConsent = {
    init: function (callback) {
      if (!window.GOVUK.useSingleConsentApi) {
        return
      }
      callback = callback || this.apiCallback
      // determine where we are and set the consent api URL accordingly
      if (!this.url) {
        this.url = 'staging'
        var environment = window.GOVUK.loadAnalytics.getEnvironment(window.GOVUK.analyticsGa4.core.trackFunctions.getHostname())
        if (environment) {
          this.url = environment.consentApiUrl
        }
      }
      // create the consent API object
      this.consentApiObj = new GovSingleConsent(callback, this.url)
    },

    apiCallback: function (consents, consentsPreferencesSet, error) {
      if (error) {
        console.error('Single consent error: ', error, window.location)
        return
      }
      if (consentsPreferencesSet) {
        if (consents && consents.usage) {
          window.GOVUK.triggerEvent(window, 'cookie-consent')
        }
      } else {
        window.GOVUK.triggerEvent(window, 'show-cookie-banner')
      }
    },

    setPreferences: function (type, options) {
      if (window.GOVUK.useSingleConsentApi) {
        try {
          switch (type) {
            case 'accept':
              this.consentApiObj.setConsents(GovSingleConsent.ACCEPT_ALL)
              break
            case 'reject':
              this.consentApiObj.setConsents(GovSingleConsent.REJECT_ALL)
              break
            default:
              this.consentApiObj.setConsents(options)
          }
        } catch (e) {
          console.error('Single consent ' + type + ' error: ', e, window.location)
        }
      }
    }
  }
}(window))

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
govuk_publishing_components-45.7.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-45.6.1 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-45.6.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-45.5.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-45.4.1 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-45.4.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-45.3.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-45.2.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-45.1.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-45.0.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-44.11.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-44.10.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-44.9.1 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-44.9.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-44.8.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-44.7.1 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-44.7.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-44.6.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-44.5.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-44.4.2 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js