forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathds-dynamic-complex.model.ts
135 lines (118 loc) · 4.88 KB
/
ds-dynamic-complex.model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { DynamicFormControlLayout } from '@ng-dynamic-forms/core';
import { hasNoValue, hasValue, isNotEmpty } from '../../../../empty.util';
import { DsDynamicInputModel } from './ds-dynamic-input.model';
import { FormFieldMetadataValueObject } from '../../models/form-field-metadata-value.model';
import { DynamicConcatModel, DynamicConcatModelConfig } from './ds-dynamic-concat.model';
import { AUTOCOMPLETE_COMPLEX_PREFIX } from './autocomplete/ds-dynamic-autocomplete.model';
import {
DEFAULT_EU_FUNDING_TYPES,
DEFAULT_MAX_CHARS_TO_AUTOCOMPLETE
} from './sponsor-autocomplete/ds-dynamic-sponsor-autocomplete.model';
export const COMPLEX_GROUP_SUFFIX = '_COMPLEX_GROUP';
export const COMPLEX_INPUT_SUFFIX = '_COMPLEX_INPUT_';
export const SEPARATOR = ';';
export const SPONSOR_METADATA_NAME = 'local.sponsor';
export const EU_PROJECT_PREFIX = 'info:eu-repo';
export const OPENAIRE_INPUT_NAME = 'openaire_id';
/**
* The complex input type `local.sponsor` has `openaire_id` input field hidden if the funding type is not EU.
* This `opeanaire_id` input field is on the index 4.
* Funding type input field is on the index 0.
*/
export const EU_IDENTIFIER_INDEX = 4;
export const FUNDING_TYPE_INDEX = 0;
/**
* Configuration for the DynamicComplexModel.
*/
export interface DynamicComplexModelConfig extends DynamicConcatModelConfig {}
/**
* The model for the Complex input field which consist of multiple input fields.
*/
export class DynamicComplexModel extends DynamicConcatModel {
constructor(config: DynamicComplexModelConfig, layout?: DynamicFormControlLayout) {
super(config, layout);
this.separator = SEPARATOR;
}
get value() {
const formValues = this.group.map((inputModel: DsDynamicInputModel) =>
(typeof inputModel.value === 'string') ?
Object.assign(new FormFieldMetadataValueObject(), { value: inputModel.value, display: inputModel.value }) :
(inputModel.value as any));
let value = '';
let allFormValuesEmpty = true;
formValues.forEach((formValue, index) => {
if (isNotEmpty(formValue) && isNotEmpty(formValue.value)) {
value += formValue.value + this.separator;
allFormValuesEmpty = false;
} else {
value += this.separator;
}
});
// remove last separator in the end of the value
value = value.slice(0, -1);
// `local.sponsor` input type has input value stored in one input field which starts with AUTOCOMPLETE_COMPLEX_PREFIX
if (this.name === SPONSOR_METADATA_NAME) {
formValues.forEach((formValue) => {
if (isNotEmpty(formValue) && isNotEmpty(formValue.value) &&
formValue.value.startsWith(AUTOCOMPLETE_COMPLEX_PREFIX)) {
// remove AUTOCOMPLETE_COMPLEX_PREFIX from the value because it cannot be in the metadata value
value = formValue.value.replace(AUTOCOMPLETE_COMPLEX_PREFIX + SEPARATOR, '');
}
});
}
// set value as empty string otherwise value will be e.g. `;;;;` and it throws error
if (allFormValuesEmpty) {
value = '';
}
if (isNotEmpty(formValues)) {
return Object.assign(new FormFieldMetadataValueObject(),{ value: value });
}
return null;
}
set value(value: string | FormFieldMetadataValueObject) {
let values;
let tempValue: string;
if (typeof value === 'string') {
tempValue = value;
} else {
tempValue = value.value;
}
if (hasNoValue(tempValue)) {
tempValue = '';
}
values = [...tempValue.split(this.separator), null].map((v) => {
return Object.assign(new FormFieldMetadataValueObject(), value, { display: v, value: v });
});
// remove undefined values
values = values.filter(v => v);
// if funding type is `EU`
let isEUFund = false;
values.forEach((val, index) => {
if (val.value) {
// do not set value if it bigger than allowed length
if (this.validateInputLength(val.value)) {
return;
}
(this.get(index) as DsDynamicInputModel).value = val;
// for `local.sponsor` input field
if (this.name === SPONSOR_METADATA_NAME) {
// if funding type is `EU`
if (index === FUNDING_TYPE_INDEX && DEFAULT_EU_FUNDING_TYPES.includes(val.value)) {
isEUFund = true;
}
// if funding type is `EU` and input field is `openaire_id` -> show `openaire_id` readonly input field
if (index === EU_IDENTIFIER_INDEX && isEUFund && val.value.includes(EU_PROJECT_PREFIX)) {
(this.get(EU_IDENTIFIER_INDEX) as DsDynamicInputModel).hidden = false;
} else {
(this.get(EU_IDENTIFIER_INDEX) as DsDynamicInputModel).hidden = true;
}
}
} else if (hasValue((this.get(index) as DsDynamicInputModel))) {
(this.get(index) as DsDynamicInputModel).value = undefined;
}
});
}
private validateInputLength(value) {
return value.length > DEFAULT_MAX_CHARS_TO_AUTOCOMPLETE;
}
}