@aujsf/core
Version:
Create forms based on JSON Schema!
139 lines • 6.76 kB
JavaScript
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { TaskQueue, bindable, bindingMode, signalBindings, NewInstance } from 'aurelia-framework';
import { getLogger } from 'aurelia-logging';
import { FormTemplateRegistry, FormContext } from '../services';
import { ErrorSchema } from '../models';
import utils from '../utils';
export class JsonSchemaForm {
constructor(element) {
this.element = element;
this.logger = getLogger('aujsf:json-schema-form');
this.status = 'initializing';
this.readonly = false;
this.validationResult = this.getDefaultValidatoinResult();
this.submit = (args) => {
alert('submit triggered:\n' + JSON.stringify(args, null, 2));
};
let debounceHandle = -1;
element.addEventListener('value-changed', () => {
clearTimeout(debounceHandle);
debounceHandle = setTimeout(this.tryValidate.bind(this), 100);
});
}
created(owningView, myView) {
return __awaiter(this, void 0, void 0, function* () {
this.owningView = owningView;
this.myView = myView;
this.taskQueue = myView.container.get(TaskQueue);
this.registry = myView.container.get(NewInstance.of(FormTemplateRegistry));
this.context = myView.container.get(NewInstance.of(FormContext));
const theme = this.getTheme();
yield Promise.all(Object.keys(theme).map(key => this.registry.add(key, theme[key])));
this.status = 'ready';
});
}
getDefaultValidatoinResult() {
return {
errorSchema: ErrorSchema.create(),
errors: [],
valid: true,
};
}
tryValidate() {
var _a;
(_a = this.taskQueue) === null || _a === void 0 ? void 0 : _a.queueMicroTask(() => __awaiter(this, void 0, void 0, function* () {
var _b, _c;
this.validationResult = (_c = yield ((_b = this.context.validator) === null || _b === void 0 ? void 0 : _b.validate(this.value))) !== null && _c !== void 0 ? _c : this.getDefaultValidatoinResult();
signalBindings('aujsf:ValueChanged');
}));
}
onsubmit() {
try {
this.submit({ value: this.value, validationResult: this.validationResult });
}
catch (error) {
this.logger.error('an error occurred while submitting', error);
}
}
schemaChanged(newSchema, oldSchema) {
var _a, _b;
this.logger.debug('schema changed', { newSchema, oldSchema });
if (newSchema && newSchema !== oldSchema) {
this.context.schema = utils.common.clone(newSchema);
this.context.uiSchema = utils.common.clone((_a = this.uiSchema) !== null && _a !== void 0 ? _a : {});
this.value = (_b = this.context.schemaDefaults) === null || _b === void 0 ? void 0 : _b.mergeDefaults(this.value, newSchema);
this.tryValidate();
}
}
uiSchemaChanged(newValue, oldValue) {
this.logger.debug('ui-schema changed', { newValue, oldValue });
this.context.uiSchema = utils.common.clone(newValue !== null && newValue !== void 0 ? newValue : {});
}
valueChanged(newValue, oldValue) {
var _a;
this.logger.debug('value changed', { newValue, oldValue });
if (this.context.schema) {
newValue = (_a = this.context.schemaDefaults) === null || _a === void 0 ? void 0 : _a.mergeDefaults(newValue, this.context.schema);
this.tryValidate();
}
this.context.value = newValue;
}
optionsChanged(newValue, oldValue) {
this.logger.debug('options changed', { newValue, oldValue });
this.context.formOptions = newValue !== null && newValue !== void 0 ? newValue : {};
}
contextsChanged(newValue) {
this.logger.debug('contexts changed', { newValue });
this.context.contexts = newValue !== null && newValue !== void 0 ? newValue : {};
}
}
__decorate([
bindable({ defaultBindingMode: bindingMode.toView }),
__metadata("design:type", Object)
], JsonSchemaForm.prototype, "schema", void 0);
__decorate([
bindable({ defaultBindingMode: bindingMode.toView }),
__metadata("design:type", Object)
], JsonSchemaForm.prototype, "uiSchema", void 0);
__decorate([
bindable({ defaultBindingMode: bindingMode.twoWay }),
__metadata("design:type", Object)
], JsonSchemaForm.prototype, "value", void 0);
__decorate([
bindable({ defaultBindingMode: bindingMode.toView }),
__metadata("design:type", Object)
], JsonSchemaForm.prototype, "options", void 0);
__decorate([
bindable({ defaultBindingMode: bindingMode.toView }),
__metadata("design:type", Object)
], JsonSchemaForm.prototype, "readonly", void 0);
__decorate([
bindable,
__metadata("design:type", Object)
], JsonSchemaForm.prototype, "contexts", void 0);
__decorate([
bindable({ defaultBindingMode: bindingMode.fromView }),
__metadata("design:type", Object)
], JsonSchemaForm.prototype, "validationResult", void 0);
__decorate([
bindable,
__metadata("design:type", Function)
], JsonSchemaForm.prototype, "submit", void 0);
//# sourceMappingURL=json-schema-form.js.map