Skip to content

Commit

Permalink
Merge pull request #123 from communitiesuk/mobile-nav-bar-pckg
Browse files Browse the repository at this point in the history
attach click event to links in nav bar so that it closes the nav bar …
  • Loading branch information
Ellie-Brakoniecki authored Jul 17, 2023
2 parents 84be850 + 96a7b36 commit e971b65
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
32 changes: 24 additions & 8 deletions gov_uk_dashboards/assets/mobile-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var mobileNav = null;
* @type {boolean}
*/
var isOpen = false;

if (typeof attachEventToDash !== 'undefined') {
attachEventToDash('mobile-menu-btn', 'click', function () {
if (mobileNav === null) {
Expand All @@ -18,14 +18,30 @@ if (typeof attachEventToDash !== 'undefined') {
mobileNav.style.display = isOpen ? 'none' : 'inline-block';
isOpen = !isOpen;
}, false)

// Wait until the 'mobile-navigation-items' element is loaded
var checkExist = setInterval(function() {
var element = document.getElementById('mobile-navigation-items');
if (element) {
clearInterval(checkExist);
var links = element.getElementsByTagName('a');
attachEventsToLinks(links);
}
}, 100);

function attachEventsToLinks(links) {
for (var i = 0; i < links.length; i++) {
var link = links[i];
link.addEventListener('click', function() {
var mobileNav =document.getElementById('nav-section');
if (mobileNav) {
mobileNav.style.display = 'none';
isOpen = false;
}
}, false);
}
}

/**
*
* @param {function} func
* @param {number} time
* @returns
*/
function debounce(func, time) {
var time = time || 100; // 100 by default if no param
var timer;
Expand All @@ -46,4 +62,4 @@ window.addEventListener(
}
}
}, 150)
);
);}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
author="Department for Levelling Up, Housing and Communities",
description="Provides access to functionality common to creating a data dashboard.",
name="gov_uk_dashboards",
version="9.13.4",
version="9.13.5",
long_description=long_description,
long_description_content_type="text/markdown",
packages=find_packages(),
Expand Down

0 comments on commit e971b65

Please sign in to comment.