Skip to content

Commit b5df878

Browse files
committed
refactor: rename directive appLinkAttributes -> appHtmlAttr
- moved to layout - feat(app-header): navbarBrand img use appHtmlAttr instead of attr.* - refactor(app-header): drop unused methods - refactor(sidebar-nav): directive appLinkAttributes -> appHtmlAttr
1 parent feff47b commit b5df878

File tree

6 files changed

+69
-99
lines changed

6 files changed

+69
-99
lines changed

projects/coreui/angular/src/lib/header/app-header.component.ts

+10-39
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, ElementRef, Input, OnInit, OnDestroy, Inject, Renderer2 } from '@angular/core';
1+
import {Component, ElementRef, Input, OnInit, OnDestroy, Inject, Renderer2} from '@angular/core';
22
import { DOCUMENT } from '@angular/common';
33

44
import { Replace } from '../shared';
@@ -15,23 +15,14 @@ import { Replace } from '../shared';
1515
<a class="navbar-brand" [href]="navbarBrandHref">
1616
<ng-template [ngIf]="navbarBrandImg">
1717
<img *ngIf="navbarBrand"
18-
[src]="imgSrc(navbarBrand)"
19-
[attr.width]="imgWidth(navbarBrand)"
20-
[attr.height]="imgHeight(navbarBrand)"
21-
[attr.alt]="imgAlt(navbarBrand)"
22-
class="navbar-brand">
18+
[appHtmlAttr]="navbarBrand"
19+
[ngClass]="'navbar-brand'">
2320
<img *ngIf="navbarBrandFull"
24-
[src]="imgSrc(navbarBrandFull)"
25-
[attr.width]="imgWidth(navbarBrandFull)"
26-
[attr.height]="imgHeight(navbarBrandFull)"
27-
[attr.alt]="imgAlt(navbarBrandFull)"
28-
class="navbar-brand-full">
21+
[appHtmlAttr]="navbarBrandFull"
22+
[ngClass]="'navbar-brand-full'">
2923
<img *ngIf="navbarBrandMinimized"
30-
[src]="imgSrc(navbarBrandMinimized)"
31-
[attr.width]="imgWidth(navbarBrandMinimized)"
32-
[attr.height]="imgHeight(navbarBrandMinimized)"
33-
[attr.alt]="imgAlt(navbarBrandMinimized)"
34-
class="navbar-brand-minimized">
24+
[appHtmlAttr]="navbarBrandMinimized"
25+
[ngClass]="'navbar-brand-minimized'">
3526
</ng-template>
3627
<ng-template [ngIf]="!navbarBrandImg">
3728
<div class="navbar-brand-full" [innerHTML]="navbarBrandText.text"></div>
@@ -73,6 +64,7 @@ export class AppHeaderComponent implements OnInit, OnDestroy {
7364
@Input() asideMenuToggler: any;
7465
@Input() mobileAsideMenuToggler: any;
7566

67+
private readonly fixedClass = 'header-fixed';
7668
navbarBrandImg: boolean;
7769

7870
constructor(
@@ -88,33 +80,12 @@ export class AppHeaderComponent implements OnInit, OnDestroy {
8880
}
8981

9082
ngOnDestroy(): void {
91-
this.renderer.removeClass(this.document.body, 'header-fixed');
83+
this.renderer.removeClass(this.document.body, this.fixedClass);
9284
}
9385

9486
isFixed(fixed: boolean = this.fixed): void {
9587
if (fixed) {
96-
this.renderer.addClass(this.document.body, 'header-fixed');
88+
this.renderer.addClass(this.document.body, this.fixedClass);
9789
}
9890
}
99-
100-
imgSrc(brand: any): void {
101-
return brand.src ? brand.src : '';
102-
}
103-
104-
imgWidth(brand: any): void {
105-
return brand.width ? brand.width : 'auto';
106-
}
107-
108-
imgHeight(brand: any): void {
109-
return brand.height ? brand.height : 'auto';
110-
}
111-
112-
imgAlt(brand: any): void {
113-
return brand.alt ? brand.alt : '';
114-
}
115-
116-
breakpoint(breakpoint: any): void {
117-
console.log(breakpoint);
118-
return breakpoint ? breakpoint : '';
119-
}
12091
}

projects/coreui/angular/src/lib/header/app-header.module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CommonModule} from '@angular/common';
22
import { NgModule } from '@angular/core';
3-
import { LayoutModule } from './../shared/layout/layout.module';
3+
import { LayoutModule } from '../shared/layout';
44

