Sha256: 7cbf3055a2a379cbe5561147119a7270e876d13c7041ed83ab4f16a2277975b7

Contents?: true

Size: 1.8 KB

Versions: 68

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

68 entries across 68 versions & 1 rubygems

Version Path
govuk_publishing_components-41.1.2 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-41.1.1 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-41.1.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-41.0.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-40.1.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-40.0.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-39.2.5 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-39.2.4 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-39.2.3 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-39.2.2 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-39.2.1 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-39.2.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-39.1.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-39.0.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-38.4.2 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-38.1.1 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-38.1.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-38.0.1 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-38.0.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js
govuk_publishing_components-37.10.0 app/assets/javascripts/govuk_publishing_components/single-consent-functions.js