forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathconcat-field-parser.ts
More file actions
137 lines (116 loc) · 4.25 KB
/
concat-field-parser.ts
File metadata and controls
137 lines (116 loc) · 4.25 KB
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
136
137
import { Inject } from '@angular/core';
import { FormFieldModel } from '@dspace/core/shared/form/models/form-field.model';
import { FormFieldMetadataValueObject } from '@dspace/core/shared/form/models/form-field-metadata-value.model';
import { MetadataSecurityConfiguration } from '@dspace/core/submission/models/metadata-security-configuration';
import {
hasNoValue,
hasValue,
isNotEmpty,
} from '@dspace/shared/utils/empty.util';
import { TranslateService } from '@ngx-translate/core';
import {
CONCAT_FIRST_INPUT_SUFFIX,
CONCAT_GROUP_SUFFIX,
CONCAT_SECOND_INPUT_SUFFIX,
DynamicConcatModel,
DynamicConcatModelConfig,
} from '../ds-dynamic-form-ui/models/ds-dynamic-concat.model';
import {
DsDynamicInputModel,
DsDynamicInputModelConfig,
} from '../ds-dynamic-form-ui/models/ds-dynamic-input.model';
import {
CONFIG_DATA,
FieldParser,
INIT_FORM_VALUES,
PARSER_OPTIONS,
SECURITY_CONFIG,
SUBMISSION_ID,
} from './field-parser';
import { ParserOptions } from './parser-options';
export class ConcatFieldParser extends FieldParser {
constructor(
@Inject(SUBMISSION_ID) submissionId: string,
@Inject(CONFIG_DATA) configData: FormFieldModel,
@Inject(INIT_FORM_VALUES) initFormValues,
@Inject(PARSER_OPTIONS) parserOptions: ParserOptions,
@Inject(SECURITY_CONFIG) securityConfig: MetadataSecurityConfiguration,
protected translate: TranslateService,
protected separator: string,
protected firstPlaceholder: string = null,
protected secondPlaceholder: string = null) {
super(submissionId, configData, initFormValues, parserOptions, securityConfig, translate);
this.separator = separator;
this.firstPlaceholder = firstPlaceholder;
this.secondPlaceholder = secondPlaceholder;
}
public modelFactory(fieldValue?: FormFieldMetadataValueObject, label?: boolean): any {
const id: string = this.configData.selectableMetadata[0].metadata;
const clsInput = {
grid: {
host: 'col-sm-6',
},
};
const groupId = id.replace(/\./g, '_') + CONCAT_GROUP_SUFFIX;
const concatGroup: DynamicConcatModelConfig = this.initModel(groupId, label, false, true);
concatGroup.group = [];
concatGroup.separator = this.separator;
const input1ModelConfig: DsDynamicInputModelConfig = this.initModel(
id + CONCAT_FIRST_INPUT_SUFFIX,
false,
true,
true,
false,
);
const input2ModelConfig: DsDynamicInputModelConfig = this.initModel(
id + CONCAT_SECOND_INPUT_SUFFIX,
false,
true,
true,
false,
);
input1ModelConfig.hideErrorMessages = true;
input2ModelConfig.hideErrorMessages = true;
if (hasNoValue(concatGroup.hint) && hasValue(input1ModelConfig.hint) && hasNoValue(input2ModelConfig.hint)) {
concatGroup.hint = input1ModelConfig.hint;
input1ModelConfig.hint = undefined;
}
if (this.configData.mandatory) {
concatGroup.required = true;
input1ModelConfig.required = true;
}
concatGroup.disabled = input1ModelConfig.readOnly;
if (isNotEmpty(this.firstPlaceholder)) {
input1ModelConfig.label = this.firstPlaceholder;
}
if (isNotEmpty(this.secondPlaceholder)) {
input2ModelConfig.label = this.secondPlaceholder;
}
// Split placeholder if is like 'placeholder1/placeholder2'
const placeholder = this.configData.label.split('/');
if (placeholder.length === 2) {
input1ModelConfig.label = placeholder[0];
input2ModelConfig.label = placeholder[1];
}
const model1 = new DsDynamicInputModel(input1ModelConfig, clsInput);
const model2 = new DsDynamicInputModel(input2ModelConfig, clsInput);
// attach the security config for children
(model1 as any).securityConfigLevel = (concatGroup as any).securityConfigLevel;
(model2 as any).securityConfigLevel = (concatGroup as any).securityConfigLevel;
concatGroup.group.push(model1);
concatGroup.group.push(model2);
const clsGroup = {
element: {
control: 'row',
},
};
this.initSecurityValue(concatGroup, fieldValue as any);
const concatModel = new DynamicConcatModel(concatGroup, clsGroup);
concatModel.name = this.getFieldId();
// Init values
if (isNotEmpty(fieldValue)) {
concatModel.value = fieldValue;
}
return concatModel;
}
}