From 15fa517ad1283598466f465ff7343f1e5f37c2c2 Mon Sep 17 00:00:00 2001 From: Patrick Cartlidge Date: Mon, 6 Nov 2023 13:16:24 +0000 Subject: [PATCH] update initialisation method in line with govuk-frontend v5 --- .../govuk_publishing_components/components/accordion.js | 7 +++++-- .../govuk_publishing_components/components/button.js | 4 ++-- .../components/character-count.js | 4 ++-- .../govuk_publishing_components/components/checkboxes.js | 7 +++---- .../components/contextual-guidance.js | 2 ++ .../components/cookie-banner.js | 1 + .../components/copy-to-clipboard.js | 1 + .../govuk_publishing_components/components/details.js | 5 +---- .../components/error-summary.js | 4 ++-- .../govuk_publishing_components/components/feedback.js | 3 +++ .../govuk_publishing_components/components/govspeak.js | 1 + .../govuk_publishing_components/components/image-card.js | 1 + .../govuk_publishing_components/components/intervention.js | 1 + .../components/layout-header.js | 4 ++-- .../components/layout-super-navigation-header.js | 2 +- .../govuk_publishing_components/components/metadata.js | 1 + .../components/modal-dialogue.js | 1 + .../components/option-select.js | 2 ++ .../govuk_publishing_components/components/print-link.js | 1 + .../govuk_publishing_components/components/radio.js | 5 +++-- .../components/reorderable-list.js | 1 + .../components/show-password.js | 1 + .../components/single-page-notification-button.js | 2 ++ .../govuk_publishing_components/components/skip-link.js | 5 +++-- .../components/step-by-step-nav.js | 2 +- .../govuk_publishing_components/components/table.js | 1 + .../govuk_publishing_components/components/tabs.js | 5 +++-- .../javascripts/govuk_publishing_components/modules.js | 5 +++-- 28 files changed, 51 insertions(+), 28 deletions(-) diff --git a/app/assets/javascripts/govuk_publishing_components/components/accordion.js b/app/assets/javascripts/govuk_publishing_components/components/accordion.js index e56818c510..795e3c81dd 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/accordion.js +++ b/app/assets/javascripts/govuk_publishing_components/components/accordion.js @@ -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) { @@ -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 () { diff --git a/app/assets/javascripts/govuk_publishing_components/components/button.js b/app/assets/javascripts/govuk_publishing_components/components/button.js index 7f95df0a54..bbedb79019 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/button.js +++ b/app/assets/javascripts/govuk_publishing_components/components/button.js @@ -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 diff --git a/app/assets/javascripts/govuk_publishing_components/components/character-count.js b/app/assets/javascripts/govuk_publishing_components/components/character-count.js index 3fac518369..d25a23bb90 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/character-count.js +++ b/app/assets/javascripts/govuk_publishing_components/components/character-count.js @@ -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 diff --git a/app/assets/javascripts/govuk_publishing_components/components/checkboxes.js b/app/assets/javascripts/govuk_publishing_components/components/checkboxes.js index 29a4b63485..fbaf88a7fe 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/checkboxes.js +++ b/app/assets/javascripts/govuk_publishing_components/components/checkboxes.js @@ -1,7 +1,6 @@ -// = 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) { @@ -9,7 +8,7 @@ window.GOVUK.Modules.GovukCheckboxes = window.GOVUKFrontend.Checkboxes; 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 () { diff --git a/app/assets/javascripts/govuk_publishing_components/components/contextual-guidance.js b/app/assets/javascripts/govuk_publishing_components/components/contextual-guidance.js index 6ae776b654..0b0b094426 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/contextual-guidance.js +++ b/app/assets/javascripts/govuk_publishing_components/components/contextual-guidance.js @@ -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 () { diff --git a/app/assets/javascripts/govuk_publishing_components/components/cookie-banner.js b/app/assets/javascripts/govuk_publishing_components/components/cookie-banner.js index 1e8127e67a..8a537e316b 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/cookie-banner.js +++ b/app/assets/javascripts/govuk_publishing_components/components/cookie-banner.js @@ -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 () { diff --git a/app/assets/javascripts/govuk_publishing_components/components/copy-to-clipboard.js b/app/assets/javascripts/govuk_publishing_components/components/copy-to-clipboard.js index 9310e4ca82..27305d6b12 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/copy-to-clipboard.js +++ b/app/assets/javascripts/govuk_publishing_components/components/copy-to-clipboard.js @@ -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 () { diff --git a/app/assets/javascripts/govuk_publishing_components/components/details.js b/app/assets/javascripts/govuk_publishing_components/components/details.js index 36665e5758..d1e30bbfad 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/details.js +++ b/app/assets/javascripts/govuk_publishing_components/components/details.js @@ -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]') } diff --git a/app/assets/javascripts/govuk_publishing_components/components/error-summary.js b/app/assets/javascripts/govuk_publishing_components/components/error-summary.js index 8378ed13e5..280b2cd5cb 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/error-summary.js +++ b/app/assets/javascripts/govuk_publishing_components/components/error-summary.js @@ -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 diff --git a/app/assets/javascripts/govuk_publishing_components/components/feedback.js b/app/assets/javascripts/govuk_publishing_components/components/feedback.js index 1b7912b210..368d867a31 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/feedback.js +++ b/app/assets/javascripts/govuk_publishing_components/components/feedback.js @@ -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 () { @@ -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)) diff --git a/app/assets/javascripts/govuk_publishing_components/components/govspeak.js b/app/assets/javascripts/govuk_publishing_components/components/govspeak.js index 505c63ba51..ec5dcac830 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/govspeak.js +++ b/app/assets/javascripts/govuk_publishing_components/components/govspeak.js @@ -4,6 +4,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; (function (Modules) { function Govspeak ($module) { this.$module = $module + this.init() } Govspeak.prototype.init = function () { diff --git a/app/assets/javascripts/govuk_publishing_components/components/image-card.js b/app/assets/javascripts/govuk_publishing_components/components/image-card.js index aac62e987a..fa708acd97 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/image-card.js +++ b/app/assets/javascripts/govuk_publishing_components/components/image-card.js @@ -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 () { diff --git a/app/assets/javascripts/govuk_publishing_components/components/intervention.js b/app/assets/javascripts/govuk_publishing_components/components/intervention.js index 1ea158a31a..a24526119e 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/intervention.js +++ b/app/assets/javascripts/govuk_publishing_components/components/intervention.js @@ -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 () { diff --git a/app/assets/javascripts/govuk_publishing_components/components/layout-header.js b/app/assets/javascripts/govuk_publishing_components/components/layout-header.js index b4c526acb0..8855a41023 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/layout-header.js +++ b/app/assets/javascripts/govuk_publishing_components/components/layout-header.js @@ -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 diff --git a/app/assets/javascripts/govuk_publishing_components/components/layout-super-navigation-header.js b/app/assets/javascripts/govuk_publishing_components/components/layout-super-navigation-header.js index 83765f4931..765326bd24 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/layout-super-navigation-header.js +++ b/app/assets/javascripts/govuk_publishing_components/components/layout-super-navigation-header.js @@ -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) { diff --git a/app/assets/javascripts/govuk_publishing_components/components/metadata.js b/app/assets/javascripts/govuk_publishing_components/components/metadata.js index 0c85703cf4..5165acace4 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/metadata.js +++ b/app/assets/javascripts/govuk_publishing_components/components/metadata.js @@ -4,6 +4,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; (function (Modules) { function Metadata ($module) { this.$module = $module + this.init() } Metadata.prototype.init = function () { diff --git a/app/assets/javascripts/govuk_publishing_components/components/modal-dialogue.js b/app/assets/javascripts/govuk_publishing_components/components/modal-dialogue.js index 241647d07d..533bfb1eb2 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/modal-dialogue.js +++ b/app/assets/javascripts/govuk_publishing_components/components/modal-dialogue.js @@ -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 () { diff --git a/app/assets/javascripts/govuk_publishing_components/components/option-select.js b/app/assets/javascripts/govuk_publishing_components/components/option-select.js index d05c991ced..13deb49bb1 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/option-select.js +++ b/app/assets/javascripts/govuk_publishing_components/components/option-select.js @@ -74,6 +74,8 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; if (checkedString) { this.attachCheckedCounter(checkedString) } + + this.init() } OptionSelect.prototype.toggleVisibility = function (isTabletOrLarger) { diff --git a/app/assets/javascripts/govuk_publishing_components/components/print-link.js b/app/assets/javascripts/govuk_publishing_components/components/print-link.js index 8a6bb78a86..0aafe654f6 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/print-link.js +++ b/app/assets/javascripts/govuk_publishing_components/components/print-link.js @@ -4,6 +4,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; (function (Modules) { function PrintLink ($module) { this.$module = $module + this.init() } PrintLink.prototype.init = function () { diff --git a/app/assets/javascripts/govuk_publishing_components/components/radio.js b/app/assets/javascripts/govuk_publishing_components/components/radio.js index 0ede1d0f19..d5e8079a8d 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/radio.js +++ b/app/assets/javascripts/govuk_publishing_components/components/radio.js @@ -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 diff --git a/app/assets/javascripts/govuk_publishing_components/components/reorderable-list.js b/app/assets/javascripts/govuk_publishing_components/components/reorderable-list.js index 8cf4d117b5..bf28c04390 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/reorderable-list.js +++ b/app/assets/javascripts/govuk_publishing_components/components/reorderable-list.js @@ -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 () { diff --git a/app/assets/javascripts/govuk_publishing_components/components/show-password.js b/app/assets/javascripts/govuk_publishing_components/components/show-password.js index 449e6e8a04..3c1ab249e4 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/show-password.js +++ b/app/assets/javascripts/govuk_publishing_components/components/show-password.js @@ -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 () { diff --git a/app/assets/javascripts/govuk_publishing_components/components/single-page-notification-button.js b/app/assets/javascripts/govuk_publishing_components/components/single-page-notification-button.js index 13faee1536..4f47dc8f66 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/single-page-notification-button.js +++ b/app/assets/javascripts/govuk_publishing_components/components/single-page-notification-button.js @@ -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 () { diff --git a/app/assets/javascripts/govuk_publishing_components/components/skip-link.js b/app/assets/javascripts/govuk_publishing_components/components/skip-link.js index bb2da95709..d0b58d2b96 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/skip-link.js +++ b/app/assets/javascripts/govuk_publishing_components/components/skip-link.js @@ -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 diff --git a/app/assets/javascripts/govuk_publishing_components/components/step-by-step-nav.js b/app/assets/javascripts/govuk_publishing_components/components/step-by-step-nav.js index 0e770f3208..57e39ee88f 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/step-by-step-nav.js +++ b/app/assets/javascripts/govuk_publishing_components/components/step-by-step-nav.js @@ -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 () { diff --git a/app/assets/javascripts/govuk_publishing_components/components/table.js b/app/assets/javascripts/govuk_publishing_components/components/table.js index b9b262ebcb..e998e776df 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/table.js +++ b/app/assets/javascripts/govuk_publishing_components/components/table.js @@ -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 () { diff --git a/app/assets/javascripts/govuk_publishing_components/components/tabs.js b/app/assets/javascripts/govuk_publishing_components/components/tabs.js index 512e0fa063..8d2887d040 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/tabs.js +++ b/app/assets/javascripts/govuk_publishing_components/components/tabs.js @@ -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 diff --git a/app/assets/javascripts/govuk_publishing_components/modules.js b/app/assets/javascripts/govuk_publishing_components/modules.js index 008255b844..ddd95fb83a 100644 --- a/app/assets/javascripts/govuk_publishing_components/modules.js +++ b/app/assets/javascripts/govuk_publishing_components/modules.js @@ -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