Skip to content

Commit

Permalink
update initialisation method in line with govuk-frontend v5
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickpatrickpatrick committed Nov 6, 2023
1 parent 3722216 commit 15fa517
Show file tree
Hide file tree
Showing 28 changed files with 51 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* global nodeListForEach */
// = require ../vendor/polyfills/common.js
// This component relies on JavaScript from GOV.UK Frontend
// = require govuk/components/accordion/accordion.js
// = require govuk/components/accordion/accordion.bundle.js

window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {}
window.GOVUK.Modules.GovukAccordion = window.GOVUKFrontend.Accordion;
window.GOVUK.Modules.GovukAccordion = window.GOVUKFrontend.Accordion.Accordion;

(function (Modules) {
function GemAccordion ($module) {
Expand All @@ -21,6 +22,8 @@ window.GOVUK.Modules.GovukAccordion = window.GOVUKFrontend.Accordion;
// language attribute pulled from data attributes
this.$module.actions = {}
this.$module.actions.locale = this.$module.getAttribute('data-locale')

this.init()
}

GemAccordion.prototype.init = function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This component relies on JavaScript from GOV.UK Frontend
// = require govuk/components/button/button.js
// = require govuk/components/button/button.bundle.js
window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {}
window.GOVUK.Modules.GovukButton = window.GOVUKFrontend.Button
window.GOVUK.Modules.GovukButton = window.GOVUKFrontend.Button.Button
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This component relies on JavaScript from GOV.UK Frontend
// = require govuk/components/character-count/character-count.js
// = require govuk/components/character-count/character-count.bundle.js
window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {}
window.GOVUK.Modules.GovukCharacterCount = window.GOVUKFrontend.CharacterCount
window.GOVUK.Modules.GovukCharacterCount = window.GOVUKFrontend.CharacterCount.CharacterCount
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// = require govuk/components/checkboxes/checkboxes.js
window.GOVUK = window.GOVUK || {}
// = require govuk/components/checkboxes/checkboxes.bundle.js
window.GOVUK.Modules = window.GOVUK.Modules || {}
window.GOVUK.Modules.GovukCheckboxes = window.GOVUKFrontend.Checkboxes;
window.GOVUK.Modules.GovukCheckboxes = window.GOVUKFrontend.Checkboxes.Checkboxes;

(function (Modules) {
function GemCheckboxes ($module) {
this.$module = $module
this.$checkboxes = this.$module.querySelectorAll('input[type=checkbox]')
this.$nestedCheckboxes = this.$module.querySelectorAll('[data-nested=true] input[type=checkbox]')

this.init();
this.init()
}

GemCheckboxes.prototype.init = function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
this.$guidance = this.$module.querySelector('.gem-c-contextual-guidance__wrapper')
this.$inputId = this.$guidance.getAttribute('for')
this.$input = this.$module.querySelector('#' + this.$inputId)

this.init()
}

ContextualGuidance.prototype.init = function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
this.$module = $module
this.$module.cookieBannerConfirmationMessage = this.$module.querySelector('.gem-c-cookie-banner__confirmation')
this.$module.cookieBannerConfirmationMessageText = this.$module.querySelector('.gem-c-cookie-banner__confirmation-message')
this.init()
}

CookieBanner.prototype.init = function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
this.$module = $module
this.$input = this.$module.querySelector('.gem-c-input')
this.$copyButton = this.$module.querySelector('.gem-c-button')
this.init()
}

CopyToClipboard.prototype.init = function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// = require govuk/components/details/details.js
window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {}
window.GOVUK.Modules.GovukDetails = window.GOVUKFrontend.Details;
window.GOVUK.Modules = window.GOVUK.Modules || {};

(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]')
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This component relies on JavaScript from GOV.UK Frontend
// = require govuk/components/error-summary/error-summary.js
// = require govuk/components/error-summary/error-summary.bundle.js
window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {}
window.GOVUK.Modules.GovukErrorSummary = window.GOVUKFrontend.ErrorSummary
window.GOVUK.Modules.GovukErrorSummary = window.GOVUKFrontend.ErrorSummary.ErrorSummary
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
this.jshiddenClass = 'js-hidden'
this.whatDoingInput = this.$module.querySelector('[name=what_doing]')
this.whatWrongInput = this.$module.querySelector('[name=what_wrong]')

this.init()
}

Feedback.prototype.init = function () {
Expand Down Expand Up @@ -73,6 +75,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
} else {
gaClientId = window.GOVUK.cookie('_ga').split('.').slice(-2).join('.')
}