55
import { AppHeaderComponent } from './app-header.component';
66

projects/coreui/angular/src/lib/shared/layout/layout.directive.ts

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Directive, HostListener, Inject, Input, OnInit, Renderer2} from '@angular/core';
1+
import {Directive, ElementRef, HostListener, Inject, Input, OnInit, Renderer2} from '@angular/core';
22
import {DOCUMENT} from '@angular/common';
33

44
import { asideMenuCssClasses, sidebarCssClasses } from '../classes';
@@ -157,3 +157,46 @@ export class AsideToggleDirective implements OnInit {
157157
this.classToggler.toggleClasses(cssClass, asideMenuCssClasses);
158158
}
159159
}
160+
161+
@Directive({
162+
selector: '[appHtmlAttr]'
163+
})
164+
export class HtmlAttributesDirective implements OnInit {
165+
@Input() appHtmlAttr: {[key: string]: string };
166+
167+
constructor(
168+
@Inject(DOCUMENT) private document: any,
169+
private renderer: Renderer2,
170+
private el: ElementRef
171+
) {}
172+
173+
ngOnInit() {
174+
const attribs = this.appHtmlAttr;
175+
for (const attr in attribs) {
176+
if (attr === 'style' && typeof(attribs[attr]) === 'object' ) {
177+
this.setStyle(attribs[attr]);
178+
} else if (attr === 'class') {
179+
this.addClass(attribs[attr]);
180+
} else {
181+
this.setAttrib(attr, attribs[attr]);
182+
}
183+
}
184+
}
185+
186+
private setStyle(styles) {
187+
for (const style in styles) {
188+
this.renderer.setStyle(this.el.nativeElement, style, styles[style] );
189+
}
190+
}
191+
192+
private addClass(classes) {
193+
const classArray = (Array.isArray(classes) ? classes : classes.split(' '));
194+
classArray.filter((element) => element.length > 0).forEach(element => {
195+
this.renderer.addClass(this.el.nativeElement, element );
196+
});
197+
}
198+
199+
private setAttrib(key, value) {
200+
this.renderer.setAttribute(this.el.nativeElement, key, value );
201+
}
202+
}

projects/coreui/angular/src/lib/shared/layout/layout.module.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
MobileSidebarToggleDirective,
77
SidebarToggleDirective,
88
SidebarMinimizeDirective,
9-
SidebarOffCanvasCloseDirective
9+
SidebarOffCanvasCloseDirective,
10+
HtmlAttributesDirective
1011
} from './layout.directive';
1112
import { ClassToggler } from '../toggle-classes';
1213

@@ -20,15 +21,17 @@ import { ClassToggler } from '../toggle-classes';
2021
MobileSidebarToggleDirective,
2122
SidebarToggleDirective,
2223
SidebarMinimizeDirective,
23-
SidebarOffCanvasCloseDirective
24+
SidebarOffCanvasCloseDirective,
25+
HtmlAttributesDirective
2426
],
2527
declarations: [
2628
AsideToggleDirective,
2729
BrandMinimizeDirective,
2830
MobileSidebarToggleDirective,
2931
SidebarToggleDirective,
3032
SidebarMinimizeDirective,
31-
SidebarOffCanvasCloseDirective
33+
SidebarOffCanvasCloseDirective,
34+
HtmlAttributesDirective
3235
],
3336
providers: [
3437
ClassToggler

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

+5-49
Original file line numberDiff line numberDiff line change
@@ -44,51 +44,6 @@ export class NavDropdownToggleDirective {
4444
}
4545
}
4646

