Skip to content

Commit

Permalink
Clean up NavigationPath code
Browse files Browse the repository at this point in the history
  • Loading branch information
tkleinke committed Feb 21, 2025
1 parent 62629ed commit 1157886
Showing 1 changed file with 47 additions and 46 deletions.
93 changes: 47 additions & 46 deletions desktop/src/app/components/resources/view/state/navigation-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ export module NavigationPath {
}


export function getSelectedSegment(navPath: NavigationPath): NavigationPathSegment {
export function getSelectedSegment(path: NavigationPath): NavigationPathSegment {

return navPath.segments
.find(on(['document', 'resource', 'id'], is(navPath.selectedSegmentId))) as NavigationPathSegment;
return path.segments
.find(on(['document', 'resource', 'id'], is(path.selectedSegmentId))) as NavigationPathSegment;
}


Expand All @@ -55,22 +55,22 @@ export module NavigationPath {
* V
* SEGMENT1, SEGMENT2, SEGMENT3
*
* setNewSelectedSegmentDoc(navpath, document4) changes the situation to
* setNewSelectedSegmentDoc(path, document4) changes the situation to
*
* V
* NP: SEGMENT1, SEGMENT2, SEGMENT4
*
* from there, setNewSelectedSegmentDoc(navpath, document5) changes the situation to
* from there, setNewSelectedSegmentDoc(path, document5) changes the situation to
*
* V
* SEGMENT1, SEGMENT2, SEGMENT4, SEGMENT5
*
* from there, setNewSelectedSegmentDoc(navpath, document1) changes the situation to
* from there, setNewSelectedSegmentDoc(path, document1) changes the situation to
*
* V
* SEGMENT1, SEGMENT2, SEGMENT4, SEGMENT5
*
* from there, setNewSelectedSegmentDoc(navpath, undefined) changes the situation to
* from there, setNewSelectedSegmentDoc(path, undefined) changes the situation to
*
* (NO SELECTED SEGMENT)
* SEGMENT1, SEGMENT2, SEGMENT4, SEGMENT5
Expand All @@ -84,7 +84,8 @@ export module NavigationPath {
navigationPath.segments = rebuildElements(
navigationPath.segments,
navigationPath.selectedSegmentId,
newSelectedSegmentDoc);
newSelectedSegmentDoc
);
}

navigationPath.selectedSegmentId = newSelectedSegmentDoc
Expand All @@ -95,71 +96,71 @@ export module NavigationPath {
}


export function setSelectedDocument(navPath: NavigationPath, document: FieldDocument|undefined) {
export function setSelectedDocument(path: NavigationPath, document: FieldDocument|undefined) {

getViewContext(navPath).selected = document;
getViewContext(path).selected = document;
}


export function getSelectedDocument(navPath: NavigationPath): FieldDocument|undefined {
export function getSelectedDocument(path: NavigationPath): FieldDocument|undefined {

return getViewContext(navPath).selected;
return getViewContext(path).selected;
}


export function setQueryString(navPath: NavigationPath, q: string) {
export function setQueryString(path: NavigationPath, q: string) {

getViewContext(navPath).q = q;
getViewContext(path).q = q;
}


export function getQueryString(navPath: NavigationPath): string {
export function getQueryString(path: NavigationPath): string {

return getViewContext(navPath).q;
return getViewContext(path).q;
}


export function setCategoryFilters(navPath: NavigationPath, categories: string[]) {
export function setCategoryFilters(path: NavigationPath, categories: string[]) {

getViewContext(navPath).categories = categories;
getViewContext(path).categories = categories;
}


export function getCategoryFilters(navPath: NavigationPath): string[] {
export function getCategoryFilters(path: NavigationPath): string[] {

return getViewContext(navPath).categories;
return getViewContext(path).categories;
}


export function shorten(navPath: NavigationPath, firstToBeExcluded: NavigationPathSegment): NavigationPath {
export function shorten(path: NavigationPath, firstToBeExcluded: NavigationPathSegment): NavigationPath {

const oldNavPath = NavigationPath.clone(navPath);
navPath.segments = takeWhile(differentFrom(firstToBeExcluded), oldNavPath.segments);
const oldPath: NavigationPath = NavigationPath.clone(path);
path.segments = takeWhile(differentFrom(firstToBeExcluded), oldPath.segments);

if (navPath.selectedSegmentId) {
const stillSelectedSegment: NavigationPathSegment = navPath.segments.find(segment => {
return segment.document.resource.id === navPath.selectedSegmentId;
if (path.selectedSegmentId) {
const stillSelectedSegment: NavigationPathSegment = path.segments.find(segment => {
return segment.document.resource.id === path.selectedSegmentId;
});
if (!stillSelectedSegment) navPath.selectedSegmentId = undefined;
if (!stillSelectedSegment) path.selectedSegmentId = undefined;
}

return navPath;
return path;
}


export function segmentNotPresent(navPath: NavigationPath, segmentId: string) {
export function segmentNotPresent(path: NavigationPath, segmentId: string) {

return !segmentId || navPath.segments.map(toResourceId).includes(segmentId);
return !segmentId || path.segments.map(toResourceId).includes(segmentId);
}


export function findInvalidSegment(operationId: string|undefined, navPath: NavigationPath,
export function findInvalidSegment(operationId: string|undefined, path: NavigationPath,
validNonRecordedInCategories: string[],
exists: (_: string) => boolean): NavigationPathSegment|undefined {

for (let segment of navPath.segments) {
for (let segment of path.segments) {
if (!NavigationPathSegment.isValid(
operationId, segment, navPath.segments, validNonRecordedInCategories, exists
operationId, segment, path.segments, validNonRecordedInCategories, exists
)) {
return segment;
}
Expand All @@ -169,15 +170,14 @@ export module NavigationPath {
}


export function isPartOfNavigationPath(document: FieldDocument, navPath: NavigationPath,
export function isPartOfNavigationPath(document: FieldDocument, path: NavigationPath,
operationId: string|undefined): boolean {

if (navPath.selectedSegmentId && Document.hasRelationTarget(document, 'liesWithin',
navPath.selectedSegmentId)) {
if (path.selectedSegmentId && Document.hasRelationTarget(document, 'liesWithin', path.selectedSegmentId)) {
return true;
}

return (!navPath.selectedSegmentId && operationId !== undefined
return (!path.selectedSegmentId && operationId !== undefined
&& Document.hasRelationTarget(document, 'isRecordedIn',operationId)
&& !Document.hasRelations(document, 'liesWithin'));
}
Expand All @@ -191,6 +191,7 @@ export module NavigationPath {
let currentResourceId: string|undefined = documentAsContext
? document.resource.id
: ModelUtil.getRelationTargetId(document, 'liesWithin', 0);

while (currentResourceId) {
const currentSegmentDoc = await get(currentResourceId);
currentResourceId = ModelUtil.getRelationTargetId(currentSegmentDoc, 'liesWithin', 0);
Expand All @@ -201,15 +202,15 @@ export module NavigationPath {
}


export function replaceSegmentsIfNecessary(navPath: NavigationPath, newSegments: Array<NavigationPathSegment>,
export function replaceSegmentsIfNecessary(path: NavigationPath, newSegments: Array<NavigationPathSegment>,
newSelectedSegmentId: string): NavigationPath {

if (!NavigationPath.segmentNotPresent(navPath, newSelectedSegmentId)) {
navPath.segments = newSegments;
if (!NavigationPath.segmentNotPresent(path, newSelectedSegmentId)) {
path.segments = newSegments;
}

navPath.selectedSegmentId = newSelectedSegmentId;
return navPath;
path.selectedSegmentId = newSelectedSegmentId;
return path;
}


Expand All @@ -231,11 +232,11 @@ export module NavigationPath {
}


function getViewContext(navPath: NavigationPath): ViewContext {
function getViewContext(path: NavigationPath): ViewContext {

return navPath.selectedSegmentId
? getSelectedSegment(navPath)
: navPath.basicContext;
return path.selectedSegmentId
? getSelectedSegment(path)
: path.basicContext;
}


Expand Down

0 comments on commit 1157886

Please sign in to comment.