import 'lodash'; import { PathUtil } from '../../../services/pathutil'; import { IUISchemaGenerator } from '../../../generators/generators'; import { AbstractControl } from '../abstract-control'; import { IGroup } from '../../../../uischema'; import { SchemaArray } from '../../../../jsonschema'; import { PathResolver } from '../../../services/path-resolver/path-resolver'; import { Testers, schemaTypeIs, optionIs } from '../../testers'; let pluralize = require('pluralize'); const readOnlyArrayTemplate = ` {{vm.label}} {{prop | capitalize}}: {{data[prop]}} {{vm.emptyMsg}} `; class ArrayReadOnlyDirective implements ng.IDirective { restrict = 'E'; templateUrl = 'read-only-array.html'; controller = ArrayController; controllerAs = 'vm'; } const arrayTemplate = ` {{vm.label}} {{vm.emptyMsg}} `; class ArrayDirective implements ng.IDirective { restrict = 'E'; templateUrl = 'array.html'; controller = ArrayController; controllerAs = 'vm'; } interface ArrayControllerScope extends ng.IScope { } class ArrayController extends AbstractControl { static $inject = ['$scope', 'UISchemaGenerator']; private properties: string[]; private submitElement = {}; private arraySchema: any; private arrayUiSchema: IGroup; constructor(scope: ArrayControllerScope, uiGenerator: IUISchemaGenerator) { super(scope); let resolvedSubSchema = PathResolver.resolveSchema( this.schema, this.schemaPath) as SchemaArray; let items = resolvedSubSchema.items; this.arraySchema = items; this.properties = _.keys(items['properties']); const detailUiSchema = this.uiSchema['options'] ? this.uiSchema['options']['detail-uischema'] : undefined; this.arrayUiSchema = detailUiSchema ? detailUiSchema : uiGenerator.generateDefaultUISchema(items, 'HorizontalLayout'); } public get buttonText() { return pluralize(PathUtil.beautifiedLastFragment(this.schemaPath), 1); } public submitCallback() { if (this.resolvedData[this.fragment] === undefined) { this.resolvedData[this.fragment] = []; } this.resolvedData[this.fragment].push(_.clone(this.submitElement)); this.submitElement = {}; } public deleteCallback(element: any) { let index = this.resolvedData[this.fragment].indexOf(element); if (index !== -1) { this.resolvedData[this.fragment].splice(index, 1); } } public get supportsSubmit() { return this.supports('submit'); } public get supportsDelete() { return this.supports('delete'); } private supports(keyword: string) { let options = this.uiSchema['options']; return !(options !== undefined && options[keyword] === false); } public get isEmpty(): boolean { return _.isEmpty(this.resolvedData) || _.isEmpty(this.resolvedData[this.fragment]); } public get emptyMsg(): string { return 'No ' + PathUtil.beautifiedLastFragment(this.schemaPath) + ' yet.'; } } export default angular .module('jsonforms.renderers.controls.array', ['jsonforms.renderers.controls']) .directive('arrayReadonlyControl', () => new ArrayReadOnlyDirective()) .directive('arrayControl', () => new ArrayDirective()) .run(['RendererService', RendererService => { RendererService.register('array-readonly-control', Testers.and( schemaTypeIs('array'), optionIs('simple', true) ), 1); RendererService.register('array-control', schemaTypeIs('array'), 1); }]) .run(['$templateCache', $templateCache => { $templateCache.put('read-only-array.html', readOnlyArrayTemplate); $templateCache.put('array.html', arrayTemplate); }]) .name;