Skip to content

Commit

Permalink
Use arrow functions to preserve this in components
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Jul 26, 2023
1 parent 6fc61cb commit 070148b
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 54 deletions.
12 changes: 4 additions & 8 deletions src/javascripts/components/back-to-top.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ BackToTop.prototype.init = function () {
let subNavIsIntersecting = false
let subNavIntersectionRatio = 0

const observer = new window.IntersectionObserver(function (entries) {
const observer = new window.IntersectionObserver((entries) => {
// Find the elements we care about from the entries
const footerEntry = entries.find(function (entry) {
return entry.target === $footer
})
const subNavEntry = entries.find(function (entry) {
return entry.target === $subNav
})
const footerEntry = entries.find((entry) => entry.target === $footer)
const subNavEntry = entries.find((entry) => entry.target === $subNav)

// If there is an entry this means the element has changed so lets check if it's intersecting.
if (footerEntry) {
Expand All @@ -58,7 +54,7 @@ BackToTop.prototype.init = function () {
} else {
this.$module.classList.remove('app-back-to-top--hidden')
}
}.bind(this))
})

observer.observe($footer)
observer.observe($subNav)
Expand Down
4 changes: 2 additions & 2 deletions src/javascripts/components/cookie-banner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ CookieBanner.prototype.init = function () {
this.$acceptButton.addEventListener('click', this.acceptCookies.bind(this))
this.$rejectButton.addEventListener('click', this.rejectCookies.bind(this))

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

CookieBanner.prototype.hideBanner = function () {
Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/components/cookie-functions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export function resetCookies () {
// Fetch the cookies in that category
const cookiesInCategory = COOKIE_CATEGORIES[cookieType]

cookiesInCategory.forEach(function (cookie) {
cookiesInCategory.forEach((cookie) => {
// Delete cookie
Cookie(cookie, null)
})
Expand Down
8 changes: 4 additions & 4 deletions src/javascripts/components/cookies-page.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ CookiesPage.prototype.init = function () {
this.$cookieFormFieldsets = this.$cookieForm.querySelectorAll('.js-cookies-page-form-fieldset')
this.$successNotification = this.$cookiePage.querySelector('.js-cookies-page-success')

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

// Show submit button
this.$cookieForm.querySelector('.js-cookies-form-button').removeAttribute('hidden')
Expand All @@ -32,12 +32,12 @@ CookiesPage.prototype.savePreferences = function (event) {

const preferences = {}

this.$cookieFormFieldsets.forEach(function ($cookieFormFieldset) {
this.$cookieFormFieldsets.forEach(($cookieFormFieldset) => {
const cookieType = this.getCookieType($cookieFormFieldset)
const selectedItem = $cookieFormFieldset.querySelector(`input[name=${cookieType}]:checked`).value

preferences[cookieType] = selectedItem === 'yes'
}.bind(this))
})

// Save preferences to cookie and show success notification
setConsentCookie(preferences)
Expand Down
12 changes: 6 additions & 6 deletions src/javascripts/components/navigation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ Navigation.prototype.setHiddenStates = function () {
this.$nav.setAttribute('hidden', '')
}

this.$navLinks.forEach(function ($navLink) {
this.$navLinks.forEach(($navLink) => {
$navLink.setAttribute('hidden', '')
})

this.$navButtons.forEach(function ($navButton) {
this.$navButtons.forEach(($navButton) => {
$navButton.removeAttribute('hidden')
})

this.$navToggler.removeAttribute('hidden')
} else if (this.mql === null || this.mql.matches) {
this.$nav.removeAttribute('hidden')

this.$navLinks.forEach(function ($navLink) {
this.$navLinks.forEach(($navLink) => {
$navLink.removeAttribute('hidden')
})

this.$navButtons.forEach(function ($navButton) {
this.$navButtons.forEach(($navButton) => {
$navButton.setAttribute('hidden', '')
})

Expand All @@ -57,7 +57,7 @@ Navigation.prototype.setHiddenStates = function () {
Navigation.prototype.setInitialAriaStates = function () {
this.$navToggler.setAttribute('aria-expanded', 'false')

this.$navButtons.forEach(function ($button, index) {
this.$navButtons.forEach(($button, index) => {
const $nextSubNav = $button.parentNode.querySelector(subNavJSClass)

if ($nextSubNav) {
Expand Down Expand Up @@ -97,7 +97,7 @@ Navigation.prototype.bindUIEvents = function () {
}
}.bind(this))

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

Expand Down
12 changes: 6 additions & 6 deletions src/javascripts/components/search.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ Search.prototype.renderResults = function () {
if (!searchIndex || !documentStore) {
return searchCallback(searchResults)
}
const lunrSearchResults = searchIndex.query(function (q) {
const lunrSearchResults = searchIndex.query((q) => {
q.term(lunr.tokenizer(searchQuery), {
wildcard: lunr.Query.wildcard.TRAILING
})
})
searchResults = lunrSearchResults.map(function (result) {
searchResults = lunrSearchResults.map((result) => {
return documentStore[result.ref]
})
searchCallback(searchResults)
Expand Down Expand Up @@ -100,7 +100,7 @@ Search.prototype.inputValueTemplate = function (result) {

Search.prototype.resultTemplate = function (result) {
function startsWithFilter (words, query) {
return words.filter(function (word) {
return words.filter((word) => {
return word.trim().toLowerCase().indexOf(query.toLowerCase()) === 0
})
}
Expand All @@ -121,7 +121,7 @@ Search.prototype.resultTemplate = function (result) {
const aliases = result.aliases.split(', ')

// Aliases containing words that start with the query
const matchedAliases = aliases.reduce(function (aliasesFiltered, alias) {
const matchedAliases = aliases.reduce((aliasesFiltered, alias) => {
const aliasWordsMatched = startsWithFilter(alias.match(/\w+/g) || [], searchQuery)

return aliasWordsMatched.length
Expand Down Expand Up @@ -177,9 +177,9 @@ Search.prototype.init = function () {
})

const searchIndexUrl = $module.getAttribute('data-search-index')
this.fetchSearchIndex(searchIndexUrl, function () {
this.fetchSearchIndex(searchIndexUrl, () => {
this.renderResults()
}.bind(this))
})
}

export default Search
34 changes: 14 additions & 20 deletions src/javascripts/components/search.tracking.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@ export function trackConfirm (searchQuery, searchResults, result) {
const searchTerm = stripPossiblePII(searchQuery)
const products =
searchResults
.map(function (result, key) {
return {
name: result.title,
category: result.section,
list: searchTerm, // Used to match an searchTerm with results
position: (key + 1)
}
})
.filter(function (product) {
// Only return the product that matches what was clicked
return product.name === result.title
})
.map((result, key) => ({
name: result.title,
category: result.section,
list: searchTerm, // Used to match an searchTerm with results
position: (key + 1)
}))
// Only return the product that matches what was clicked
.filter((product) => product.name === result.title)

addToDataLayer({
event: 'site-search',
Expand All @@ -60,14 +56,12 @@ export function trackSearchResults (searchQuery, searchResults) {

const hasResults = (searchResults.length > 0)
// Impressions is Google Analytics lingo for what people have seen.
const impressions = searchResults.map(function (result, key) {
return {
name: result.title,
category: result.section,
list: searchTerm, // Used to match an searchTerm with results
position: (key + 1)
}
})
const impressions = searchResults.map((result, key) => ({
name: result.title,
category: result.section,
list: searchTerm, // Used to match an searchTerm with results
position: (key + 1)
}))

addToDataLayer({
event: 'site-search',
Expand Down
14 changes: 7 additions & 7 deletions src/javascripts/components/tabs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ AppTabs.prototype.init = function () {
this.enhanceMobileTabs()

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

// Reset all tabs and panels to closed state
// We also add all our default ARIA goodness here
Expand Down Expand Up @@ -72,7 +72,7 @@ AppTabs.prototype.onClick = function (event) {
*/
AppTabs.prototype.enhanceMobileTabs = function () {
// Loop through mobile tabs...
this.$mobileTabs.forEach(function ($tab) {
this.$mobileTabs.forEach(($tab) => {
// ...construct a button equivalent of each anchor...
const $button = document.createElement('button')
$button.setAttribute('aria-controls', $tab.getAttribute('aria-controls'))
Expand All @@ -85,7 +85,7 @@ AppTabs.prototype.enhanceMobileTabs = function () {
// ...and replace the anchor with the button
$tab.parentNode.appendChild($button)
$tab.parentNode.removeChild($tab)
}.bind(this))
})

// Replace the value of $mobileTabs with the new buttons
this.$mobileTabs = this.$module.querySelectorAll('.js-tabs__heading button')
Expand All @@ -95,12 +95,12 @@ AppTabs.prototype.enhanceMobileTabs = function () {
* Reset tabs and panels to closed state
*/
AppTabs.prototype.resetTabs = function () {
this.$panels.forEach(function ($panel) {
this.$panels.forEach(($panel) => {
// We don't want to hide the panel if there are no tabs present to show it
if (!$panel.classList.contains('js-tabs__container--no-tabs')) {
this.closePanel($panel.id)
}
}.bind(this))
})
}

/**
Expand Down Expand Up @@ -140,7 +140,7 @@ AppTabs.prototype.closePanel = function (panelId) {
*/
AppTabs.prototype.getMobileTab = function (panelId) {
let result = null
this.$mobileTabs.forEach(function ($tab) {
this.$mobileTabs.forEach(($tab) => {
if ($tab.getAttribute('aria-controls') === panelId) {
result = $tab
}
Expand Down

0 comments on commit 070148b

Please sign in to comment.