Skip to content

Commit a7b67e4

Browse files
kshepherdgithub-actions[bot]
authored andcommitted
[TLC-1202] Centralise getTypeBindRelations to single static method
(cherry picked from commit 9244c24)
1 parent 6406bb5 commit a7b67e4

File tree

3 files changed

+50
-98
lines changed

3 files changed

+50
-98
lines changed

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

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ import {
99
} from '@angular/forms';
1010
import {
1111
DISABLED_MATCHER_PROVIDER,
12-
DynamicFormControlRelation,
1312
DynamicFormRelationService,
1413
HIDDEN_MATCHER,
1514
HIDDEN_MATCHER_PROVIDER,
16-
MATCH_VISIBLE,
17-
OR_OPERATOR,
1815
REQUIRED_MATCHER_PROVIDER,
1916
} from '@ng-dynamic-forms/core';
2017

@@ -85,7 +82,7 @@ describe('DSDynamicTypeBindRelationService test suite', () => {
8582
});
8683
it('Should get 1 related form models for mock relation model data', () => {
8784
const testModel = mockInputWithTypeBindModel;
88-
testModel.typeBindRelations = getTypeBindRelations(['boundType']);
85+
testModel.typeBindRelations = DsDynamicTypeBindRelationService.getTypeBindRelations(['boundType'], 'dc.type');
8986
const relatedModels = service.getRelatedFormModel(testModel);
9087
expect(relatedModels).toHaveSize(1);
9188
});
@@ -94,7 +91,7 @@ describe('DSDynamicTypeBindRelationService test suite', () => {
9491
describe('Test matchesCondition method', () => {
9592
it('Should receive one subscription to dc.type type binding"', () => {
9693
const testModel = mockInputWithTypeBindModel;
97-
testModel.typeBindRelations = getTypeBindRelations(['boundType']);
94+
testModel.typeBindRelations = DsDynamicTypeBindRelationService.getTypeBindRelations(['boundType'], 'dc.type');
9895
const dcTypeControl = new UntypedFormControl();
9996
dcTypeControl.setValue('boundType');
10097
let subscriptions = service.subscribeRelations(testModel, dcTypeControl);
@@ -103,7 +100,7 @@ describe('DSDynamicTypeBindRelationService test suite', () => {
103100

104101
it('Expect hasMatch to be true (ie. this should be hidden)', () => {
105102
const testModel = mockInputWithTypeBindModel;
106-
testModel.typeBindRelations = getTypeBindRelations(['boundType']);
103+
testModel.typeBindRelations = DsDynamicTypeBindRelationService.getTypeBindRelations(['boundType'], 'dc.type');
107104
const dcTypeControl = new UntypedFormControl();
108105
dcTypeControl.setValue('boundType');
109106
testModel.typeBindRelations[0].when[0].value = 'anotherType';
@@ -118,7 +115,7 @@ describe('DSDynamicTypeBindRelationService test suite', () => {
118115

119116
it('Expect hasMatch to be false (ie. this should NOT be hidden)', () => {
120117
const testModel = mockInputWithTypeBindModel;
121-
testModel.typeBindRelations = getTypeBindRelations(['boundType']);
118+
testModel.typeBindRelations = DsDynamicTypeBindRelationService.getTypeBindRelations(['boundType'], 'dc.type');
122119
const dcTypeControl = new UntypedFormControl();
123120
dcTypeControl.setValue('boundType');
124121
testModel.typeBindRelations[0].when[0].value = 'boundType';
@@ -134,18 +131,3 @@ describe('DSDynamicTypeBindRelationService test suite', () => {
134131
});
135132

136133
});
137-
138-
function getTypeBindRelations(configuredTypeBindValues: string[]): DynamicFormControlRelation[] {
139-
const bindValues = [];
140-
configuredTypeBindValues.forEach((value) => {
141-
bindValues.push({
142-
id: 'dc.type',
143-
value: value,
144-
});
145-
});
146-
return [{
147-
match: MATCH_VISIBLE,
148-
operator: OR_OPERATOR,
149-
when: bindValues,
150-
}];
151-
}

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

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
DynamicFormControlModel,
1414
DynamicFormControlRelation,
1515
DynamicFormRelationService,
16-
MATCH_DISABLED,
1716
MATCH_ENABLED,
1817
MATCH_VISIBLE,
1918
OR_OPERATOR,
@@ -36,6 +35,48 @@ import { DYNAMIC_FORM_CONTROL_TYPE_RELATION_GROUP } from './ds-dynamic-form-cons
3635
@Injectable({ providedIn: 'root' })
3736
export class DsDynamicTypeBindRelationService {
3837

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+
3980
constructor(@Optional() @Inject(DYNAMIC_MATCHERS) private dynamicMatchers: DynamicFormControlMatcher[],
4081
protected dynamicFormRelationService: DynamicFormRelationService,
4182
protected formBuilderService: FormBuilderService,
@@ -218,30 +259,4 @@ export class DsDynamicTypeBindRelationService {
218259
return subscriptions;
219260
}
220261

221-
/**
222-
* Helper function to construct a typeBindRelations array
223-
* @param configuredTypeBindValues
224-
*/
225-
public getTypeBindRelations(configuredTypeBindValues: string[]): DynamicFormControlRelation[] {
226-
const bindValues = [];
227-
configuredTypeBindValues.forEach((value) => {
228-
bindValues.push({
229-
id: 'dc.type',
230-
value: value,
231-
});
232-
});
233-
return [
234-
{
235-
match: MATCH_ENABLED,
236-
operator: OR_OPERATOR,
237-
when: bindValues,
238-
},
239-
{
240-
match: MATCH_VISIBLE,
241-
operator: OR_OPERATOR,
242-
when: bindValues,
243-
},
244-
];
245-
}
246-
247262
}

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

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@ import {
22
Inject,
33
InjectionToken,
44
} from '@angular/core';
5-
import {
6-
DynamicFormControlLayout,
7-
DynamicFormControlRelation,
8-
MATCH_DISABLED,
9-
MATCH_ENABLED,
10-
MATCH_VISIBLE,
11-
OR_OPERATOR,
12-
} from '@ng-dynamic-forms/core';
5+
import { DynamicFormControlLayout } from '@ng-dynamic-forms/core';
136
import { TranslateService } from '@ngx-translate/core';
147
import uniqueId from 'lodash/uniqueId';
158

@@ -22,6 +15,7 @@ import {
2215
isNotNull,
2316
isNotUndefined,
2417
} from '../../../empty.util';
18+
import { DsDynamicTypeBindRelationService } from '../ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service';
2519
import {
2620
DsDynamicInputModel,
2721
DsDynamicInputModelConfig,
@@ -100,7 +94,7 @@ export abstract class FieldParser {
10094
metadataFields: this.getAllFieldIds(),
10195
hasSelectableMetadata: isNotEmpty(this.configData.selectableMetadata),
10296
isDraggable,
103-
typeBindRelations: isNotEmpty(this.configData.typeBind) ? this.getTypeBindRelations(this.configData.typeBind,
97+
typeBindRelations: isNotEmpty(this.configData.typeBind) ? DsDynamicTypeBindRelationService.getTypeBindRelations(this.configData.typeBind,
10498
this.parserOptions.typeField) : null,
10599
groupFactory: () => {
106100
let model;
@@ -329,7 +323,7 @@ export abstract class FieldParser {
329323

330324
// If typeBind is configured
331325
if (isNotEmpty(this.configData.typeBind)) {
332-
(controlModel as DsDynamicInputModel).typeBindRelations = this.getTypeBindRelations(this.configData.typeBind,
326+
(controlModel as DsDynamicInputModel).typeBindRelations = DsDynamicTypeBindRelationService.getTypeBindRelations(this.configData.typeBind,
333327
this.parserOptions.typeField);
334328
}
335329

@@ -358,45 +352,6 @@ export abstract class FieldParser {
358352
);
359353
}
360354

361-
/**
362-
* Get the type bind values from the REST data for a specific field
363-
* The return value is any[] in the method signature but in reality it's
364-
* returning the 'relation' that'll be used for a dynamic matcher when filtering
365-
* fields in type bind, made up of a 'match' outcome (make this field visible), an 'operator'
366-
* (OR) and a 'when' condition (the bindValues array).
367-
* @param configuredTypeBindValues array of types from the submission definition (CONFIG_DATA)
368-
* @param typeField
369-
* @private
370-
* @return DynamicFormControlRelation[] array with one relation in it, for type bind matching to show a field
371-
*/
372-
private getTypeBindRelations(configuredTypeBindValues: string[], typeField: string): DynamicFormControlRelation[] {
373-
const bindValues = [];
374-
configuredTypeBindValues.forEach((value) => {
375-
bindValues.push({
376-
id: typeField,
377-
value: value,
378-
});
379-
});
380-
// match: MATCH_VISIBLE means that if true, the field / component will be visible
381-
// operator: OR means that all the values in the 'when' condition will be compared with OR, not AND
382-
// when: the list of values to match against, in this case the list of strings from <type-bind>...</type-bind>
383-
// Example: Field [x] will be VISIBLE if item type = book OR item type = book_part
384-
//
385-
// The opposing match value will be the dc.type for the workspace item
386-
return [
387-
{
388-
match: MATCH_ENABLED,
389-
operator: OR_OPERATOR,
390-
when: bindValues,
391-
},
392-
{
393-
match: MATCH_VISIBLE,
394-
operator: OR_OPERATOR,
395-
when: bindValues,
396-
},
397-
];
398-
}
399-
400355
protected hasRegex() {
401356
return hasValue(this.configData.input.regex);
402357
}

0 commit comments

Comments
 (0)