-
Notifications
You must be signed in to change notification settings - Fork 6
/
multi-language-form.service.ts
140 lines (117 loc) · 4.42 KB
/
multi-language-form.service.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
import { ChangeDetectorRef, Injectable } from '@angular/core';
import { FormBuilder, ValidatorFn } from '@angular/forms';
import { UserSelectors } from '@dasch-swiss/vre/shared/app-state';
import { Store } from '@ngxs/store';
import { DaschLanguage, isDaschLanguage } from './dash-language.type';
import { MultiLanguageFormArray } from './multi-language-form-array.type';
/** Component Provider used in combination with
* MultiLanguageInputComponent and MultiLanguageTextareaComponent.
* Those two components have the same business logic (here), but differ in their html.
*/
@Injectable()
export class MultiLanguageFormService {
readonly availableLanguages: DaschLanguage[] = ['de', 'fr', 'it', 'en', 'rm'];
selectedLanguageIndex: number;
formArray: MultiLanguageFormArray;
validators: ValidatorFn[];
inputValue: string | null = null;
get selectedFormControl() {
const index = this.formArray
.getRawValue()
.findIndex(v => v.language === this.availableLanguages[this.selectedLanguageIndex]);
return this.formArray.controls[index];
}
get selectedLanguageControl() {
return this.selectedFormControl.controls.value;
}
get invalidErrors() {
for (const control of this.formArray.controls) {
if (control.controls.value.errors) {
return { language: control.getRawValue().language, error: control.controls.value.errors };
}
}
if (this.formArray.errors) {
return { error: this.formArray.errors, language: undefined };
}
return undefined;
}
constructor(
private _store: Store,
private _fb: FormBuilder,
private _cd: ChangeDetectorRef
) {}
onInit(formArray: MultiLanguageFormArray, validators: ValidatorFn[]) {
this.formArray = formArray;
this.selectedLanguageIndex = this._setupLanguageIndex();
this.validators = validators;
const newFormControl = this.formArray.controls.find(
control => control.value.language === this.availableLanguages[this.selectedLanguageIndex]
);
this.inputValue = newFormControl ? this.selectedLanguageControl.value : null;
this.formArray.valueChanges.subscribe(array => {
if (array.length === 0) {
this.inputValue = null;
this._cd.detectChanges(); // TODO remove later
}
});
}
onInputChange(newText: string) {
if (newText === '') {
if (this.inputValue && this.inputValue.length > 0) {
this.formArray.removeAt(this.formArray.controls.indexOf(this.selectedFormControl));
this.inputValue = null;
}
return;
}
if (this.inputValue === null) {
this.formArray.push(
this._fb.nonNullable.group({
language: this.availableLanguages[this.selectedLanguageIndex],
value: [newText, this.validators],
})
);
}
this.selectedLanguageControl.setValue(newText);
this.inputValue = newText;
}
getFormControlWithLanguage(lang: string) {
return this.formArray.controls.find(control => control.value.language === lang && control.value.value !== '');
}
changeLanguage(languageIndex: number) {
this.selectedLanguageIndex = languageIndex;
const existingControl = this.formArray.controls.find(
control => control.value.language === this.availableLanguages[this.selectedLanguageIndex]
);
this.inputValue = existingControl ? this.selectedLanguageControl.value : null;
}
private _setupLanguageIndex(): number {
const responseLanguages = this.formArray
.getRawValue()
.map(v => v.language)
.filter(language => this.availableLanguages.includes(language));
const userFavoriteLanguage =
(this._store.selectSnapshot(UserSelectors.language) as string) || navigator.language.substring(0, 2);
if (responseLanguages.length === 0) {
if (!isDaschLanguage(userFavoriteLanguage)) {
return 0;
}
// form is empty, push a new value
const indexFavoriteLanguage = this.availableLanguages.indexOf(userFavoriteLanguage);
// with user favorite language
if (indexFavoriteLanguage !== -1) {
return indexFavoriteLanguage;
// with default language
} else {
return 0;
}
}
if (
isDaschLanguage(userFavoriteLanguage) &&
responseLanguages.includes(userFavoriteLanguage) &&
this.availableLanguages.includes(userFavoriteLanguage)
) {
return this.availableLanguages.indexOf(userFavoriteLanguage);
}
return this.availableLanguages.indexOf(responseLanguages[0]);
}
}