angular2-json-schema-form
Version:
Angular 2 JSON Schema Form builder
176 lines • 8.38 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = require("@angular/core");
var _ = require("lodash");
var index_1 = require("./utilities/index");
var JsonSchemaFormService = (function () {
function JsonSchemaFormService() {
this.JsonFormCompatibility = false;
this.ReactJsonSchemaFormCompatibility = false;
this.AngularSchemaFormCompatibility = false;
this.globalOptions = {
addSubmit: true,
debug: false,
fieldsRequired: false,
pristine: { errors: true, success: true },
setSchemaDefaults: true,
formDefaults: {
feedback: true,
},
};
this.initialValues = {};
this.schema = {};
this.layout = [];
this.formGroupTemplate = {};
this.formGroup = null;
this.framework = null;
this.arrayMap = new Map();
this.dataMap = new Map();
this.dataCircularRefMap = new Map();
this.schemaCircularRefMap = new Map();
this.layoutRefLibrary = {};
this.schemaRefLibrary = {};
this.templateRefLibrary = {};
}
JsonSchemaFormService.prototype.initializeControl = function (ctx) {
ctx.formControl = this.getControl(ctx);
ctx.boundControl = !!ctx.formControl;
if (ctx.boundControl) {
ctx.controlName = this.getControlName(ctx);
ctx.controlValue = ctx.formControl.value;
ctx.formControl.valueChanges.subscribe(function (v) { return ctx.controlValue = v; });
ctx.controlDisabled = ctx.formControl.disabled;
}
else {
ctx.controlName = ctx.layoutNode.name;
ctx.controlValue = ctx.layoutNode.value;
var dataPointer = this.getDataPointer(ctx);
if (dataPointer) {
console.error('warning: control "' + dataPointer +
'" is not bound to the Angular 2 FormGroup.');
}
}
return ctx.boundControl;
};
JsonSchemaFormService.prototype.updateValue = function (ctx, value) {
ctx.controlValue = value;
if (ctx.boundControl) {
ctx.formControl.setValue(value);
ctx.formControl.markAsDirty();
}
ctx.layoutNode.value = value;
};
JsonSchemaFormService.prototype.getControl = function (ctx) {
if (!ctx.layoutNode || !ctx.layoutNode.dataPointer ||
ctx.layoutNode.type === '$ref')
return null;
return index_1.getControl(this.formGroup, this.getDataPointer(ctx));
};
JsonSchemaFormService.prototype.getControlValue = function (ctx) {
if (!ctx.layoutNode || !ctx.layoutNode.dataPointer ||
ctx.layoutNode.type === '$ref')
return null;
var control = index_1.getControl(this.formGroup, this.getDataPointer(ctx));
return control ? control.value : null;
};
JsonSchemaFormService.prototype.getControlGroup = function (ctx) {
if (!ctx.layoutNode || !ctx.layoutNode.dataPointer)
return null;
return index_1.getControl(this.formGroup, this.getDataPointer(ctx), true);
};
JsonSchemaFormService.prototype.getControlName = function (ctx) {
if (!ctx.layoutNode || !ctx.layoutNode.dataPointer || !ctx.dataIndex)
return null;
return index_1.JsonPointer.toKey(index_1.JsonPointer.toIndexedPointer(ctx.layoutNode.dataPointer, ctx.dataIndex, this.arrayMap));
};
JsonSchemaFormService.prototype.getLayoutArray = function (ctx) {
return index_1.JsonPointer.get(this.layout, this.getLayoutPointer(ctx), 0, -1);
};
JsonSchemaFormService.prototype.getDataPointer = function (ctx) {
if (!ctx.layoutNode || !ctx.layoutNode.dataPointer || !ctx.dataIndex)
return null;
return index_1.JsonPointer.toIndexedPointer(ctx.layoutNode.dataPointer, ctx.dataIndex, this.arrayMap);
};
JsonSchemaFormService.prototype.getLayoutPointer = function (ctx) {
if (!ctx.layoutNode || !ctx.layoutNode.layoutPointer || !ctx.layoutIndex)
return null;
return index_1.JsonPointer.toIndexedPointer(ctx.layoutNode.layoutPointer, ctx.layoutIndex);
};
JsonSchemaFormService.prototype.isControlBound = function (ctx) {
if (!ctx.layoutNode || !ctx.layoutNode.dataPointer || !ctx.dataIndex)
return false;
var control = this.getControlGroup(ctx);
if (!control)
return false;
return control.controls.hasOwnProperty(index_1.JsonPointer.toKey(this.getDataPointer(ctx)));
};
JsonSchemaFormService.prototype.addItem = function (ctx) {
if (!ctx.layoutNode || !ctx.layoutNode.$ref || !ctx.dataIndex ||
!ctx.layoutNode.layoutPointer || !ctx.layoutIndex)
return false;
var newFormGroup = index_1.buildFormGroup(index_1.JsonPointer.get(this.templateRefLibrary, [ctx.layoutNode.$ref]));
if (ctx.layoutNode.arrayItem) {
this.getControlGroup(ctx).push(newFormGroup);
}
else {
this.getControlGroup(ctx).addControl(this.getControlName(ctx), newFormGroup);
}
var newLayoutNode = _.cloneDeep(index_1.JsonPointer.get(this.layoutRefLibrary, [ctx.layoutNode.$ref]));
index_1.JsonPointer.forEachDeep(newLayoutNode, function (value, pointer) {
if (index_1.hasOwn(value, '_id'))
value._id = _.uniqueId();
if (!ctx.layoutNode.arrayItem || ctx.layoutNode.circularReference) {
if (index_1.hasOwn(value, 'dataPointer')) {
value.dataPointer = ctx.layoutNode.dataPointer + value.dataPointer;
}
if (index_1.hasOwn(value, 'layoutPointer')) {
value.layoutPointer = ctx.layoutNode.layoutPointer + value.layoutPointer;
}
}
});
index_1.JsonPointer.insert(this.layout, this.getLayoutPointer(ctx), newLayoutNode);
return true;
};
JsonSchemaFormService.prototype.moveArrayItem = function (ctx, oldIndex, newIndex) {
if (!ctx.layoutNode || !ctx.layoutNode.dataPointer || !ctx.dataIndex ||
!ctx.layoutNode.layoutPointer || !ctx.layoutIndex ||
!index_1.isDefined(oldIndex) || !index_1.isDefined(newIndex))
return false;
var formArray = this.getControlGroup(ctx);
formArray.controls.splice(newIndex, 0, formArray.controls.splice(oldIndex, 1)[0]);
formArray.updateValueAndValidity();
formArray._onCollectionChange();
var layoutArray = this.getLayoutArray(ctx);
layoutArray.splice(newIndex, 0, layoutArray.splice(oldIndex, 1)[0]);
return true;
};
JsonSchemaFormService.prototype.removeItem = function (ctx) {
if (!ctx.layoutNode || !ctx.layoutNode.dataPointer || !ctx.dataIndex ||
!ctx.layoutNode.layoutPointer || !ctx.layoutIndex)
return false;
if (ctx.layoutNode.arrayItem) {
this.getControlGroup(ctx).removeAt(ctx.dataIndex[ctx.dataIndex.length - 1]);
}
else {
this.getControlGroup(ctx).removeControl(this.getControlName(ctx));
}
var layoutPointer = this.getLayoutPointer(ctx);
index_1.JsonPointer.remove(this.layout, layoutPointer);
return true;
};
return JsonSchemaFormService;
}());
JsonSchemaFormService = __decorate([
core_1.Injectable(),
__metadata("design:paramtypes", [])
], JsonSchemaFormService);
exports.JsonSchemaFormService = JsonSchemaFormService;
//# sourceMappingURL=json-schema-form.service.js.map