@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
135 lines (134 loc) • 6.19 kB
JavaScript
/**
* CodeAnalizerComment: Updated 1 imports on 2024-09-21 23:07:24
* Update:: import { check4This } to '@mikezimm/fps-core-v7/lib/logic/Links/CheckSearch;'
*/
// import { IFPSHubNav } from '../HubSiteData/fetchMyHubNav';
import { check4This, Check4 } from '../../../../logic/Links/CheckSearch';
/**
*
* 2024-12-31: Has been moved to fps-core-v7
* import { findSimilarNavNodes, findSpecificNavNodes, } from "@mikezimm/fps-core-v7/lib/restAPIs/sites/nav/functions/findNavNodes";
*
* @param hubNav
* @param prop 'Title' or 'Url' are valid properties to compare
* @param value Title or Url will be the value you are looking to find
* @param level 0, 1, or 2 defines a specific children level. 0 is visible lables/links
* @returns
*/
export function findSimilarNavNodes(hubNav, prop, value, level = 79) {
var _a;
const NavFound = [];
if (hubNav.status !== 'Success') {
// There was an error
if (check4This(Check4.checkResults_Eq_true) === true)
console.log(`fps-library-v2 Error: findSimilarNavNodes ~ 19`, hubNav);
}
else {
// const topNav: IFPSHubNavigationItem[] = hubNav.nav.navigation;
/**
* 2024-12-31: Was this but gave typescript warning in fps-core-v7. Going to use ChatGPT suggestion since I know it already works as designed
*
* const topNav: IFPSHubNavigationItem[] = hubNav[ 'nav.navigation' ] ?? hubNav.items;
*/
const topNav = (_a = hubNav['nav.navigation']) !== null && _a !== void 0 ? _a : hubNav.items;
topNav.map(navItem0 => {
navItem0.IdTree = [navItem0.Id];
if (navItem0[prop] === value && (level === 79 || level === 0))
NavFound.push(navItem0.IdTree);
if (navItem0.Children && navItem0.Children.length > 0) {
navItem0.Children.map(navItem1 => {
navItem1.IdTree = [...navItem0.IdTree, navItem1.Id];
if (navItem1[prop] === value && (level === 79 || level === 1))
NavFound.push(navItem1.IdTree);
if (navItem1.Children && navItem1.Children.length > 0) {
navItem1.Children.map(navItem2 => {
navItem2.IdTree = [...navItem1.IdTree, navItem2.Id];
if (navItem2[prop] === value && (level === 79 || level === 2))
NavFound.push(navItem2.IdTree);
});
}
});
}
});
}
if (check4This(Check4.checkResults_Eq_true) === true)
console.log(`fps-library-v2 Success: findSimilarNavNodes ~ 42`, NavFound, prop, value, level, hubNav);
return NavFound;
}
/**
*
* @param hubNav
* @param prop
* @param parentNodeTree
* @returns
*/
export function findSpecificNavNodes(hubNav, prop, parentNodeTree) {
const NavFound = [];
if (hubNav.ok !== true) {
// There was an error
console.log(`getNavNode error: hubNav =`, hubNav);
if (check4This(Check4.checkResults_Eq_true) === true)
console.log(`fps-library-v2 Error: findSpecificNavNodes ~ 61`, parentNodeTree, hubNav);
}
else {
const topNav = hubNav['nav.navigation'];
// use filter to see if value is found in first layer of children
const layer0 = topNav.filter(nav => { return nav[prop] === parentNodeTree[0]; });
layer0.map(layer => { layer.IdTree = [layer.Id]; });
if (parentNodeTree.length === 1) { // Should only be in first layer
// Found in first layer
if (layer0.length > 0)
layer0.map(layer => NavFound.push(layer.IdTree));
}
else if (parentNodeTree.length > 1) {
// matched first layer and will now look into children
// const layer1 = layer0[0].Children.filter( nav => { return nav[prop] === parentNodeTree[1] });
const layer1 = [];
layer0.map(layer => {
layer.Children.map(nav => {
nav.IdTree = [...layer.IdTree, nav.Id];
if (nav[prop] === parentNodeTree[1])
layer1.push(nav);
});
});
// console.log( `findSpecificNavNodes layer1`, layer1 );
if (parentNodeTree.length === 2) { // Should only be in second layer
// Found in second layer and should only be in second layer
if (layer1.length > 0)
layer1.map(layer => NavFound.push(layer.IdTree));
}
else if (layer1.length > 0 && parentNodeTree.length > 2) {
// matched second layer and will now look into children
const layer2 = [];
layer1.map(layer => {
layer.Children.map(nav => {
nav.IdTree = [...layer.IdTree, nav.Id];
if (nav[prop] === parentNodeTree[2])
layer2.push(nav);
});
});
// console.log( `findSpecificNavNodes layer2`, layer2 );
if (parentNodeTree.length === 3) { // Should only be in third layer
// Found in third layer and should only be in third layer
if (layer2.length > 0)
layer2.map(layer => {
NavFound.push(layer.IdTree);
});
}
else {
// Did not find in third layer, return nothing
}
}
else {
// Did not find in second layer, return nothing
}
}
else {
// Did not find in first layer, return nothing
}
}
if (check4This(Check4.checkResults_Eq_true) === true)
console.log(`fps-library-v2 Success: findSpecificNavNodes ~ 123`, NavFound, prop, parentNodeTree, hubNav);
return NavFound;
}
//# sourceMappingURL=findNavNodes.js.map