Sha256: f0005c1aa718a42dda4f0c86b544abbb07132e38100b2d5c1a05c7f8009c9496

Contents?: true

Size: 1.81 KB

Versions: 20

Compression:

Stored size: 1.81 KB

Contents

/* eslint-env jquery */
// = require govuk/components/details/details.js
window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {}
window.GOVUK.Modules.Details = window.GOVUKFrontend;

(function (Modules) {
  function GovukDetails () { }

  GovukDetails.prototype.start = function ($module) {
    this.$module = $module[0]

    var customTrackLabel = this.$module.getAttribute('data-track-label')

    // If a custom label has been provided, we can simply call the tracking module
    if (customTrackLabel) {
      var trackDetails = new window.GOVUK.Modules.GemTrackClick()
      trackDetails.start($module)
    } else {
      // If no custom label is set, we use the open/close status as the label
      var detailsClick = this.$module.querySelector('[data-details-track-click]')

      if (detailsClick) {
        detailsClick.addEventListener('click', function (event) {
          this.trackDefault(this.$module)
        }.bind(this))
      }
    }
  }

  GovukDetails.prototype.trackDefault = function (element) {
    if (window.GOVUK.analytics && window.GOVUK.analytics.trackEvent) {
      var componentStatus = (element.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.GovukDetails = GovukDetails
})(window.GOVUK.Modules)

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
govuk_publishing_components-24.13.4 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.13.3 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.13.2 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.13.1 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.13.0 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.12.0 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.11.1 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.11.0 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.10.3 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.10.2 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.10.1 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.10.0 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.9.4 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.9.3 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.9.2 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.9.1 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.9.0 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.8.0 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.7.1 app/assets/javascripts/govuk_publishing_components/components/details.js
govuk_publishing_components-24.7.0 app/assets/javascripts/govuk_publishing_components/components/details.js