this.setHiddenValuesNotUsefulForm(gaClientId)
}.bind(this))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
(function (Modules) {
function Govspeak ($module) {
this.$module = $module
this.init()
}

Govspeak.prototype.init = function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
function ImageCard ($module) {
this.$module = $module
this.$module.youtubeLink = this.$module.querySelector('.gem-c-image-card__image-wrapper div')
this.init()
}

ImageCard.prototype.init = function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
this.$closeLink = this.$module.querySelector('.js-dismiss-link')
this.$campaignName = this.$module.getAttribute('data-intervention-name')
this.$campaignCookie = window.GOVUK.cookie('intervention_campaign') || ''
this.init()
}

Intervention.prototype.init = function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This component relies on JavaScript from GOV.UK Frontend
// = require govuk/components/header/header.js
// = require govuk/components/header/header.bundle.js
window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {}
window.GOVUK.Modules.GovukHeader = window.GOVUKFrontend.Header
window.GOVUK.Modules.GovukHeader = window.GOVUKFrontend.Header.Header
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};

this.hiddenButtons = this.$module.querySelectorAll('button[hidden]')

this.init();
this.init()
}

SuperNavigationMegaMenu.prototype.buttonHandler = function (event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
(function (Modules) {
function Metadata ($module) {
this.$module = $module
this.init()
}

Metadata.prototype.init = function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
this.$closeButton = this.$module.querySelector('.gem-c-modal-dialogue__close-button')
this.$html = document.querySelector('html')
this.$body = document.querySelector('body')
this.init()
}

ModalDialogue.prototype.init = function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
if (checkedString) {
this.attachCheckedCounter(checkedString)
}

this.init()
}

OptionSelect.prototype.toggleVisibility = function (isTabletOrLarger) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
(function (Modules) {
function PrintLink ($module) {
this.$module = $module
this.init()
}

PrintLink.prototype.init = function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// This component relies on JavaScript from GOV.UK Frontend
// = require govuk/components/radios/radios.js
// = require govuk/components/radios/radios.bundle.js

window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {}
window.GOVUK.Modules.GovukRadios = window.GOVUKFrontend.Radios
window.GOVUK.Modules.GovukRadios = window.GOVUKFrontend.Radios.Radios
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
this.$module = $module
this.$upButtons = this.$module.querySelectorAll('.js-reorderable-list-up')
this.$downButtons = this.$module.querySelectorAll('.js-reorderable-list-down')
this.init()
}

ReorderableList.prototype.init = function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
function ShowPassword ($module) {
this.$module = $module
this.input = this.$module.querySelector('.gem-c-input')
this.init()
}

ShowPassword.prototype.init = function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
this.personalisationEndpoint = '/api/personalisation/check-email-subscription?base_path=' + this.basePath
// This attribute is passed through to the personalisation API to ensure the updated button has the same button_location for analytics
if (this.buttonLocation) this.personalisationEndpoint += '&button_location=' + this.buttonLocation

this.init()
}

SinglePageNotificationButton.prototype.init = function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// This component relies on JavaScript from GOV.UK Frontend
// = require govuk/components/skip-link/skip-link.js
// = require govuk/components/skip-link/skip-link.bundle.js

window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {}
window.GOVUK.Modules.GovukSkipLink = window.GOVUKFrontend.SkipLink
window.GOVUK.Modules.GovukSkipLink = window.GOVUKFrontend.SkipLink.SkipLink
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
this.$module.activeStepClass = 'gem-c-step-nav__step--active'
this.$module.activeLinkHref = '#content'
this.$module.uniqueId = false
this.init();
this.init()
}

Gemstepnav.prototype.init = function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
for (var i = 0; i < this.tableRows.length; i++) {
this.tableRowsContent.push(this.tableRows[i].textContent.toUpperCase())
}
this.init()
}

Table.prototype.init = function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// This component relies on JavaScript from GOV.UK Frontend
// = require govuk/components/tabs/tabs.js
// = require govuk/components/tabs/tabs.bundle.js

window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {}
window.GOVUK.Modules.GovukTabs = window.GOVUKFrontend.Tabs
window.GOVUK.Modules.GovukTabs = window.GOVUKFrontend.Tabs.Tabs
5 changes: 3 additions & 2 deletions app/assets/javascripts/govuk_publishing_components/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@
var started = element.getAttribute('data-' + moduleNames[j] + '-module-started')
if (typeof GOVUK.Modules[moduleName] === 'function' && !started) {
// Vanilla JavaScript GOV.UK Modules and GOV.UK Frontend Modules
if (GOVUK.Modules[moduleName].prototype.init) {
if (GOVUK.Modules[moduleName]) {
try {
new GOVUK.Modules[moduleName](element).init()
/* eslint-disable no-new */
new GOVUK.Modules[moduleName](element)
element.setAttribute('data-' + moduleNames[j] + '-module-started', true)
} catch (e) {
// if there's a problem with the module, catch the error to allow other modules to start
Expand Down

0 comments on commit 15fa517

Please sign in to comment.