Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize for fhir #1

Open
wants to merge 6 commits into
base: 2.6.6-master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.6.6-forked (2022-08-26)

- Customize creating of form property for FHIR Extension schema.

# 2.6.6 (2021-08-12)

- Fix HTML IDs in elements nested in arrays
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions projects/schema-form/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "ngx-schema-form",
"version": "2.6.6",
"name": "@lhncbc/ngx-schema-form",
"version": "2.6.6-forked",
"repository": {
"type": "git",
"url": "git+https://github.com/guillotinaweb/ngx-schema-form"
"url": "git+https://github.com/lhncbc/ngx-schema-form"
},
"keywords": [
"angular",
Expand All @@ -13,15 +13,16 @@
],
"license": "MIT",
"bugs": {
"url": "https://github.com/guillotinaweb/ngx-schema-form/issues"
"url": "https://github.com/lhncbc/ngx-schema-form/issues"
},
"contributors": [
"Frank Bessou <[email protected]>",
"Eric Brehault <[email protected]>",
"Simon Bats <[email protected]>",
"Gabor Pankotay <[email protected]>",
"Juan Manuel Verges",
"Daniele Pecora <[email protected]>"
"Daniele Pecora <[email protected]>",
"Ajay Kanduru <[email protected]>"
],
"dependencies": {
"tslib": "^2.0.0"
Expand Down
7 changes: 7 additions & 0 deletions projects/schema-form/src/lib/form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,22 @@ export class FormComponent implements OnChanges, ControlValueAccessor {

@Input() bindings: { [path: string]: Binding } = {};

// tslint:disable-next-line:no-output-on-prefix
@Output() onChange = new EventEmitter<{ value: any }>();

@Output() modelChange = new EventEmitter<any>();

@Output() isValid = new EventEmitter<boolean>();

// tslint:disable-next-line:no-output-on-prefix
@Output() onErrorChange = new EventEmitter<{ value: any[] }>();

// tslint:disable-next-line:no-output-on-prefix
@Output() onErrorsChange = new EventEmitter<{value: any}>();

// tslint:disable-next-line:no-output-on-prefix
@Output() modelReset = new EventEmitter<{value: any}>();

rootProperty: FormProperty = null;

private onChangeCallback: any;
Expand Down Expand Up @@ -156,6 +162,7 @@ export class FormComponent implements OnChanges, ControlValueAccessor {
if (this.schema && (changes.model || changes.schema )) {
this.rootProperty.reset(this.model, false);
this.cdr.detectChanges();
this.modelReset.next({value: this.rootProperty.value});
}

}
Expand Down
30 changes: 17 additions & 13 deletions projects/schema-form/src/lib/model/arrayproperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,30 @@ export class ArrayProperty extends PropertyGroup {
}

addItem(value: any = null): FormProperty {
let newProperty = this.addProperty();
newProperty.reset(value, false);
return newProperty;
return this.addProperty(value);
}

private addProperty() {
let itemSchema = this.schema.items
/**
* For some FHIR schemas, such as Extension, we need information from the url to limit the associated value[x] fields.
* @param value
* @private
*/
private addProperty(value) {
let itemSchema = this.schema.items;
if (Array.isArray(this.schema.items)) {
const itemSchemas = this.schema.items as object[]
const itemSchemas = this.schema.items as object[];
if (itemSchemas.length > (<FormProperty[]>this.properties).length) {
itemSchema = itemSchema[(<FormProperty[]>this.properties).length]
itemSchema = itemSchema[(<FormProperty[]>this.properties).length];
} else if (this.schema.additionalItems) {
itemSchema = this.schema.additionalItems
itemSchema = this.schema.additionalItems;
} else {
// souldn't add new items since schema is undefined for the item at its position
return null
// shouldn't add new items since schema is undefined for the item at its position
return null;
}
}
let newProperty = this.formPropertyFactory.createProperty(itemSchema, this);
const newProperty = this.formPropertyFactory.createProperty(itemSchema, this, null, value);
(<FormProperty[]>this.properties).push(newProperty);
newProperty.reset(value, false);
return newProperty;
}

Expand Down Expand Up @@ -86,9 +90,9 @@ export class ArrayProperty extends PropertyGroup {


private resetProperties(value: any) {
for (let idx in value) {
for (const idx in value) {
if (value.hasOwnProperty(idx)) {
let property = this.addProperty();
const property = this.addProperty(value[idx]);
property.reset(value[idx], true);
}
}
Expand Down
9 changes: 5 additions & 4 deletions projects/schema-form/src/lib/model/formpropertyfactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class FormPropertyFactory {
private logger: LogService) {
}

createProperty(schema: ISchema, parent: PropertyGroup = null, propertyId?: string): FormProperty {
createProperty(schema: ISchema, parent: PropertyGroup = null, propertyId: string = null, value?: any): FormProperty {
let newProperty = null;
let path = '';
let _canonicalPath = '';
Expand All @@ -33,7 +33,7 @@ export class FormPropertyFactory {
path += '*';
_canonicalPath += '*';
} else {
throw 'Instanciation of a FormProperty with an unknown parent type: ' + parent.type;
throw new Error('Instanciation of a FormProperty with an unknown parent type: ' + parent.type);
}
_canonicalPath = (parent._canonicalPath || parent.path) + _canonicalPath;
} else {
Expand All @@ -43,7 +43,7 @@ export class FormPropertyFactory {

if (schema.$ref) {
const refSchema = this.schemaValidatorFactory.getSchema(parent.root.schema, schema.$ref);
newProperty = this.createProperty(refSchema, parent, path);
newProperty = this.createProperty(refSchema, parent, path, value);
} else {
const type: FieldType = this.isUnionType(schema.type)
&& this.isValidNullableUnionType(schema.type as TNullableFieldType)
Expand All @@ -54,7 +54,8 @@ export class FormPropertyFactory {
if (PROPERTY_TYPE_MAPPING[type]) {
if (type === 'object' || type === 'array') {
newProperty = PROPERTY_TYPE_MAPPING[type](
this.schemaValidatorFactory, this.validatorRegistry, this.expressionCompilerFactory, schema, parent, path, this, this.logger);
this.schemaValidatorFactory, this.validatorRegistry, this.expressionCompilerFactory, schema, parent, path, this, this.logger,
value);
} else {
newProperty = PROPERTY_TYPE_MAPPING[type](
this.schemaValidatorFactory, this.validatorRegistry, this.expressionCompilerFactory, schema, parent, path, this.logger);
Expand Down
19 changes: 10 additions & 9 deletions projects/schema-form/src/lib/model/objectproperty.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import { DefaultLogService, LogLevel } from '../log.service';

describe('ObjectProperty', () => {

let A_VALIDATOR_REGISTRY = new ValidatorRegistry();
let A_SCHEMA_VALIDATOR_FACTORY = new ZSchemaValidatorFactory();
let A_PROPERTY_BINDING_REGISTRY=new PropertyBindingRegistry();
let A_EXPRESSION_COMPILER_FACTORY = new JEXLExpressionCompilerFactory();
let A_LOGGER = new DefaultLogService(LogLevel.off);
let A_FORM_PROPERTY_FACTORY = new FormPropertyFactory(A_SCHEMA_VALIDATOR_FACTORY, A_VALIDATOR_REGISTRY, A_PROPERTY_BINDING_REGISTRY, A_EXPRESSION_COMPILER_FACTORY, A_LOGGER);

const A_VALIDATOR_REGISTRY = new ValidatorRegistry();
const A_SCHEMA_VALIDATOR_FACTORY = new ZSchemaValidatorFactory();
const A_PROPERTY_BINDING_REGISTRY = new PropertyBindingRegistry();
const A_EXPRESSION_COMPILER_FACTORY = new JEXLExpressionCompilerFactory();
const A_LOGGER = new DefaultLogService(LogLevel.off);
const A_FORM_PROPERTY_FACTORY = new FormPropertyFactory(A_SCHEMA_VALIDATOR_FACTORY, A_VALIDATOR_REGISTRY, A_PROPERTY_BINDING_REGISTRY,
A_EXPRESSION_COMPILER_FACTORY, A_LOGGER);


const THE_OBJECT_SCHEMA: ISchema = {
type: 'object',
Expand Down Expand Up @@ -48,9 +49,9 @@ describe('ObjectProperty', () => {

it('should create same properties as in the schema', () => {

for (let propertyId in THE_OBJECT_SCHEMA.properties) {
for (const propertyId in THE_OBJECT_SCHEMA.properties) {
if (THE_OBJECT_SCHEMA.properties.hasOwnProperty(propertyId)) {
let property = objProperty.getProperty(propertyId);
const property = objProperty.getProperty(propertyId);
expect(property).toBeDefined();
}
}
Expand Down
37 changes: 32 additions & 5 deletions projects/schema-form/src/lib/model/objectproperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@ export class ObjectProperty extends PropertyGroup {
schema: ISchema,
parent: PropertyGroup,
path: string,
logger: LogService) {
logger: LogService,
value?: any) {
super(schemaValidatorFactory, validatorRegistry, expressionCompilerFactory, schema, parent, path, logger);
this.createProperties();
if (path.match(/\/(extension|modifiedExtension)\/\*$/)) {
this.createPropertiesExtension(value);
} else {
this.createProperties();
}
}

setValue(value: any, onlySelf: boolean) {
for (const propertyId in value) {
if (value.hasOwnProperty(propertyId)) {
if (!this.properties[propertyId]) {
const propertySchema = this.schema.properties[propertyId];
this.properties[propertyId] = this.formPropertyFactory.createProperty(propertySchema, this, propertyId, value[propertyId]);
this.propertiesId.push(propertyId);
}
this.properties[propertyId].setValue(value[propertyId], true);
}
}
Expand All @@ -40,7 +50,7 @@ export class ObjectProperty extends PropertyGroup {

resetProperties(value: any) {
for (const propertyId in this.schema.properties) {
if (this.schema.properties.hasOwnProperty(propertyId)) {
if (this.properties[propertyId] && this.schema.properties.hasOwnProperty(propertyId)) {
this.properties[propertyId].reset(value[propertyId], true);
}
}
Expand All @@ -58,6 +68,22 @@ export class ObjectProperty extends PropertyGroup {
}
}

createPropertiesExtension(value: any) {
this.properties = {};
this.propertiesId = [];
const propList: string[] = value ? Object.keys(value) : this.schema.properties ? Object.keys(this.schema.properties) : [];
for (const propertyId of propList) {
if (this.schema.properties.hasOwnProperty(propertyId)) {
const propertySchema = this.schema.properties[propertyId];
if (propertySchema) {
this.properties[propertyId] = this.formPropertyFactory.createProperty(propertySchema, this, propertyId,
value ? value[propertyId] : null);
this.propertiesId.push(propertyId);
}
}
}
}

public _hasValue(): boolean {
return !!Object.keys(this.value).length;
}
Expand Down Expand Up @@ -98,8 +124,9 @@ PROPERTY_TYPE_MAPPING.object = (
parent: PropertyGroup,
path: string,
formPropertyFactory: FormPropertyFactory,
logger: LogService
logger: LogService,
value?: any
) => {
return new ObjectProperty(
formPropertyFactory, schemaValidatorFactory, validatorRegistry, expressionCompilerFactory, schema, parent, path, logger);
formPropertyFactory, schemaValidatorFactory, validatorRegistry, expressionCompilerFactory, schema, parent, path, logger, value);
};