UNPKG

ng-zorro-antd

Version:

An enterprise-class UI components based on Ant Design and Angular

908 lines (905 loc) • 24.4 kB
import { DatePipe, NgTemplateOutlet } from "@angular/common"; import * as i0 from "@angular/core"; import { ChangeDetectorRef, Component, DestroyRef, EventEmitter, Input, NgModule, Output, ViewEncapsulation, booleanAttribute, computed, forwardRef, inject, input } from "@angular/core"; import { takeUntilDestroyed, toSignal } from "@angular/core/rxjs-interop"; import * as i2 from "@angular/forms"; import { FormBuilder, FormsModule, NG_VALIDATORS, NG_VALUE_ACCESSOR, Validators } from "@angular/forms"; import { map } from "rxjs/operators"; import { CronExpressionParser } from "cron-parser"; import { NzFormStatusService } from "ng-zorro-antd/core/form"; import { getStatusClassNames } from "ng-zorro-antd/core/util"; import { NzI18nService } from "ng-zorro-antd/i18n"; import * as i1$1 from "ng-zorro-antd/input"; import { NzInputModule } from "ng-zorro-antd/input"; import * as i1 from "ng-zorro-antd/icon"; import { NzIconModule } from "ng-zorro-antd/icon"; var NzCronExpressionInputComponent = class NzCronExpressionInputComponent { value = "0"; label = "second"; disabled = false; focusEffect = new EventEmitter(); blurEffect = new EventEmitter(); getValue = new EventEmitter(); focusInputEffect(event) { this.focusEffect.emit(this.label); event.target.select(); } blurInputEffect() { this.blurEffect.emit(); } setValue() { this.getValue.emit({ label: this.label, value: this.value }); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCronExpressionInputComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "16.1.0", version: "22.2.2", type: NzCronExpressionInputComponent, isStandalone: true, selector: "nz-cron-expression-input", inputs: { value: "value", label: "label", disabled: [ "disabled", "disabled", booleanAttribute ] }, outputs: { focusEffect: "focusEffect", blurEffect: "blurEffect", getValue: "getValue" }, exportAs: ["nzCronExpressionInput"], ngImport: i0, template: ` <div class="ant-cron-expression-input"> <input nz-input [(ngModel)]="value" [ngModelOptions]="{ standalone: true }" [name]="label" [disabled]="disabled" (focus)="focusInputEffect($event)" (blur)="blurInputEffect()" (ngModelChange)="setValue()" /> </div> `, isInline: true, dependencies: [ { kind: "ngmodule", type: NzInputModule }, { kind: "directive", type: i1$1.NzInputDirective, selector: "input[nz-input],textarea[nz-input]", inputs: [ "nzVariant", "nzSize", "nzStatus", "disabled", "readonly" ], exportAs: ["nzInput"] }, { kind: "ngmodule", type: FormsModule }, { kind: "directive", type: i2.DefaultValueAccessor, selector: "input:not([type=checkbox]):not([ngNoCva])[formControlName],textarea:not([ngNoCva])[formControlName],input:not([type=checkbox]):not([ngNoCva])[formControl],textarea:not([ngNoCva])[formControl],input:not([type=checkbox]):not([ngNoCva])[ngModel],textarea:not([ngNoCva])[ngModel],[ngDefaultControl]" }, { kind: "directive", type: i2.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i2.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: [ "name", "disabled", "ngModel", "ngModelOptions" ], outputs: ["ngModelChange"], exportAs: ["ngModel"] } ], encapsulation: i0.ViewEncapsulation.None }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCronExpressionInputComponent, decorators: [{ type: Component, args: [{ selector: "nz-cron-expression-input", exportAs: "nzCronExpressionInput", encapsulation: ViewEncapsulation.None, template: ` <div class="ant-cron-expression-input"> <input nz-input [(ngModel)]="value" [ngModelOptions]="{ standalone: true }" [name]="label" [disabled]="disabled" (focus)="focusInputEffect($event)" (blur)="blurInputEffect()" (ngModelChange)="setValue()" /> </div> `, imports: [NzInputModule, FormsModule] }] }], propDecorators: { value: [{ type: Input }], label: [{ type: Input }], disabled: [{ type: Input, args: [{ transform: booleanAttribute }] }], focusEffect: [{ type: Output }], blurEffect: [{ type: Output }], getValue: [{ type: Output }] } }); var NzCronExpressionLabelComponent = class NzCronExpressionLabelComponent { type = "second"; locale; labelFocus = null; static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCronExpressionLabelComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "22.2.2", type: NzCronExpressionLabelComponent, isStandalone: true, selector: "nz-cron-expression-label", inputs: { type: "type", locale: "locale", labelFocus: "labelFocus" }, exportAs: ["nzCronExpressionLabel"], ngImport: i0, template: ` <div class="ant-cron-expression-label" [class.ant-cron-expression-label-foucs]="labelFocus === type"> <label> {{ locale[type] }} </label> </div> `, isInline: true, encapsulation: i0.ViewEncapsulation.None }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCronExpressionLabelComponent, decorators: [{ type: Component, args: [{ selector: "nz-cron-expression-label", exportAs: "nzCronExpressionLabel", encapsulation: ViewEncapsulation.None, template: ` <div class="ant-cron-expression-label" [class.ant-cron-expression-label-foucs]="labelFocus === type"> <label> {{ locale[type] }} </label> </div> ` }] }], propDecorators: { type: [{ type: Input }], locale: [{ type: Input }], labelFocus: [{ type: Input }] } }); var NzCronExpressionPreviewComponent = class NzCronExpressionPreviewComponent { cdr = inject(ChangeDetectorRef); TimeList = []; visible = true; locale; nzSemantic = null; loadMorePreview = new EventEmitter(); isExpand = true; setExpand() { this.isExpand = !this.isExpand; this.cdr.markForCheck(); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCronExpressionPreviewComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "22.2.2", type: NzCronExpressionPreviewComponent, isStandalone: true, selector: "nz-cron-expression-preview", inputs: { TimeList: "TimeList", visible: [ "visible", "visible", booleanAttribute ], locale: "locale", nzSemantic: "nzSemantic" }, outputs: { loadMorePreview: "loadMorePreview" }, exportAs: ["nzCronExpressionPreview"], ngImport: i0, template: ` <div class="ant-collapse ant-collapse-borderless ant-cron-expression-preview"> <div class="ant-cron-expression-preview-dateTime" [class.ant-cron-expression-preview-dateTime-center]="!isExpand"> @if (visible) { @if (!nzSemantic) { {{ TimeList[0] | date: 'yyyy-MM-dd HH:mm:ss' }} } @else { <ng-template [ngTemplateOutlet]="nzSemantic" /> } } @else { {{ locale.cronError }} } </div> @if (visible && !isExpand) { <div class="ant-cron-expression-preview-content"> <ul class="ant-cron-expression-preview-list"> @for (item of TimeList; track item) { <li> {{ item | date: 'yyyy-MM-dd HH:mm:ss' }} </li> } <li><a (click)="loadMorePreview.emit()">···</a></li> </ul> </div> } <ul class="ant-cron-expression-preview-icon"> @if (isExpand) { <li><nz-icon nzType="down" nzTheme="outline" (click)="setExpand()" /></li> } @else { <li><nz-icon nzType="up" nzTheme="outline" (click)="setExpand()" /></li> } </ul> </div> `, isInline: true, dependencies: [ { kind: "directive", type: NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: [ "ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector" ] }, { kind: "ngmodule", type: NzIconModule }, { kind: "directive", type: i1.NzIconDirective, selector: "nz-icon,[nz-icon]", inputs: [ "nzType", "nzTheme", "nzTwotoneColor", "nzSpin", "nzRotate", "nzIconfont", "aria-label" ], exportAs: ["nzIcon"] }, { kind: "pipe", type: DatePipe, name: "date" } ], encapsulation: i0.ViewEncapsulation.None }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCronExpressionPreviewComponent, decorators: [{ type: Component, args: [{ selector: "nz-cron-expression-preview", exportAs: "nzCronExpressionPreview", encapsulation: ViewEncapsulation.None, template: ` <div class="ant-collapse ant-collapse-borderless ant-cron-expression-preview"> <div class="ant-cron-expression-preview-dateTime" [class.ant-cron-expression-preview-dateTime-center]="!isExpand"> @if (visible) { @if (!nzSemantic) { {{ TimeList[0] | date: 'yyyy-MM-dd HH:mm:ss' }} } @else { <ng-template [ngTemplateOutlet]="nzSemantic" /> } } @else { {{ locale.cronError }} } </div> @if (visible && !isExpand) { <div class="ant-cron-expression-preview-content"> <ul class="ant-cron-expression-preview-list"> @for (item of TimeList; track item) { <li> {{ item | date: 'yyyy-MM-dd HH:mm:ss' }} </li> } <li><a (click)="loadMorePreview.emit()">···</a></li> </ul> </div> } <ul class="ant-cron-expression-preview-icon"> @if (isExpand) { <li><nz-icon nzType="down" nzTheme="outline" (click)="setExpand()" /></li> } @else { <li><nz-icon nzType="up" nzTheme="outline" (click)="setExpand()" /></li> } </ul> </div> `, imports: [ NgTemplateOutlet, DatePipe, NzIconModule ] }] }], propDecorators: { TimeList: [{ type: Input }], visible: [{ type: Input, args: [{ transform: booleanAttribute }] }], locale: [{ type: Input }], nzSemantic: [{ type: Input }], loadMorePreview: [{ type: Output }] } }); function labelsOfType(type) { if (type === "spring") return [ "second", "minute", "hour", "day", "month", "week" ]; return [ "minute", "hour", "day", "month", "week" ]; } var NzCronExpressionComponent = class NzCronExpressionComponent { formBuilder = inject(FormBuilder); cdr = inject(ChangeDetectorRef); i18n = inject(NzI18nService); destroyRef = inject(DestroyRef); nzFormStatusService = inject(NzFormStatusService, { optional: true }); nzStatus = input("", ...ngDevMode ? [{ debugName: "nzStatus" }] : /* istanbul ignore next */ []); nzSize = "default"; nzType = "linux"; nzCollapseDisable = false; nzExtra = null; nzSemantic = null; nzBorderless = false; nzDisabled = false; locale; focus = false; labelFocus = null; labels = labelsOfType(this.nzType); interval; nextTimeList = []; isNzDisableFirstChange = true; cronValidatorFn = (control) => { if (control.value) try { const cron = this.labels.map((label) => control.value[label]).join(" "); CronExpressionParser.parse(cron); } catch { return { error: true }; } return null; }; form = this.formBuilder.nonNullable.group({ second: ["0", Validators.required], minute: ["*", Validators.required], hour: ["*", Validators.required], day: ["*", Validators.required], month: ["*", Validators.required], week: ["*", Validators.required] }, { validators: this.cronValidatorFn }); inheritedStatus = this.nzFormStatusService ? toSignal(this.nzFormStatusService.formStatusChanges.pipe(map(({ status }) => status)), { initialValue: "" }) : this.nzStatus; internalFormStatus = toSignal(this.form.statusChanges, { initialValue: this.form.status }); finalStatus = computed(() => this.internalFormStatus() === "INVALID" ? "error" : this.inheritedStatus(), ...ngDevMode ? [{ debugName: "finalStatus" }] : /* istanbul ignore next */ []); statusClasses = computed(() => getStatusClassNames("ant-input", this.finalStatus()), ...ngDevMode ? [{ debugName: "statusClasses" }] : /* istanbul ignore next */ []); onChange = () => {}; onTouch = () => null; convertFormat(value) { const values = value.split(" "); const valueObject = this.labels.reduce((obj, label, idx) => { obj[label] = values[idx]; return obj; }, {}); this.form.patchValue(valueObject); } writeValue(value) { if (value) this.convertFormat(value); } registerOnChange(fn) { this.onChange = fn; } registerOnTouched(fn) { this.onTouch = fn; } validate() { return this.form.valid ? null : { error: true }; } setDisabledState(isDisabled) { this.nzDisabled = this.isNzDisableFirstChange && this.nzDisabled || isDisabled; this.isNzDisableFirstChange = false; this.cdr.markForCheck(); } ngOnInit() { this.i18n.localeChange.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => { this.locale = this.i18n.getLocaleData("CronExpression"); this.cdr.markForCheck(); }); this.cronFormType(); this.previewDate(this.form.value); this.form.valueChanges.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((value) => { this.onChange(Object.values(value).join(" ")); this.previewDate(value); this.cdr.markForCheck(); }); } ngOnChanges(changes) { const { nzType } = changes; if (nzType) { this.labels = labelsOfType(this.nzType); this.cronFormType(); } } cronFormType() { if (this.nzType === "spring") this.form.controls.second.enable(); else this.form.controls.second.disable(); } previewDate(value) { try { this.interval = CronExpressionParser.parse(Object.values(value).join(" ")); this.nextTimeList = [ this.interval.next().toDate(), this.interval.next().toDate(), this.interval.next().toDate(), this.interval.next().toDate(), this.interval.next().toDate() ]; } catch { return; } } loadMorePreview() { this.nextTimeList = [ ...this.nextTimeList, this.interval.next().toDate(), this.interval.next().toDate(), this.interval.next().toDate(), this.interval.next().toDate(), this.interval.next().toDate() ]; this.cdr.markForCheck(); } focusEffect(value) { this.focus = true; this.labelFocus = value; this.cdr.markForCheck(); } blurEffect() { this.focus = false; this.labelFocus = null; this.cdr.markForCheck(); } getValue(item) { this.form.controls[item.label].patchValue(item.value); this.cdr.markForCheck(); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCronExpressionComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "22.2.2", type: NzCronExpressionComponent, isStandalone: true, selector: "nz-cron-expression", inputs: { nzStatus: { classPropertyName: "nzStatus", publicName: "nzStatus", isSignal: true, isRequired: false, transformFunction: null }, nzSize: { classPropertyName: "nzSize", publicName: "nzSize", isSignal: false, isRequired: false, transformFunction: null }, nzType: { classPropertyName: "nzType", publicName: "nzType", isSignal: false, isRequired: false, transformFunction: null }, nzCollapseDisable: { classPropertyName: "nzCollapseDisable", publicName: "nzCollapseDisable", isSignal: false, isRequired: false, transformFunction: booleanAttribute }, nzExtra: { classPropertyName: "nzExtra", publicName: "nzExtra", isSignal: false, isRequired: false, transformFunction: null }, nzSemantic: { classPropertyName: "nzSemantic", publicName: "nzSemantic", isSignal: false, isRequired: false, transformFunction: null }, nzBorderless: { classPropertyName: "nzBorderless", publicName: "nzBorderless", isSignal: false, isRequired: false, transformFunction: booleanAttribute }, nzDisabled: { classPropertyName: "nzDisabled", publicName: "nzDisabled", isSignal: false, isRequired: false, transformFunction: booleanAttribute } }, providers: [{ provide: NG_VALIDATORS, useExisting: forwardRef(() => NzCronExpressionComponent), multi: true }, { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => NzCronExpressionComponent), multi: true }], exportAs: ["nzCronExpression"], usesOnChanges: true, ngImport: i0, template: ` <div class="ant-cron-expression"> <div class="ant-cron-expression-content"> <div class="ant-input ant-cron-expression-input-group" [class]="statusClasses()" [class.ant-input-lg]="nzSize === 'large'" [class.ant-input-sm]="nzSize === 'small'" [class.ant-input-borderless]="nzBorderless" [class.ant-input-focused]="focus" [class.ant-input-disabled]="nzDisabled" > @for (label of labels; track label) { <nz-cron-expression-input [value]="form.controls[label].value" [label]="label" [disabled]="nzDisabled" (focusEffect)="focusEffect($event)" (blurEffect)="blurEffect()" (getValue)="getValue($event)" /> } </div> <div class="ant-cron-expression-label-group" [class.ant-input-lg]="nzSize === 'large'" [class.ant-cron-expression-label-group-default]="nzSize === 'default'" [class.ant-input-sm]="nzSize === 'small'" > @for (label of labels; track label) { <nz-cron-expression-label [type]="label" [labelFocus]="labelFocus" [locale]="locale" /> } </div> @if (!nzCollapseDisable) { <nz-cron-expression-preview [TimeList]="nextTimeList" [visible]="form.valid" [locale]="locale" [nzSemantic]="nzSemantic" (loadMorePreview)="loadMorePreview()" /> } </div> @if (nzExtra) { <div class="ant-cron-expression-map"> <ng-template [ngTemplateOutlet]="nzExtra" /> </div> } </div> `, isInline: true, dependencies: [ { kind: "component", type: NzCronExpressionInputComponent, selector: "nz-cron-expression-input", inputs: [ "value", "label", "disabled" ], outputs: [ "focusEffect", "blurEffect", "getValue" ], exportAs: ["nzCronExpressionInput"] }, { kind: "component", type: NzCronExpressionLabelComponent, selector: "nz-cron-expression-label", inputs: [ "type", "locale", "labelFocus" ], exportAs: ["nzCronExpressionLabel"] }, { kind: "component", type: NzCronExpressionPreviewComponent, selector: "nz-cron-expression-preview", inputs: [ "TimeList", "visible", "locale", "nzSemantic" ], outputs: ["loadMorePreview"], exportAs: ["nzCronExpressionPreview"] }, { kind: "directive", type: NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: [ "ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector" ] } ], encapsulation: i0.ViewEncapsulation.None }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCronExpressionComponent, decorators: [{ type: Component, args: [{ selector: "nz-cron-expression", exportAs: "nzCronExpression", encapsulation: ViewEncapsulation.None, template: ` <div class="ant-cron-expression"> <div class="ant-cron-expression-content"> <div class="ant-input ant-cron-expression-input-group" [class]="statusClasses()" [class.ant-input-lg]="nzSize === 'large'" [class.ant-input-sm]="nzSize === 'small'" [class.ant-input-borderless]="nzBorderless" [class.ant-input-focused]="focus" [class.ant-input-disabled]="nzDisabled" > @for (label of labels; track label) { <nz-cron-expression-input [value]="form.controls[label].value" [label]="label" [disabled]="nzDisabled" (focusEffect)="focusEffect($event)" (blurEffect)="blurEffect()" (getValue)="getValue($event)" /> } </div> <div class="ant-cron-expression-label-group" [class.ant-input-lg]="nzSize === 'large'" [class.ant-cron-expression-label-group-default]="nzSize === 'default'" [class.ant-input-sm]="nzSize === 'small'" > @for (label of labels; track label) { <nz-cron-expression-label [type]="label" [labelFocus]="labelFocus" [locale]="locale" /> } </div> @if (!nzCollapseDisable) { <nz-cron-expression-preview [TimeList]="nextTimeList" [visible]="form.valid" [locale]="locale" [nzSemantic]="nzSemantic" (loadMorePreview)="loadMorePreview()" /> } </div> @if (nzExtra) { <div class="ant-cron-expression-map"> <ng-template [ngTemplateOutlet]="nzExtra" /> </div> } </div> `, providers: [{ provide: NG_VALIDATORS, useExisting: forwardRef(() => NzCronExpressionComponent), multi: true }, { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => NzCronExpressionComponent), multi: true }], imports: [ NzCronExpressionInputComponent, NzCronExpressionLabelComponent, NzCronExpressionPreviewComponent, NgTemplateOutlet ] }] }], propDecorators: { nzStatus: [{ type: i0.Input, args: [{ isSignal: true, alias: "nzStatus", required: false }] }], nzSize: [{ type: Input }], nzType: [{ type: Input }], nzCollapseDisable: [{ type: Input, args: [{ transform: booleanAttribute }] }], nzExtra: [{ type: Input }], nzSemantic: [{ type: Input }], nzBorderless: [{ type: Input, args: [{ transform: booleanAttribute }] }], nzDisabled: [{ type: Input, args: [{ transform: booleanAttribute }] }] } }); var NzCronExpressionModule = class NzCronExpressionModule { static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCronExpressionModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "22.2.2", ngImport: i0, type: NzCronExpressionModule, imports: [ NzCronExpressionComponent, NzCronExpressionLabelComponent, NzCronExpressionInputComponent, NzCronExpressionPreviewComponent ], exports: [NzCronExpressionComponent] }); static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCronExpressionModule, imports: [ NzCronExpressionComponent, NzCronExpressionInputComponent, NzCronExpressionPreviewComponent ] }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCronExpressionModule, decorators: [{ type: NgModule, args: [{ imports: [ NzCronExpressionComponent, NzCronExpressionLabelComponent, NzCronExpressionInputComponent, NzCronExpressionPreviewComponent ], exports: [NzCronExpressionComponent] }] }] }); export { NzCronExpressionComponent, NzCronExpressionModule }; //# sourceMappingURL=ng-zorro-antd-cron-expression.mjs.map