47-
@Directive({
48-
selector: '[appLinkAttributes]'
49-
})
50-
export class LinkAttributesDirective implements OnInit {
51-
@Input() appLinkAttributes: {[key: string]: string };
52-
53-
constructor(
54-
@Inject(DOCUMENT) private document: any,
55-
private renderer: Renderer2,
56-
private el: ElementRef
57-
) {}
58-
59-
ngOnInit() {
60-
const attribs = this.appLinkAttributes;
61-
for (const attr in attribs) {
62-
if (attr === 'style' && typeof(attribs[attr]) === 'object' ) {
63-
this.setStyle(attribs[attr]);
64-
} else if (attr === 'class') {
65-
this.addClass(attribs[attr]);
66-
} else {
67-
this.setAttrib(attr, attribs[attr]);
68-
}
69-
}
70-
}
71-
72-
private setStyle(styles) {
73-
for (const style in styles) {
74-
this.renderer.setStyle(this.el.nativeElement, style, styles[style] );
75-
}
76-
}
77-
78-
private addClass(classes) {
79-
const classArray = Array.isArray(classes) ? classes : classes.split(' ');
80-
classArray.forEach(element => {
81-
this.renderer.addClass(this.el.nativeElement, element );
82-
});
83-
}
84-
85-
private setAttrib(key, value) {
86-
const newAttr = this.document.createAttribute(key);
87-
newAttr.value = value;
88-
this.renderer.setAttribute(this.el.nativeElement, key, value );
89-
}
90-
}
91-
9247
@Component({
9348
selector: 'app-sidebar-nav',
9449
template: `
@@ -102,7 +57,8 @@ export class LinkAttributesDirective implements OnInit {
10257
<app-sidebar-nav-item [item]='navitem'></app-sidebar-nav-item>
10358
</ng-template>
10459
</ng-template>
105-
</ul>`
60+
</ul>
61+
`
10662
})
10763
export class AppSidebarNavComponent implements OnChanges {
10864
@Input() navItems: Array<any>;
@@ -175,21 +131,21 @@ export class AppSidebarNavItemComponent implements OnInit {
175131
<ng-container [ngSwitch]="getLinkType()">
176132
<a *ngSwitchCase="'disabled'"
177133
[attr.disabled]="true"
178-
[appLinkAttributes]="link.attributes"
134+
[appHtmlAttr]="link.attributes"
179135
href=""
180136
[ngClass]="getClasses()">
181137
<i *ngIf="isIcon()" class="nav-icon {{ link.icon }}"></i>
182138
{{ link.name }}
183139
<span *ngIf="isBadge()" [ngClass]="'badge badge-' + link.badge.variant">{{ link.badge.text }}</span>
184140
</a>
185-
<a *ngSwitchCase="'external'" [ngClass]="getClasses()" href="{{link.url}}" [appLinkAttributes]="link.attributes">
141+
<a *ngSwitchCase="'external'" [ngClass]="getClasses()" href="{{link.url}}" [appHtmlAttr]="link.attributes">
186142
<i *ngIf="isIcon()" class="nav-icon {{ link.icon }}"></i>
187143
{{ link.name }}
188144
<span *ngIf="isBadge()" [ngClass]="'badge badge-' + link.badge.variant">{{ link.badge.text }}</span>
189145
</a>
190146
<a *ngSwitchDefault
191147
[ngClass]="getClasses()"
192-
[appLinkAttributes]="link.attributes"
148+
[appHtmlAttr]="link.attributes"
193149
routerLinkActive="active"
194150
[routerLink]="[link.url]"
195151
(click)="hideMobile()">

projects/coreui/angular/src/lib/sidebar/app-sidebar.module.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { CommonModule } from '@angular/common';
22
import { NgModule} from '@angular/core';
33
import { RouterModule } from '@angular/router';
4-
import { LayoutModule } from './../shared/layout/layout.module';
4+
import { LayoutModule } from '../shared/layout';
55

66
// App Sidebar Component
77
import { AppSidebarFooterComponent } from './app-sidebar-footer.component';
@@ -16,8 +16,7 @@ import {
1616
AppSidebarNavLinkComponent,
1717
AppSidebarNavTitleComponent,
1818
NavDropdownDirective,
19-
NavDropdownToggleDirective,
20-
LinkAttributesDirective
19+
NavDropdownToggleDirective
2120
} from './app-sidebar-nav.component';
2221

2322
@NgModule({
@@ -39,7 +38,6 @@ import {
3938
AppSidebarNavTitleComponent,
4039
NavDropdownDirective,
4140
NavDropdownToggleDirective,
42-
LinkAttributesDirective,
4341
LayoutModule
4442
],
4543
declarations: [
@@ -55,8 +53,7 @@ import {
5553
AppSidebarNavLinkComponent,
5654
AppSidebarNavTitleComponent,
5755
NavDropdownDirective,
58-
NavDropdownToggleDirective,
59-
LinkAttributesDirective
56+
NavDropdownToggleDirective
6057
]
6158
})
6259
export class AppSidebarModule { }

0 commit comments

Comments
 (0)