Skip to content

Commit

Permalink
Convert all components to ES2015 classes
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Dec 4, 2023
1 parent 59a30e4 commit 108142c
Show file tree
Hide file tree
Showing 11 changed files with 672 additions and 652 deletions.
2 changes: 1 addition & 1 deletion src/javascripts/application.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ $tabs.forEach(($tabs) => {
})

// Do this after initialising tabs
OptionsTable.init()
new OptionsTable().init()

// Add copy to clipboard to code blocks inside tab containers
const $codeBlocks = document.querySelectorAll('[data-module="app-copy"] pre')
Expand Down
102 changes: 52 additions & 50 deletions src/javascripts/components/back-to-top.mjs
Original file line number Diff line number Diff line change
@@ -1,63 +1,65 @@
function BackToTop($module) {
this.$module = $module
}

BackToTop.prototype.init = function () {
if (!this.$module) {
return
class BackToTop {
constructor($module) {
this.$module = $module
}

// Check if we can use Intersection Observers
if (!('IntersectionObserver' in window)) {
// If there's no support fallback to regular behaviour
// Since JavaScript is enabled we can remove the default hidden state
return this.$module.classList.remove('app-back-to-top--hidden')
}
init() {
if (!this.$module) {
return
}

const $footer = document.querySelector('.app-footer')
const $subNav = document.querySelector('.app-subnav')
// Check if we can use Intersection Observers
if (!('IntersectionObserver' in window)) {
// If there's no support fallback to regular behaviour
// Since JavaScript is enabled we can remove the default hidden state
return this.$module.classList.remove('app-back-to-top--hidden')
}

// Check if there is anything to observe
if (!$footer || !$subNav) {
return
}
const $footer = document.querySelector('.app-footer')
const $subNav = document.querySelector('.app-subnav')

let footerIsIntersecting = false
let subNavIsIntersecting = false
let subNavIntersectionRatio = 0
// Check if there is anything to observe
if (!$footer || !$subNav) {
return
}

const observer = new window.IntersectionObserver((entries) => {
// Find the elements we care about from the entries
const footerEntry = entries.find((entry) => entry.target === $footer)
const subNavEntry = entries.find((entry) => entry.target === $subNav)
let footerIsIntersecting = false
let subNavIsIntersecting = false
let subNavIntersectionRatio = 0

// If there is an entry this means the element has changed so lets check if it's intersecting.
if (footerEntry) {
footerIsIntersecting = footerEntry.isIntersecting
}
if (subNavEntry) {
subNavIsIntersecting = subNavEntry.isIntersecting
subNavIntersectionRatio = subNavEntry.intersectionRatio
}
const observer = new window.IntersectionObserver((entries) => {
// Find the elements we care about from the entries
const footerEntry = entries.find((entry) => entry.target === $footer)
const subNavEntry = entries.find((entry) => entry.target === $subNav)

// If the subnav or the footer not visible then fix the back to top link to follow the user
if (subNavIsIntersecting || footerIsIntersecting) {
this.$module.classList.remove('app-back-to-top--fixed')
} else {
this.$module.classList.add('app-back-to-top--fixed')
}
// If there is an entry this means the element has changed so lets check if it's intersecting.
if (footerEntry) {
footerIsIntersecting = footerEntry.isIntersecting
}
if (subNavEntry) {
subNavIsIntersecting = subNavEntry.isIntersecting
subNavIntersectionRatio = subNavEntry.intersectionRatio
}

// If the subnav is visible but you can see it all at once, then a back to top link is likely not as useful.
// We hide the link but make it focusable for screen readers users who might still find it useful.
if (subNavIsIntersecting && subNavIntersectionRatio === 1) {
this.$module.classList.add('app-back-to-top--hidden')
} else {
this.$module.classList.remove('app-back-to-top--hidden')
}
})
// If the subnav or the footer not visible then fix the back to top link to follow the user
if (subNavIsIntersecting || footerIsIntersecting) {
this.$module.classList.remove('app-back-to-top--fixed')
} else {
this.$module.classList.add('app-back-to-top--fixed')
}

// If the subnav is visible but you can see it all at once, then a back to top link is likely not as useful.
// We hide the link but make it focusable for screen readers users who might still find it useful.
if (subNavIsIntersecting && subNavIntersectionRatio === 1) {
this.$module.classList.add('app-back-to-top--hidden')
} else {
this.$module.classList.remove('app-back-to-top--hidden')
}
})

observer.observe($footer)
observer.observe($subNav)
observer.observe($footer)
observer.observe($subNav)
}
}

export default BackToTop
150 changes: 75 additions & 75 deletions src/javascripts/components/cookie-banner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,95 +7,95 @@ const cookieMessageSelector = '.js-cookie-banner-message'
const cookieConfirmationAcceptSelector = '.js-cookie-banner-confirmation-accept'
const cookieConfirmationRejectSelector = '.js-cookie-banner-confirmation-reject'

function CookieBanner($module) {
this.$module = $module
}

CookieBanner.prototype.init = function () {
this.$cookieBanner = this.$module
this.$acceptButton = this.$module.querySelector(cookieBannerAcceptSelector)
this.$rejectButton = this.$module.querySelector(cookieBannerRejectSelector)
this.$cookieMessage = this.$module.querySelector(cookieMessageSelector)
this.$cookieConfirmationAccept = this.$module.querySelector(
cookieConfirmationAcceptSelector
)
this.$cookieConfirmationReject = this.$module.querySelector(
cookieConfirmationRejectSelector
)
this.$cookieBannerHideButtons = this.$module.querySelectorAll(
cookieBannerHideButtonSelector
)

// Exit if no cookie banner module
// or if we're on the cookies page to avoid circular journeys
if (!this.$cookieBanner || this.onCookiesPage()) {
return
class CookieBanner {
constructor($module) {
this.$module = $module
}

// Show the cookie banner to users who have not consented or have an
// outdated consent cookie
const currentConsentCookie = CookieFunctions.getConsentCookie()

if (
!currentConsentCookie ||
!CookieFunctions.isValidConsentCookie(currentConsentCookie)
) {
// If the consent cookie version is not valid, we need to remove any cookies which have been
// set previously
CookieFunctions.resetCookies()

this.$cookieBanner.removeAttribute('hidden')
init() {
this.$cookieBanner = this.$module
this.$acceptButton = this.$module.querySelector(cookieBannerAcceptSelector)
this.$rejectButton = this.$module.querySelector(cookieBannerRejectSelector)
this.$cookieMessage = this.$module.querySelector(cookieMessageSelector)
this.$cookieConfirmationAccept = this.$module.querySelector(
cookieConfirmationAcceptSelector
)
this.$cookieConfirmationReject = this.$module.querySelector(
cookieConfirmationRejectSelector
)
this.$cookieBannerHideButtons = this.$module.querySelectorAll(
cookieBannerHideButtonSelector
)

// Exit if no cookie banner module
// or if we're on the cookies page to avoid circular journeys
if (!this.$cookieBanner || this.onCookiesPage()) {
return
}

// Show the cookie banner to users who have not consented or have an
// outdated consent cookie
const currentConsentCookie = CookieFunctions.getConsentCookie()

if (
!currentConsentCookie ||
!CookieFunctions.isValidConsentCookie(currentConsentCookie)
) {
// If the consent cookie version is not valid, we need to remove any cookies which have been
// set previously
CookieFunctions.resetCookies()

this.$cookieBanner.removeAttribute('hidden')
}

this.$acceptButton.addEventListener('click', () => this.acceptCookies())
this.$rejectButton.addEventListener('click', () => this.rejectCookies())

this.$cookieBannerHideButtons.forEach(($cookieBannerHideButton) => {
$cookieBannerHideButton.addEventListener('click', () => this.hideBanner())
})
}

this.$acceptButton.addEventListener('click', () => this.acceptCookies())
this.$rejectButton.addEventListener('click', () => this.rejectCookies())

this.$cookieBannerHideButtons.forEach(($cookieBannerHideButton) => {
$cookieBannerHideButton.addEventListener('click', () => this.hideBanner())
})
}
hideBanner() {
this.$cookieBanner.setAttribute('hidden', true)
}

CookieBanner.prototype.hideBanner = function () {
this.$cookieBanner.setAttribute('hidden', true)
}
acceptCookies() {
// Do actual cookie consent bit
CookieFunctions.setConsentCookie({ analytics: true })

CookieBanner.prototype.acceptCookies = function () {
// Do actual cookie consent bit
CookieFunctions.setConsentCookie({ analytics: true })
// Hide banner and show confirmation message
this.$cookieMessage.setAttribute('hidden', true)
this.revealConfirmationMessage(this.$cookieConfirmationAccept)
}

// Hide banner and show confirmation message
this.$cookieMessage.setAttribute('hidden', true)
this.revealConfirmationMessage(this.$cookieConfirmationAccept)
}
rejectCookies() {
// Do actual cookie consent bit
CookieFunctions.setConsentCookie({ analytics: false })

CookieBanner.prototype.rejectCookies = function () {
// Do actual cookie consent bit
CookieFunctions.setConsentCookie({ analytics: false })
// Hide banner and show confirmation message
this.$cookieMessage.setAttribute('hidden', true)
this.revealConfirmationMessage(this.$cookieConfirmationReject)
}

// Hide banner and show confirmation message
this.$cookieMessage.setAttribute('hidden', true)
this.revealConfirmationMessage(this.$cookieConfirmationReject)
}
revealConfirmationMessage(confirmationMessage) {
confirmationMessage.removeAttribute('hidden')

CookieBanner.prototype.revealConfirmationMessage = function (
confirmationMessage
) {
confirmationMessage.removeAttribute('hidden')
// Set tabindex to -1 to make the confirmation banner focusable with JavaScript
if (!confirmationMessage.getAttribute('tabindex')) {
confirmationMessage.setAttribute('tabindex', '-1')

// Set tabindex to -1 to make the confirmation banner focusable with JavaScript
if (!confirmationMessage.getAttribute('tabindex')) {
confirmationMessage.setAttribute('tabindex', '-1')
confirmationMessage.addEventListener('blur', () => {
confirmationMessage.removeAttribute('tabindex')
})
}

confirmationMessage.addEventListener('blur', () => {
confirmationMessage.removeAttribute('tabindex')
})
confirmationMessage.focus()
}

confirmationMessage.focus()
}

CookieBanner.prototype.onCookiesPage = function () {
return window.location.pathname === '/cookies/'
onCookiesPage() {
return window.location.pathname === '/cookies/'
}
}

export default CookieBanner
Loading

0 comments on commit 108142c

Please sign in to comment.