Skip to content

Commit 7eec180

Browse files
authored
feat: adding following supporting methods to navigation and link (#878)
* feat: adding following supporting methods to navigation and link - Added a method that returns an observable and checks whether a path is active in a reactive fashion - Added a plain method that returns whether a path is active or not * refactor: fix lint
1 parent c452358 commit 7eec180

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

projects/common/src/navigation/navigation.service.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
UrlTree
1515
} from '@angular/router';
1616
import { from, Observable, of } from 'rxjs';
17-
import { filter, map, share, skip, take } from 'rxjs/operators';
17+
import { distinctUntilChanged, filter, map, share, skip, startWith, take } from 'rxjs/operators';
1818
import { throwIfNil } from '../utilities/lang/lang-utils';
1919
import { Dictionary } from '../utilities/types/types';
2020
import { TraceRoute } from './trace-route';
@@ -185,6 +185,20 @@ export class NavigationService {
185185
return route;
186186
}
187187

188+
public isPathActiveAndChanges(path: string[]): Observable<boolean> {
189+
const urlTree = this.router.createUrlTree(path);
190+
191+
return this.router.events.pipe(
192+
startWith(),
193+
map(() => this.router.isActive(urlTree, false)),
194+
distinctUntilChanged()
195+
);
196+
}
197+
198+
public isPathActive(path: string[]): boolean {
199+
return this.router.isActive(this.router.createUrlTree(path), false);
200+
}
201+
188202
public isRelativePathActive(path: string[], relativeTo: ActivatedRoute = this.getCurrentActivatedRoute()): boolean {
189203
return this.router.isActive(this.router.createUrlTree(path, { relativeTo: relativeTo }), false);
190204
}

projects/components/src/link/link.component.scss

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,12 @@
55
text-decoration-line: none;
66
text-decoration: none;
77
color: inherit;
8-
@include link-hover;
8+
9+
&:not(.disabled) {
10+
@include link-hover;
11+
}
12+
13+
.disabled {
14+
pointer-events: none;
15+
}
916
}

projects/components/src/link/link.component.test.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ describe('Link component', () => {
4444
]
4545
});
4646

47-
expect(spectator.query('.ht-link')).toExist();
47+
const anchorElement = spectator.query('.ht-link');
48+
expect(anchorElement).toExist();
49+
expect(anchorElement).not.toHaveClass('disabled');
4850
const routerLinkDirective = spectator.query(RouterLinkWithHref);
4951

5052
expect(routerLinkDirective).toBeDefined();
@@ -73,7 +75,9 @@ describe('Link component', () => {
7375
]
7476
});
7577

76-
expect(spectator.query('.ht-link')).toExist();
78+
const anchorElement = spectator.query('.ht-link');
79+
expect(anchorElement).toExist();
80+
expect(anchorElement).not.toHaveClass('disabled');
7781
const routerLinkDirective = spectator.query(RouterLinkWithHref);
7882

7983
expect(routerLinkDirective).toBeDefined();
@@ -96,7 +100,35 @@ describe('Link component', () => {
96100
]
97101
});
98102

99-
expect(spectator.query('.ht-link')).toExist();
103+
const anchorElement = spectator.query('.ht-link');
104+
expect(anchorElement).toExist();
105+
expect(anchorElement).not.toHaveClass('disabled');
106+
const routerLinkDirective = spectator.query(RouterLinkWithHref);
107+
108+
expect(routerLinkDirective).toBeDefined();
109+
expect(routerLinkDirective?.routerLink).toEqual(['/test']);
110+
expect(routerLinkDirective?.skipLocationChange).toBeUndefined();
111+
expect(routerLinkDirective?.queryParams).toBeUndefined();
112+
expect(routerLinkDirective?.queryParamsHandling).toBeUndefined();
113+
expect(routerLinkDirective?.replaceUrl).toBeUndefined();
114+
});
115+
116+
test('should apply disabled style when disabled', () => {
117+
spectator = createHost(`<ht-link [paramsOrUrl]="paramsOrUrl" [disabled]="true"></ht-link>`, {
118+
hostProps: {
119+
paramsOrUrl: '/test'
120+
},
121+
providers: [
122+
mockProvider(NavigationService, {
123+
buildNavigationParams: jest.fn().mockReturnValue({ path: ['/test'], extras: {} })
124+
})
125+
]
126+
});
127+
128+
const anchorElement = spectator.query('.ht-link');
129+
expect(anchorElement).toExist();
130+
expect(anchorElement).toHaveClass('ht-link disabled');
131+
100132
const routerLinkDirective = spectator.query(RouterLinkWithHref);
101133

102134
expect(routerLinkDirective).toBeDefined();

projects/components/src/link/link.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { NavigationParams, NavigationPath, NavigationService } from '@hypertrace
1010
<a
1111
*ngIf="this.navigationPath"
1212
class="ht-link"
13+
[ngClass]="{ disabled: this.disabled }"
1314
[routerLink]="this.navigationPath"
1415
[queryParams]="this.navigationOptions?.queryParams"
1516
[queryParamsHandling]="this.navigationOptions?.queryParamsHandling"
@@ -24,6 +25,9 @@ export class LinkComponent implements OnChanges {
2425
@Input()
2526
public paramsOrUrl?: NavigationParams | string;
2627

28+
@Input()
29+
public disabled?: boolean;
30+
2731
public navigationPath?: NavigationPath;
2832
public navigationOptions?: NavigationExtras;
2933

0 commit comments

Comments
 (0)