diff --git a/.eslintrc.js b/.eslintrc.js index df5016e29..d8d2e468a 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -15,12 +15,12 @@ module.exports = { 'linebreak-style': ['error', 'unix'], // enforce unix linebreaks 'no-param-reassign': [2, { props: false }], // allow modifying properties of param 'no-use-before-define': [2, { functions: false }], - // 'no-console': [ - // 'error', - // { - // allow: ['warn', 'error', 'info', 'debug'], - // }, - // ], + 'no-console': [ + 'error', + { + allow: ['warn', 'error', 'info', 'debug'], + }, + ], 'no-unused-vars': ['error', { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }], }, }; diff --git a/blocks/header/header.js b/blocks/header/header.js index 8529c9970..3a2938e4d 100644 --- a/blocks/header/header.js +++ b/blocks/header/header.js @@ -14,7 +14,7 @@ import { renderAuthDropdown } from './renderAuthDropdown.js'; import { isDesktop, - parseNavSections, + parseUrlHashTags, toggleAllNavSections, toggleMenu, } from './menu/menu.js'; @@ -65,7 +65,7 @@ export default async function decorate(block) { } }); }); - parseUrlHashTags(); + parseUrlHashTags(navSections); } const navTools = nav.querySelector('.nav-tools'); diff --git a/blocks/header/menu/menu.js b/blocks/header/menu/menu.js index b6d2578ea..86765b2b1 100644 --- a/blocks/header/menu/menu.js +++ b/blocks/header/menu/menu.js @@ -104,13 +104,34 @@ function toggleMenu(nav, navSections, forceExpanded = null) { } } +/** + * Applies parsed hash tags based on conditions + * + * @param aElement + * @param hashTags + */ +function applyCondition(aElement, hashTags) { + hashTags.forEach((hashTag) => { + const { namespace, value } = { ...hashTag }; + + if (namespace === 'display_for_') { + if (value === 'desktop_only' && !isDesktop) { + aElement.parentNode.removeChild(aElement); + } + if (value === 'mobile_only' && isDesktop) { + aElement.parentNode.removeChild(aElement); + } + } + }); +} + /** * Parses nav fragment; disable visible elements based on hash tags * * @param navSections * @returns {*} */ -function parseUrlHashTags() { +function parseUrlHashTags(navSections) { const aElements = navSections.querySelectorAll('li > a'); aElements.forEach((aElement) => { @@ -118,7 +139,8 @@ function parseUrlHashTags() { if (link) { const hashTags = parseHashTag(link); console.table(`Hash tags for ${link}`, hashTags); - // liElement.parentNode.removeChild(liElement); + + applyCondition(aElement, hashTags); } }); }