govuk_publishing_components
govuk_publishing_components copied to clipboard
Several js functions in our gem don't follow our js module pattern
We have several legacy functions which are structured differently from our typical module js pattern eg: as IIFE's (Immediately Invoked Function Expression). These are:
- [x] header-navigation
- [ ] cookie-function
- [ ] current-location
- [ ] primary-links
- [ ] trigger-event
- [ ] govspeak/barchart-enhancement
- [ ] govspeak/youtube-link-enhancement
This causes a couple of issues:
- It is in conflict with our convention of using the javascript module pattern to initialise our js
- It is possible to initialise twice if imported in 2 separate places (example of issue https://github.com/alphagov/static/pull/2572) whereas modules are only evaluated once per element
- We are unable to test these functions as them not being modules makes them behave inconsistently in a jasmine environment
To solve this, we need to investigate which of these can be converted into modules completely and the modulise these functions.
There are real chances the header-navigation
script will be deprecated if the new navigation performs well, which is why it's probably not a priority.
The modules code was designed to operate on specific chunks of markup, initialised using the data-module
attribute. The remaining files listed in this issue don't operate in this way - they either provide library like functions or are depended on by other bits of code. Specifically:
-
cookie-functions.js
gets initialised and called on every page for the cookie consent mechanism and it wouldn't make sense to tie this to a specific piece of markup -
trigger-event.js
is a library function designed mainly to help with testing, but is also called by the analytics and cookies code -
barchart-enhancement
is called by govspeak to apply to markup within a govspeak block. While it could be part of the govspeak module it is distinct enough to warrant a separate file -
youtube-link-enhancement
is similar to the above
Each of these files has a test file. While it may be possible to initialise some of this code twice, I don't think that converting them to modules is the best approach.