-
Notifications
You must be signed in to change notification settings - Fork 17
/
coronavirus-landing-page.js
55 lines (45 loc) · 2.18 KB
/
coronavirus-landing-page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//= require govuk_publishing_components/vendor/polyfills/closest
window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {};
(function (Modules) {
function CoronavirusLandingPage ($module) {
this.module = $module
}
CoronavirusLandingPage.prototype.init = function () {
// Confirm the user is on the coronavirus landing page
if (this.checkOnLandingPage()) {
this.globarBarSeen()
}
this.addAccordionOpenAllTracking()
}
CoronavirusLandingPage.prototype.checkOnLandingPage = function () {
return window.location.pathname === '/coronavirus'
}
CoronavirusLandingPage.prototype.globarBarSeen = function () {
if (window.GOVUK.cookie('global_bar_seen')) {
// Get current version of global bar, if cookie has been set
var currentBannerVersion = JSON.parse(window.GOVUK.cookie('global_bar_seen')).version
// Automatically hide the additional information section in the banner by setting the cookie
window.GOVUK.setCookie('global_bar_seen', JSON.stringify({ count: 999, version: currentBannerVersion }), { days: 84 })
}
}
// we want to track the accordion show/hide all control but this is added dynamically
// after page load by the accordion component, so we attach the event listener to the
// accordion parent element then filter click events to only track that control
CoronavirusLandingPage.prototype.addAccordionOpenAllTracking = function () {
var accordions = this.module.querySelectorAll('.gem-c-accordion')
for (var i = 0; i < accordions.length; i++) {
accordions[i].addEventListener('click', function (e) {
var clicked = e.target.closest('button')
if (clicked && clicked.classList.contains('gem-c-accordion__open-all')) {
var expanded = clicked.getAttribute('aria-expanded') === 'true'
var label = expanded ? 'Expand all' : 'Collapse all'
var action = expanded ? 'accordionOpened' : 'accordionClosed'
var options = { transport: 'beacon', label: label }
GOVUK.analytics.trackEvent('pageElementInteraction', action, options)
}
})
}
}
Modules.CoronavirusLandingPage = CoronavirusLandingPage
})(window.GOVUK.Modules)