-
Notifications
You must be signed in to change notification settings - Fork 276
/
attachment-unit-form.component.ts
167 lines (142 loc) · 5.11 KB
/
attachment-unit-form.component.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { Component, ElementRef, EventEmitter, Input, OnChanges, OnInit, Output, ViewChild } from '@angular/core';
import dayjs from 'dayjs/esm';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { TranslateService } from '@ngx-translate/core';
import { faQuestionCircle, faTimes } from '@fortawesome/free-solid-svg-icons';
import { FILE_EXTENSIONS } from 'app/shared/constants/file-extensions.constants';
import { Competency } from 'app/entities/competency.model';
import { MAX_FILE_SIZE } from 'app/shared/constants/input.constants';
export interface AttachmentUnitFormData {
formProperties: FormProperties;
fileProperties: FileProperties;
}
// matches structure of the reactive form
export interface FormProperties {
name?: string;
description?: string;
releaseDate?: dayjs.Dayjs;
version?: number;
updateNotificationText?: string;
competencies?: Competency[];
}
// file input is a special case and is not included in the reactive form structure
export interface FileProperties {
file?: File;
fileName?: string;
}
@Component({
selector: 'jhi-attachment-unit-form',
templateUrl: './attachment-unit-form.component.html',
})
export class AttachmentUnitFormComponent implements OnInit, OnChanges {
@Input()
formData: AttachmentUnitFormData;
@Input()
isEditMode = false;
// A human-readable list of allowed file extensions
readonly allowedFileExtensions = FILE_EXTENSIONS.join(', ');
// The list of file extensions for the "accept" attribute of the file input field
readonly acceptedFileExtensionsFileBrowser = FILE_EXTENSIONS.map((ext) => '.' + ext).join(',');
faQuestionCircle = faQuestionCircle;
@Output()
formSubmitted: EventEmitter<AttachmentUnitFormData> = new EventEmitter<AttachmentUnitFormData>();
form: FormGroup;
@Input()
hasCancelButton: boolean;
@Output()
onCancel: EventEmitter<any> = new EventEmitter<any>();
faTimes = faTimes;
// have to handle the file input as a special case at is not part of the reactive form
@ViewChild('fileInput', { static: false })
fileInput: ElementRef;
file: File;
fileName?: string;
fileInputTouched = false;
isFileTooBig: boolean;
constructor(
private translateService: TranslateService,
private fb: FormBuilder,
) {}
ngOnChanges(): void {
this.initializeForm();
if (this.isEditMode && this.formData) {
this.setFormValues(this.formData);
}
}
ngOnInit(): void {
this.initializeForm();
}
private initializeForm() {
if (this.form) {
return;
}
this.form = this.fb.group({
name: [undefined as string | undefined, [Validators.required, Validators.maxLength(255)]],
description: [undefined as string | undefined, [Validators.maxLength(1000)]],
releaseDate: [undefined as dayjs.Dayjs | undefined],
version: [{ value: 1, disabled: true, readOnly: true }],
updateNotificationText: [undefined as string | undefined, [Validators.maxLength(1000)]],
competencies: [undefined as Competency[] | undefined],
});
}
onFileChange(event: Event): void {
const input = event.target as HTMLInputElement;
if (!input.files?.length) {
return;
}
this.file = input.files[0];
this.fileName = this.file.name;
// automatically set the name in case it is not yet specified
if (this.form && (this.nameControl?.value == undefined || this.nameControl?.value == '')) {
this.form.patchValue({
// without extension
name: this.file.name.replace(/\.[^/.]+$/, ''),
});
}
this.isFileTooBig = this.file.size > MAX_FILE_SIZE;
}
get nameControl() {
return this.form.get('name');
}
get descriptionControl() {
return this.form.get('description');
}
get releaseDateControl() {
return this.form.get('releaseDate');
}
get updateNotificationTextControl() {
return this.form.get('updateNotificationText');
}
get versionControl() {
return this.form.get('version');
}
get isSubmitPossible() {
return !(this.form.invalid || !this.fileName) && !this.isFileTooBig;
}
submitForm() {
const formValue = this.form.value;
const formProperties: FormProperties = { ...formValue };
const fileProperties: FileProperties = {
file: this.file,
fileName: this.fileName,
};
this.formSubmitted.emit({
formProperties,
fileProperties,
});
}
private setFormValues(formData: AttachmentUnitFormData) {
if (formData?.formProperties) {
this.form.patchValue(formData.formProperties);
}
if (formData?.fileProperties?.file) {
this.file = formData?.fileProperties?.file;
}
if (formData?.fileProperties?.fileName) {
this.fileName = formData?.fileProperties?.fileName;
}
}
cancelForm() {
this.onCancel.emit();
}
}