forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathdate-field-parser.ts
More file actions
42 lines (38 loc) · 1.79 KB
/
date-field-parser.ts
File metadata and controls
42 lines (38 loc) · 1.79 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
import { FormFieldMetadataValueObject } from '@dspace/core/shared/form/models/form-field-metadata-value.model';
import { isNotEmpty } from '@dspace/shared/utils/empty.util';
import { DS_DATE_PICKER_SEPARATOR } from '../ds-dynamic-form-ui/models/date-picker/date-picker.component';
import {
DynamicDsDateControlModelConfig,
DynamicDsDatePickerModel,
} from '../ds-dynamic-form-ui/models/date-picker/date-picker.model';
import { FieldParser } from './field-parser';
export class DateFieldParser extends FieldParser {
public modelFactory(fieldValue?: FormFieldMetadataValueObject, label?: boolean): any {
let malformedDate = false;
const inputDateModelConfig: DynamicDsDateControlModelConfig = this.initModel(null, label, true);
inputDateModelConfig.legend = this.configData.repeatable ? null : this.configData.label;
inputDateModelConfig.disabled = inputDateModelConfig.readOnly;
inputDateModelConfig.toggleIcon = 'fas fa-calendar';
this.setValues(inputDateModelConfig as any, fieldValue);
// Init Data and validity check
if (isNotEmpty(inputDateModelConfig.value)) {
// todo: model value could be object or Date according to its type annotation
// eslint-disable-next-line @typescript-eslint/no-base-to-string
const value = inputDateModelConfig.value.toString();
if (value.length >= 4) {
const valuesArray = value.split(DS_DATE_PICKER_SEPARATOR);
if (valuesArray.length < 4) {
for (let i = 0; i < valuesArray.length; i++) {
const len = i === 0 ? 4 : 2;
if (valuesArray[i].length !== len) {
malformedDate = true;
}
}
}
}
}
const dateModel = new DynamicDsDatePickerModel(inputDateModelConfig);
dateModel.malformedDate = malformedDate;
return dateModel;
}
}