Update Current Navigation to version 1.1.4 with new features and improvements

This commit is contained in:
Torsten Brendgen
2026-08-18 21:18:11 +02:00
parent 9b73bb4614
commit 61a968562d
9 changed files with 100 additions and 18 deletions
+45 -5
View File
@@ -36,6 +36,14 @@ export function resolveTermUrl(term: ICurrentNavigationTerm, siteCollectionUrl:
export function normalizeNavigationPath(value: string): string {
let path: string = String(value || '').trim().toLowerCase();
path = path.replace(/^https?:\/\/[^/]+/i, '').split('#')[0].split('?')[0].replace(/\/+$/, '');
try { path = decodeURIComponent(path); } catch (error) { /* Keep malformed URLs comparable as supplied. */ }
return path || '/';
}
export function normalizeRootWebPath(value: string): string {
let path: string = normalizeNavigationPath(value);
path = path.replace(/\/(?:sitepages|pages)\/.*\.aspx$/i, '');
path = path.replace(/\/(?:default|home)\.aspx$/i, '');
return path || '/';
}
@@ -45,26 +53,58 @@ export function selectCurrentNavigationRoot(
siteCollectionUrl: string
): ICurrentNavigationTerm | undefined {
const webPath: string = normalizeNavigationPath(currentWebUrl);
const hierarchyRoot: ICurrentNavigationTerm = selectRootByTermHierarchy(
terms, webPath, siteCollectionUrl
);
if (hierarchyRoot) { return hierarchyRoot; }
let selected: ICurrentNavigationTerm | undefined;
let selectedScore: number = -1;
for (let i: number = 0; i < terms.length; i++) {
if (!parseBoolean(getLocalProperty(terms[i], CurrentNavigationProperties.root))) { continue; }
const url: string = resolveTermUrl(terms[i], siteCollectionUrl);
if (!url) { continue; }
const candidatePath: string = normalizeNavigationPath(url);
const candidatePath: string = normalizeRootWebPath(url);
let score: number = -1;
if (candidatePath === webPath) {
score = 30000 + candidatePath.length;
} else if (candidatePath.indexOf(webPath + '/') === 0) {
score = 20000 + webPath.length;
} else if (webPath.indexOf(candidatePath + '/') === 0) {
score = 10000 + candidatePath.length;
} else if (isDescendantPath(webPath, candidatePath)) {
score = 20000 + candidatePath.length;
}
if (score > selectedScore) { selected = terms[i]; selectedScore = score; }
}
return selected;
}
function selectRootByTermHierarchy(
terms: ICurrentNavigationTerm[],
webPath: string,
siteCollectionUrl: string
): ICurrentNavigationTerm | undefined {
let selected: ICurrentNavigationTerm | undefined;
let selectedDepth: number = -1;
for (let i: number = 0; i < terms.length; i++) {
const termUrl: string = resolveTermUrl(terms[i], siteCollectionUrl);
if (!termUrl || normalizeRootWebPath(termUrl) !== webPath) { continue; }
const currentPath: string = String(terms[i].PathOfTerm || '').toLowerCase();
for (let j: number = 0; j < terms.length; j++) {
if (!parseBoolean(getLocalProperty(terms[j], CurrentNavigationProperties.root))) { continue; }
const rootPath: string = String(terms[j].PathOfTerm || '').toLowerCase();
const isAncestor: boolean = currentPath === rootPath || currentPath.indexOf(rootPath + ';') === 0;
const rootDepth: number = pathDepth(rootPath);
if (isAncestor && rootDepth > selectedDepth) {
selected = terms[j];
selectedDepth = rootDepth;
}
}
}
return selected;
}
function isDescendantPath(path: string, parentPath: string): boolean {
return parentPath === '/' ? path !== '/' : path.indexOf(parentPath + '/') === 0;
}
export function buildCurrentNavigationItems(
terms: ICurrentNavigationTerm[],
root: ICurrentNavigationTerm,