angular2-schema-form
Version:
Angular2 Schema Form (DISCLAIMER: it is not related to angular-schema-form)
75 lines (74 loc) • 2.96 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var 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 function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { PropertyGroup } from './formproperty';
var ArrayProperty = (function (_super) {
__extends(ArrayProperty, _super);
function ArrayProperty(formPropertyFactory, schemaValidatorFactory, validatorRegistry, schema, parent, path) {
var _this = _super.call(this, schemaValidatorFactory, validatorRegistry, schema, parent, path) || this;
_this.formPropertyFactory = formPropertyFactory;
return _this;
}
ArrayProperty.prototype.addItem = function (value) {
if (value === void 0) { value = null; }
var newProperty = this.addProperty();
newProperty.reset(value, false);
return newProperty;
};
ArrayProperty.prototype.addProperty = function () {
var newProperty = this.formPropertyFactory.createProperty(this.schema.items, this);
this.properties.push(newProperty);
return newProperty;
};
ArrayProperty.prototype.removeItem = function (index) {
this.properties.splice(index, 1);
this.updateValueAndValidity(false, true);
};
ArrayProperty.prototype.setValue = function (value, onlySelf) {
this.createProperties();
this.resetProperties(value);
this.updateValueAndValidity(onlySelf, true);
};
ArrayProperty.prototype._hasValue = function () {
return true;
};
ArrayProperty.prototype._updateValue = function () {
this.reduceValue();
};
ArrayProperty.prototype.reduceValue = function () {
var value = [];
this.forEachChild(function (property, _) {
if (property.visible && property._hasValue()) {
value.push(property.value);
}
});
this._value = value;
};
ArrayProperty.prototype.reset = function (value, onlySelf) {
if (onlySelf === void 0) { onlySelf = true; }
value = value || this.schema.default || [];
this.properties = [];
this.resetProperties(value);
this.updateValueAndValidity(onlySelf, true);
};
ArrayProperty.prototype.createProperties = function () {
this.properties = [];
};
ArrayProperty.prototype.resetProperties = function (value) {
for (var idx in value) {
if (value.hasOwnProperty(idx)) {
var property = this.addProperty();
property.reset(value[idx], true);
}
}
};
return ArrayProperty;
}(PropertyGroup));
export { ArrayProperty };