ngx-schema-forms
Version:
New features: - Ajv schema validator. - Angular forms compatible: Property tree is created using FormGroup, FormArray and FormControl classes. - Array now properly loads initial data from model. - WidgetTyep: WidgetRegistry now supports WidgetType, now wo
1,419 lines (1,398 loc) • 365 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('rxjs'), require('rxjs/operators'), require('@angular/forms'), require('z-schema'), require('ajv'), require('@angular/common')) :
typeof define === 'function' && define.amd ? define('ngx-schema-forms', ['exports', '@angular/core', 'rxjs', 'rxjs/operators', '@angular/forms', 'z-schema', 'ajv', '@angular/common'], factory) :
(factory((global['ngx-schema-forms'] = {}),global.ng.core,global.rxjs,global.rxjs.operators,global.ng.forms,null,null,global.ng.common));
}(this, (function (exports,core,rxjs,operators,forms,ZSchema,Ajv,common) { 'use strict';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
var ActionRegistry = (function () {
function ActionRegistry() {
this.actions = {};
}
/**
* @return {?}
*/
ActionRegistry.prototype.clear = /**
* @return {?}
*/
function () {
this.actions = {};
};
/**
* @param {?} actionId
* @param {?} action
* @return {?}
*/
ActionRegistry.prototype.register = /**
* @param {?} actionId
* @param {?} action
* @return {?}
*/
function (actionId, action) {
this.actions[actionId] = action;
};
/**
* @param {?} actionId
* @return {?}
*/
ActionRegistry.prototype.get = /**
* @param {?} actionId
* @return {?}
*/
function (actionId) {
return this.actions[actionId];
};
return ActionRegistry;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
var ValidatorRegistry = (function () {
function ValidatorRegistry() {
this.validators = {};
}
/**
* @param {?} path
* @param {?} validator
* @return {?}
*/
ValidatorRegistry.prototype.register = /**
* @param {?} path
* @param {?} validator
* @return {?}
*/
function (path, validator) {
this.validators[path] = validator;
};
/**
* @param {?} path
* @return {?}
*/
ValidatorRegistry.prototype.get = /**
* @param {?} path
* @return {?}
*/
function (path) {
return this.validators[path];
};
/**
* @return {?}
*/
ValidatorRegistry.prototype.clear = /**
* @return {?}
*/
function () {
this.validators = {};
};
return ValidatorRegistry;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/** @enum {string} */
var SchemaPropertyType = {
String: 'string',
Object: 'object',
Array: 'array',
Boolean: 'boolean',
Integer: 'integer',
Number: 'number',
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/**
* @abstract
*/
var /**
* @abstract
*/ SchemaValidatorFactory = (function () {
function SchemaValidatorFactory() {
}
return SchemaValidatorFactory;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/** @enum {string} */
var WidgetType = {
Field: 'field',
Fieldset: 'fieldset',
Button: 'button',
};
var WidgetRegistry = (function () {
function WidgetRegistry() {
this.widgets = {};
this.defaultWidget = {};
}
/**
* @param {?} widget
* @param {?=} type
* @return {?}
*/
WidgetRegistry.prototype.setDefaultWidget = /**
* @param {?} widget
* @param {?=} type
* @return {?}
*/
function (widget, type) {
if (type === void 0) {
type = WidgetType.Field;
}
this.defaultWidget[type] = widget;
};
/**
* @param {?=} type
* @return {?}
*/
WidgetRegistry.prototype.getDefaultWidget = /**
* @param {?=} type
* @return {?}
*/
function (type) {
if (type === void 0) {
type = WidgetType.Field;
}
return this.defaultWidget[type];
};
/**
* @param {?} id
* @param {?=} type
* @return {?}
*/
WidgetRegistry.prototype.hasWidget = /**
* @param {?} id
* @param {?=} type
* @return {?}
*/
function (id, type) {
if (type === void 0) {
type = WidgetType.Field;
}
if (!this.widgets.hasOwnProperty(type)) {
return false;
}
return this.widgets[type].hasOwnProperty(id);
};
/**
* @param {?} id
* @param {?} widget
* @param {?=} type
* @return {?}
*/
WidgetRegistry.prototype.register = /**
* @param {?} id
* @param {?} widget
* @param {?=} type
* @return {?}
*/
function (id, widget, type) {
if (type === void 0) {
type = WidgetType.Field;
}
if (!this.widgets.hasOwnProperty(type)) {
this.widgets[type] = {};
}
this.widgets[type][id] = widget;
};
/**
* @template T
* @param {?} id
* @param {?=} type
* @return {?}
*/
WidgetRegistry.prototype.getWidgetType = /**
* @template T
* @param {?} id
* @param {?=} type
* @return {?}
*/
function (id, type) {
if (type === void 0) {
type = WidgetType.Field;
}
if (this.hasWidget(id, type)) {
return this.widgets[type][id];
}
return this.getDefaultWidget(type);
};
return WidgetRegistry;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
var WidgetFactory = (function () {
function WidgetFactory(widgetRegistry, factoryResolver) {
this.widgetRegistry = widgetRegistry;
this.factoryResolver = factoryResolver;
}
/**
* @template T
* @param {?} container
* @param {?} id
* @param {?=} opts
* @return {?}
*/
WidgetFactory.prototype.createWidget = /**
* @template T
* @param {?} container
* @param {?} id
* @param {?=} opts
* @return {?}
*/
function (container, id, opts) {
if (opts === void 0) {
opts = {
type: WidgetType.Field
};
}
/** @type {?} */
var componentClass = this.widgetRegistry.getWidgetType(id, opts.type);
/** @type {?} */
var componentFactory = this.factoryResolver
.resolveComponentFactory(componentClass);
return container.createComponent(componentFactory, undefined, // index
// index
opts.injector);
};
WidgetFactory.decorators = [
{ type: core.Injectable }
];
/** @nocollapse */
WidgetFactory.ctorParameters = function () {
return [
{ type: WidgetRegistry },
{ type: core.ComponentFactoryResolver }
];
};
return WidgetFactory;
}());
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
/* global Reflect, Promise */
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b)
if (b.hasOwnProperty(p))
d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
function __decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
r = Reflect.decorate(decorators, target, key, desc);
else
for (var i = decorators.length - 1; i >= 0; i--)
if (d = decorators[i])
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function __metadata(metadataKey, metadataValue) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
return Reflect.metadata(metadataKey, metadataValue);
}
function __values(o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
if (m)
return m.call(o);
return {
next: function () {
if (o && i >= o.length)
o = void 0;
return { value: o && o[i++], done: !o };
}
};
}
function __read(o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m)
return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
ar.push(r.value);
}
catch (error) {
e = { error: error };
}
finally {
try {
if (r && !r.done && (m = i["return"]))
m.call(i);
}
finally {
if (e)
throw e.error;
}
}
return ar;
}
function __spread() {
for (var ar = [], i = 0; i < arguments.length; i++)
ar = ar.concat(__read(arguments[i]));
return ar;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
var FormPropertyErrors = (function () {
function FormPropertyErrors(errors) {
this.errors = errors;
}
/**
* @return {?}
*/
FormPropertyErrors.prototype.getMessages = /**
* @return {?}
*/
function () {
var _this = this;
/** @type {?} */
var errorsPaths = Object.keys(this.errors);
if (!errorsPaths.length) {
return [];
}
return errorsPaths
.reduce(function (messages, path) {
/** @type {?} */
var message = _this.errors[path]["message"];
if (!message) {
messages.push('Missing validation error "message" for property ' + path);
return messages;
}
messages.push(message);
return messages;
}, []);
};
return FormPropertyErrors;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/**
* @template T
* @param {?} Base
* @return {?}
*/
function ControlProperty(Base) {
/**
* @abstract
*/
var /**
* @abstract
*/ Property = (function (_super) {
__extends(Property, _super);
function Property() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var _this = _super.apply(this, __spread(args)) || this;
_this.nonEmptyValueChanges = new core.EventEmitter();
_this.visibilityChanges = new rxjs.BehaviorSubject(true);
_this._visible = true;
return _this;
}
Object.defineProperty(Property.prototype, "id", {
get: /**
* @return {?}
*/ function () {
return this.path.toLowerCase().slice(1).replace(/\//g, '-');
},
enumerable: true,
configurable: true
});
Object.defineProperty(Property.prototype, "isRoot", {
get: /**
* @return {?}
*/ function () {
return this === this.root;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Property.prototype, "name", {
get: /**
* @return {?}
*/ function () {
return this.path.split('/').pop();
},
enumerable: true,
configurable: true
});
Object.defineProperty(Property.prototype, "visible", {
get: /**
* @return {?}
*/ function () {
return this._visible;
},
enumerable: true,
configurable: true
});
/**
* @return {?}
*/
Property.prototype.getErrors = /**
* @return {?}
*/
function () {
/** @type {?} */
var errors = this.errors;
if (!errors) {
return null;
}
return new FormPropertyErrors((_a = {}, _a[this.path] = errors, _a));
var _a;
};
/**
* @param {?} visible
* @param {?=} opts
* @return {?}
*/
Property.prototype.setVisible = /**
* @param {?} visible
* @param {?=} opts
* @return {?}
*/
function (visible, opts) {
if (opts === void 0) {
opts = { disable: false };
}
this._visible = visible;
if (opts.disable) {
if (this.visible) {
this.enable();
}
else {
this.disable();
}
}
this.visibilityChanges.next(this.visible);
};
// visible if AT LEAST ONE of the properties it depends on is visible
// AND has a value in the list
/**
* @return {?}
*/
Property.prototype.bindVisibility = /**
* @return {?}
*/
function () {
var _this = this;
/** @type {?} */
var visibleIf = this.schema["visibleIf"];
if (visibleIf === undefined) {
return;
}
/** @type {?} */
var paths = Object.keys(visibleIf);
if (typeof visibleIf === 'object' && paths.length === 0) {
this.setVisible(false);
return;
}
/** @type {?} */
var observables = [];
var _loop_1 = function (path) {
if (!visibleIf.hasOwnProperty(path)) {
return "continue";
}
/** @type {?} */
var property = this_1.root.get(path);
if (!property) {
console.warn("Couldn't find property " + path + " for visibility check of " + this_1.path);
return "continue";
}
/** @type {?} */
var values = visibleIf[path];
/** @type {?} */
var observable = property.valueChanges.pipe(operators.startWith(values.includes(property.value)), operators.map(function (value) {
return values.includes('$ANY$') || values.includes(value);
}));
observables.push(observable);
};
var this_1 = this;
try {
for (var paths_1 = __values(paths), paths_1_1 = paths_1.next(); !paths_1_1.done; paths_1_1 = paths_1.next()) {
var path = paths_1_1.value;
_loop_1(path);
}
}
catch (e_1_1) {
e_1 = { error: e_1_1 };
}
finally {
try {
if (paths_1_1 && !paths_1_1.done && (_a = paths_1.return))
_a.call(paths_1);
}
finally {
if (e_1)
throw e_1.error;
}
}
// TODO unsubscribe
rxjs.combineLatest(observables)
.subscribe(function (values) {
_this.setVisible(values.includes(true));
});
var e_1, _a;
};
/**
* @param {?} path
* @return {?}
*/
Property.prototype.get = /**
* @param {?} path
* @return {?}
*/
function (path) {
if (typeof path === 'string' && path.includes('/')) {
path = this.normalizePath(path);
}
return _super.prototype.get.call(this, path);
};
/**
* @param {?} path
* @return {?}
*/
Property.prototype.normalizePath = /**
* @param {?} path
* @return {?}
*/
function (path) {
if (path[0] === '/') {
path = path.slice(1);
}
return path.split('/');
};
return Property;
}(Base));
return Property;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
var GenericProperty = (function (_super) {
__extends(GenericProperty, _super);
function GenericProperty(path, schema) {
var _this = _super.call(this, schema["default"]) || this;
_this.path = path;
_this.schema = schema;
return _this;
}
/**
* @return {?}
*/
GenericProperty.prototype._updateValue = /**
* @return {?}
*/
function () {
if (this.value === null || this.value === '') {
this.nonEmptyValue = undefined;
return;
}
this.nonEmptyValue = this.value;
};
return GenericProperty;
}(ControlProperty(forms.FormControl)));
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
var NumberProperty = (function (_super) {
__extends(NumberProperty, _super);
function NumberProperty() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* @param {?} value
* @param {?=} options
* @return {?}
*/
NumberProperty.prototype.setValue = /**
* @param {?} value
* @param {?=} options
* @return {?}
*/
function (value, options) {
if (options === void 0) {
options = {};
}
_super.prototype.setValue.call(this, +value, options);
};
return NumberProperty;
}(GenericProperty));
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
var BooleanProperty = (function (_super) {
__extends(BooleanProperty, _super);
function BooleanProperty() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* @param {?} value
* @param {?=} options
* @return {?}
*/
BooleanProperty.prototype.setValue = /**
* @param {?} value
* @param {?=} options
* @return {?}
*/
function (value, options) {
if (options === void 0) {
options = {};
}
if (typeof value !== 'boolean') {
value = Boolean(value);
}
_super.prototype.setValue.call(this, value, options);
};
return BooleanProperty;
}(GenericProperty));
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
var StringProperty = (function (_super) {
__extends(StringProperty, _super);
function StringProperty() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* @param {?} value
* @param {?=} options
* @return {?}
*/
StringProperty.prototype.setValue = /**
* @param {?} value
* @param {?=} options
* @return {?}
*/
function (value, options) {
if (options === void 0) {
options = {};
}
if (typeof value !== 'string') {
value = "" + value;
}
_super.prototype.setValue.call(this, value, options);
};
return StringProperty;
}(GenericProperty));
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
var ArrayProperty = (function (_super) {
__extends(ArrayProperty, _super);
function ArrayProperty(formPropertyFactory, path, schema) {
var _this = _super.call(this, []) || this;
_this.formPropertyFactory = formPropertyFactory;
_this.path = path;
_this.schema = schema;
return _this;
}
/**
* @return {?}
*/
ArrayProperty.prototype._updateValue = /**
* @return {?}
*/
function () {
var _this = this;
// to avoid ts complaints
_super.prototype['_updateValue'].call(this);
this.nonEmptyValue = this.controls
.filter(function (control) {
/** @type {?} */
var enabled = control.enabled || _this.disabled;
return control.nonEmptyValue !== undefined && enabled;
})
.map(function (control) { return control.value; });
};
/**
* @return {?}
*/
ArrayProperty.prototype.getErrors = /**
* @return {?}
*/
function () {
/** @type {?} */
var aggregatedErrors = this.controls
.reduce(function (errors, property) {
/** @type {?} */
var propertyErrors = property.getErrors();
if (!propertyErrors) {
return errors;
}
return Object.assign(errors, propertyErrors.errors);
}, {});
if (this.errors) {
aggregatedErrors[this.path] = this.errors;
}
if (!Object.keys(aggregatedErrors).length) {
return null;
}
return new FormPropertyErrors(aggregatedErrors);
};
/**
* @param {?} value
* @param {?=} options
* @return {?}
*/
ArrayProperty.prototype.patchValue = /**
* @param {?} value
* @param {?=} options
* @return {?}
*/
function (value, options) {
var _this = this;
if (options === void 0) {
options = {};
}
value.forEach(function (newValue, index) {
_this.addPropertyAt(index);
if (_this.at(index)) {
_this.at(index).patchValue(newValue, { onlySelf: true, emitEvent: options.emitEvent });
}
});
this.updateValueAndValidity(options);
};
/**
* @return {?}
*/
ArrayProperty.prototype.addProperty = /**
* @return {?}
*/
function () {
/** @type {?} */
var property = this.getPropertyFromSchemaItems();
_super.prototype.push.call(this, property);
property.bindVisibility();
};
/**
* @param {?} index
* @return {?}
*/
ArrayProperty.prototype.addPropertyAt = /**
* @param {?} index
* @return {?}
*/
function (index) {
/** @type {?} */
var property = this.getPropertyFromSchemaItems();
this.insert(index, property);
property.bindVisibility();
};
/**
* @return {?}
*/
ArrayProperty.prototype.bindVisibility = /**
* @return {?}
*/
function () {
_super.prototype.bindVisibility.call(this);
this.controls.forEach(function (control) {
control.bindVisibility();
});
};
/**
* @param {?} fn
* @param {?=} opts
* @return {?}
*/
ArrayProperty.prototype.forEach = /**
* @param {?} fn
* @param {?=} opts
* @return {?}
*/
function (fn, opts) {
if (opts === void 0) {
opts = { includeSelf: true };
}
if (opts.includeSelf) {
fn(this);
}
try {
for (var _a = __values(this.controls), _b = _a.next(); !_b.done; _b = _a.next()) {
var control = _b.value;
/** @type {?} */
var property = (control);
if (property.forEach instanceof Function) {
property.forEach(fn, { includeSelf: true });
continue;
}
fn(property);
}
}
catch (e_1_1) {
e_1 = { error: e_1_1 };
}
finally {
try {
if (_b && !_b.done && (_c = _a.return))
_c.call(_a);
}
finally {
if (e_1)
throw e_1.error;
}
}
var e_1, _c;
};
/**
* @return {?}
*/
ArrayProperty.prototype.getPropertyFromSchemaItems = /**
* @return {?}
*/
function () {
return this.formPropertyFactory.createProperty(this.schema["items"], this);
};
return ArrayProperty;
}(ControlProperty(forms.FormArray)));
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
var ObjectProperty = (function (_super) {
__extends(ObjectProperty, _super);
function ObjectProperty(path, schema) {
var _this = _super.call(this, {}) || this;
_this.path = path;
_this.schema = schema;
return _this;
}
/**
* @return {?}
*/
ObjectProperty.prototype._updateValue = /**
* @return {?}
*/
function () {
var _this = this;
// to avoid ts complaints
_super.prototype['_updateValue'].call(this);
this.nonEmptyValue = this['_reduceChildren']({}, function (result, control, name) {
if (control.nonEmptyValue === undefined) {
return result;
}
if (control.enabled || _this.disabled) {
result[name] = control.nonEmptyValue;
}
return result;
});
};
/**
* @return {?}
*/
ObjectProperty.prototype.getErrors = /**
* @return {?}
*/
function () {
var _this = this;
/** @type {?} */
var aggregatedErrors = Object.keys(this.controls)
.reduce(function (errors, key) {
/** @type {?} */
var property = (_this.controls[key]);
/** @type {?} */
var propertyErrors = property.getErrors();
if (!propertyErrors) {
return errors;
}
return Object.assign(errors, propertyErrors.errors);
}, {});
if (this.errors) {
aggregatedErrors[this.path] = this.errors;
}
if (!Object.keys(aggregatedErrors).length) {
return null;
}
return new FormPropertyErrors(aggregatedErrors);
};
/**
* @return {?}
*/
ObjectProperty.prototype.bindVisibility = /**
* @return {?}
*/
function () {
_super.prototype.bindVisibility.call(this);
for (var key in this.controls) {
if (this.controls.hasOwnProperty(key)) {
((this.controls[key])).bindVisibility();
}
}
};
/**
* @param {?} fn
* @param {?=} opts
* @return {?}
*/
ObjectProperty.prototype.forEach = /**
* @param {?} fn
* @param {?=} opts
* @return {?}
*/
function (fn, opts) {
if (opts === void 0) {
opts = { includeSelf: true };
}
if (opts.includeSelf) {
fn(this);
}
for (var key in this.controls) {
if (this.controls.hasOwnProperty(key)) {
/** @type {?} */
var property = ((this.controls[key]));
if (property.forEach instanceof Function) {
property.forEach(fn, { includeSelf: true });
continue;
}
fn(property);
}
}
};
return ObjectProperty;
}(ControlProperty(forms.FormGroup)));
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/**
* @param {?} o
* @return {?}
*/
function isBlank(o) {
return o === null || o === undefined;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/**
* @param {?} message
* @param {?} path
* @return {?}
*/
function formatMessage(message, path) {
return "Parsing error on " + path + ": " + message;
}
/**
* @param {?} message
* @param {?} path
* @return {?}
*/
function schemaError(message, path) {
/** @type {?} */
var mesg = formatMessage(message, path);
throw new Error(mesg);
}
/**
* @param {?} message
* @param {?} path
* @return {?}
*/
function schemaWarning(message, path) {
/** @type {?} */
var mesg = formatMessage(message, path);
throw new Error(mesg);
}
var SchemaPreprocessor = (function () {
function SchemaPreprocessor() {
}
/**
* @param {?} jsonSchema
* @param {?=} path
* @return {?}
*/
SchemaPreprocessor.preprocess = /**
* @param {?} jsonSchema
* @param {?=} path
* @return {?}
*/
function (jsonSchema, path) {
if (path === void 0) {
path = '/';
}
jsonSchema = jsonSchema || {};
if (jsonSchema.type === 'object') {
SchemaPreprocessor.checkProperties(jsonSchema, path);
SchemaPreprocessor.checkAndCreateFieldsets(jsonSchema, path);
}
else if (jsonSchema.type === 'array') {
SchemaPreprocessor.checkItems(jsonSchema, path);
}
SchemaPreprocessor.normalizeWidget(jsonSchema);
SchemaPreprocessor.recursiveCheck(jsonSchema, path);
};
/**
* @param {?} jsonSchema
* @param {?} path
* @return {?}
*/
SchemaPreprocessor.checkProperties = /**
* @param {?} jsonSchema
* @param {?} path
* @return {?}
*/
function (jsonSchema, path) {
if (isBlank(jsonSchema.properties)) {
jsonSchema.properties = {};
schemaWarning('Provided json schema does not contain a \'properties\' entry. Output schema will be empty', path);
}
};
/**
* @param {?} jsonSchema
* @param {?} path
* @return {?}
*/
SchemaPreprocessor.checkAndCreateFieldsets = /**
* @param {?} jsonSchema
* @param {?} path
* @return {?}
*/
function (jsonSchema, path) {
if (jsonSchema.fieldsets === undefined) {
if (jsonSchema.order !== undefined) {
SchemaPreprocessor.replaceOrderByFieldsets(jsonSchema);
}
else {
SchemaPreprocessor.createFieldsets(jsonSchema);
}
}
SchemaPreprocessor.checkFieldsUsage(jsonSchema, path);
};
/**
* @param {?} jsonSchema
* @param {?} path
* @return {?}
*/
SchemaPreprocessor.checkFieldsUsage = /**
* @param {?} jsonSchema
* @param {?} path
* @return {?}
*/
function (jsonSchema, path) {
/** @type {?} */
var fieldsId = Object.keys(jsonSchema.properties);
/** @type {?} */
var usedFields = {};
try {
for (var _a = __values(jsonSchema.fieldsets), _b = _a.next(); !_b.done; _b = _a.next()) {
var fieldset = _b.value;
try {
for (var _c = __values(fieldset.fields), _d = _c.next(); !_d.done; _d = _c.next()) {
var fieldId = _d.value;
if (usedFields[fieldId] === undefined) {
usedFields[fieldId] = [];
}
usedFields[fieldId].push(fieldset.id);
}
}
catch (e_1_1) {
e_1 = { error: e_1_1 };
}
finally {
try {
if (_d && !_d.done && (_e = _c.return))
_e.call(_c);
}
finally {
if (e_1)
throw e_1.error;
}
}
}
}
catch (e_2_1) {
e_2 = { error: e_2_1 };
}
finally {
try {
if (_b && !_b.done && (_f = _a.return))
_f.call(_a);
}
finally {
if (e_2)
throw e_2.error;
}
}
try {
for (var fieldsId_1 = __values(fieldsId), fieldsId_1_1 = fieldsId_1.next(); !fieldsId_1_1.done; fieldsId_1_1 = fieldsId_1.next()) {
var fieldId = fieldsId_1_1.value;
if (usedFields.hasOwnProperty(fieldId)) {
if (usedFields[fieldId].length > 1) {
schemaError(fieldId + " is referenced by more than one fieldset: " + usedFields[fieldId], path);
}
delete usedFields[fieldId];
}
else if (jsonSchema.required.indexOf(fieldId) > -1) {
schemaError(fieldId + " is a required field but it is not referenced as part of a 'order' or a 'fieldset' property", path);
}
else {
delete jsonSchema[fieldId];
schemaWarning("Removing unreferenced field " + fieldId, path);
}
}
}
catch (e_3_1) {
e_3 = { error: e_3_1 };
}
finally {
try {
if (fieldsId_1_1 && !fieldsId_1_1.done && (_g = fieldsId_1.return))
_g.call(fieldsId_1);
}
finally {
if (e_3)
throw e_3.error;
}
}
for (var remainingfieldsId in usedFields) {
if (usedFields.hasOwnProperty(remainingfieldsId)) {
schemaWarning("Referencing non-existent field " + remainingfieldsId + " in one or more fieldsets", path);
}
}
var e_2, _f, e_1, _e, e_3, _g;
};
/**
* @param {?} jsonSchema
* @return {?}
*/
SchemaPreprocessor.createFieldsets = /**
* @param {?} jsonSchema
* @return {?}
*/
function (jsonSchema) {
jsonSchema.order = Object.keys(jsonSchema.properties);
SchemaPreprocessor.replaceOrderByFieldsets(jsonSchema);
};
/**
* @param {?} jsonSchema
* @return {?}
*/
SchemaPreprocessor.replaceOrderByFieldsets = /**
* @param {?} jsonSchema
* @return {?}
*/
function (jsonSchema) {
jsonSchema.fieldsets = [{
id: 'fieldset-default',
title: jsonSchema.title || '',
description: jsonSchema.description || '',
name: jsonSchema.name || '',
fields: jsonSchema.order
}];
delete jsonSchema.order;
};
/**
* @param {?} fieldSchema
* @return {?}
*/
SchemaPreprocessor.normalizeWidget = /**
* @param {?} fieldSchema
* @return {?}
*/
function (fieldSchema) {
/** @type {?} */
var widget = fieldSchema.widget;
if (widget === undefined) {
widget = { 'id': fieldSchema.type };
}
else if (typeof widget === 'string') {
widget = { 'id': widget };
}
fieldSchema.widget = widget;
};
/**
* @param {?} jsonSchema
* @param {?} path
* @return {?}
*/
SchemaPreprocessor.checkItems = /**
* @param {?} jsonSchema
* @param {?} path
* @return {?}
*/
function (jsonSchema, path) {
if (jsonSchema.items === undefined) {
schemaError('No \'items\' property in array', path);
}
};
/**
* @param {?} jsonSchema
* @param {?} path
* @return {?}
*/
SchemaPreprocessor.recursiveCheck = /**
* @param {?} jsonSchema
* @param {?} path
* @return {?}
*/
function (jsonSchema, path) {
if (jsonSchema.type === 'object') {
/*
for (const fieldId in jsonSchema.properties) {
if (jsonSchema.properties.hasOwnProperty(fieldId)) {
const fieldSchema = jsonSchema.properties[fieldId];
SchemaPreprocessor.preprocess(fieldSchema, path + fieldId + '/');
}
}
*/
if (jsonSchema.hasOwnProperty('definitions')) {
for (var fieldId in jsonSchema.definitions) {
if (jsonSchema.definitions.hasOwnProperty(fieldId)) {
/** @type {?} */
var fieldSchema = jsonSchema.definitions[fieldId];
SchemaPreprocessor.removeRecursiveRefProperties(fieldSchema, "#/definitions/" + fieldId);
// formPropertyFactory recursive is used instead
// SchemaPreprocessor.preprocess(fieldSchema, path + fieldId + '/');
}
}
}
} // else if (jsonSchema.type === 'array') {
// formPropertyFactory recursive is used instead
// SchemaPreprocessor.preprocess(jsonSchema.items, path + '*/');
// }
};
/**
* @param {?} jsonSchema
* @param {?} definitionPath
* @return {?}
*/
SchemaPreprocessor.removeRecursiveRefProperties = /**
* @param {?} jsonSchema
* @param {?} definitionPath
* @return {?}
*/
function (jsonSchema, definitionPath) {
// to avoid infinite loop
if (jsonSchema.type === 'object') {
for (var fieldId in jsonSchema.properties) {
if (jsonSchema.properties.hasOwnProperty(fieldId)) {
if (jsonSchema.properties[fieldId].$ref
&& jsonSchema.properties[fieldId].$ref === definitionPath) {
delete jsonSchema.properties[fieldId];
}
else if (jsonSchema.properties[fieldId].type === 'object') {
SchemaPreprocessor.removeRecursiveRefProperties(jsonSchema.properties[fieldId], definitionPath);
}
}
}
}
};
return SchemaPreprocessor;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
var FormPropertyFactory = (function () {
function FormPropertyFactory(schemaValidatorFactory, validatorRegistry) {
this.schemaValidatorFactory = schemaValidatorFactory;
this.validatorRegistry = validatorRegistry;
}
/**
* @param {?} schema
* @param {?=} propertyParent
* @param {?=} propertyKey
* @return {?}
*/
FormPropertyFactory.prototype.createProperty = /**
* @param {?} schema
* @param {?=} propertyParent
* @param {?=} propertyKey
* @return {?}
*/
function (schema, propertyParent, propertyKey) {
/** @type {?} */
var property;
/** @type {?} */
var path = this.generatePath(propertyParent, propertyKey);
SchemaPreprocessor.preprocess(schema, path);
// TODO test for parsing for reference schema
if (schema["$ref"]) {
/** @type {?} */
var refSchema = this.schemaValidatorFactory.getSchema(((propertyParent.root)).schema, schema["$ref"]);
property = this.createProperty(refSchema, propertyParent, propertyKey || path);
}
else {
switch (schema["type"]) {
case SchemaPropertyType.Integer:
case SchemaPropertyType.Number:
property = new NumberProperty(path, schema);
break;
case SchemaPropertyType.String:
property = new StringProperty(path, schema);
break;
case SchemaPropertyType.Boolean:
property = new BooleanProperty(path, schema);
break;
case SchemaPropertyType.Object:
property = new ObjectProperty(path, schema);
break;
case SchemaPropertyType.Array:
if (schema["widget"].id === 'array') {
property = new ArrayProperty(this, path, schema);
}
else {
schema["default"] = [];
property = new GenericProperty(path, schema);
}
break;
default:
throw new TypeError("Undefined type " + schema["type"]);
}
}
this.initializeFormProperty(property, propertyParent);
return property;
};
/**
* @param {?} property
* @param {?=} propertyParent
* @return {?}
*/
FormPropertyFactory.prototype.initializeFormProperty = /**
* @param {?} property
* @param {?=} propertyParent
* @return {?}
*/
function (property, propertyParent) {
if (propertyParent) {
property.setParent(propertyParent);
}
this.bindCustomValidator(property);
if (property instanceof ObjectProperty) {
for (var key in property.schema["properties"]) {
if (property.schema["properties"].hasOwnProperty(key)) {