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' ;
2
2
import { Replace } from './../shared' ;
3
3
4
4
@Directive ( {
@@ -29,6 +29,46 @@ export class NavDropdownToggleDirective {
29
29
}
30
30
}
31
31
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
+
32
72
@Component ( {
33
73
selector : 'app-sidebar-nav' ,
34
74
template : `
@@ -103,33 +143,61 @@ export class AppSidebarNavItemComponent implements OnInit {
103
143
ngOnInit ( ) {
104
144
Replace ( this . el ) ;
105
145
}
106
-
107
146
}
108
147
109
148
@Component ( {
110
149
selector : 'app-sidebar-nav-link' ,
111
150
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">
123
163
<i *ngIf="isIcon()" class="nav-icon {{ link.icon }}"></i>
124
164
{{ link.name }}
125
165
<span *ngIf="isBadge()" [ngClass]="'badge badge-' + link.badge.variant">{{ link.badge.text }}</span>
126
166
</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>
128
178
`
129
179
} )
130
180
export class AppSidebarNavLinkComponent implements OnInit {
131
181
@Input ( ) link : any ;
132
182
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
+
133
201
public hasVariant ( ) {
134
202
return this . link . variant ? true : false ;
135
203
}
@@ -138,6 +206,10 @@ export class AppSidebarNavLinkComponent implements OnInit {
138
206
return this . link . badge ? true : false ;
139
207
}
140
208
209
+ public isDisabled ( ) {
210
+ return this . link . attributes && this . link . attributes . disabled ? true : false ;
211
+ }
212
+
141
213
public isExternalLink ( ) {
142
214
return this . link . url . substring ( 0 , 4 ) === 'http' ? true : false ;
143
215
}
0 commit comments