Skip to content

Commit

Permalink
PP-13452 remove nunjucksify dependency from build toolchain
Browse files Browse the repository at this point in the history
- bundle nunjucks in clientside code and inline multiselect template
  • Loading branch information
nlsteers committed Jan 6, 2025
1 parent 18df97b commit e749ad6
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 90 deletions.
10 changes: 1 addition & 9 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,7 @@ module.exports = function (grunt) {
options: {
browserifyOptions: {
standalone: 'module'
},
transform: [
[
'nunjucksify',
{
extension: '.njk'
}
]
]
}
}
},

Expand Down
63 changes: 45 additions & 18 deletions app/browsered/multi-select.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
// TODO: we should probably do some browser testing in this project to prove this all works as intended

const multiSelect = require('../views/includes/multi-select.njk')

// Polyfills introduced as a temporary fix to make Smoketests pass. See PP-3489
require('./polyfills')
const nunjucks = require('nunjucks')

const multiSelectTemplate = `
<div id="{{ id }}__container" class="multi-select">
<button type="button"
class="multi-select__title"
id="{{ id }}" aria-expanded="false">
<div><span class="govuk-visually-hidden">Currently selected: </span><span
class="multi-select-current-selections"></span></div>
</button>
<div role="group" aria-labelledby="option-select-title-{{ name }}"
class="multi-select-dropdown"
id="list-of-sectors-{{ name }}"
style="visibility:hidden;">
<button type="button" class="govuk-button govuk-button--secondary multi-select-dropdown__close-button">Close</button>
<div class="multi-select-dropdown__inner-container govuk-checkboxes govuk-checkboxes--small" data-module="govuk-checkboxes">
{% for item in items %}
<div class="govuk-checkboxes__item">
<input class="govuk-checkboxes__input" name="{{ name }}" value="{{ item.value }}" id="{{ item.id }}"
type="checkbox" {% if item.checked %}checked{% endif %}>
<label for="{{ item.id }}" class="govuk-label govuk-checkboxes__label">{{ item.text }}</label>
</div>
{% endfor %}
</div>
</div>
</div>
`

const MAXIMUM_VISIBLE_ITEMS = 8.5 // Maximum amount of items to show in dropdown
const MINIMUM_VISIBLE_ITEMS = 3.5 // Minimum amount of items to show in dropdown (assuming total is larger than this value)
Expand Down Expand Up @@ -43,7 +67,7 @@ function progressivelyEnhanceSelects () {
return { value, id, checked, text }
})
}
select.outerHTML = multiSelect(configuration)
select.outerHTML = nunjucks.renderString(multiSelectTemplate, configuration)
const newMultiSelect = document.getElementById(`${configuration.id}__container`)

const openButton = [...newMultiSelect.querySelectorAll(OPEN_BUTTON_SELECTOR)][0]
Expand All @@ -53,20 +77,6 @@ function progressivelyEnhanceSelects () {
const dropdown = [...newMultiSelect.querySelectorAll(DROPDOWN_SELECTOR)][0]
const scrollContainer = [...newMultiSelect.querySelectorAll(SCROLL_CONTAINER_SELECTOR)][0]

// close if user clicks outside the dropdown
document.addEventListener('click', (event) => {
const dropdowns = document.querySelectorAll(DROPDOWN_SELECTOR)
dropdowns.forEach(dropdown => {
if (!dropdown.contains(event.target) &&
!event.target.closest(OPEN_BUTTON_SELECTOR)) {
dropdown.style.visibility = 'hidden'
dropdown.closest(TOP_LEVEL_SELECTOR)
.querySelector(OPEN_BUTTON_SELECTOR)
.setAttribute('aria-expanded', false)
}
})
}, false)

openButton.addEventListener('click', onOpenButtonClick, false)
closeButton.addEventListener('click', onCloseButtonClick, false)
items.forEach(item => {
Expand All @@ -82,6 +92,7 @@ function progressivelyEnhanceSelects () {
})

closeMultiSelectOnEscapeKeypress()
closeMultiSelectOnOutOfBoundsClick()
}

const closeMultiSelectOnEscapeKeypress = function () {
Expand All @@ -102,6 +113,22 @@ const closeMultiSelectOnEscapeKeypress = function () {
}
}

const closeMultiSelectOnOutOfBoundsClick = function () {
// close if user clicks outside the dropdown
document.addEventListener('click', (event) => {
const dropdowns = document.querySelectorAll(DROPDOWN_SELECTOR)
dropdowns.forEach(dropdown => {
if (!dropdown.contains(event.target) &&
!event.target.closest(OPEN_BUTTON_SELECTOR)) {
dropdown.style.visibility = 'hidden'
dropdown.closest(TOP_LEVEL_SELECTOR)
.querySelector(OPEN_BUTTON_SELECTOR)
.setAttribute('aria-expanded', false)
}
})
}, false)
}

const onCloseButtonClick = event => {
const { target } = event
event.stopPropagation()
Expand Down
15 changes: 5 additions & 10 deletions app/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ const logger = createLogger({
]
})

const nsDebugFormat = printf(({ level, message, timestamp }) => {
const simpleLoggingFormat = printf(({ level, message, timestamp, ...metadata }) => {
return `${timestamp} [${level}]: ${message}`
})

const nsDebugLogger = createLogger({
const simpleLogger = createLogger({
format: combine(
colorize({
colors: {
Expand All @@ -41,7 +41,7 @@ const nsDebugLogger = createLogger({
timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
nsDebugFormat
simpleLoggingFormat
),
transports: [
new transports.Console({
Expand All @@ -50,14 +50,9 @@ const nsDebugLogger = createLogger({
]
})

const nsDebug = process.env.GOVUK_PAY__USE_BASIC_LOGGER === 'true'

module.exports = (loggerName) => {
if (process.env.GOVUK_PAY__USE_BASIC_LOGGER === 'true') {
return console
return simpleLogger
}
const childLogger = nsDebug
? nsDebugLogger.child({ logger_name: loggerName })
: logger.child({ logger_name: loggerName })
return addSentryToErrorLevel(childLogger)
return addSentryToErrorLevel(logger.child({ logger_name: loggerName }))
}
23 changes: 0 additions & 23 deletions app/views/includes/multi-select.njk

This file was deleted.

37 changes: 9 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"dependencies": {
"@aws-sdk/client-s3": "3.693.0",
"@aws-sdk/s3-request-presigner": "3.693.0",
"@govuk-pay/pay-js-commons": "^6.0.15",
"@govuk-pay/pay-js-commons": "^6.0.22",
"@govuk-pay/pay-js-metrics": "^1.0.6",
"@sentry/node": "6.12.0",
"accessible-autocomplete": "2.0.4",
Expand Down Expand Up @@ -171,7 +171,6 @@
"nock": "13.3.3",
"nodemon": "3.0.1",
"notp": "2.0.3",
"nunjucksify": "2.2.0",
"portfinder": "1.0.32",
"proxyquire": "~2.1.0",
"sass": "^1.66.1",
Expand Down

0 comments on commit e749ad6

Please sign in to comment.