Skip to content

Commit

Permalink
Use arrow functions to preserve this in event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Jul 20, 2023
1 parent b341c6e commit c2d402c
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/javascripts/components/cookie-banner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ CookieBanner.prototype.init = function () {
this.$cookieBanner.removeAttribute('hidden')
}

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

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

Expand Down Expand Up @@ -75,7 +75,7 @@ CookieBanner.prototype.revealConfirmationMessage = function (confirmationMessage
if (!confirmationMessage.getAttribute('tabindex')) {
confirmationMessage.setAttribute('tabindex', '-1')

confirmationMessage.addEventListener('blur', function () {
confirmationMessage.addEventListener('blur', () => {
confirmationMessage.removeAttribute('tabindex')
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/components/cookies-page.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CookiesPage.prototype.init = function () {
// Show submit button
this.$cookieForm.querySelector('.js-cookies-form-button').removeAttribute('hidden')

this.$cookieForm.addEventListener('submit', this.savePreferences.bind(this))
this.$cookieForm.addEventListener('submit', (event) => this.savePreferences(event))
}

CookiesPage.prototype.savePreferences = function (event) {
Expand Down
4 changes: 2 additions & 2 deletions src/javascripts/components/example-page.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ ExamplePage.prototype.preventFormSubmission = function ($form) {
if (!$form) {
return false
}
$form.addEventListener('submit', function (e) {
e.preventDefault()
$form.addEventListener('submit', (event) => {
event.preventDefault()
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/components/example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Example.prototype.init = function () {
return iFrameResize({ scrolling: 'omit' }, $module)
}

$module.addEventListener('load', function () {
$module.addEventListener('load', () => {
try {
iFrameResize({ scrolling: 'omit' }, $module)
} catch (err) {
Expand Down
10 changes: 5 additions & 5 deletions src/javascripts/components/navigation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Navigation.prototype.bindUIEvents = function () {
const $navToggler = this.$navToggler
const $navButtons = this.$navButtons

$navToggler.addEventListener('click', function (event) {
$navToggler.addEventListener('click', () => {
if (this.mobileNavOpen) {
$nav.classList.remove(navActiveClass)
$navToggler.classList.remove(navMenuButtonActiveClass)
Expand All @@ -95,10 +95,10 @@ Navigation.prototype.bindUIEvents = function () {

this.mobileNavOpen = true
}
}.bind(this))
})

$navButtons.forEach(($button) => {
$button.addEventListener('click', function (event) {
$button.addEventListener('click', () => {
const $nextSubNav = $button.parentNode.querySelector(subNavJSClass)

if ($nextSubNav) {
Expand All @@ -124,12 +124,12 @@ Navigation.prototype.init = function () {
this.mql = window.matchMedia('(min-width: 40.0625em)')

if ('addEventListener' in this.mql) {
this.mql.addEventListener('change', this.setHiddenStates.bind(this))
this.mql.addEventListener('change', () => this.setHiddenStates())
} else {
// addListener is a deprecated function, however addEventListener
// isn't supported by Safari < 14. We therefore add this in as
// a fallback for those browsers
this.mql.addListener(this.setHiddenStates.bind(this))
this.mql.addListener(() => this.setHiddenStates())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/components/search.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Search.prototype.init = function () {
const $input = $module.querySelector('.app-site-search__input')

// Ensure if the user stops using the search that we do not send tracking events
$input.addEventListener('blur', function (event) {
$input.addEventListener('blur', () => {
clearTimeout(inputDebounceTimer)
})

Expand Down
5 changes: 2 additions & 3 deletions src/javascripts/components/tabs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ AppTabs.prototype.init = function () {

// Add bindings to desktop tabs
this.$desktopTabs.forEach(($tab) => {
$tab.addEventListener('click', this.onClick.bind(this))
$tab.addEventListener('click', (event) => this.onClick(event))
})

// Reset all tabs and panels to closed state
Expand Down Expand Up @@ -80,8 +80,7 @@ AppTabs.prototype.enhanceMobileTabs = function () {
$button.classList.add('app-tabs__heading-button')
$button.innerHTML = $tab.innerHTML
// ...bind controls...
$button.bindClick = this.onClick.bind(this)
$button.addEventListener('click', $button.bindClick)
$button.addEventListener('click', (event) => this.onClick(event))
// ...and replace the anchor with the button
$tab.parentNode.appendChild($button)
$tab.parentNode.removeChild($tab)
Expand Down

0 comments on commit c2d402c

Please sign in to comment.