Skip to content

Commit

Permalink
splitting buildpath into 2 functions (#2756)
Browse files Browse the repository at this point in the history
Co-authored-by: Ndricim Rrapi <[email protected]>
  • Loading branch information
hardl and ndricimrr authored Jun 20, 2022
1 parent 66547fc commit 7c9bcfa
Showing 1 changed file with 110 additions and 30 deletions.
140 changes: 110 additions & 30 deletions core/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,7 @@
const buildPath = (params, srcNode, srcNodePathParams) => {
const localNode = srcNode || currentNode;
const localPathParams = GenericHelpers.isEmptyObject(srcNodePathParams)
? pathParams
: srcNodePathParams;
const localPathParams = srcNodePathParams || pathParams;
let localNavPath = navigationPath;
if (srcNode) {
let parent = srcNode.parent;
Expand All @@ -428,7 +426,6 @@
}
localNavPath = [...localNavPath].reverse();
}
let path = params.link;
if (params.fromVirtualTreeRoot) {
// from a parent node specified with virtualTree: true
Expand All @@ -443,21 +440,6 @@
getSubPath(node, localPathParams),
params.link
);
// boolean predicate, changes the path only if getCurrentPath function used
const isGetCurrentPathRequired =
!GenericHelpers.isEmptyObject(localPathParams) &&
!path.includes('virtualSegment_') &&
!params.link &&
params.getCurrentPath &&
Object.keys(localPathParams)[0].includes('virtualSegment_');
if (isGetCurrentPathRequired) {
let virtualPath = '';
Object.entries(localPathParams).forEach((virtualParam) => {
virtualPath += '/' + virtualParam[1];
});
path = virtualPath;
}
} else if (params.fromParent) {
// from direct parent
path = Routing.concatenatePath(
Expand All @@ -479,11 +461,10 @@
const node = [...localNavPath]
.reverse()
.find((n) => navigationContext === n.navigationContext);
const pathUpToContext = getSubPath(node, localPathParams);
const fullPath = getSubPath(localNode, localPathParams);
path = params.getCurrentPath
? fullPath.substring(pathUpToContext.length)
: Routing.concatenatePath(pathUpToContext, params.link);
path = Routing.concatenatePath(
getSubPath(node, localPathParams),
params.link
);
} else if (params.intent) {
path = RoutingHelpers.getIntentPath(params.link);
} else if (params.relative) {
Expand All @@ -492,11 +473,6 @@
getSubPath(localNode, localPathParams),
params.link
);
} else {
// retrieve path for getCurrentPath method when no options used
if (params.getCurrentPath) {
path = getSubPath(localNode, localPathParams);
}
}
if (params.nodeParams && Object.keys(params.nodeParams).length > 0) {
path += path.includes('?') ? '&' : '?';
Expand Down Expand Up @@ -1102,6 +1078,110 @@
window.open(nodepath, '_blank', 'noopener,noreferrer');
};
// TODO needs to be consolidated with buildPath
const buildPathForGetCurrent = (params, srcNode, srcNodePathParams) => {
const localNode = srcNode || currentNode;
const localPathParams = GenericHelpers.isEmptyObject(srcNodePathParams)
? pathParams
: srcNodePathParams;
let localNavPath = navigationPath;
if (srcNode) {
let parent = srcNode.parent;
localNavPath = [srcNode];
while (parent) {
localNavPath.push(parent);
parent = parent.parent;
}
localNavPath = [...localNavPath].reverse();
}
let path = params.link;
if (params.fromVirtualTreeRoot) {
// from a parent node specified with virtualTree: true
const node = [...localNavPath].reverse().find((n) => n.virtualTree);
if (!node) {
console.error(
'LuigiClient Error: fromVirtualTreeRoot() is not possible, not inside a virtualTree navigation. Docs: https://docs.luigi-project.io/docs/navigation-parameters-reference/?section=virtualtree'
);
return;
}
path = Routing.concatenatePath(
getSubPath(node, localPathParams),
params.link
);
// boolean predicate, changes the path only if getCurrentPath function used
const isGetCurrentPathRequired =
!GenericHelpers.isEmptyObject(localPathParams) &&
!path.includes('virtualSegment_') &&
!params.link &&
params.getCurrentPath &&
Object.keys(localPathParams)[0].includes('virtualSegment_');
if (isGetCurrentPathRequired) {
let virtualPath = '';
Object.entries(localPathParams).forEach((virtualParam) => {
virtualPath += '/' + virtualParam[1];
});
path = virtualPath;
}
} else if (params.fromParent) {
// from direct parent
path = Routing.concatenatePath(
getSubPath(localNode.parent, localPathParams),
params.link
);
} else if (params.fromClosestContext) {
// from the closest navigation context
const node = [...localNavPath]
.reverse()
.find((n) => n.navigationContext && n.navigationContext.length > 0);
path = Routing.concatenatePath(
getSubPath(node, localPathParams),
params.link
);
} else if (params.fromContext) {
// from a given navigation context
const navigationContext = params.fromContext;
const node = [...localNavPath]
.reverse()
.find((n) => navigationContext === n.navigationContext);
const pathUpToContext = getSubPath(node, localPathParams);
const fullPath = getSubPath(localNode, localPathParams);
path = params.getCurrentPath
? fullPath.substring(pathUpToContext.length)
: Routing.concatenatePath(pathUpToContext, params.link);
} else if (params.intent) {
path = RoutingHelpers.getIntentPath(params.link);
} else if (params.relative) {
// relative
path = Routing.concatenatePath(
getSubPath(localNode, localPathParams),
params.link
);
} else {
// retrieve path for getCurrentPath method when no options used
if (params.getCurrentPath) {
path = getSubPath(localNode, localPathParams);
}
}
if (params.nodeParams && Object.keys(params.nodeParams).length > 0) {
path += path.includes('?') ? '&' : '?';
Object.entries(params.nodeParams).forEach((entry, index) => {
path +=
encodeURIComponent(
RoutingHelpers.getContentViewParamPrefix() + entry[0]
) +
'=' +
encodeURIComponent(entry[1]) +
(index < Object.keys(params.nodeParams).length - 1 ? '&' : '');
});
}
return path;
};
function init(node) {
const isolateAllViews = LuigiConfig.getConfigValue(
'navigation.defaults.isolateView'
Expand Down Expand Up @@ -1461,7 +1541,7 @@
const srcPathParams = iframe.luigi.pathParams;
const data = e.data.data;
data.getCurrentPath = true;
const path = buildPath(data, srcNode, srcPathParams);
const path = buildPathForGetCurrent(data, srcNode, srcPathParams);
// send answer back to client
const message = {
Expand Down

0 comments on commit 7c9bcfa

Please sign in to comment.