Skip to content

Commit 5331b43

Browse files
[DURACOM-152] Fixed read-only visibility for submission form fields
1 parent 829b111 commit 5331b43

10 files changed

Lines changed: 67 additions & 13 deletions

src/app/shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export class DsDynamicInputModel extends DynamicInputModel {
5555
this.metadataFields = config.metadataFields;
5656
this.hint = config.hint;
5757
this.readOnly = config.readOnly;
58+
this.disabled = config.readOnly;
5859
this.value = config.value;
5960
this.relationship = config.relationship;
6061
this.submissionId = config.submissionId;

src/app/shared/form/builder/ds-dynamic-form-ui/models/onebox/dynamic-onebox.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
[ngbTypeahead]="search"
4040
[placeholder]="model.placeholder"
4141
[readonly]="model.readOnly"
42+
[disabled]="model.readOnly"
4243
[resultTemplate]="rt"
4344
[type]="model.inputType"
4445
[(ngModel)]="currentValue"
@@ -63,6 +64,7 @@
6364
[name]="model.name"
6465
[placeholder]="model.placeholder"
6566
[readonly]="true"
67+
[disabled]="model.readOnly"
6668
[type]="model.inputType"
6769
[value]="currentValue?.display"
6870
(focus)="onFocus($event)"

src/app/shared/form/builder/ds-dynamic-form-ui/models/onebox/dynamic-onebox.component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ export class DsDynamicOneboxComponent extends DsDynamicVocabularyComponent imple
216216
* @param event The click event fired
217217
*/
218218
openTree(event) {
219+
if (this.model.readOnly) {
220+
return;
221+
}
219222
event.preventDefault();
220223
event.stopImmediatePropagation();
221224
this.subs.push(this.vocabulary$.pipe(

src/app/shared/form/builder/ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.component.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
role="combobox"
44
[attr.aria-label]="model.label"
55
[attr.aria-owns]="'combobox_' + id + '_listbox'">
6-
<i ngbDropdownToggle class="position-absolute scrollable-dropdown-toggle"
7-
aria-hidden="true"></i>
6+
<i *ngIf="!model.readOnly" ngbDropdownToggle class="position-absolute scrollable-dropdown-toggle"
7+
aria-hidden="true"></i>
8+
<i *ngIf="model.readOnly" class="dropdown-toggle position-absolute toggle-icon"
9+
aria-hidden="true"></i>
810
<input class="form-control"
911
[attr.aria-controls]="'combobox_' + id + '_listbox'"
1012
[attr.aria-activedescendant]="'combobox_' + id + '_selected'"
@@ -15,6 +17,7 @@
1517
[id]="id"
1618
[name]="model.name"
1719
[readonly]="true"
20+
[disabled]="model.readOnly"
1821
[type]="model.inputType"
1922
[value]="(currentValue | async)"
2023
(blur)="onBlur($event)"

src/app/shared/form/builder/ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.component.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@
3232
.scrollable-dropdown-input[readonly]{
3333
background-color: #fff;
3434
}
35+
36+
.toggle-icon {
37+
padding: 0.7rem 0.7rem 0 0.7rem;
38+
}

src/app/shared/form/builder/models/form-field.model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SectionVisibility } from './../../../../submission/objects/section-visibility.model';
12
import { autoserialize } from 'cerialize';
23

34
import { LanguageCode } from './form-field-language-value.model';
@@ -124,4 +125,7 @@ export class FormFieldModel {
124125
*/
125126
@autoserialize
126127
value: any;
128+
129+
@autoserialize
130+
visibility: SectionVisibility;
127131
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
SUBMISSION_ID
2020
} from './field-parser';
2121
import { DsDynamicInputModel, DsDynamicInputModelConfig } from '../ds-dynamic-form-ui/models/ds-dynamic-input.model';
22+
import { VisibilityType } from '../../../../submission/sections/visibility-type';
23+
import isEqual from 'lodash/isEqual';
2224

2325
export class ConcatFieldParser extends FieldParser {
2426

@@ -83,6 +85,10 @@ export class ConcatFieldParser extends FieldParser {
8385
input1ModelConfig.required = true;
8486
}
8587

88+
if (isNotEmpty(this.configData.visibility) && isEqual(this.configData.visibility.main, VisibilityType.READONLY)) {
89+
concatGroup.disabled = true;
90+
}
91+
8692
if (isNotEmpty(this.firstPlaceholder)) {
8793
input1ModelConfig.placeholder = this.firstPlaceholder;
8894
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { SectionVisibility } from './../../../../submission/objects/section-visibility.model';
2+
import { VisibilityType } from './../../../../submission/sections/visibility-type';
13
import { Inject, InjectionToken } from '@angular/core';
24

35
import uniqueId from 'lodash/uniqueId';
@@ -280,8 +282,8 @@ export abstract class FieldParser {
280282
controlModel.id = (this.fieldId).replace(/\./g, '_');
281283

282284
// Set read only option
283-
controlModel.readOnly = this.parserOptions.readOnly;
284-
controlModel.disabled = this.parserOptions.readOnly;
285+
controlModel.readOnly = this.parserOptions.readOnly || this.isFieldReadOnly(this.configData.visibility, this.parserOptions.submissionScope);
286+
controlModel.disabled = controlModel.readOnly;
285287
if (hasValue(this.configData.selectableRelationship)) {
286288
controlModel.relationship = Object.assign(new RelationshipOptions(), this.configData.selectableRelationship);
287289
}
@@ -319,6 +321,17 @@ export abstract class FieldParser {
319321
return controlModel;
320322
}
321323

324+
/**
325+
* Check if a field is read-only with the given scope
326+
* @param visibility
327+
* @param submissionScope
328+
*/
329+
private isFieldReadOnly(visibility: SectionVisibility, submissionScope: string) {
330+
return isNotEmpty(submissionScope)
331+
&& isNotEmpty(visibility)
332+
&& visibility.main === VisibilityType.READONLY;
333+
}
334+
322335
/**
323336
* Get the type bind values from the REST data for a specific field
324337
* The return value is any[] in the method signature but in reality it's

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,20 @@ export class OneboxFieldParser extends FieldParser {
5959
this.setLabel(inputSelectGroup, label);
6060
inputSelectGroup.required = isNotEmpty(this.configData.mandatory);
6161

62+
const inputModelConfig: DsDynamicInputModelConfig = this.initModel(newId + QUALDROP_VALUE_SUFFIX, label, false, false);
63+
inputModelConfig.hint = null;
64+
this.setValues(inputModelConfig, fieldValue);
65+
6266
const selectModelConfig: DynamicSelectModelConfig<any> = this.initModel(newId + QUALDROP_METADATA_SUFFIX, label, false, false);
6367
selectModelConfig.hint = null;
6468
this.setOptions(selectModelConfig);
6569
if (isNotEmpty(fieldValue)) {
6670
selectModelConfig.value = fieldValue.metadata;
6771
}
68-
inputSelectGroup.group.push(new DynamicSelectModel(selectModelConfig, clsSelect));
69-
70-
const inputModelConfig: DsDynamicInputModelConfig = this.initModel(newId + QUALDROP_VALUE_SUFFIX, label, false, false);
71-
inputModelConfig.hint = null;
72-
this.setValues(inputModelConfig, fieldValue);
72+
selectModelConfig.disabled = inputModelConfig.readOnly;
7373
inputSelectGroup.readOnly = selectModelConfig.disabled && inputModelConfig.readOnly;
7474

75+
inputSelectGroup.group.push(new DynamicSelectModel(selectModelConfig, clsSelect));
7576
inputSelectGroup.group.push(new DsDynamicInputModel(inputModelConfig, clsInput));
7677

7778
return new DynamicQualdropModel(inputSelectGroup, clsGroup);

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { SectionVisibility } from './../../../../submission/objects/section-visibility.model';
12
import { Injectable, Injector } from '@angular/core';
23

34
import { DYNAMIC_FORM_CONTROL_TYPE_ARRAY, DynamicFormGroupModelConfig } from '@ng-dynamic-forms/core';
45
import uniqueId from 'lodash/uniqueId';
56

6-
import { isEmpty } from '../../../empty.util';
7+
import { isEmpty, isNotEmpty } from '../../../empty.util';
78
import { DynamicRowGroupModel } from '../ds-dynamic-form-ui/models/ds-dynamic-row-group-model';
89
import { FormFieldModel } from '../models/form-field.model';
910
import { CONFIG_DATA, FieldParser, INIT_FORM_VALUES, PARSER_OPTIONS, SUBMISSION_ID } from './field-parser';
@@ -12,6 +13,7 @@ import { ParserOptions } from './parser-options';
1213
import { ParserType } from './parser-type';
1314
import { setLayout } from './parser.utils';
1415
import { DYNAMIC_FORM_CONTROL_TYPE_RELATION_GROUP } from '../ds-dynamic-form-ui/ds-dynamic-form-constants';
16+
import { VisibilityType } from '../../../../submission/sections/visibility-type';
1517

1618
export const ROW_ID_PREFIX = 'df-row-group-config-';
1719

@@ -118,15 +120,30 @@ export class RowParser {
118120
return parsedResult;
119121
}
120122

121-
checksFieldScope(fieldScope, submissionScope) {
122-
return (isEmpty(fieldScope) || isEmpty(submissionScope) || fieldScope === submissionScope);
123+
checksFieldScope(fieldScope, submissionScope, visibility: SectionVisibility) {
124+
return (isEmpty(fieldScope) || !this.isHidden(visibility, fieldScope, submissionScope));
125+
}
126+
127+
/**
128+
* Check if the field is hidden or not, based on the visibility and the submission scope
129+
* @param visibility The visibility of the field
130+
* @param scope the scope of the field
131+
* @param submissionScope the scope of the submission
132+
* @returns If the field is hidden or not
133+
*/
134+
private isHidden(visibility: SectionVisibility, scope: string, submissionScope: string): boolean {
135+
return isEmpty(visibility)
136+
|| (isNotEmpty(visibility) && visibility.main !== VisibilityType.READONLY)
137+
&& isNotEmpty(submissionScope)
138+
&& (isNotEmpty(scope)
139+
&& scope !== submissionScope);
123140
}
124141

125142
filterScopedFields(fields: FormFieldModel[], submissionScope): FormFieldModel[] {
126143
const filteredFields: FormFieldModel[] = [];
127144
fields.forEach((field: FormFieldModel) => {
128145
// Whether field scope doesn't match the submission scope, skip it
129-
if (this.checksFieldScope(field.scope, submissionScope)) {
146+
if (this.checksFieldScope(field.scope, submissionScope, field.visibility)) {
130147
filteredFields.push(field);
131148
}
132149
});

0 commit comments

Comments
 (0)