1- import { Component , Directive , ElementRef , HostBinding , HostListener , Input , OnInit , Renderer2 , ViewEncapsulation } from '@angular/core' ;
1+ import { Component , Directive , ElementRef , Inject , Injectable , HostBinding , HostListener , Input , OnInit , Renderer2 , ViewEncapsulation } from '@angular/core' ;
22import { Replace } from './../shared' ;
33
44@Directive ( {
@@ -29,6 +29,46 @@ export class NavDropdownToggleDirective {
2929 }
3030}
3131
32+ @Directive ( {
33+ selector : '[appLinkAttributes]'
34+ } )
35+ export class LinkAttributesDirective implements OnInit {
36+ @Input ( ) appLinkAttributes : { [ key : string ] : string } ;
37+ constructor ( private renderer : Renderer2 , private el : ElementRef ) { }
38+
39+ ngOnInit ( ) {
40+ const attribs = this . appLinkAttributes
41+ for ( let attr in attribs ) {
42+ if ( attr === 'style' && typeof ( attribs [ attr ] ) === 'object' ) {
43+ this . setStyle ( attribs [ attr ] ) ;
44+ } else if ( attr === 'class' ) {
45+ this . addClass ( attribs [ attr ] ) ;
46+ } else {
47+ this . setAttrib ( attr , attribs [ attr ] ) ;
48+ }
49+ }
50+ }
51+
52+ private setStyle ( styles ) {
53+ for ( let style in styles ) {
54+ this . renderer . setStyle ( this . el . nativeElement , style , styles [ style ] ) ;
55+ }
56+ }
57+
58+ private addClass ( classes ) {
59+ let classArray = Array . isArray ( classes ) ? classes : classes . split ( " " )
60+ classArray . forEach ( element => {
61+ this . renderer . addClass ( this . el . nativeElement , element ) ;
62+ } ) ;
63+ }
64+
65+ private setAttrib ( key , value ) {
66+ let newAttr = document . createAttribute ( key ) ;
67+ newAttr . value = value ;
68+ this . renderer . setAttribute ( this . el . nativeElement , key , value ) ;
69+ }
70+ }
71+
3272@Component ( {
3373 selector : 'app-sidebar-nav' ,
3474 template : `
@@ -103,33 +143,61 @@ export class AppSidebarNavItemComponent implements OnInit {
103143 ngOnInit ( ) {
104144 Replace ( this . el ) ;
105145 }
106-
107146}
108147
109148@Component ( {
110149 selector : 'app-sidebar-nav-link' ,
111150 template : `
112- <a *ngIf="!isExternalLink(); else external"
113- [ngClass]="hasVariant() ? 'nav-link nav-link-' + link.variant : 'nav-link'"
114- routerLinkActive="active"
115- [routerLink]="[link.url]"
116- (click)="hideMobile()">
117- <i *ngIf="isIcon()" class="nav-icon {{ link.icon }}"></i>
118- {{ link.name }}
119- <span *ngIf="isBadge()" [ngClass]="'badge badge-' + link.badge.variant">{{ link.badge.text }}</span>
120- </a>
121- <ng-template #external>
122- <a [ngClass]="hasVariant() ? 'nav-link nav-link-' + link.variant : 'nav-link'" href="{{link.url}}">
151+ <ng-container [ngSwitch]="getLinkType()">
152+ <a *ngSwitchCase="'disabled'"
153+ [attr.disabled]="true"
154+ [appLinkAttributes]="link.attributes"
155+ href=""
156+ [ngClass]="getClasses()"
157+ >
158+ <i *ngIf="isIcon()" class="nav-icon {{ link.icon }}"></i>
159+ {{ link.name }}
160+ <span *ngIf="isBadge()" [ngClass]="'badge badge-' + link.badge.variant">{{ link.badge.text }}</span>
161+ </a>
162+ <a *ngSwitchCase="'external'" [ngClass]="getClasses()" href="{{link.url}}" [appLinkAttributes]="link.attributes">
123163 <i *ngIf="isIcon()" class="nav-icon {{ link.icon }}"></i>
124164 {{ link.name }}
125165 <span *ngIf="isBadge()" [ngClass]="'badge badge-' + link.badge.variant">{{ link.badge.text }}</span>
126166 </a>
127- </ng-template>
167+ <a *ngSwitchDefault
168+ [ngClass]="getClasses()"
169+ [appLinkAttributes]="link.attributes"
170+ routerLinkActive="active"
171+ [routerLink]="[link.url]"
172+ (click)="hideMobile()">
173+ <i *ngIf="isIcon()" class="nav-icon {{ link.icon }}"></i>
174+ {{ link.name }}
175+ <span *ngIf="isBadge()" [ngClass]="'badge badge-' + link.badge.variant">{{ link.badge.text }}</span>
176+ </a>
177+ </ng-container>
128178 `
129179} )
130180export class AppSidebarNavLinkComponent implements OnInit {
131181 @Input ( ) link : any ;
132182
183+ public getClasses ( ) {
184+ const disabled = this . isDisabled ( )
185+ const classes = {
186+ 'nav-link' : true ,
187+ 'disabled' : disabled ,
188+ 'btn-link' : disabled
189+ }
190+ if ( this . hasVariant ( ) ) {
191+ const variant = `nav-link-${ this . link . variant } ` ;
192+ classes [ variant ] = true ;
193+ }
194+ return classes
195+ }
196+
197+ public getLinkType ( ) {
198+ return this . isDisabled ( ) ? 'disabled' : this . isExternalLink ( ) ? 'external' : ''
199+ }
200+
133201 public hasVariant ( ) {
134202 return this . link . variant ? true : false ;
135203 }
@@ -138,6 +206,10 @@ export class AppSidebarNavLinkComponent implements OnInit {
138206 return this . link . badge ? true : false ;
139207 }
140208
209+ public isDisabled ( ) {
210+ return this . link . attributes && this . link . attributes . disabled ? true : false ;
211+ }
212+
141213 public isExternalLink ( ) {
142214 return this . link . url . substring ( 0 , 4 ) === 'http' ? true : false ;
143215 }
0 commit comments