Skip to content

Commit

Permalink
[MWPW-157924] - Added functionality to disable Active Element Detecti…
Browse files Browse the repository at this point in the history
…on (#2892)

Added functionality to disable Active Element Detection
  • Loading branch information
Deva309 committed Sep 19, 2024
1 parent 54c3cc0 commit e3844f2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
11 changes: 9 additions & 2 deletions libs/blocks/global-navigation/global-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {
addMepHighlightAndTargetId,
isDarkMode,
darkIcons,
setDisableAEDState,
getDisableAEDState,
} from './utilities/utilities.js';

import { replaceKey, replaceKeyArray } from '../../features/placeholders.js';
Expand Down Expand Up @@ -837,8 +839,9 @@ class Gnav {

if (!hasActiveLink()) {
const sections = this.elements.mainNav.querySelectorAll('.feds-navItem--section');
const disableAED = getDisableAEDState();

if (sections.length === 1) {
if (!disableAED && sections.length === 1) {
sections[0].classList.add(selectors.activeNavItem.slice(1));
setActiveLink(true);
}
Expand Down Expand Up @@ -1024,7 +1027,11 @@ const getSource = async () => {
export default async function init(block) {
try {
const { mep } = getConfig();
const url = await getSource();
const sourceUrl = await getSource();
const [url, hash = ''] = sourceUrl.split('#');
if (hash === '_noActiveItem') {
setDisableAEDState();
}
const content = await fetchAndProcessPlainHtml({ url });
if (!content) return null;
const gnav = new Gnav({
Expand Down
14 changes: 12 additions & 2 deletions libs/blocks/global-navigation/utilities/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function getAnalyticsValue(str, index) {

export function getExperienceName() {
const experiencePath = getMetadata('gnav-source');
const explicitExperience = experiencePath?.split('/').pop();
const explicitExperience = experiencePath?.split('#')[0]?.split('/').pop();
if (explicitExperience?.length
&& explicitExperience !== 'gnav') return explicitExperience;

Expand Down Expand Up @@ -257,14 +257,24 @@ export function setActiveDropdown(elem) {
});
}

// Disable AED(Active Element Detection)
export const [setDisableAEDState, getDisableAEDState] = (() => {
let disableAED = false;
return [
() => { disableAED = true; },
() => disableAED,
];
})();

export const [hasActiveLink, setActiveLink, getActiveLink] = (() => {
let activeLinkFound;

return [
() => activeLinkFound,
(val) => { activeLinkFound = !!val; },
(area) => {
if (hasActiveLink() || !(area instanceof HTMLElement)) return null;
const disableAED = getDisableAEDState();
if (disableAED || hasActiveLink() || !(area instanceof HTMLElement)) return null;
const { origin, pathname } = window.location;
const url = `${origin}${pathname}`;
const activeLink = [
Expand Down
9 changes: 9 additions & 0 deletions test/blocks/global-navigation/global-navigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,15 @@ describe('global navigation', () => {
fetchStub.calledOnceWith('http://localhost:2000/gnav.plain.html'),
).to.be.true;
});

it('disable AED(Active Element Detetction) if gnav-souce used with hash "#_noActiveItem" modifier', async () => {
const gnavMeta = toFragment`<meta name="gnav-source" content="https://adobe.com/federal${customPath}#_noActiveItem">`;
document.head.append(gnavMeta);
document.body.replaceChildren(toFragment`<header class="global-navigation"></header>`);
await initGnav(document.body.querySelector('header'));
const isActiveElement = !!document.querySelector('.global-navigation .feds-navItem--active');
expect(isActiveElement).to.be.false;
});
});

describe('Dynamic nav', () => {
Expand Down

0 comments on commit e3844f2

Please sign in to comment.