Skip to content

Commit e6cdef2

Browse files
committed
refactor(SidebarNavLink): add INavData type to items, cleanup
1 parent c6d79f2 commit e6cdef2

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

projects/coreui/angular/src/lib/sidebar/app-sidebar-nav/app-sidebar-nav-link.component.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {Component, EventEmitter, Input, OnDestroy, OnInit, Output} from '@angular/core';
2-
import {SidebarNavHelper} from '../app-sidebar-nav.service';
32
import {NavigationEnd, Router} from '@angular/router';
43
import {Observable, Subscription} from 'rxjs';
54
import {filter} from 'rxjs/operators';
65

6+
import {SidebarNavHelper} from '../app-sidebar-nav.service';
7+
import {INavData} from '../app-sidebar-nav';
8+
79
@Component({
810
selector: 'app-sidebar-nav-link-content, cui-sidebar-nav-link-content',
911
template: `
@@ -30,13 +32,13 @@ export class AppSidebarNavLinkContentComponent {
3032
})
3133
export class AppSidebarNavLinkComponent implements OnInit, OnDestroy {
3234

33-
protected _item: any;
35+
protected _item: INavData;
3436

3537
@Input()
36-
set item(item: any) {
38+
set item(item: INavData) {
3739
this._item = JSON.parse(JSON.stringify(item));
3840
}
39-
get item(): any {
41+
get item(): INavData {
4042
return this._item;
4143
}
4244

@@ -45,6 +47,7 @@ export class AppSidebarNavLinkComponent implements OnInit, OnDestroy {
4547
public linkType: string;
4648
public href: string;
4749
public linkActive: boolean;
50+
private url: string;
4851

4952
private navigationEndObservable: Observable<NavigationEnd>;
5053
private navSubscription: Subscription;
@@ -60,12 +63,13 @@ export class AppSidebarNavLinkComponent implements OnInit, OnDestroy {
6063
}
6164

6265
ngOnInit() {
66+
this.url = typeof this.item.url === 'string' ? this.item.url : this.router.serializeUrl(this.router.createUrlTree(this.item.url)) ;
6367
this.linkType = this.getLinkType();
64-
this.href = this.isDisabled() ? '' : (this.item.href || this.item.url);
65-
this.linkActive = this.router.url.split(/[?#(]/)[0] === this.item.url.split(/[?#(]/)[0];
68+
this.href = this.isDisabled() ? '' : (this.item.href || this.url);
69+
this.linkActive = this.router.url.split(/[?#(;]/)[0] === this.href.split(/[?#(;]/)[0];
6670
this.navSubscription = this.navigationEndObservable.subscribe(event => {
67-
const itemUrlArray = this.item.url.split(/[?#(]/)[0].split('/');
68-
const urlArray = event.urlAfterRedirects.split(/[?#(]/)[0].split('/');
71+
const itemUrlArray = this.href.split(/[?#(;]/)[0].split('/');
72+
const urlArray = event.urlAfterRedirects.split(/[?#(;]/)[0].split('/');
6973
this.linkActive = itemUrlArray.every((value, index) => value === urlArray[index]);
7074
});
7175
}
@@ -83,8 +87,7 @@ export class AppSidebarNavLinkComponent implements OnInit, OnDestroy {
8387
}
8488

8589
public isExternalLink() {
86-
const linkPath = Array.isArray(this.item.url) ? this.item.url[0] : this.item.url;
87-
return !!this.item.href || linkPath.substring(0, 4) === 'http';
90+
return !!this.item.href || this.url.substring(0, 4) === 'http';
8891
}
8992

9093
linkClicked() {

0 commit comments

Comments
 (0)