Skip to content

Commit

Permalink
chore: clean constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Jan 19, 2024
1 parent f57dc57 commit 0821fec
Show file tree
Hide file tree
Showing 21 changed files with 74 additions and 106 deletions.
5 changes: 3 additions & 2 deletions packages/auth/src/store/cookie-storage.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ describe('auth: cookie-storage', () => {
delete data[key];
})
}
}
},
CookieStorageStore
]
});
store = new CookieStorageStore();
store = TestBed.inject(CookieStorageStore);
});

it('should be never return null', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/chart/water-wave/water-wave.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('chart: water-wave', () => {
const styleSpy = spyOn(page.comp.renderer, 'setStyle');
page.context.animate = false;
page.context.height = 100;
spyOnProperty(page.comp.el.nativeElement.parentNode, 'offsetWidth').and.returnValue(50);
spyOnProperty(page.comp.el.parentNode, 'offsetWidth').and.returnValue(50);
page.dcFirst();
expect(styleSpy.calls.mostRecent().args[2]).toBe('scale(0.5)');
}));
Expand Down
4 changes: 2 additions & 2 deletions packages/theme/src/pipes/safe/html.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Pipe, PipeTransform } from '@angular/core';
import { Pipe, PipeTransform, inject } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';

@Pipe({ name: 'html', standalone: true })
export class HTMLPipe implements PipeTransform {
constructor(private dom: DomSanitizer) {}
private readonly dom = inject(DomSanitizer);

transform(html: string): string | SafeHtml {
return html ? this.dom.bypassSecurityTrustHtml(html) : '';
Expand Down
4 changes: 2 additions & 2 deletions packages/theme/src/pipes/safe/url.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Pipe, PipeTransform } from '@angular/core';
import { Pipe, PipeTransform, inject } from '@angular/core';
import { DomSanitizer, SafeUrl } from '@angular/platform-browser';

@Pipe({ name: 'url', standalone: true })
export class URLPipe implements PipeTransform {
constructor(private dom: DomSanitizer) {}
private readonly dom = inject(DomSanitizer);

transform(url: string): string | SafeUrl {
return url ? this.dom.bypassSecurityTrustUrl(url) : '';
Expand Down
4 changes: 2 additions & 2 deletions packages/theme/src/pipes/yn/yn.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Pipe, PipeTransform } from '@angular/core';
import { Pipe, PipeTransform, inject } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';

export type YNMode = 'full' | 'icon' | 'text';
Expand Down Expand Up @@ -37,7 +37,7 @@ export function yn(value: boolean, opt?: YNOptions): string {

@Pipe({ name: 'yn', standalone: true })
export class YNPipe implements PipeTransform {
constructor(private dom: DomSanitizer) {}
private readonly dom = inject(DomSanitizer);

transform(value: boolean, yes?: string, no?: string, mode?: YNMode, isSafeHtml: boolean = true): SafeHtml {
const html = yn(value, { yes, no, mode });
Expand Down
9 changes: 3 additions & 6 deletions packages/theme/src/services/drawer/drawer.helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, Optional, SkipSelf, TemplateRef, Type } from '@angular/core';
import { Injectable, TemplateRef, Type, inject } from '@angular/core';
import { Observable, Observer } from 'rxjs';

import { deepMerge } from '@delon/util/other';
Expand Down Expand Up @@ -50,16 +50,13 @@ export interface DrawerHelperOptions {
*/
@Injectable({ providedIn: 'root' })
export class DrawerHelper {
private readonly srv = inject(NzDrawerService);
private readonly parentDrawer = inject(DrawerHelper, { optional: true, skipSelf: true });
private openDrawersAtThisLevel: NzDrawerRef[] = [];
get openDrawers(): NzDrawerRef[] {
return this.parentDrawer ? this.parentDrawer.openDrawers : this.openDrawersAtThisLevel;
}

constructor(
private srv: NzDrawerService,
@Optional() @SkipSelf() private parentDrawer: DrawerHelper
) {}

/**
* 构建一个抽屉
*/
Expand Down
8 changes: 3 additions & 5 deletions packages/theme/src/services/http/http.client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { HttpClient, HttpContext, HttpEvent, HttpHeaders, HttpParams, HttpResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import { Observable, of, delay, finalize, switchMap, tap } from 'rxjs';

import { AlainConfigService, AlainThemeHttpClientConfig } from '@delon/util/config';
Expand All @@ -17,11 +17,9 @@ export type HttpObserve = 'body' | 'events' | 'response';
*/
@Injectable({ providedIn: 'root' })
export class _HttpClient {
private readonly http = inject(HttpClient);
private cog: AlainThemeHttpClientConfig;
constructor(
private http: HttpClient,
cogSrv: AlainConfigService
) {
constructor(cogSrv: AlainConfigService) {
this.cog = cogSrv.merge('themeHttp', {
nullValueHandling: 'include',
dateValueHandling: 'timestamp'
Expand Down
14 changes: 5 additions & 9 deletions packages/theme/src/services/i18n/i18n-url.guard.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import { Inject, Injectable, Optional, inject } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivateChildFn, CanActivateFn } from '@angular/router';
import { Observable, of } from 'rxjs';

import { AlainConfigService } from '@delon/util/config';

import { AlainI18NService, ALAIN_I18N_TOKEN } from './i18n';
import { ALAIN_I18N_TOKEN } from './i18n';

@Injectable({ providedIn: 'root' })
export class AlainI18NGuardService {
constructor(
@Optional()
@Inject(ALAIN_I18N_TOKEN)
private i18nSrv: AlainI18NService,
private cogSrv: AlainConfigService
) {}
private readonly i18nSrv = inject(ALAIN_I18N_TOKEN, { optional: true });
private readonly cogSrv = inject(AlainConfigService);

process(route: ActivatedRouteSnapshot): Observable<boolean> {
const lang = route.params && route.params[this.cogSrv.get('themeI18n')?.paramNameOfUrlGuard ?? 'i18n'];
if (lang != null) {
this.i18nSrv.use(lang);
this.i18nSrv?.use(lang);
}
return of(true);
}
Expand Down
19 changes: 8 additions & 11 deletions packages/theme/src/services/menu/menu.service.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
import { Inject, Injectable, OnDestroy, Optional } from '@angular/core';
import { Injectable, OnDestroy, inject } from '@angular/core';
import { BehaviorSubject, Observable, Subscription, share } from 'rxjs';

import { ACLService } from '@delon/acl';
import type { NzSafeAny } from 'ng-zorro-antd/core/types';

import { Menu, MenuIcon, MenuInner } from './interface';
import { AlainI18NService, ALAIN_I18N_TOKEN } from '../i18n/i18n';
import { ALAIN_I18N_TOKEN } from '../i18n/i18n';

/**
* 菜单服务,[在线文档](https://ng-alain.com/theme/menu)
*/
@Injectable({ providedIn: 'root' })
export class MenuService implements OnDestroy {
private readonly i18nSrv = inject(ALAIN_I18N_TOKEN, { optional: true });
private readonly aclService = inject(ACLService, { optional: true });
private _change$: BehaviorSubject<Menu[]> = new BehaviorSubject<Menu[]>([]);
private i18n$: Subscription;
private i18n$?: Subscription;
private data: Menu[] = [];
/**
* 是否完全受控菜单打开状态,默认:`false`
*/
openStrictly = false;

constructor(
@Optional()
@Inject(ALAIN_I18N_TOKEN)
private i18nSrv: AlainI18NService,
@Optional() private aclService: ACLService
) {
this.i18n$ = this.i18nSrv.change.subscribe(() => this.resume());
constructor() {
this.i18n$ = this.i18nSrv?.change.subscribe(() => this.resume());
}

get change(): Observable<Menu[]> {
Expand Down Expand Up @@ -348,6 +345,6 @@ export class MenuService implements OnDestroy {

ngOnDestroy(): void {
this._change$.unsubscribe();
this.i18n$.unsubscribe();
this.i18n$?.unsubscribe();
}
}
10 changes: 4 additions & 6 deletions packages/util/browser/cookie.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Platform } from '@angular/cdk/platform';
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable } from '@angular/core';
import { Injectable, inject } from '@angular/core';

import type { NzSafeAny } from 'ng-zorro-antd/core/types';

Expand All @@ -25,6 +25,9 @@ export interface CookieOptions {
*/
@Injectable({ providedIn: 'root' })
export class CookieService {
private readonly _doc = inject(DOCUMENT);
private readonly platform = inject(Platform);

private get doc(): Document {
return this._doc || document;
}
Expand All @@ -38,11 +41,6 @@ export class CookieService {
return this.platform.isBrowser ? this.doc.cookie : '';
}

constructor(
@Inject(DOCUMENT) private _doc: NzSafeAny,
private platform: Platform
) {}

/**
* Get all cookie key-value pairs
*
Expand Down
16 changes: 6 additions & 10 deletions src/app/layout/header/search-box.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Platform } from '@angular/cdk/platform';
import { AfterViewInit, ChangeDetectionStrategy, Component, ElementRef, Inject, ViewChild } from '@angular/core';
import { AfterViewInit, ChangeDetectionStrategy, Component, ElementRef, ViewChild, inject } from '@angular/core';
import { Router } from '@angular/router';

import { ALAIN_I18N_TOKEN, I18nPipe } from '@delon/theme';
Expand All @@ -8,8 +8,6 @@ import type { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { NzInputModule } from 'ng-zorro-antd/input';

import { I18NService } from '@core';

declare const docsearch: NzSafeAny;

@Component({
Expand All @@ -24,16 +22,14 @@ declare const docsearch: NzSafeAny;
imports: [NzIconModule, NzInputModule, I18nPipe]
})
export class HeaderSearchComponent implements AfterViewInit {
private readonly i18n = inject(ALAIN_I18N_TOKEN);
private readonly platform = inject(Platform);
private readonly router = inject(Router);
private readonly lazySrv = inject(LazyService);

@ViewChild('searchInput', { static: false })
searchInput!: ElementRef<HTMLInputElement>;

constructor(
@Inject(ALAIN_I18N_TOKEN) private i18n: I18NService,
private platform: Platform,
private router: Router,
private lazySrv: LazyService
) {}

ngAfterViewInit(): void {
this.initDocSearch();
}
Expand Down
6 changes: 2 additions & 4 deletions src/app/routes/404/404.component.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Component, Inject } from '@angular/core';
import { Component, inject } from '@angular/core';
import { RouterLink } from '@angular/router';

import { ALAIN_I18N_TOKEN } from '@delon/theme';

import { I18NService } from '@core';

@Component({
selector: 'not-found',
templateUrl: './404.component.html',
standalone: true,
imports: [RouterLink]
})
export class NotFoundComponent {
constructor(@Inject(ALAIN_I18N_TOKEN) public i18n: I18NService) {}
readonly i18n = inject(ALAIN_I18N_TOKEN);
}
12 changes: 5 additions & 7 deletions src/app/shared/components/edit-button/edit-button.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Component, Inject, Input } from '@angular/core';
import { Component, Input, inject } from '@angular/core';

import { ALAIN_I18N_TOKEN, I18nPipe } from '@delon/theme';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { NzToolTipModule } from 'ng-zorro-antd/tooltip';

import { I18NService, MetaService } from '@core';
import { MetaService } from '@core';

@Component({
selector: 'edit-button',
Expand All @@ -23,15 +23,13 @@ import { I18NService, MetaService } from '@core';
imports: [I18nPipe, NzIconModule, NzToolTipModule]
})
export class EditButtonComponent {
private readonly meta = inject(MetaService);
private readonly i18n = inject(ALAIN_I18N_TOKEN);

_full!: string;

@Input()
set item(data: { urls: string }) {
this._full = `${this.meta.github}/edit/master/${this.i18n.get(data.urls)}`;
}

constructor(
private meta: MetaService,
@Inject(ALAIN_I18N_TOKEN) private i18n: I18NService
) {}
}
14 changes: 5 additions & 9 deletions src/app/shared/components/main-menu/main-menu.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Component,
DestroyRef,
EventEmitter,
Inject,
OnInit,
Output,
inject
Expand All @@ -19,7 +18,7 @@ import { NzMenuModule } from 'ng-zorro-antd/menu';
import { NzTagModule } from 'ng-zorro-antd/tag';
import { NzToolTipModule } from 'ng-zorro-antd/tooltip';

import { I18NService, MetaService } from '@core';
import { MetaService } from '@core';

@Component({
selector: 'main-menu, [main-menu]',
Expand All @@ -29,7 +28,10 @@ import { I18NService, MetaService } from '@core';
imports: [RouterLink, RouterLinkActive, NzToolTipModule, NzBadgeModule, NzTagModule, NzMenuModule]
})
export class MainMenuComponent implements OnInit {
private destroy$ = inject(DestroyRef);
private readonly meta = inject(MetaService);
private readonly i18n = inject(ALAIN_I18N_TOKEN);
private readonly cdr = inject(ChangeDetectorRef);
private readonly destroy$ = inject(DestroyRef);
count = 0;

@Output() readonly to = new EventEmitter<string>();
Expand All @@ -38,12 +40,6 @@ export class MainMenuComponent implements OnInit {
return this.meta.menus!;
}

constructor(
private meta: MetaService,
@Inject(ALAIN_I18N_TOKEN) private i18n: I18NService,
private cdr: ChangeDetectorRef
) {}

ngOnInit(): void {
this.i18n.change.pipe(takeUntilDestroyed(this.destroy$)).subscribe(() => this.cdr.markForCheck());
this.count = this.meta.menus?.reduce((p: number, c: NzSafeAny) => (p += c.list.length), 0);
Expand Down
12 changes: 5 additions & 7 deletions src/app/shared/components/match-router/match-router.directive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AfterViewInit, Directive, ElementRef, Input, OnDestroy, Renderer2 } from '@angular/core';
import { AfterViewInit, Directive, ElementRef, Input, OnDestroy, Renderer2, inject } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { Subscription, filter } from 'rxjs';

Expand All @@ -12,6 +12,10 @@ export interface MatchRouterOptions {
standalone: true
})
export class MatchRouterDirective implements AfterViewInit, OnDestroy {
private readonly router = inject(Router);
private readonly element = inject(ElementRef);
private readonly renderer = inject(Renderer2);

private _options!: MatchRouterOptions;
private url$?: Subscription;

Expand All @@ -20,12 +24,6 @@ export class MatchRouterDirective implements AfterViewInit, OnDestroy {
this._options = { cls: 'ant-menu-item-selected', ...val };
}

constructor(
private router: Router,
private element: ElementRef<HTMLElement>,
private renderer: Renderer2
) {}

private setCls(): void {
if (!this.router.navigated) {
return;
Expand Down
Loading

0 comments on commit 0821fec

Please sign in to comment.