UNPKG

@aujsf/core

Version:
210 lines 8.21 kB
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); }; import { bindingMode, computedFrom, bindable, BindingEngine, Container, inlineView } from 'aurelia-framework'; import { JsonPointer } from 'jsonpointerx'; import { FormTemplateRegistry, FormContext, ViewProvider, Enhancer } from '../services'; import { ErrorSchema } from '../models'; import utils from '../utils'; import { BindingSignaler } from 'aurelia-templating-resources'; import { ViewBase } from './view-base'; let AujsfBase = class AujsfBase extends ViewBase { constructor(_element, _container, _templateRegistry, context, viewProvider, signaler, engine) { super(); this._element = _element; this._container = _container; this._templateRegistry = _templateRegistry; this.context = context; this.viewProvider = viewProvider; this.signaler = signaler; this.engine = engine; this.uiSchema = {}; this._bindHandle = -1; } created(owningView, myView) { this.owningView = owningView; this.myView = myView; } get readonly() { var _a; return this.parentReadonly || ((_a = this.uiSchema['ui:read-only']) !== null && _a !== void 0 ? _a : this.schema.readOnly) || ('const' in this.schema && utils.common.notNullOrUndefined(this.schema.const)); } get title() { var _a, _b, _c; if ('ui:title' in this.uiSchema && this.uiSchema['ui:title'] === false) { return; } return (_b = (_a = this.getTitleTemplate()) !== null && _a !== void 0 ? _a : this.schema.title) !== null && _b !== void 0 ? _b : utils.common.startCase((_c = utils.array.last(this.pointer.segments)) !== null && _c !== void 0 ? _c : 'root'); } get description() { var _a; return (_a = this.uiSchema['ui:description']) !== null && _a !== void 0 ? _a : this.schema.description; } get placeholder() { var _a; return (_a = this.uiSchema['ui:placeholder']) !== null && _a !== void 0 ? _a : ''; } get id() { return this.pointer.toString(); } get hasErrors() { return this.errors['es:hasErrors']; } get errorMessages() { return this.errors['es:getErrorMessages'](this.uiSchema); } bind(bctx, obctx) { this._logger.debug('binding', this); this._bctx = bctx; this._obctx = obctx; this.resolveUISchemaDefaults(); this.bound(); this._logger.debug('bound', this); this.enhance(); } bound() { } resolveUISchemaDefaults() { var _a, _b; this.uiSchema = (_a = this.uiSchema) !== null && _a !== void 0 ? _a : {}; this.uiSchema['ui:view'] = (_b = this.uiSchema['ui:view']) !== null && _b !== void 0 ? _b : this.viewProvider.getTemplate(this); } getTitleTemplate() { if ('ui:title' in this.uiSchema && typeof this.uiSchema['ui:title'] === 'string') { return utils.common.fillTemplate(this.uiSchema['ui:title'], this); } } enhance() { this._logger.debug('creating view strategy', this); const viewName = this.uiSchema['ui:view'] || 'hidden'; if (viewName === 'unknown' || !this._templateRegistry.has(viewName)) { this.view = this._container.get(Enhancer).error({ message: `the ui:view '${viewName}' was not found`, element: this._element, }); return; } const template = this._templateRegistry.get(viewName); this.view = this._container.get(Enhancer).enhanceTemplate({ element: this._element, bindingContext: this, container: this.myView.container, template, attributes: this.uiSchema['ui:view-class'] ? [['class', this.uiSchema['ui:view-class']]] : undefined, }); } rebind() { clearTimeout(this._bindHandle); this._bindHandle = setTimeout(() => { this._logger.debug('rebinding'); this.bind(this._bctx, this._obctx); }, 100); } dispatchEvent(name, detail, element = this._element) { element.dispatchEvent(new CustomEvent(name, { bubbles: true, detail, })); } valueChanged(newValue, oldValue) { const event = { newValue, oldValue, pointer: this.pointer, pointerString: this.pointer.toString() || '/', uiSchema: this.uiSchema, schema: this.schema, }; this.dispatchEvent('value-changed', event); this.context.events.emitPointerChange(event); } schemaChanged(...args) { this._logger.debug('schema binding changed', Object.assign({}, args)); this.rebind(); } }; __decorate([ bindable({ defaultBindingMode: bindingMode.twoWay }), __metadata("design:type", Object) ], AujsfBase.prototype, "value", void 0); __decorate([ bindable({ changeHandler: 'schemaChanged' }), __metadata("design:type", Object) ], AujsfBase.prototype, "schema", void 0); __decorate([ bindable({ changeHandler: 'schemaChanged' }), __metadata("design:type", Object) ], AujsfBase.prototype, "uiSchema", void 0); __decorate([ bindable, __metadata("design:type", JsonPointer) ], AujsfBase.prototype, "pointer", void 0); __decorate([ bindable, __metadata("design:type", Boolean) ], AujsfBase.prototype, "required", void 0); __decorate([ bindable, __metadata("design:type", Boolean) ], AujsfBase.prototype, "parentReadonly", void 0); __decorate([ bindable, __metadata("design:type", ErrorSchema) ], AujsfBase.prototype, "errors", void 0); __decorate([ computedFrom('parentReadonly', 'schema'), __metadata("design:type", Boolean), __metadata("design:paramtypes", []) ], AujsfBase.prototype, "readonly", null); __decorate([ computedFrom('uiSchema', 'schema', 'pointer'), __metadata("design:type", Object), __metadata("design:paramtypes", []) ], AujsfBase.prototype, "title", null); __decorate([ computedFrom('uiSchema', 'schema'), __metadata("design:type", Object), __metadata("design:paramtypes", []) ], AujsfBase.prototype, "description", null); __decorate([ computedFrom('schema', 'uiSchema'), __metadata("design:type", String), __metadata("design:paramtypes", []) ], AujsfBase.prototype, "placeholder", null); __decorate([ computedFrom('pointer'), __metadata("design:type", String), __metadata("design:paramtypes", []) ], AujsfBase.prototype, "id", null); __decorate([ computedFrom('errors'), __metadata("design:type", Boolean), __metadata("design:paramtypes", []) ], AujsfBase.prototype, "hasErrors", null); __decorate([ computedFrom('errors', 'uiSchema'), __metadata("design:type", Array), __metadata("design:paramtypes", []) ], AujsfBase.prototype, "errorMessages", null); AujsfBase = __decorate([ inlineView(`<template></template>`), __metadata("design:paramtypes", [Element, Container, FormTemplateRegistry, FormContext, ViewProvider, BindingSignaler, BindingEngine]) ], AujsfBase); export { AujsfBase }; //# sourceMappingURL=aujsf-base.js.map