UNPKG

@nestjs-mod/afat

Version:

Common utilites for AFAT (Angular, Formly, Antd, Transloco)

274 lines (263 loc) 14.4 kB
import * as i0 from '@angular/core'; import { ChangeDetectionStrategy, Component, Inject, Injectable, Pipe } from '@angular/core'; import * as i3$1 from '@angular/forms'; import { ReactiveFormsModule } from '@angular/forms'; import * as i4 from '@ngx-formly/core'; import { FieldType, FormlyModule } from '@ngx-formly/core'; import * as i5 from 'ng-zorro-antd/date-picker'; import { NzDatePickerModule } from 'ng-zorro-antd/date-picker'; import * as i1 from 'ng-zorro-antd/button'; import { NzButtonModule } from 'ng-zorro-antd/button'; import * as i2 from 'ng-zorro-antd/core/transition-patch'; import * as i3 from 'ng-zorro-antd/core/wave'; import { AsyncPipe } from '@angular/common'; import * as i1$1 from '@jsverse/transloco'; import { toCamelCase, TranslocoService } from '@jsverse/transloco'; import { map, of, throwError, Observable, finalize } from 'rxjs'; import { DATE_INPUT_FORMATS } from '@nestjs-mod/misc'; import * as i2$1 from '@jsverse/transloco-locale'; import { TRANSLOCO_LOCALE_LANG_MAPPING } from '@jsverse/transloco-locale'; import * as dateFnsLocales from 'date-fns/locale'; import * as ngZorroLocales from 'ng-zorro-antd/i18n'; import { __decorate, __metadata } from 'tslib'; import { UntilDestroy } from '@ngneat/until-destroy'; class ButtonInputComponent extends FieldType { onClick($event) { if (this.props['onClick']) { this.props['onClick']($event); } } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: ButtonInputComponent, deps: null, target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "19.0.5", type: ButtonInputComponent, isStandalone: true, selector: "button-input", usesInheritance: true, ngImport: i0, template: ` <button nz-button [type]="props['type']" [nzType]="props['btnType']" (click)="onClick($event)"> {{ props['text'] }} </button> `, isInline: true, dependencies: [{ kind: "ngmodule", type: ReactiveFormsModule }, { kind: "ngmodule", type: FormlyModule }, { kind: "ngmodule", type: NzDatePickerModule }, { kind: "ngmodule", type: NzButtonModule }, { kind: "component", type: i1.NzButtonComponent, selector: "button[nz-button], a[nz-button]", inputs: ["nzBlock", "nzGhost", "nzSearch", "nzLoading", "nzDanger", "disabled", "tabIndex", "nzType", "nzShape", "nzSize"], exportAs: ["nzButton"] }, { kind: "directive", type: i2.ɵNzTransitionPatchDirective, selector: "[nz-button], nz-button-group, [nz-icon], nz-icon, [nz-menu-item], [nz-submenu], nz-select-top-control, nz-select-placeholder, nz-input-group", inputs: ["hidden"] }, { kind: "directive", type: i3.NzWaveDirective, selector: "[nz-wave],button[nz-button]:not([nzType=\"link\"]):not([nzType=\"text\"])", inputs: ["nzWaveExtraNode"], exportAs: ["nzWave"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: ButtonInputComponent, decorators: [{ type: Component, args: [{ selector: 'button-input', changeDetection: ChangeDetectionStrategy.OnPush, imports: [ReactiveFormsModule, FormlyModule, NzDatePickerModule, NzButtonModule], template: ` <button nz-button [type]="props['type']" [nzType]="props['btnType']" (click)="onClick($event)"> {{ props['text'] }} </button> `, standalone: true, }] }] }); class ActiveLangService { translocoService; translocoLocaleService; nzI18nService; langToLocaleMapping; constructor(translocoService, translocoLocaleService, nzI18nService, langToLocaleMapping) { this.translocoService = translocoService; this.translocoLocaleService = translocoLocaleService; this.nzI18nService = nzI18nService; this.langToLocaleMapping = langToLocaleMapping; } applyActiveLang(lang) { const { locale, localeInSnakeCase, localeInCamelCase } = this.normalizeLangKey(lang); this.translocoService.setActiveLang(lang); this.translocoLocaleService.setLocale(locale); if (ngZorroLocales[localeInSnakeCase]) { this.nzI18nService.setLocale(ngZorroLocales[localeInSnakeCase]); } if (dateFnsLocales[lang]) { this.nzI18nService.setDateLocale(dateFnsLocales[lang]); } if (dateFnsLocales[localeInCamelCase]) { this.nzI18nService.setDateLocale(dateFnsLocales[localeInCamelCase]); } } normalizeLangKey(lang) { const locale = this.langToLocaleMapping[lang]; const localeInCamelCase = toCamelCase(locale); const localeInSnakeCase = locale.split('-').join('_'); return { locale, localeInSnakeCase, localeInCamelCase }; } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: ActiveLangService, deps: [{ token: i1$1.TranslocoService }, { token: i2$1.TranslocoLocaleService }, { token: ngZorroLocales.NzI18nService }, { token: TRANSLOCO_LOCALE_LANG_MAPPING }], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: ActiveLangService, providedIn: 'root' }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: ActiveLangService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: () => [{ type: i1$1.TranslocoService }, { type: i2$1.TranslocoLocaleService }, { type: ngZorroLocales.NzI18nService }, { type: undefined, decorators: [{ type: Inject, args: [TRANSLOCO_LOCALE_LANG_MAPPING] }] }] }); class DateInputComponent extends FieldType { translocoService; activeLangService; format$; constructor(translocoService, activeLangService) { super(); this.translocoService = translocoService; this.activeLangService = activeLangService; this.format$ = translocoService.langChanges$.pipe(map((lang) => { const { locale } = this.activeLangService.normalizeLangKey(lang); return DATE_INPUT_FORMATS[locale] ? DATE_INPUT_FORMATS[locale] : DATE_INPUT_FORMATS['en-US']; })); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: DateInputComponent, deps: [{ token: i1$1.TranslocoService }, { token: ActiveLangService }], target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "19.0.5", type: DateInputComponent, isStandalone: true, selector: "date-input", usesInheritance: true, ngImport: i0, template: ` <nz-date-picker [formControl]="formControl" [formlyAttributes]="field" [nzShowTime]="true" [nzFormat]="(format$ | async)!" ></nz-date-picker> `, isInline: true, dependencies: [{ kind: "ngmodule", type: ReactiveFormsModule }, { kind: "directive", type: i3$1.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i3$1.FormControlDirective, selector: "[formControl]", inputs: ["formControl", "disabled", "ngModel"], outputs: ["ngModelChange"], exportAs: ["ngForm"] }, { kind: "ngmodule", type: FormlyModule }, { kind: "directive", type: i4.ɵFormlyAttributes, selector: "[formlyAttributes]", inputs: ["formlyAttributes", "id"] }, { kind: "ngmodule", type: NzDatePickerModule }, { kind: "component", type: i5.NzDatePickerComponent, selector: "nz-date-picker,nz-week-picker,nz-month-picker,nz-quarter-picker,nz-year-picker,nz-range-picker", inputs: ["nzAllowClear", "nzAutoFocus", "nzDisabled", "nzBorderless", "nzInputReadOnly", "nzInline", "nzOpen", "nzDisabledDate", "nzLocale", "nzPlaceHolder", "nzPopupStyle", "nzDropdownClassName", "nzSize", "nzStatus", "nzFormat", "nzDateRender", "nzDisabledTime", "nzRenderExtraFooter", "nzShowToday", "nzMode", "nzShowNow", "nzRanges", "nzDefaultPickerValue", "nzSeparator", "nzSuffixIcon", "nzBackdrop", "nzId", "nzPlacement", "nzShowWeekNumber", "nzShowTime"], outputs: ["nzOnPanelChange", "nzOnCalendarChange", "nzOnOk", "nzOnOpenChange"], exportAs: ["nzDatePicker"] }, { kind: "pipe", type: AsyncPipe, name: "async" }], changeDetection: i0.ChangeDetectionStrategy.OnPush }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: DateInputComponent, decorators: [{ type: Component, args: [{ selector: 'date-input', changeDetection: ChangeDetectionStrategy.OnPush, imports: [ReactiveFormsModule, FormlyModule, NzDatePickerModule, AsyncPipe], template: ` <nz-date-picker [formControl]="formControl" [formlyAttributes]="field" [nzShowTime]="true" [nzFormat]="(format$ | async)!" ></nz-date-picker> `, standalone: true, }] }], ctorParameters: () => [{ type: i1$1.TranslocoService }, { type: ActiveLangService }] }); const COMMON_FORMLY_FIELDS = [ { name: 'date-input', component: DateInputComponent, extends: 'input', }, { name: 'button-input', component: ButtonInputComponent, wrappers: ['form-field'], defaultOptions: { props: { btnType: 'dashed', type: 'button', }, }, }, ]; function getQueryMetaByParams(params) { return { curPage: params.pageIndex, perPage: params.pageSize, sort: params.sort .filter((s) => s.value) .reduce((all, cur) => { if (cur.value === 'ascend') { return { ...all, [cur.key]: 'asc' }; } if (cur.value === 'descend') { return { ...all, [cur.key]: 'desc' }; } return all; }, {}), }; } class NzTableSortOrderDetectorPipe { transform(value) { if (value === 'asc') { return 'ascend'; } if (value === 'desc') { return 'descend'; } return null; } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: NzTableSortOrderDetectorPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); static ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "19.0.5", ngImport: i0, type: NzTableSortOrderDetectorPipe, isStandalone: true, name: "nzTableSortOrderDetector" }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: NzTableSortOrderDetectorPipe, decorators: [{ type: Pipe, args: [{ standalone: true, name: 'nzTableSortOrderDetector', }] }] }); let ValidationService = class ValidationService { translocoService; constructor(translocoService) { this.translocoService = translocoService; } catchAndProcessServerError( // eslint-disable-next-line @typescript-eslint/no-explicit-any err, setFormlyFields) { const error = err.error; if (error?.code?.toLowerCase()?.includes('validation')) { setFormlyFields({ errors: error.metadata }); return of(null); } return throwError(() => err); } appendServerErrorsAsValidatorsToFields(fields, errors) { return (fields || []).map((f) => { const error = errors?.find((e) => e.property === f.key); if (error) { f.validators = Object.fromEntries(error.constraints.map((c) => { if (typeof f.key === 'string') { c.description = c.description.replace(f.key, this.translocoService.translate('field "{{label}}"', { label: f.props?.label?.toLowerCase(), })); } return [ c.name === 'isNotEmpty' ? 'required' : c.name, { expression: () => false, message: () => c.description, }, ]; })); } return f; }); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: ValidationService, deps: [{ token: i1$1.TranslocoService }], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: ValidationService, providedIn: 'root' }); }; ValidationService = __decorate([ UntilDestroy(), __metadata("design:paramtypes", [TranslocoService]) ], ValidationService); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: ValidationService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: () => [{ type: i1$1.TranslocoService }] }); function webSocket({ address, eventName, options, }) { const wss = new WebSocket(address.replace('/api', '').replace('http', 'ws'), options); return new Observable((observer) => { wss.addEventListener('open', () => { wss.addEventListener('message', ({ data }) => { observer.next(JSON.parse(data.toString())); }); wss.addEventListener('error', (err) => { observer.error(err); if (wss?.readyState == WebSocket.OPEN) { wss.close(); } }); wss.send(JSON.stringify({ event: eventName, data: true, })); }); }).pipe(finalize(() => { if (wss?.readyState == WebSocket.OPEN) { wss.close(); } })); } /** * Generated bundle index. Do not edit. */ export { ActiveLangService, ButtonInputComponent, COMMON_FORMLY_FIELDS, DateInputComponent, NzTableSortOrderDetectorPipe, ValidationService, getQueryMetaByParams, webSocket }; //# sourceMappingURL=nestjs-mod-afat.mjs.map