Skip to content

Commit b22a29e

Browse files
committed
refactor: migrate constructor-based dependency injection to inject function
1 parent 2e9a2ca commit b22a29e

File tree

62 files changed

+688
-703
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+688
-703
lines changed

projects/coreui-angular-chartjs/src/lib/chartjs.component.ts

+6-25
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,4 @@
1-
import {
2-
afterRender,
3-
AfterViewInit,
4-
booleanAttribute,
5-
ChangeDetectionStrategy,
6-
ChangeDetectorRef,
7-
Component,
8-
ElementRef,
9-
EventEmitter,
10-
HostBinding,
11-
Input,
12-
NgZone,
13-
numberAttribute,
14-
OnChanges,
15-
OnDestroy,
16-
Output,
17-
Renderer2,
18-
SimpleChanges,
19-
ViewChild
20-
} from '@angular/core';
1+
import { afterRender, AfterViewInit, booleanAttribute, ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, EventEmitter, HostBinding, Input, NgZone, numberAttribute, OnChanges, OnDestroy, Output, Renderer2, SimpleChanges, ViewChild, inject } from '@angular/core';
212

223
import merge from 'lodash-es/merge';
234

@@ -38,6 +19,10 @@ let nextId = 0;
3819
// host: { ngSkipHydration: 'true' }
3920
})
4021
export class ChartjsComponent implements AfterViewInit, OnDestroy, OnChanges {
22+
private readonly ngZone = inject(NgZone);
23+
private readonly renderer = inject(Renderer2);
24+
private readonly changeDetectorRef = inject(ChangeDetectorRef);
25+
4126
/**
4227
* Enables custom html based tooltips instead of standard tooltips.
4328
* @type boolean
@@ -126,11 +111,7 @@ export class ChartjsComponent implements AfterViewInit, OnDestroy, OnChanges {
126111
};
127112
}
128113

129-
constructor(
130-
private readonly ngZone: NgZone,
131-
private readonly renderer: Renderer2,
132-
private readonly changeDetectorRef: ChangeDetectorRef
133-
) {
114+
constructor() {
134115
// todo: verify afterRender / afterNextRender for chartjs (spec fails with 17.0.10)
135116
afterRender({
136117
write: () => {

projects/coreui-angular/src/lib/breadcrumb/breadcrumb-router/breadcrumb-router.component.ts

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges } from '@angular/core';
1+
import { Component, inject, Input, OnChanges, OnDestroy, OnInit, SimpleChanges } from '@angular/core';
22
import { Observable, Observer } from 'rxjs';
33
import { AsyncPipe } from '@angular/common';
44

@@ -8,14 +8,12 @@ import { BreadcrumbRouterService } from './breadcrumb-router.service';
88
import { BreadcrumbItemComponent } from '../breadcrumb-item/breadcrumb-item.component';
99

1010
@Component({
11-
selector: 'c-breadcrumb-router, [cBreadcrumbRouter]',
12-
templateUrl: './breadcrumb-router.component.html',
13-
imports: [BreadcrumbComponent, BreadcrumbItemComponent, AsyncPipe]
11+
selector: 'c-breadcrumb-router, [cBreadcrumbRouter]',
12+
templateUrl: './breadcrumb-router.component.html',
13+
imports: [BreadcrumbComponent, BreadcrumbItemComponent, AsyncPipe]
1414
})
1515
export class BreadcrumbRouterComponent implements OnChanges, OnDestroy, OnInit {
16-
constructor(
17-
public service: BreadcrumbRouterService
18-
) {}
16+
readonly service = inject(BreadcrumbRouterService);
1917

2018
/**
2119
* Optional array of IBreadcrumbItem to override default BreadcrumbRouter behavior. [docs]
@@ -36,13 +34,11 @@ export class BreadcrumbRouterComponent implements OnChanges, OnDestroy, OnInit {
3634

3735
setup(): void {
3836
if (this.items && this.items.length > 0) {
39-
this.breadcrumbs = new Observable<IBreadcrumbItem[]>(
40-
(observer: Observer<IBreadcrumbItem[]>) => {
41-
if (this.items) {
42-
observer.next(this.items);
43-
}
37+
this.breadcrumbs = new Observable<IBreadcrumbItem[]>((observer: Observer<IBreadcrumbItem[]>) => {
38+
if (this.items) {
39+
observer.next(this.items);
4440
}
45-
);
41+
});
4642
}
4743
}
4844

projects/coreui-angular/src/lib/breadcrumb/breadcrumb-router/breadcrumb-router.service.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable } from '@angular/core';
1+
import { inject, Injectable } from '@angular/core';
22
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
33
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
44
import { BehaviorSubject, Observable } from 'rxjs';
@@ -9,21 +9,25 @@ import { IBreadcrumbItem } from '../breadcrumb-item/breadcrumb-item';
99
providedIn: 'root'
1010
})
1111
export class BreadcrumbRouterService {
12+
readonly #router = inject(Router);
13+
readonly #activatedRoute = inject(ActivatedRoute);
14+
1215
public outlet = 'primary';
1316

14-
readonly #breadcrumbsBehaviorSubject: BehaviorSubject<IBreadcrumbItem[]> = new BehaviorSubject<IBreadcrumbItem[]>(new Array<IBreadcrumbItem>());
17+
readonly #breadcrumbsBehaviorSubject: BehaviorSubject<IBreadcrumbItem[]> = new BehaviorSubject<IBreadcrumbItem[]>(
18+
new Array<IBreadcrumbItem>()
19+
);
1520
readonly breadcrumbs$: Observable<IBreadcrumbItem[]> = this.#breadcrumbsBehaviorSubject.asObservable();
1621

17-
constructor(private router: Router, private route: ActivatedRoute) {
18-
19-
this.router.events
22+
constructor() {
23+
this.#router.events
2024
.pipe(
2125
takeUntilDestroyed(),
2226
filter((event) => event instanceof NavigationEnd)
2327
)
2428
.subscribe((event) => {
2529
const breadcrumbs: any[] = [];
26-
let currentRoute: ActivatedRoute | null = this.route.root;
30+
let currentRoute: ActivatedRoute | null = this.#activatedRoute.root;
2731
let url = '';
2832
do {
2933
const childrenRoutes: ActivatedRoute[] = currentRoute.children;

projects/coreui-angular/src/lib/carousel/carousel-control/carousel-control.component.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ElementRef,
66
HostBinding,
77
HostListener,
8+
inject,
89
Input,
910
ViewChild
1011
} from '@angular/core';
@@ -16,24 +17,22 @@ import { CarouselState } from '../carousel-state';
1617
templateUrl: './carousel-control.component.html'
1718
})
1819
export class CarouselControlComponent implements AfterViewInit {
19-
constructor(
20-
private changeDetectorRef: ChangeDetectorRef,
21-
private carouselState: CarouselState
22-
) {}
20+
readonly #changeDetectorRef = inject(ChangeDetectorRef);
21+
readonly #carouselState = inject(CarouselState);
2322

24-
private _caption?: string;
2523
/**
2624
* Carousel control caption. [docs]
2725
* @type string
2826
*/
2927
@Input()
3028
set caption(value) {
31-
this._caption = value;
29+
this.#caption = value;
3230
}
3331

3432
get caption(): string {
35-
return !!this._caption ? this._caption : this.direction === 'prev' ? 'Previous' : 'Next';
33+
return !!this.#caption ? this.#caption : this.direction === 'prev' ? 'Previous' : 'Next';
3634
}
35+
#caption?: string;
3736

3837
/**
3938
* Carousel control direction. [docs]
@@ -79,11 +78,11 @@ export class CarouselControlComponent implements AfterViewInit {
7978

8079
ngAfterViewInit(): void {
8180
this.hasContent = this.content?.nativeElement.childNodes.length ?? false;
82-
this.changeDetectorRef.detectChanges();
81+
this.#changeDetectorRef.detectChanges();
8382
}
8483

8584
private play(direction = this.direction): void {
86-
const nextIndex = this.carouselState.direction(direction);
87-
this.carouselState.state = { activeItemIndex: nextIndex };
85+
const nextIndex = this.#carouselState.direction(direction);
86+
this.#carouselState.state = { activeItemIndex: nextIndex };
8887
}
8988
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnDestroy, OnInit } from '@angular/core';
1+
import { Component, inject, OnDestroy, OnInit } from '@angular/core';
22
import { Subscription } from 'rxjs';
33

44
import { CarouselState } from '../carousel-state';
@@ -9,14 +9,12 @@ import { CarouselService } from '../carousel.service';
99
templateUrl: './carousel-indicators.component.html'
1010
})
1111
export class CarouselIndicatorsComponent implements OnInit, OnDestroy {
12-
constructor(
13-
private carouselService: CarouselService,
14-
private carouselState: CarouselState
15-
) {}
12+
readonly #carouselService = inject(CarouselService);
13+
readonly #carouselState = inject(CarouselState);
1614

1715
items: (number | undefined)[] = [];
1816
active = 0;
19-
private carouselIndexSubscription?: Subscription;
17+
#carouselIndexSubscription?: Subscription;
2018

2119
ngOnInit(): void {
2220
this.carouselStateSubscribe();
@@ -29,20 +27,20 @@ export class CarouselIndicatorsComponent implements OnInit, OnDestroy {
2927
onClick(index: number): void {
3028
if (index !== this.active) {
3129
const direction = index < this.active ? 'prev' : 'next';
32-
this.carouselState.state = { direction, activeItemIndex: index };
30+
this.#carouselState.state = { direction, activeItemIndex: index };
3331
}
3432
}
3533

3634
private carouselStateSubscribe(subscribe: boolean = true): void {
3735
if (subscribe) {
38-
this.carouselIndexSubscription = this.carouselService.carouselIndex$.subscribe((nextIndex) => {
39-
this.items = this.carouselState?.state?.items?.map((item) => item.index) ?? [];
36+
this.#carouselIndexSubscription = this.#carouselService.carouselIndex$.subscribe((nextIndex) => {
37+
this.items = this.#carouselState?.state?.items?.map((item) => item.index) ?? [];
4038
if ('active' in nextIndex) {
4139
this.active = nextIndex.active ?? 0;
4240
}
4341
});
4442
} else {
45-
this.carouselIndexSubscription?.unsubscribe();
43+
this.#carouselIndexSubscription?.unsubscribe();
4644
}
4745
}
4846
}

projects/coreui-angular/src/lib/carousel/carousel-inner/carousel-inner.component.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Component,
55
ContentChildren,
66
HostBinding,
7+
inject,
78
QueryList
89
} from '@angular/core';
910

@@ -18,23 +19,23 @@ import { CarouselState } from '../carousel-state';
1819
animations: [slideAnimation, fadeAnimation]
1920
})
2021
export class CarouselInnerComponent implements AfterContentInit, AfterContentChecked {
21-
constructor(private carouselState: CarouselState) {}
22+
readonly #carouselState = inject(CarouselState);
2223

2324
@HostBinding('class.carousel-inner') carouselInnerClass = true;
2425
activeIndex?: number;
2526
animate?: boolean;
2627
slide = { left: true };
2728
transition = 'slide';
2829
@ContentChildren(CarouselItemComponent) private contentItems!: QueryList<CarouselItemComponent>;
29-
private prevContentItems!: QueryList<CarouselItemComponent>;
30+
#prevContentItems!: QueryList<CarouselItemComponent>;
3031

3132
ngAfterContentInit(): void {
3233
this.setItems();
3334
}
3435

3536
ngAfterContentChecked(): void {
3637
this.setItems();
37-
const state = this.carouselState?.state;
38+
const state = this.#carouselState?.state;
3839
const nextIndex = state?.activeItemIndex;
3940
const nextDirection = state?.direction;
4041
if (this.activeIndex !== nextIndex) {
@@ -46,9 +47,9 @@ export class CarouselInnerComponent implements AfterContentInit, AfterContentChe
4647
}
4748

4849
setItems(): void {
49-
if (this.prevContentItems !== this.contentItems) {
50-
this.prevContentItems = this.contentItems;
51-
this.carouselState.setItems(this.contentItems);
50+
if (this.#prevContentItems !== this.contentItems) {
51+
this.#prevContentItems = this.contentItems;
52+
this.#carouselState.setItems(this.contentItems);
5253
}
5354
}
5455
}

projects/coreui-angular/src/lib/carousel/carousel-item/carousel-item.component.ts

+11-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ChangeDetectorRef,
55
Component,
66
HostBinding,
7+
inject,
78
Input,
89
OnDestroy
910
} from '@angular/core';
@@ -18,23 +19,26 @@ import { CarouselService } from '../carousel.service';
1819
host: { class: 'carousel-item' }
1920
})
2021
export class CarouselItemComponent implements OnDestroy, AfterViewInit {
22+
readonly #carouselService = inject(CarouselService);
23+
readonly #changeDetectorRef = inject(ChangeDetectorRef);
24+
2125
index?: number;
22-
private carouselIndexSubscription?: Subscription;
26+
#carouselIndexSubscription?: Subscription;
2327

2428
/**
2529
* @ignore
2630
*/
2731
@Input({ transform: booleanAttribute })
2832
set active(value) {
29-
this._active = value;
30-
this.changeDetectorRef.markForCheck();
33+
this.#active = value;
34+
this.#changeDetectorRef.markForCheck();
3135
}
3236

3337
get active(): boolean {
34-
return this._active;
38+
return this.#active;
3539
}
3640

37-
private _active = false;
41+
#active = false;
3842

3943
/**
4044
* Time delay before cycling to next item. If -1, uses carousel interval value.
@@ -51,11 +55,6 @@ export class CarouselItemComponent implements OnDestroy, AfterViewInit {
5155
};
5256
}
5357

54-
constructor(
55-
private carouselService: CarouselService,
56-
private changeDetectorRef: ChangeDetectorRef
57-
) {}
58-
5958
ngOnDestroy(): void {
6059
this.carouselStateSubscribe(false);
6160
}
@@ -68,13 +67,13 @@ export class CarouselItemComponent implements OnDestroy, AfterViewInit {
6867

6968
private carouselStateSubscribe(subscribe: boolean = true): void {
7069
if (subscribe) {
71-
this.carouselIndexSubscription = this.carouselService.carouselIndex$.subscribe((nextIndex) => {
70+
this.#carouselIndexSubscription = this.#carouselService.carouselIndex$.subscribe((nextIndex) => {
7271
if ('active' in nextIndex) {
7372
this.active = nextIndex.active === this.index;
7473
}
7574
});
7675
} else {
77-
this.carouselIndexSubscription?.unsubscribe();
76+
this.#carouselIndexSubscription?.unsubscribe();
7877
}
7978
}
8079
}

0 commit comments

Comments
 (0)