-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MWPW_137257 sidenav block #118
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,97 @@ | ||
import { createTag } from '../../scripts/utils.js'; | ||
import { html, css, LitElement } from '/creativecloud/deps/lit-all.min.js'; | ||
|
||
import('/creativecloud/deps/lit-all.min.js'); | ||
import('/creativecloud/deps/merch-spectrum.min.js'); | ||
import('../../deps/sidenav.js'); | ||
import('/creativecloud/deps/merch-sidenav.js'); | ||
|
||
const getValueFromLabel = (label) => label | ||
const getValueFromLabel = (content) => content | ||
.trim() | ||
.toLowerCase() | ||
.replace(/(and|-)/g, '') | ||
.replace(/( and )/g, ' ') | ||
.replace(/-/g, '') | ||
.replace(/\s+/g, '-'); | ||
|
||
const appendSidenavItem = (parent, li) => { | ||
const label = li.childNodes[0]?.textContent; | ||
const value = getValueFromLabel(label); | ||
const item = createTag('sp-sidenav-item', { label, value }); | ||
parent.append(item); | ||
const childList = li.querySelector('ul'); | ||
if (childList) { | ||
childList.querySelectorAll('li').forEach((grandChild) => { | ||
appendSidenavItem(item, grandChild); | ||
}); | ||
const getCategories = (arrayCategories) => { | ||
const tag = createTag('merch-sidenav-list', { deeplink: 'category' }); | ||
const mapParents = {}; | ||
arrayCategories.forEach((item) => { | ||
if (item.Name?.length > 0) { | ||
const parentName = item.Name.split('|')[0].trim(); | ||
if (!mapParents[parentName]) { | ||
mapParents[parentName] = createTag('sp-sidenav-item', { label: parentName, value: getValueFromLabel(parentName) }); | ||
tag.append(mapParents[parentName]); | ||
} | ||
const childName = item.Name.split('|')[1]?.trim(); | ||
if (childName) { | ||
const childNode = createTag('sp-sidenav-item', { label: childName, value: getValueFromLabel(childName) }); | ||
mapParents[parentName].append(childNode); | ||
} | ||
} | ||
}); | ||
return tag; | ||
}; | ||
|
||
const getTypes = (arrayTypes) => { | ||
const tag = createTag('merch-sidenav-checkbox-group', { title: 'Types', deeplink: 'types' }); | ||
arrayTypes.forEach((item) => { | ||
if (item.Name?.length > 0) { | ||
const checkbox = createTag('sp-checkbox', { emphasized: '', name: getValueFromLabel(item.Name) }); | ||
checkbox.append(document.createTextNode(item.Name)); | ||
tag.append(checkbox); | ||
} | ||
}); | ||
return tag; | ||
}; | ||
|
||
const appendFilters = async (root, link) => { | ||
const payload = link.textContent.trim(); | ||
const resp = await fetch(payload); | ||
if (resp.ok) { | ||
const json = await resp.json(); | ||
const arrayCategories = json.data.filter((item) => item.Type === 'Categories'); | ||
if (arrayCategories.length > 0) { | ||
root.append(getCategories(arrayCategories)); | ||
} | ||
const arrayTypes = json.data.filter((item) => item.Type === 'Types'); | ||
if (arrayTypes.length > 0) { | ||
root.append(getTypes(arrayTypes)); | ||
} | ||
} | ||
parent.append(item); | ||
}; | ||
|
||
export default function init(el) { | ||
const paragraph = el.querySelector('p'); | ||
paragraph?.remove(); | ||
const root = el.querySelector('ul'); | ||
if (root) { | ||
const title = paragraph?.textContent; | ||
const rootNav = createTag('filter-sidenav', { title }); | ||
root.querySelectorAll(':scope > li').forEach((li) => { | ||
appendSidenavItem(rootNav, li); | ||
}); | ||
root.parentNode.replaceChild(rootNav, root); | ||
function appendSearch(rootNav, searchText) { | ||
if (searchText) { | ||
const spectrumSearch = createTag('sp-search', { placeholder: searchText }); | ||
const search = createTag('merch-search', { deeplink: 'search' }); | ||
search.append(spectrumSearch); | ||
rootNav.append(search); | ||
} | ||
} | ||
|
||
function appendResources(rootNav, resourceLink) { | ||
const literals = resourceLink.textContent.split(':'); | ||
const title = literals[0].trim(); | ||
const el = createTag('merch-sidenav-list', { title }); | ||
const label = literals[1].trim(); | ||
const link = createTag('sp-sidenav-item', { href: resourceLink.href }); | ||
if (resourceLink.href && resourceLink.href.startsWith('http')) { | ||
link.append(document.createTextNode(label)); | ||
const icon = createTag('sp-icon-link-out-light', { class: 'right', slot: 'icon' }); | ||
link.append(icon); | ||
} | ||
el.append(link); | ||
rootNav.append(el); | ||
} | ||
|
||
export default async function init(el) { | ||
const title = el.querySelector('h2')?.textContent.trim(); | ||
const rootNav = createTag('merch-sidenav', { title }); | ||
const searchText = el.querySelector('p')?.textContent.trim(); | ||
appendSearch(rootNav, searchText); | ||
const links = el.querySelectorAll('a'); | ||
await appendFilters(rootNav, links[0]); | ||
if (links.length > 1) { | ||
appendResources(rootNav, links[1]); | ||
} | ||
el.replaceWith(rootNav); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fetch should be surrounded with try catch and failure should be gracefully handled.