ohayolibs
Version:
Ohayo is a set of essential modules for ohayojp.
133 lines (109 loc) • 3.95 kB
text/typescript
import { AfterViewInit, ChangeDetectorRef, Directive, HostBinding, Inject, Injector } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { LocaleData } from '@ohayo/theme';
import { takeUntil } from 'rxjs/operators';
import { ErrorData } from './errors';
import { SFValue } from './interface';
import { ArrayProperty } from './model/array.property';
import { FormProperty } from './model/form.property';
import { ObjectProperty } from './model/object.property';
import { SFSchema } from './schema';
import { SFOptionalHelp, SFUISchemaItem } from './schema/ui';
import { SFItemComponent } from './sf-item.component';
import { SFComponent } from './sf.component';
import { di } from './utils';
import { SFArrayWidgetSchema, SFObjectWidgetSchema } from './widgets';
export abstract class Widget<T extends FormProperty, UIT extends SFUISchemaItem> implements AfterViewInit {
formProperty: T;
error: string;
showError = false;
id = '';
schema: SFSchema;
ui: UIT;
firstVisual = false;
get cls(): string | string[] {
return this.ui.class || '';
}
get disabled(): boolean | null {
if (this.schema.readOnly === true || this.sfComp!.disabled) {
return true;
}
return null;
}
get l(): LocaleData {
return this.formProperty.root.widget.sfComp!.locale;
}
get oh(): SFOptionalHelp {
return this.ui.optionalHelp as SFOptionalHelp;
}
get dom(): DomSanitizer {
return this.injector.get(DomSanitizer);
}
get cleanValue(): boolean {
return this.sfComp?.cleanValue!;
}
constructor(
public readonly cd: ChangeDetectorRef,
public readonly injector: Injector,
public readonly sfItemComp?: SFItemComponent,
public readonly sfComp?: SFComponent,
) { }
ngAfterViewInit(): void {
this.formProperty.errorsChanges.pipe(takeUntil(this.sfItemComp!.unsubscribe$)).subscribe((errors: ErrorData[] | null) => {
if (errors == null) return;
di(this.ui, 'errorsChanges', this.formProperty.path, errors);
// 不显示首次校验视觉
if (this.firstVisual) {
this.showError = errors.length > 0;
this.error = this.showError ? (errors[0].message as string) : '';
this.cd.detectChanges();
}
this.firstVisual = true;
});
this.afterViewInit();
}
setValue(value: SFValue): void {
this.formProperty.setValue(value, false);
di(this.ui, 'valueChanges', this.formProperty.path, this.formProperty);
}
get value(): any {
return this.formProperty.value;
}
detectChanges(onlySelf: boolean = false): void {
if (onlySelf) {
this.cd.markForCheck();
} else {
this.formProperty.root.widget.cd.markForCheck();
}
}
abstract reset(value: SFValue): void;
abstract afterViewInit(): void;
}
export class ControlWidget extends Widget<FormProperty, SFUISchemaItem> {
reset(_value: SFValue): void { }
afterViewInit(): void { }
}
export class ControlUIWidget<UIT extends SFUISchemaItem> extends Widget<FormProperty, UIT> {
reset(_value: SFValue): void { }
afterViewInit(): void { }
}
export class ArrayLayoutWidget extends Widget<ArrayProperty, SFArrayWidgetSchema> implements AfterViewInit {
reset(_value: SFValue): void { }
afterViewInit(): void { }
ngAfterViewInit(): void {
this.formProperty.errorsChanges.pipe(takeUntil(this.sfItemComp!.unsubscribe$)).subscribe(() => this.cd.detectChanges());
}
}
export class ObjectLayoutWidget extends Widget<ObjectProperty, SFObjectWidgetSchema> implements AfterViewInit {
reset(_value: SFValue): void { }
afterViewInit(): void { }
ngAfterViewInit(): void {
this.formProperty.errorsChanges.pipe(takeUntil(this.sfItemComp!.unsubscribe$)).subscribe(() => this.cd.detectChanges());
}
}