From 9ebfe62ee7aa0330a17e2fedbd63ce59beb0d114 Mon Sep 17 00:00:00 2001 From: Mustafa Daglioglu Date: Tue, 23 Apr 2024 14:26:02 +0300 Subject: [PATCH] Change method and property names in the routes.service.ts --- docs/en/UI/Angular/Modifying-the-Menu.md | 8 +- .../core/src/lib/services/routes.service.ts | 81 +++++++++---------- .../core/src/lib/tests/routes.service.spec.ts | 37 ++++----- 3 files changed, 61 insertions(+), 65 deletions(-) diff --git a/docs/en/UI/Angular/Modifying-the-Menu.md b/docs/en/UI/Angular/Modifying-the-Menu.md index 78733ef658..dba2c9be91 100644 --- a/docs/en/UI/Angular/Modifying-the-Menu.md +++ b/docs/en/UI/Angular/Modifying-the-Menu.md @@ -153,7 +153,7 @@ export class AppModule {} ``` **Note:** -Route items's `name` property is must be a unique key. If there are multiple items with the same name, the last one will be displayed in the menu. If you want to display multiple items with the same name, you can call the `disableFiltering` method of the `RoutesService` to disable the filtering. +Route items's `name` property is must be a unique key. If there are multiple items with the same name, the last one will be displayed in the menu. If you want to display multiple items with the same name, you can call the `setSingularizeStatus(false)` method of the `RoutesService` to disable the singularization of the names. This method should be called before adding the routes. If you want to enable the singularization of the names, you can call the `setSingularizeStatus(true)` method of the `RoutesService` to enable the singularization of the names. This method should be called before adding the routes. The default value of the singularization status is `true`. The default value of the singularization status is `true`. ```typescript import { RoutesService } from '@abp/ng.core'; @@ -162,7 +162,7 @@ import { Component } from '@angular/core'; @Component(/* component metadata */) export class AppComponent { constructor(private routes: RoutesService) { - routes.disableFiltering(); + routes.setSingularizeStatus(false); } } ``` @@ -241,7 +241,7 @@ After adding the `routes` property as described above, the navigation menu looks ## How to Patch or Remove a Navigation Element -The `patch` method of `RoutesService` finds a route by its name and replaces its configuration with the new configuration passed as the second parameter. Similarly, `remove` method finds a route and removes it along with its children. Also you can use `delete` method to delete the routes with given properties. +The `patch` method of `RoutesService` finds a route by its name and replaces its configuration with the new configuration passed as the second parameter. Similarly, `remove` method finds a route and removes it along with its children. Also you can use `removeByParams` method to delete the routes with given properties. ```js // this.routes is instance of RoutesService @@ -266,7 +266,7 @@ this.routes.patch('::Menu:Home', newHomeRouteConfig); this.routes.remove(['Your navigation']); // or -this.routes.delete({ name: 'Your navigation' }); +this.routes.removeByParams({ name: 'Your navigation' }); ``` - Moved the _Home_ navigation under the _Administration_ dropdown based on given `parentName`. diff --git a/npm/ng-packs/packages/core/src/lib/services/routes.service.ts b/npm/ng-packs/packages/core/src/lib/services/routes.service.ts index 4c1cae7e39..6837e57aca 100644 --- a/npm/ng-packs/packages/core/src/lib/services/routes.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/routes.service.ts @@ -26,7 +26,7 @@ export abstract class AbstractTreeService[]>([]); protected othersGroup: string; - private filterRoutesEnabled = true; + protected shouldSingularizeRoutes = true; get flat(): T[] { return this._flat$.value; @@ -94,53 +94,25 @@ export abstract class AbstractTreeService(); - items.forEach(item => map.set(item[this.id], item)); - - const flatItems = this.filterWith(map); - map.forEach(pushValueTo(flatItems)); + let flatItems: T[] = []; - flatItems.sort(this.sort); - return this.publish(flatItems); - } else { - const flatItems = this.flat.concat(items); - flatItems.sort(this.sort); - return this.publish(flatItems); + if (!this.shouldSingularizeRoutes) { + flatItems = [...this.flat, ...items]; } - } - - delete(params: Partial): T[] { - const willRemoveItems = this.flat.filter(item => { - const keys = Object.keys(params) as Array>; - const isValid = keys.every(key => item[key] === params[key]); - - return isValid; - }); - - if (willRemoveItems?.length) { - willRemoveItems.forEach(item => { - this.delete({ - [this.parentId]: item[this.id], - } as Partial); - }); - const flatItems = this.flat.filter(item => !willRemoveItems.includes(item)); - return this.publish(flatItems); + if (this.shouldSingularizeRoutes) { + const map = new Map(); + items.forEach(item => map.set(item[this.id], item)); + flatItems = this.filterWith(map); + map.forEach(pushValueTo(flatItems)); } - return this.flat; - } - - disableFiltering(): void { - this.filterRoutesEnabled = false; - } + flatItems.sort(this.sort); - enableFiltering(): void { - this.filterRoutesEnabled = true; + return this.publish(flatItems); } - find(predicate: (item: TreeNode) => boolean, tree = this.tree): TreeNode | null { + find(predicate: (item: TreeNode) => boolean, tree = this.tree): TreeNode | null { return tree.reduce | null>((acc, node) => { if (acc) { return acc; @@ -180,6 +152,29 @@ export abstract class AbstractTreeService): T[] | null { + if (!params) { + return null; + } + + const keys = Object.keys(params) as Array>; + if (keys.length === 0) { + return null; + } + + const excludedList = this.flat.filter(item => keys.every(key => item[key] === params[key])); + if (!excludedList?.length) { + return null; + } + + for (const item of excludedList) { + this.removeByParams({ [this.parentId]: item[this.id] } as Partial); + } + + const flatItems = this.flat.filter(item => !excludedList.includes(item)); + return this.publish(flatItems); + } + search(params: Partial, tree = this.tree): TreeNode | null { const searchKeys = Object.keys(params) as Array>; @@ -188,13 +183,17 @@ export abstract class AbstractTreeService node.item[key] === params[key])) { + if (searchKeys.every(key => node[key] === params[key])) { return node; } return this.search(params, node.children); }, null); } + + setSingularizeStatus(singularize = true): void { + this.shouldSingularizeRoutes = singularize; + } } @Injectable() diff --git a/npm/ng-packs/packages/core/src/lib/tests/routes.service.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/routes.service.spec.ts index 0c993606b4..a751b84d6d 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/routes.service.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/routes.service.spec.ts @@ -172,9 +172,9 @@ describe('Routes Service', () => { }); }); - describe('#disableFiltering', () => { - it('should allow to duplicate routes', () => { - service.disableFiltering(); + describe('#setSingularizeStatus', () => { + it('should allow to duplicate routes when called with false', () => { + service.setSingularizeStatus(false); service.add(routes); @@ -183,8 +183,8 @@ describe('Routes Service', () => { expect(flat.length).toBe(routes.length); }); - it('should allow to duplicate routes with the same name', () => { - service.disableFiltering(); + it('should allow to duplicate routes with the same name when called with false', () => { + service.setSingularizeStatus(false); service.add([...routes, { path: '/foo/bar/test', name: 'bar', parentName: 'foo', order: 2 }]); @@ -193,8 +193,8 @@ describe('Routes Service', () => { expect(flat.length).toBe(routes.length + 1); }); - it('should allow to routes with the same name but different parentName', () => { - service.disableFiltering(); + it('should allow to routes with the same name but different parentName when called with false', () => { + service.setSingularizeStatus(false); service.add([ { path: '/foo/bar', name: 'bar', parentName: 'foo', order: 2 }, @@ -205,15 +205,13 @@ describe('Routes Service', () => { expect(flat.length).toBe(2); }); - }); - describe('#enableFiltering', () => { - it('should not allow to duplicate routes', () => { - service.disableFiltering(); + it('should not allow to duplicate routes when called with true', () => { + service.setSingularizeStatus(false); service.add(routes); - service.enableFiltering(); + service.setSingularizeStatus(true); service.add(routes); @@ -222,8 +220,8 @@ describe('Routes Service', () => { expect(flat.length).toBe(5); }); - it('should not allow to duplicate routes with the same name', () => { - service.enableFiltering(); + it('should not allow to duplicate routes with the same name when called with true', () => { + service.setSingularizeStatus(true); service.add([...routes, { path: '/foo/bar/test', name: 'bar', parentName: 'any', order: 2 }]); const flat = service.flat; @@ -302,11 +300,11 @@ describe('Routes Service', () => { }); }); - describe('#delete', () => { + describe('#removeByParams', () => { it('should remove route based on given route', () => { service.add(routes); - service.delete({ + service.removeByParams({ name: 'bar', parentName: 'foo', }); @@ -321,7 +319,7 @@ describe('Routes Service', () => { }); it('should remove if more than one route has the same properties', () => { - service.disableFiltering(); + service.setSingularizeStatus(false); service.add([ ...routes, @@ -335,7 +333,7 @@ describe('Routes Service', () => { }, ]); - service.delete({ + service.removeByParams({ path: '/foo/bar', name: 'bar', parentName: 'foo', @@ -345,7 +343,6 @@ describe('Routes Service', () => { }); const flat = service.flat; - console.log(flat); expect(flat.length).toBe(5); const notFound = service.search({ @@ -363,7 +360,7 @@ describe('Routes Service', () => { service.add(routes); const flatLengthBeforeRemove = service.flat.length; - service.delete({ + service.removeByParams({ name: 'bar', parentName: 'baz', });