Sha256: 3a03ad94e71a0a9810d884b9d827f2c6483e41b3eae755704eeb0042c71bc161

Contents?: true

Size: 1.9 KB

Versions: 140

Compression:

Stored size: 1.9 KB

Contents

window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {};

(function (Modules) {
  function Intervention ($module) {
    this.$module = $module
    this.$closeLink = this.$module.querySelector('.js-dismiss-link')
    this.$campaignName = this.$module.getAttribute('data-intervention-name')
    this.$campaignCookie = window.GOVUK.cookie('intervention_campaign') || ''
  }

  Intervention.prototype.init = function () {
    this.$cookieHasCampaign = this.cookieHasCampaign()

    if (this.$closeLink) {
      this.$module.close = this.handleClose.bind(this)
      this.$closeLink.addEventListener('click', this.$module.close)
    }

    if (this.$cookieHasCampaign) {
      this.hideBanner()
    }
  }

  Intervention.prototype.handleClose = function (event) {
    event.preventDefault()

    if (this.$cookieHasCampaign) {
      this.setCookies()
    }
    this.hideBanner()
  }

  Intervention.prototype.cookieHasCampaign = function () {
    if (this.$campaignCookie) {
      return this.cookieValues().includes(this.$campaignName)
    }
    return false
  }

  Intervention.prototype.cookieValues = function () {
    if (this.$campaignCookie !== null && this.$campaignCookie.length > 0) {
      return this.$campaignCookie.split(',')
    }
  }

  Intervention.prototype.appendCookieValues = function () {
    return this.$campaignCookie + ',' + this.$campaignName
  }

  Intervention.prototype.setCookies = function () {
    if (this.$campaignCookie) {
      window.GOVUK.setCookie('intervention_campaign', this.appendCookieValues(), { days: 30 })
    } else {
      window.GOVUK.setCookie('intervention_campaign', this.$campaignName, { days: 30 })
    }
  }

  Intervention.prototype.hideBanner = function () {
    if (!this.$cookieHasCampaign) {
      this.setCookies()
    }

    this.$module.hidden = true
    this.$module.style.display = 'none'
  }

  Modules.Intervention = Intervention
})(window.GOVUK.Modules)

Version data entries

140 entries across 140 versions & 1 rubygems

Version Path
govuk_publishing_components-51.1.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-51.0.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-50.0.1 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-50.0.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-49.1.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-49.0.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-48.0.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-47.0.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-46.4.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-46.3.1 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-46.3.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-46.2.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-46.1.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-46.0.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-45.10.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-45.9.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-45.8.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-45.7.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-45.6.1 app/assets/javascripts/govuk_publishing_components/components/intervention.js
govuk_publishing_components-45.6.0 app/assets/javascripts/govuk_publishing_components/components/intervention.js