Browse Source

Change method and property names in the routes.service.ts

pull/19440/head
Mustafa Daglioglu 2 years ago
parent
commit
9ebfe62ee7
  1. 8
      docs/en/UI/Angular/Modifying-the-Menu.md
  2. 81
      npm/ng-packs/packages/core/src/lib/services/routes.service.ts
  3. 37
      npm/ng-packs/packages/core/src/lib/tests/routes.service.spec.ts

8
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`.

81
npm/ng-packs/packages/core/src/lib/services/routes.service.ts

@ -26,7 +26,7 @@ export abstract class AbstractTreeService<T extends { [key: string | number | sy
private _visible$ = new BehaviorSubject<TreeNode<T>[]>([]);
protected othersGroup: string;
private filterRoutesEnabled = true;
protected shouldSingularizeRoutes = true;
get flat(): T[] {
return this._flat$.value;
@ -94,53 +94,25 @@ export abstract class AbstractTreeService<T extends { [key: string | number | sy
}
add(items: T[]): T[] {
if (this.filterRoutesEnabled) {
const map = new Map<string, T>();
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>): T[] {
const willRemoveItems = this.flat.filter(item => {
const keys = Object.keys(params) as Array<keyof Partial<T>>;
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<T>);
});
const flatItems = this.flat.filter(item => !willRemoveItems.includes(item));
return this.publish(flatItems);
if (this.shouldSingularizeRoutes) {
const map = new Map<string, T>();
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<T>) => boolean, tree = this.tree): TreeNode<T> | null {
find(predicate: (item: TreeNode<T>) => boolean, tree = this.tree): TreeNode<T> | null {
return tree.reduce<TreeNode<T> | null>((acc, node) => {
if (acc) {
return acc;
@ -180,6 +152,29 @@ export abstract class AbstractTreeService<T extends { [key: string | number | sy
return this.publish(flatItems);
}
removeByParams(params: Partial<T>): T[] | null {
if (!params) {
return null;
}
const keys = Object.keys(params) as Array<keyof Partial<T>>;
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<T>);
}
const flatItems = this.flat.filter(item => !excludedList.includes(item));
return this.publish(flatItems);
}
search(params: Partial<T>, tree = this.tree): TreeNode<T> | null {
const searchKeys = Object.keys(params) as Array<keyof Partial<T>>;
@ -188,13 +183,17 @@ export abstract class AbstractTreeService<T extends { [key: string | number | sy
return acc;
}
if (searchKeys.every(key => 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()

37
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',
});

Loading…
Cancel
Save