Skip to content

Commit

Permalink
chore: add st comp
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Jan 19, 2024
1 parent f5fcbfe commit c202c83
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 75 deletions.
10 changes: 6 additions & 4 deletions packages/abc/st/st-export.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, Optional } from '@angular/core';
import { Injectable, inject } from '@angular/core';

import { XlsxExportResult, XlsxService } from '@delon/abc/xlsx';
import { deepGet } from '@delon/util/other';
Expand All @@ -9,7 +9,7 @@ import { _STColumn } from './st.types';

@Injectable()
export class STExport {
constructor(@Optional() private xlsxSrv: XlsxService) {}
private readonly xlsxSrv = inject(XlsxService, { optional: true });

private _stGet(item: NzSafeAny, col: STColumn, index: number, colIndex: number): NzSafeAny {
const ret: { [key: string]: NzSafeAny } = { t: 's', v: '' };
Expand Down Expand Up @@ -59,7 +59,7 @@ export class STExport {
if (invalidFn(col)) continue;
if (!wpx && col._width != null) wpx = true;
++validColCount;
const columnName = this.xlsxSrv.numberToSchema(validColCount);
const columnName = this.xlsxSrv!.numberToSchema(validColCount);
sheet[`${columnName}1`] = {
t: 's',
v: typeof col.title === 'object' ? col.title.text : col.title
Expand All @@ -74,13 +74,15 @@ export class STExport {
}

if (validColCount > 0 && dataLen > 0) {
sheet['!ref'] = `A1:${this.xlsxSrv.numberToSchema(validColCount)}${dataLen + 1}`;
sheet['!ref'] = `A1:${this.xlsxSrv!.numberToSchema(validColCount)}${dataLen + 1}`;
}

return sheets;
}

async export(opt: STExportOptions): Promise<XlsxExportResult> {
if (this.xlsxSrv == null) return Promise.reject();

const sheets = this.genSheet(opt);
return this.xlsxSrv.export({
sheets,
Expand Down
7 changes: 4 additions & 3 deletions packages/abc/st/st-filter.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
EventEmitter,
Input,
Output,
ViewEncapsulation
ViewEncapsulation,
inject
} from '@angular/core';

import { LocaleData } from '@delon/theme';
Expand Down Expand Up @@ -134,6 +135,8 @@ import { _STColumn } from './st.types';
encapsulation: ViewEncapsulation.None
})
export class STFilterComponent {
private readonly cdr = inject(ChangeDetectorRef);

visible = false;
@Input() col!: _STColumn;
@Input() locale: LocaleData = {};
Expand All @@ -144,8 +147,6 @@ export class STFilterComponent {
return this.f.icon as STIcon;
}

constructor(private cdr: ChangeDetectorRef) {}

stopPropagation($event: MouseEvent): void {
$event.stopPropagation();
}
Expand Down
9 changes: 3 additions & 6 deletions packages/abc/st/st-row.directive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Directive, Host, Injectable, Input, OnInit, TemplateRef } from '@angular/core';
import { Directive, Injectable, Input, OnInit, TemplateRef, inject } from '@angular/core';

@Injectable()
export class STRowSource {
Expand All @@ -20,15 +20,12 @@ export class STRowSource {

@Directive({ selector: '[st-row]' })
export class STRowDirective implements OnInit {
private readonly source = inject(STRowSource, { host: true });
private readonly ref = inject(TemplateRef);
@Input('st-row') id!: string;

@Input() type?: 'title';

constructor(
private ref: TemplateRef<void>,
@Host() private source: STRowSource
) {}

ngOnInit(): void {
this.source.add(this.type, this.id, this.ref);
}
Expand Down
10 changes: 4 additions & 6 deletions packages/abc/st/st-widget-host.directive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Directive, Input, OnInit, ViewContainerRef } from '@angular/core';
import { Directive, Input, OnInit, ViewContainerRef, inject } from '@angular/core';

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

Expand All @@ -7,14 +7,12 @@ import { STColumn, STData } from './st.interfaces';

@Directive({ selector: '[st-widget-host]' })
export class STWidgetHostDirective implements OnInit {
private readonly stWidgetRegistry = inject(STWidgetRegistry);
private readonly viewContainerRef = inject(ViewContainerRef);

@Input() record!: STData;
@Input() column!: STColumn;

constructor(
private stWidgetRegistry: STWidgetRegistry,
private viewContainerRef: ViewContainerRef
) {}

ngOnInit(): void {
const widget = this.column.widget!;
const componentType = this.stWidgetRegistry.get(widget.type);
Expand Down
89 changes: 34 additions & 55 deletions packages/abc/st/st.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ import { CdkVirtualScrollViewport } from '@angular/cdk/scrolling';
import { DecimalPipe, DOCUMENT } from '@angular/common';
import {
AfterViewInit,
booleanAttribute,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
DestroyRef,
ElementRef,
EventEmitter,
Host,
inject,
Inject,
Input,
OnChanges,
Optional,
Output,
SimpleChange,
SimpleChanges,
Expand All @@ -27,7 +25,6 @@ import { Router } from '@angular/router';
import { isObservable, Observable, of, filter, catchError, map, finalize, throwError, lastValueFrom } from 'rxjs';

import {
AlainI18NService,
ALAIN_I18N_TOKEN,
DatePipe,
DelonLocaleService,
Expand All @@ -37,7 +34,7 @@ import {
YNPipe
} from '@delon/theme';
import { AlainConfigService, AlainSTConfig } from '@delon/util/config';
import { BooleanInput, InputBoolean, InputNumber, NumberInput, toBoolean } from '@delon/util/decorator';
import { toBoolean, toNumber } from '@delon/util/decorator';
import { deepCopy, deepMergeKey } from '@delon/util/other';
import type { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzContextMenuService, NzDropdownMenuComponent } from 'ng-zorro-antd/dropdown';
Expand Down Expand Up @@ -97,21 +94,16 @@ import { _STColumn, _STDataValue, _STHeader, _STTdNotify, _STTdNotifyType } from
encapsulation: ViewEncapsulation.None
})
export class STComponent implements AfterViewInit, OnChanges {
static ngAcceptInputType_ps: NumberInput;
static ngAcceptInputType_pi: NumberInput;
static ngAcceptInputType_total: NumberInput;
static ngAcceptInputType_loadingDelay: NumberInput;
static ngAcceptInputType_bordered: BooleanInput;
static ngAcceptInputType_expandRowByClick: BooleanInput;
static ngAcceptInputType_expandAccordion: BooleanInput;
static ngAcceptInputType_responsive: BooleanInput;
static ngAcceptInputType_responsiveHideHeaderFooter: BooleanInput;
static ngAcceptInputType_virtualScroll: BooleanInput;
static ngAcceptInputType_virtualItemSize: NumberInput;
static ngAcceptInputType_virtualMaxBufferPx: NumberInput;
static ngAcceptInputType_virtualMinBufferPx: NumberInput;

private destroy$ = inject(DestroyRef);
private readonly i18nSrv = inject(ALAIN_I18N_TOKEN, { optional: true });
private readonly el: HTMLElement = inject(ElementRef).nativeElement;
private readonly cdr = inject(ChangeDetectorRef);
private readonly doc = inject(DOCUMENT);
private readonly exportSrv = inject(STExport);
private readonly columnSource = inject(STColumnSource);
private readonly dataSource = inject(STDataSource);
private readonly delonI18n = inject(DelonLocaleService);
private readonly cms = inject(NzContextMenuService);
private readonly destroy$ = inject(DestroyRef);
private totalTpl = ``;
private inied = false;
cog!: AlainSTConfig;
Expand Down Expand Up @@ -167,13 +159,13 @@ export class STComponent implements AfterViewInit, OnChanges {
@Input() data?: string | STData[] | Observable<STData[]>;
@Input() columns?: STColumn[] | null;
@Input() contextmenu?: STContextmenuFn | null;
@Input() @InputNumber() ps = 10;
@Input() @InputNumber() pi = 1;
@Input() @InputNumber() total = 0;
@Input({ transform: toNumber }) ps = 10;
@Input({ transform: toNumber }) pi = 1;
@Input({ transform: toNumber }) total = 0;
@Input() loading: boolean | null = null;
@Input() @InputNumber() loadingDelay = 0;
@Input({ transform: toNumber }) loadingDelay = 0;
@Input() loadingIndicator: TemplateRef<void> | null = null;
@Input() @InputBoolean() bordered = false;
@Input({ transform: booleanAttribute }) bordered = false;
@Input() size!: 'small' | 'middle' | 'default';
@Input() scroll: { x?: string | null; y?: string | null } = { x: null, y: null };
@Input() singleSort?: STSingleSort | null;
Expand Down Expand Up @@ -214,22 +206,22 @@ export class STComponent implements AfterViewInit, OnChanges {
this._resizable = typeof val === 'object' ? val : { disabled: !toBoolean(val) };
}
@Input() header?: string | TemplateRef<void> | null;
@Input() @InputBoolean() showHeader = true;
@Input({ transform: booleanAttribute }) showHeader = true;
@Input() footer?: string | TemplateRef<void> | null;
@Input() bodyHeader?: TemplateRef<{ $implicit: STStatisticalResults }> | null;
@Input() body?: TemplateRef<{ $implicit: STStatisticalResults }> | null;
@Input() @InputBoolean() expandRowByClick = false;
@Input() @InputBoolean() expandAccordion = false;
@Input({ transform: booleanAttribute }) expandRowByClick = false;
@Input({ transform: booleanAttribute }) expandAccordion = false;
@Input() expand: TemplateRef<{ $implicit: NzSafeAny; index: number }> | null = null;
@Input() noResult?: string | TemplateRef<void> | null;
@Input() @InputBoolean() responsive: boolean = true;
@Input() @InputBoolean() responsiveHideHeaderFooter?: boolean;
@Input({ transform: booleanAttribute }) responsive: boolean = true;
@Input({ transform: booleanAttribute }) responsiveHideHeaderFooter?: boolean;
@Output() readonly error = new EventEmitter<STError>();
@Output() readonly change = new EventEmitter<STChange>();
@Input() @InputBoolean() virtualScroll = false;
@Input() @InputNumber() virtualItemSize = 54;
@Input() @InputNumber() virtualMaxBufferPx = 200;
@Input() @InputNumber() virtualMinBufferPx = 100;
@Input({ transform: booleanAttribute }) virtualScroll = false;
@Input({ transform: toNumber }) virtualItemSize = 54;
@Input({ transform: toNumber }) virtualMaxBufferPx = 200;
@Input({ transform: toNumber }) virtualMinBufferPx = 100;
@Input() customRequest?: (options: STCustomRequestOptions) => Observable<NzSafeAny>;
@Input() virtualForTrackBy: TrackByFunction<STData> = index => index;

Expand All @@ -251,18 +243,7 @@ export class STComponent implements AfterViewInit, OnChanges {
return this.columns == null;
}

constructor(
@Optional() @Inject(ALAIN_I18N_TOKEN) i18nSrv: AlainI18NService,
private cdr: ChangeDetectorRef,
private el: ElementRef,
private exportSrv: STExport,
@Inject(DOCUMENT) private doc: NzSafeAny,
private columnSource: STColumnSource,
private dataSource: STDataSource,
private delonI18n: DelonLocaleService,
configSrv: AlainConfigService,
private cms: NzContextMenuService
) {
constructor(configSrv: AlainConfigService) {
this.delonI18n.change.pipe(takeUntilDestroyed()).subscribe(() => {
this.locale = this.delonI18n.getData('st');
if (this._columns.length > 0) {
Expand All @@ -271,7 +252,7 @@ export class STComponent implements AfterViewInit, OnChanges {
}
});

i18nSrv.change
this.i18nSrv?.change
.pipe(
takeUntilDestroyed(),
filter(() => this._columns.length > 0)
Expand Down Expand Up @@ -466,7 +447,7 @@ export class STComponent implements AfterViewInit, OnChanges {

private _toTop(enforce?: boolean): void {
if (!(enforce == null ? this.page.toTop : enforce)) return;
const el = this.el.nativeElement as HTMLElement;
const el = this.el;
el.scrollIntoView();
// fix header height
this.doc.documentElement.scrollTop -= this.page.toTopOffset!;
Expand Down Expand Up @@ -919,6 +900,11 @@ export class STComponent implements AfterViewInit, OnChanges {
encapsulation: ViewEncapsulation.None
})
export class STTdComponent {
private readonly stComp = inject(STComponent, { host: true });
private readonly router = inject(Router);
private readonly modalHelper = inject(ModalHelper);
private readonly drawerHelper = inject(DrawerHelper);

@Input() c!: _STColumn;
@Input() cIdx!: number;
@Input() data!: STData[];
Expand All @@ -931,13 +917,6 @@ export class STTdComponent {
return { pi, ps, total };
}

constructor(
@Host() private stComp: STComponent,
private router: Router,
private modalHelper: ModalHelper,
private drawerHelper: DrawerHelper
) {}

private report(type: _STTdNotifyType): void {
this.n.emit({ type, item: this.i, col: this.c });
}
Expand Down
2 changes: 1 addition & 1 deletion packages/abc/st/test/st.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,7 @@ describe('abc: st', () => {
describe('#export', () => {
let exportSrv: STExport;
beforeEach(() => {
exportSrv = comp['exportSrv'] = {
exportSrv = (comp as any)['exportSrv'] = {
export: jasmine.createSpy('export')
} as any;
});
Expand Down

0 comments on commit c202c83

Please sign in to comment.