Skip to content

Commit 5b85a1b

Browse files
committed
move active path logic into workspace route manager
1 parent cb36f69 commit 5b85a1b

File tree

5 files changed

+35
-22
lines changed

5 files changed

+35
-22
lines changed

src/packages/core/workspace/controllers/workspace-route-manager.controller.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
2-
import { UmbArrayState } from '@umbraco-cms/backoffice/observable-api';
3-
import type { UmbRoute } from '@umbraco-cms/backoffice/router';
2+
import { UmbArrayState, UmbStringState } from '@umbraco-cms/backoffice/observable-api';
3+
import type { IComponentRoute, UmbRoute } from '@umbraco-cms/backoffice/router';
44

55
/**
66
* The workspace route manager.
@@ -12,19 +12,38 @@ export class UmbWorkspaceRouteManager extends UmbControllerBase {
1212
#routes = new UmbArrayState<UmbRoute>([], (x) => x.path);
1313
public readonly routes = this.#routes.asObservable();
1414

15+
#activeLocalPath = new UmbStringState('');
16+
1517
/**
1618
* Set the routes for the workspace.
1719
* @param {Array<UmbRoute>} routes The routes for the workspace.
1820
* @memberof UmbWorkspaceRouteManager
1921
*/
2022
setRoutes(routes: Array<UmbRoute>) {
21-
this.#routes.setValue([
23+
const allRoutes = [
2224
...routes,
2325
{
2426
path: `**`,
2527
component: async () => (await import('@umbraco-cms/backoffice/router')).UmbRouteNotFoundElement,
2628
},
27-
]);
29+
] as Array<IComponentRoute>;
30+
31+
const mappedRoutes = allRoutes.map((route) => {
32+
// override the setup method to set the active local path
33+
const oldSetupCallback = route.setup;
34+
35+
route.setup = (_component: any, info: any) => {
36+
this.#activeLocalPath.setValue(info.match.fragments.consumed);
37+
38+
if (oldSetupCallback) {
39+
oldSetupCallback(_component, info);
40+
}
41+
};
42+
43+
return route;
44+
});
45+
46+
this.#routes.setValue([...mappedRoutes]);
2847
}
2948

3049
/**
@@ -35,4 +54,13 @@ export class UmbWorkspaceRouteManager extends UmbControllerBase {
3554
getRoutes(): Array<UmbRoute> {
3655
return this.#routes.getValue();
3756
}
57+
58+
/**
59+
* Get the active local path.
60+
* @returns {*} {string}
61+
* @memberof UmbWorkspaceRouteManager
62+
*/
63+
getActiveLocalPath(): string {
64+
return this.#activeLocalPath.getValue();
65+
}
3866
}

src/packages/core/workspace/entity-detail/entity-detail-workspace-base.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ export abstract class UmbEntityDetailWorkspaceContextBase<
4545
readonly parentUnique = this.#parent.asObservablePart((parent) => (parent ? parent.unique : undefined));
4646
readonly parentEntityType = this.#parent.asObservablePart((parent) => (parent ? parent.entityType : undefined));
4747

48-
#routerActiveLocalPath = '';
49-
5048
#initResolver?: () => void;
5149
#initialized = false;
5250

@@ -168,14 +166,6 @@ export abstract class UmbEntityDetailWorkspaceContextBase<
168166
await this._detailRepository!.delete(unique);
169167
}
170168

171-
protected _setActivePathSegment(segment: string) {
172-
this.#routerActiveLocalPath = segment;
173-
}
174-
175-
protected _getActivePathSegment() {
176-
return this.#routerActiveLocalPath;
177-
}
178-
179169
/**
180170
* @description method to check if the workspace is about to navigate away.
181171
* @protected
@@ -185,7 +175,7 @@ export abstract class UmbEntityDetailWorkspaceContextBase<
185175
*/
186176
protected _checkWillNavigateAway(newUrl: string) {
187177
const workspaceBasePath = UMB_WORKSPACE_PATH_PATTERN.generateLocal({ entityType: this.getEntityType() });
188-
const currentWorkspacePathIdentifier = '/' + workspaceBasePath + '/' + this._getActivePathSegment();
178+
const currentWorkspacePathIdentifier = '/' + workspaceBasePath + '/' + this.routes.getActiveLocalPath();
189179
return !newUrl.includes(currentWorkspacePathIdentifier);
190180
}
191181

src/packages/data-type/workspace/data-type-workspace.context.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ export class UmbDataTypeWorkspaceContext
9090
path: 'create/parent/:entityType/:parentUnique',
9191
component: UmbDataTypeWorkspaceEditorElement,
9292
setup: (_component, info) => {
93-
this._setActivePathSegment(info.match.fragments.consumed);
9493
const parentEntityType = info.match.params.entityType;
9594
const parentUnique = info.match.params.parentUnique === 'null' ? null : info.match.params.parentUnique;
9695
this.create({ parent: { entityType: parentEntityType, unique: parentUnique } });
@@ -106,7 +105,6 @@ export class UmbDataTypeWorkspaceContext
106105
path: 'edit/:unique',
107106
component: UmbDataTypeWorkspaceEditorElement,
108107
setup: (_component, info) => {
109-
this._setActivePathSegment(info.match.fragments.consumed);
110108
const unique = info.match.params.unique;
111109
this.load(unique);
112110
},

src/packages/language/workspace/language/language-workspace.context.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ export class UmbLanguageWorkspaceContext
3333
{
3434
path: 'create',
3535
component: UmbLanguageWorkspaceEditorElement,
36-
setup: async (_component, info) => {
37-
this._setActivePathSegment(info.match.fragments.consumed);
36+
setup: async () => {
3837
this.create({ parent: { entityType: UMB_LANGUAGE_ROOT_ENTITY_TYPE, unique: null } });
3938

4039
new UmbWorkspaceIsNewRedirectController(
@@ -50,7 +49,6 @@ export class UmbLanguageWorkspaceContext
5049
setup: (_component, info) => {
5150
this.removeUmbControllerByAlias('isNewRedirectController');
5251
this.load(info.match.params.unique);
53-
this._setActivePathSegment(info.match.fragments.consumed);
5452
},
5553
},
5654
]);

src/packages/user/user/workspace/user/user-workspace.context.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ export class UmbUserWorkspaceContext
4747
{
4848
path: 'edit/:id',
4949
component: UmbUserWorkspaceEditorElement,
50-
setup: (component, info) => {
51-
this._setActivePathSegment(info.match.fragments.consumed);
50+
setup: (_component, info) => {
5251
const id = info.match.params.id;
5352
this.load(id);
5453
},

0 commit comments

Comments
 (0)