angular2-schema-form
Version:
Angular2 Schema Form (DISCLAIMER: it is not related to angular-schema-form)
129 lines (128 loc) • 5.03 kB
JavaScript
import { ChangeDetectorRef, Component, EventEmitter, Input, Output } from '@angular/core';
import { ActionRegistry, FormPropertyFactory, SchemaPreprocessor, ValidatorRegistry } from './model';
import { SchemaValidatorFactory } from './schemavalidatorfactory';
import { WidgetFactory } from './widgetfactory';
import { TerminatorService } from './terminator.service';
export function useFactory(schemaValidatorFactory, validatorRegistry) {
return new FormPropertyFactory(schemaValidatorFactory, validatorRegistry);
}
;
var FormComponent = (function () {
function FormComponent(formPropertyFactory, actionRegistry, validatorRegistry, cdr, terminator) {
this.formPropertyFactory = formPropertyFactory;
this.actionRegistry = actionRegistry;
this.validatorRegistry = validatorRegistry;
this.cdr = cdr;
this.terminator = terminator;
this.schema = null;
this.actions = {};
this.validators = {};
this.onChange = new EventEmitter();
this.modelChange = new EventEmitter();
this.isValid = new EventEmitter();
this.onErrorChange = new EventEmitter();
this.onErrorsChange = new EventEmitter();
this.rootProperty = null;
}
FormComponent.prototype.ngOnChanges = function (changes) {
var _this = this;
if (changes.validators) {
this.setValidators();
}
if (changes.actions) {
this.setActions();
}
if (this.schema && !this.schema.type) {
this.schema.type = 'object';
}
if (this.schema && changes.schema) {
if (!changes.schema.firstChange) {
this.terminator.destroy();
}
SchemaPreprocessor.preprocess(this.schema);
this.rootProperty = this.formPropertyFactory.createProperty(this.schema);
this.rootProperty.valueChanges.subscribe(function (value) {
if (_this.modelChange.observers.length > 0) {
if (_this.model) {
Object.assign(_this.model, value);
}
else {
_this.model = value;
}
_this.modelChange.emit(value);
}
_this.onChange.emit({ value: value });
});
this.rootProperty.errorsChanges.subscribe(function (value) {
_this.onErrorChange.emit({ value: value });
_this.isValid.emit(!(value && value.length));
});
}
if (this.schema && (changes.model || changes.schema)) {
this.rootProperty.reset(this.model, false);
this.cdr.detectChanges();
}
};
FormComponent.prototype.setValidators = function () {
this.validatorRegistry.clear();
if (this.validators) {
for (var validatorId in this.validators) {
if (this.validators.hasOwnProperty(validatorId)) {
this.validatorRegistry.register(validatorId, this.validators[validatorId]);
}
}
}
};
FormComponent.prototype.setActions = function () {
this.actionRegistry.clear();
if (this.actions) {
for (var actionId in this.actions) {
if (this.actions.hasOwnProperty(actionId)) {
this.actionRegistry.register(actionId, this.actions[actionId]);
}
}
}
};
FormComponent.prototype.reset = function () {
this.rootProperty.reset(null, true);
};
return FormComponent;
}());
export { FormComponent };
FormComponent.decorators = [
{ type: Component, args: [{
selector: 'sf-form',
template: "\n <form>\n <sf-form-element\n *ngIf=\"rootProperty\" [formProperty]=\"rootProperty\"></sf-form-element>\n </form>",
providers: [
ActionRegistry,
ValidatorRegistry,
SchemaPreprocessor,
WidgetFactory,
{
provide: FormPropertyFactory,
useFactory: useFactory,
deps: [SchemaValidatorFactory, ValidatorRegistry]
},
TerminatorService,
]
},] },
];
/** @nocollapse */
FormComponent.ctorParameters = function () { return [
{ type: FormPropertyFactory, },
{ type: ActionRegistry, },
{ type: ValidatorRegistry, },
{ type: ChangeDetectorRef, },
{ type: TerminatorService, },
]; };
FormComponent.propDecorators = {
'schema': [{ type: Input },],
'model': [{ type: Input },],
'actions': [{ type: Input },],
'validators': [{ type: Input },],
'onChange': [{ type: Output },],
'modelChange': [{ type: Output },],
'isValid': [{ type: Output },],
'onErrorChange': [{ type: Output },],
'onErrorsChange': [{ type: Output },],
};