Sha256: 53ba7241e79d572f0952ef8e586ee05621f34df3a31c644be4a192e018c297d0

Contents?: true

Size: 1.85 KB

Versions: 161

Compression:

Stored size: 1.85 KB

Contents

// = require govuk/components/details/details.js
window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {}
window.GOVUK.Modules.GovukDetails = window.GOVUKFrontend.Details;

(function (Modules) {
  function GemDetails ($module) {
    this.$module = $module
    this.$summary = this.$module.querySelector('.govuk-details__summary')

    this.customTrackLabel = this.$summary.getAttribute('data-track-label')
    this.detailsClick = this.$module.querySelector('[data-details-track-click]')
  }

  GemDetails.prototype.init = function () {
    if (this.customTrackLabel) { // If a custom label has been provided, we can simply call the tracking module
      var trackDetails = new window.GOVUK.Modules.GemTrackClick(this.$summary)
      trackDetails.init()
    } else if (this.detailsClick) { // If no custom label is set, we use the open/close status as the label
      this.detailsClick.addEventListener('click', function (event) {
        this.trackDefault(this.$summary)
      }.bind(this))
    }
  }

  GemDetails.prototype.trackDefault = function (element) {
    if (window.GOVUK.analytics && window.GOVUK.analytics.trackEvent) {
      var componentStatus = (this.$module.getAttribute('open') == null) ? 'open' : 'closed'
      var trackCategory = element.getAttribute('data-track-category')
      var trackAction = element.getAttribute('data-track-action')
      var trackOptions = element.getAttribute('data-track-options')

      if (trackOptions) {
        trackOptions = JSON.parse(trackOptions)
      }

      if (typeof trackOptions !== 'object' || trackOptions === null) {
        trackOptions = {}
      }

      trackOptions.label = componentStatus

      if (trackAction && trackCategory) {
        window.GOVUK.analytics.trackEvent(trackCategory, trackAction, trackOptions)
      }
    }
  }

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

Version data entries

161 entries across 161 versions & 1 rubygems

Version Path
govuk_publishing_components-39.2.5 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-39.2.4 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-39.2.3 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-39.2.2 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-39.2.1 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-39.2.0 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-39.1.0 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-39.0.0 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-38.4.2 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-38.1.1 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-38.1.0 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-38.0.1 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-38.0.0 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-37.10.0 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-37.9.1 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-37.9.0 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-37.8.1 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-37.8.0 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-37.7.1 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-37.7.0 app/assets/javascripts/govuk_publishing_components/components/details.js