@chatterton/angular2-schema-form
Version:
Angular2 Schema Form (DISCLAIMER: it is not related to angular-schema-form)
116 lines (96 loc) • 2.79 kB
text/typescript
import {
ChangeDetectorRef,
Component,
OnChanges,
EventEmitter,
Input,
Output
} from '@angular/core';
import {
Action,
ActionRegistry,
FormPropertyFactory,
FormProperty,
SchemaPreprocessor,
ValidatorRegistry,
Validator
} from './model';
import { SchemaValidatorFactory, ZSchemaValidatorFactory } from './schemavalidatorfactory';
import { WidgetFactory } from './widgetfactory';
export class FormComponent implements OnChanges {
schema: any = null;
model: any;
actions: {[actionId: string]: Action} = {};
validators: {[path: string]: Validator} = {};
onChange = new EventEmitter<{value: any}>();
rootProperty: FormProperty = null;
constructor(
private formPropertyFactory: FormPropertyFactory,
private actionRegistry: ActionRegistry,
private validatorRegistry: ValidatorRegistry,
private cdr: ChangeDetectorRef
) { }
ngOnChanges(changes: any) {
if (changes.validators) {
this.setValidators();
}
if (changes.actions) {
this.setActions();
}
if (!this.schema.type) {
this.schema.type = 'object';
}
if (this.schema && changes.schema) {
SchemaPreprocessor.preprocess(this.schema);
this.rootProperty = this.formPropertyFactory.createProperty(this.schema);
this.rootProperty.valueChanges.subscribe(value => { this.onChange.emit({value: value}); });
}
if (this.schema && (changes.model || changes.schema )) {
this.rootProperty.reset(this.model, false);
this.cdr.detectChanges();
}
}
private setValidators() {
this.validatorRegistry.clear();
if (this.validators) {
for (let validatorId in this.validators) {
if (this.validators.hasOwnProperty(validatorId)) {
this.validatorRegistry.register(validatorId, this.validators[validatorId]);
}
}
}
}
private setActions() {
this.actionRegistry.clear();
if (this.actions) {
for (let actionId in this.actions) {
if (this.actions.hasOwnProperty(actionId)) {
this.actionRegistry.register(actionId, this.actions[actionId]);
}
}
}
}
public reset() {
this.rootProperty.reset(null, true);
}
}