Skip to content

Commit 589765c

Browse files
kshepherdgithub-actions[bot]
authored andcommitted
[TLC-1202] Move getTypeBindRelations to util function
(cherry picked from commit f16dda8)
1 parent a7b67e4 commit 589765c

File tree

4 files changed

+56
-51
lines changed

4 files changed

+56
-51
lines changed

src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
import { FormBuilderService } from '../form-builder.service';
2424
import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model';
2525
import { DsDynamicTypeBindRelationService } from './ds-dynamic-type-bind-relation.service';
26+
import { getTypeBindRelations } from './type-bind.utils';
2627

2728
describe('DSDynamicTypeBindRelationService test suite', () => {
2829
let service: DsDynamicTypeBindRelationService;
@@ -82,7 +83,7 @@ describe('DSDynamicTypeBindRelationService test suite', () => {
8283
});
8384
it('Should get 1 related form models for mock relation model data', () => {
8485
const testModel = mockInputWithTypeBindModel;
85-
testModel.typeBindRelations = DsDynamicTypeBindRelationService.getTypeBindRelations(['boundType'], 'dc.type');
86+
testModel.typeBindRelations = getTypeBindRelations(['boundType'], 'dc.type');
8687
const relatedModels = service.getRelatedFormModel(testModel);
8788
expect(relatedModels).toHaveSize(1);
8889
});
@@ -91,7 +92,7 @@ describe('DSDynamicTypeBindRelationService test suite', () => {
9192
describe('Test matchesCondition method', () => {
9293
it('Should receive one subscription to dc.type type binding"', () => {
9394
const testModel = mockInputWithTypeBindModel;
94-
testModel.typeBindRelations = DsDynamicTypeBindRelationService.getTypeBindRelations(['boundType'], 'dc.type');
95+
testModel.typeBindRelations = getTypeBindRelations(['boundType'], 'dc.type');
9596
const dcTypeControl = new UntypedFormControl();
9697
dcTypeControl.setValue('boundType');
9798
let subscriptions = service.subscribeRelations(testModel, dcTypeControl);
@@ -100,7 +101,7 @@ describe('DSDynamicTypeBindRelationService test suite', () => {
100101

101102
it('Expect hasMatch to be true (ie. this should be hidden)', () => {
102103
const testModel = mockInputWithTypeBindModel;
103-
testModel.typeBindRelations = DsDynamicTypeBindRelationService.getTypeBindRelations(['boundType'], 'dc.type');
104+
testModel.typeBindRelations = getTypeBindRelations(['boundType'], 'dc.type');
104105
const dcTypeControl = new UntypedFormControl();
105106
dcTypeControl.setValue('boundType');
106107
testModel.typeBindRelations[0].when[0].value = 'anotherType';
@@ -115,7 +116,7 @@ describe('DSDynamicTypeBindRelationService test suite', () => {
115116

116117
it('Expect hasMatch to be false (ie. this should NOT be hidden)', () => {
117118
const testModel = mockInputWithTypeBindModel;
118-
testModel.typeBindRelations = DsDynamicTypeBindRelationService.getTypeBindRelations(['boundType'], 'dc.type');
119+
testModel.typeBindRelations = getTypeBindRelations(['boundType'], 'dc.type');
119120
const dcTypeControl = new UntypedFormControl();
120121
dcTypeControl.setValue('boundType');
121122
testModel.typeBindRelations[0].when[0].value = 'boundType';

src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.ts

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import {
1313
DynamicFormControlModel,
1414
DynamicFormControlRelation,
1515
DynamicFormRelationService,
16-
MATCH_ENABLED,
17-
MATCH_VISIBLE,
1816
OR_OPERATOR,
1917
} from '@ng-dynamic-forms/core';
2018
import { Subscription } from 'rxjs';
@@ -35,48 +33,6 @@ import { DYNAMIC_FORM_CONTROL_TYPE_RELATION_GROUP } from './ds-dynamic-form-cons
3533
@Injectable({ providedIn: 'root' })
3634
export class DsDynamicTypeBindRelationService {
3735

38-
/**
39-
* Get the type bind values from the REST data for a specific field
40-
* The return value is any[] in the method signature but in reality it's
41-
* returning the 'relation' that'll be used for a dynamic matcher when filtering
42-
* fields in type bind, made up of a 'match' outcome (make this field visible), an 'operator'
43-
* (OR) and a 'when' condition (the bindValues array).
44-
* @param configuredTypeBindValues array of types from the submission definition (CONFIG_DATA)
45-
* @param typeField
46-
* @private
47-
* @return DynamicFormControlRelation[] array with one relation in it, for type bind matching to show a field
48-
*/
49-
public static getTypeBindRelations(configuredTypeBindValues: string[], typeField: string): DynamicFormControlRelation[] {
50-
const bindValues = [];
51-
configuredTypeBindValues.forEach((value) => {
52-
bindValues.push({
53-
id: typeField,
54-
value: value,
55-
});
56-
});
57-
// match: MATCH_VISIBLE means that if true, the field / component will be visible
58-
// operator: OR means that all the values in the 'when' condition will be compared with OR, not AND
59-
// when: the list of values to match against, in this case the list of strings from <type-bind>...</type-bind>
60-
// Example: Field [x] will be VISIBLE if item type = book OR item type = book_part
61-
//
62-
// The opposing match value will be the dc.type for the workspace item
63-
//
64-
// MATCH_ENABLED is now also returned, so that hidden type-bound fields that are 'required'
65-
// do not trigger false validation errors
66-
return [
67-
{
68-
match: MATCH_ENABLED,
69-
operator: OR_OPERATOR,
70-
when: bindValues,
71-
},
72-
{
73-
match: MATCH_VISIBLE,
74-
operator: OR_OPERATOR,
75-
when: bindValues,
76-
},
77-
];
78-
}
79-
8036
constructor(@Optional() @Inject(DYNAMIC_MATCHERS) private dynamicMatchers: DynamicFormControlMatcher[],
8137
protected dynamicFormRelationService: DynamicFormRelationService,
8238
protected formBuilderService: FormBuilderService,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import {
2+
DynamicFormControlRelation,
3+
MATCH_ENABLED,
4+
MATCH_VISIBLE,
5+
OR_OPERATOR,
6+
} from '@ng-dynamic-forms/core';
7+
8+
/**
9+
* Get the type bind values from the REST data for a specific field
10+
* The return value is any[] in the method signature but in reality it's
11+
* returning the 'relation' that'll be used for a dynamic matcher when filtering
12+
* fields in type bind, made up of a 'match' outcome (make this field visible), an 'operator'
13+
* (OR) and a 'when' condition (the bindValues array).
14+
* @param configuredTypeBindValues array of types from the submission definition (CONFIG_DATA)
15+
* @param typeField
16+
* @private
17+
* @return DynamicFormControlRelation[] array with one relation in it, for type bind matching to show a field
18+
*/
19+
export function getTypeBindRelations(configuredTypeBindValues: string[], typeField: string): DynamicFormControlRelation[] {
20+
const bindValues = [];
21+
configuredTypeBindValues.forEach((value) => {
22+
bindValues.push({
23+
id: typeField,
24+
value: value,
25+
});
26+
});
27+
// match: MATCH_VISIBLE means that if true, the field / component will be visible
28+
// operator: OR means that all the values in the 'when' condition will be compared with OR, not AND
29+
// when: the list of values to match against, in this case the list of strings from <type-bind>...</type-bind>
30+
// Example: Field [x] will be VISIBLE if item type = book OR item type = book_part
31+
//
32+
// The opposing match value will be the dc.type for the workspace item
33+
//
34+
// MATCH_ENABLED is now also returned, so that hidden type-bound fields that are 'required'
35+
// do not trigger false validation errors
36+
return [
37+
{
38+
match: MATCH_ENABLED,
39+
operator: OR_OPERATOR,
40+
when: bindValues,
41+
},
42+
{
43+
match: MATCH_VISIBLE,
44+
operator: OR_OPERATOR,
45+
when: bindValues,
46+
},
47+
];
48+
}

src/app/shared/form/builder/parsers/field-parser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
isNotNull,
1616
isNotUndefined,
1717
} from '../../../empty.util';
18-
import { DsDynamicTypeBindRelationService } from '../ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service';
1918
import {
2019
DsDynamicInputModel,
2120
DsDynamicInputModelConfig,
@@ -24,6 +23,7 @@ import {
2423
DynamicRowArrayModel,
2524
DynamicRowArrayModelConfig,
2625
} from '../ds-dynamic-form-ui/models/ds-dynamic-row-array-model';
26+
import { getTypeBindRelations } from '../ds-dynamic-form-ui/type-bind.utils';
2727
import { FormFieldModel } from '../models/form-field.model';
2828
import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model';
2929
import { RelationshipOptions } from '../models/relationship-options.model';
@@ -94,7 +94,7 @@ export abstract class FieldParser {
9494
metadataFields: this.getAllFieldIds(),
9595
hasSelectableMetadata: isNotEmpty(this.configData.selectableMetadata),
9696
isDraggable,
97-
typeBindRelations: isNotEmpty(this.configData.typeBind) ? DsDynamicTypeBindRelationService.getTypeBindRelations(this.configData.typeBind,
97+
typeBindRelations: isNotEmpty(this.configData.typeBind) ? getTypeBindRelations(this.configData.typeBind,
9898
this.parserOptions.typeField) : null,
9999
groupFactory: () => {
100100
let model;
@@ -323,7 +323,7 @@ export abstract class FieldParser {
323323

324324
// If typeBind is configured
325325
if (isNotEmpty(this.configData.typeBind)) {
326-
(controlModel as DsDynamicInputModel).typeBindRelations = DsDynamicTypeBindRelationService.getTypeBindRelations(this.configData.typeBind,
326+
(controlModel as DsDynamicInputModel).typeBindRelations = getTypeBindRelations(this.configData.typeBind,
327327
this.parserOptions.typeField);
328328
}
329329

0 commit comments

Comments
 (0)