Skip to content

Commit

Permalink
Follow GOV.UK Frontend coding standards for element checks
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Jul 20, 2023
1 parent 507bb55 commit f413560
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 16 deletions.
16 changes: 12 additions & 4 deletions src/javascripts/application.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ initAll()

// Initialise cookie banner
const $cookieBanner = document.querySelector('[data-module="govuk-cookie-banner"]')
new CookieBanner($cookieBanner).init()
if ($cookieBanner) {
new CookieBanner($cookieBanner).init()
}

// Initialise analytics if consent is given
const userConsent = getConsentCookie()
Expand Down Expand Up @@ -51,12 +53,18 @@ new Navigation().init()

// Initialise search
const $searchContainer = document.querySelector('[data-module="app-search"]')
new Search($searchContainer).init()
if ($searchContainer) {
new Search($searchContainer).init()
}

// Initialise back to top
const $backToTop = document.querySelector('[data-module="app-back-to-top"]')
new BackToTop($backToTop).init()
if ($backToTop) {
new BackToTop($backToTop).init()
}

// Initialise cookie page
const $cookiesPage = document.querySelector('[data-module="app-cookies-page"]')
new CookiesPage($cookiesPage).init()
if ($cookiesPage) {
new CookiesPage($cookiesPage).init()
}
7 changes: 7 additions & 0 deletions src/javascripts/components/back-to-top.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
class BackToTop {
/**
* @param {Element} $module - HTML element
*/
constructor ($module) {
if (!($module instanceof HTMLElement)) {
return this
}

this.$module = $module
}

Expand Down
11 changes: 11 additions & 0 deletions src/javascripts/components/cookie-banner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ const cookieConfirmationAcceptSelector = '.js-cookie-banner-confirmation-accept'
const cookieConfirmationRejectSelector = '.js-cookie-banner-confirmation-reject'

class CookieBanner {
/**
* @param {Element} $module - HTML element
*/
constructor ($module) {
if (!($module instanceof HTMLElement)) {
return this
}

this.$module = $module
}

init () {
if (!this.$module) {
return
}

this.$cookieBanner = this.$module
this.$acceptButton = this.$module.querySelector(cookieBannerAcceptSelector)
this.$rejectButton = this.$module.querySelector(cookieBannerRejectSelector)
Expand Down
37 changes: 28 additions & 9 deletions src/javascripts/components/cookies-page.mjs
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
import { getConsentCookie, setConsentCookie } from './cookie-functions.mjs'

class CookiesPage {
/**
* @param {Element} $module - HTML element
*/
constructor ($module) {
this.$module = $module
if (!($module instanceof HTMLElement)) {
return this
}

const $cookieForm = $module.querySelector('.js-cookies-page-form')
if (!($cookieForm instanceof HTMLFormElement)) {
return this
}

/** @satisfies {NodeListOf<HTMLFieldSetElement>} */
const $cookieFormFieldsets = $cookieForm.querySelectorAll('.js-cookies-page-form-fieldset')
const $cookieFormButton = $cookieForm.querySelector('.js-cookies-form-button')

const $successNotification = $module.querySelector('.js-cookies-page-success')
if (!($successNotification instanceof HTMLElement)) {
return this
}

this.$page = $module
this.$cookieForm = $cookieForm
this.$cookieFormFieldsets = $cookieFormFieldsets
this.$cookieFormButton = $cookieFormButton
this.$successNotification = $successNotification
}

init () {
this.$cookiePage = this.$module

if (!this.$cookiePage) {
if (!this.$page || !this.$cookieForm) {
return
}

this.$cookieForm = this.$cookiePage.querySelector('.js-cookies-page-form')
this.$cookieFormFieldsets = this.$cookieForm.querySelectorAll('.js-cookies-page-form-fieldset')
this.$successNotification = this.$cookiePage.querySelector('.js-cookies-page-success')

this.$cookieFormFieldsets.forEach(($cookieFormFieldset) => {
this.showUserPreference($cookieFormFieldset, getConsentCookie())
$cookieFormFieldset.removeAttribute('hidden')
})

// Show submit button
this.$cookieForm.querySelector('.js-cookies-form-button').removeAttribute('hidden')
this.$cookieFormButton.removeAttribute('hidden')

this.$cookieForm.addEventListener('submit', (event) => this.savePreferences(event))
}
Expand Down
7 changes: 7 additions & 0 deletions src/javascripts/components/copy.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import ClipboardJS from 'clipboard'

class Copy {
/**
* @param {Element} $module - HTML element
*/
constructor ($module) {
if (!($module instanceof HTMLElement)) {
return this
}

this.$module = $module
}

Expand Down
7 changes: 7 additions & 0 deletions src/javascripts/components/example-page.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
class ExamplePage {
/**
* @param {Document} $module - HTML document
*/
constructor ($module) {
if (!$module) {
return this
}

this.$module = $module
}

Expand Down
3 changes: 3 additions & 0 deletions src/javascripts/components/example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import iFrameResize from 'iframe-resizer/js/iframeResizer.js'
* @param {Element} $module - HTML element to use for example
*/
class Example {
/**
* @param {Element} $module - HTML element
*/
constructor ($module) {
if (!($module instanceof HTMLIFrameElement)) {
return
Expand Down
3 changes: 3 additions & 0 deletions src/javascripts/components/navigation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const subNavActiveClass = 'app-navigation__subnav--active'
const subNavJSClass = '.js-app-navigation__subnav'

class Navigation {
/**
* @param {Element} [$module] - HTML element
*/
constructor ($module) {
this.$module = $module || document

Expand Down
8 changes: 6 additions & 2 deletions src/javascripts/components/options-table.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ class OptionsTable {

if (exampleName) {
const $tabLink = document.querySelector(`a[href="#${exampleName}-nunjucks"]`)
const $tabHeading = $tabLink ? $tabLink.parentElement : null
if (!($tabLink instanceof HTMLAnchorElement)) {
return
}

const $tabHeading = $tabLink.parentElement
const $optionsDetailsElement = document.getElementById(`options-${exampleName}-details`)

if ($tabHeading && $optionsDetailsElement) {
const $tabsElement = $optionsDetailsElement.parentElement
const $detailsSummary = $optionsDetailsElement.querySelector('.govuk-details__summary')
const $detailsText = $optionsDetailsElement.querySelector('.govuk-details__text')

if ($detailsSummary && $detailsText) {
if ($detailsSummary && $detailsText instanceof HTMLElement) {
$tabLink.setAttribute('aria-expanded', 'true')
$tabHeading.className += ' app-tabs__item--current'
$tabsElement.removeAttribute('hidden')
Expand Down
9 changes: 8 additions & 1 deletion src/javascripts/components/search.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let documentStore = null

let statusMessage = null
let searchQuery = ''
let searchCallback = function () {}
let searchCallback = function (searchResults) {}
// Results that are rendered by the autocomplete
let searchResults = []

Expand All @@ -32,7 +32,14 @@ const DEBOUNCE_TIME_TO_WAIT = function () {
}

class Search {
/**
* @param {Element} $module - HTML element
*/
constructor ($module) {
if (!($module instanceof HTMLElement)) {
return this
}

this.$module = $module
}

Expand Down
12 changes: 12 additions & 0 deletions src/javascripts/components/tabs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
*/

class AppTabs {
/**
* @param {Element} $module - HTML element
*/
constructor ($module) {
if (!($module instanceof HTMLElement)) {
return this
}

this.$module = $module
this.$mobileTabs = this.$module.querySelectorAll('.js-tabs__heading a')
this.$desktopTabs = this.$module.querySelectorAll('.js-tabs__item a')
Expand Down Expand Up @@ -48,7 +55,12 @@ class AppTabs {
*/
onClick (event) {
event.preventDefault()

const $currentTab = event.target
if (!($currentTab instanceof HTMLElement)) {
return
}

const panelId = $currentTab.getAttribute('aria-controls')
const $panel = this.getPanel(panelId)
const isTabAlreadyOpen = $currentTab.getAttribute('aria-expanded') === 'true'
Expand Down

0 comments on commit f413560

Please sign in to comment.