ciyidogan commited on
Commit
23aadc9
·
verified ·
1 Parent(s): cf134f3

Upload caption-dialog.component.ts

Browse files
flare-ui/src/app/dialogs/caption-dialog/caption-dialog.component.ts ADDED
@@ -0,0 +1,240 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Component, Inject, OnInit } from '@angular/core';
2
+ import { CommonModule } from '@angular/common';
3
+ import { FormBuilder, FormGroup, FormArray, Validators, ReactiveFormsModule } from '@angular/forms';
4
+ import { MatDialogRef, MAT_DIALOG_DATA, MatDialogModule } from '@angular/material/dialog';
5
+ import { MatFormFieldModule } from '@angular/material/form-field';
6
+ import { MatInputModule } from '@angular/material/input';
7
+ import { MatButtonModule } from '@angular/material/button';
8
+ import { MatIconModule } from '@angular/material/icon';
9
+ import { MatListModule } from '@angular/material/list';
10
+ import { MatChipsModule } from '@angular/material/chips';
11
+
12
+ interface LocalizedCaption {
13
+ locale_code: string;
14
+ caption: string;
15
+ }
16
+
17
+ @Component({
18
+ selector: 'app-caption-dialog',
19
+ standalone: true,
20
+ imports: [
21
+ CommonModule,
22
+ ReactiveFormsModule,
23
+ MatDialogModule,
24
+ MatFormFieldModule,
25
+ MatInputModule,
26
+ MatButtonModule,
27
+ MatIconModule,
28
+ MatListModule,
29
+ MatChipsModule
30
+ ],
31
+ template: `
32
+ <h2 mat-dialog-title>Manage Captions</h2>
33
+
34
+ <mat-dialog-content>
35
+ <p class="hint">Add captions for different languages. The default locale ({{ getLocaleName(data.defaultLocale) }}) is required.</p>
36
+
37
+ <form [formGroup]="form">
38
+ <div formArrayName="captions" class="captions-list">
39
+ <div *ngFor="let caption of captions.controls; let i = index"
40
+ [formGroupName]="i"
41
+ class="caption-row">
42
+
43
+ <mat-chip-listbox class="locale-chip" [disabled]="true">
44
+ <mat-chip-option [selected]="true">
45
+ {{ getLocaleName(caption.get('locale_code')?.value) }}
46
+ </mat-chip-option>
47
+ </mat-chip-listbox>
48
+
49
+ <mat-form-field appearance="outline" class="caption-field">
50
+ <mat-label>Caption</mat-label>
51
+ <input matInput formControlName="caption"
52
+ [placeholder]="'Caption in ' + getLocaleName(caption.get('locale_code')?.value)">
53
+ <mat-error *ngIf="caption.get('caption')?.hasError('required')">
54
+ Caption is required
55
+ </mat-error>
56
+ </mat-form-field>
57
+
58
+ <button mat-icon-button color="warn"
59
+ (click)="removeCaption(i)"
60
+ [disabled]="caption.get('locale_code')?.value === data.defaultLocale && captions.length === 1"
61
+ class="remove-button">
62
+ <mat-icon>delete</mat-icon>
63
+ </button>
64
+ </div>
65
+ </div>
66
+
67
+ <div class="add-section" *ngIf="availableLocales.length > 0">
68
+ <button mat-stroked-button (click)="addCaption()">
69
+ <mat-icon>add</mat-icon>
70
+ Add Caption for Another Language
71
+ </button>
72
+
73
+ <mat-chip-listbox class="available-locales">
74
+ <mat-chip-option *ngFor="let locale of availableLocales"
75
+ [value]="locale"
76
+ (click)="addCaptionForLocale(locale)">
77
+ {{ getLocaleName(locale) }}
78
+ </mat-chip-option>
79
+ </mat-chip-listbox>
80
+ </div>
81
+ </form>
82
+ </mat-dialog-content>
83
+
84
+ <mat-dialog-actions align="end">
85
+ <button mat-button (click)="cancel()">Cancel</button>
86
+ <button mat-raised-button color="primary"
87
+ (click)="save()"
88
+ [disabled]="!form.valid">
89
+ Save
90
+ </button>
91
+ </mat-dialog-actions>
92
+ `,
93
+ styles: [`
94
+ mat-dialog-content {
95
+ min-width: 500px;
96
+ max-width: 600px;
97
+ }
98
+
99
+ .hint {
100
+ color: rgba(0, 0, 0, 0.6);
101
+ font-size: 14px;
102
+ margin-bottom: 16px;
103
+ }
104
+
105
+ .captions-list {
106
+ margin-bottom: 24px;
107
+ }
108
+
109
+ .caption-row {
110
+ display: flex;
111
+ align-items: flex-start;
112
+ gap: 16px;
113
+ margin-bottom: 16px;
114
+ }
115
+
116
+ .locale-chip {
117
+ flex: 0 0 120px;
118
+ margin-top: 8px;
119
+ }
120
+
121
+ .caption-field {
122
+ flex: 1;
123
+ }
124
+
125
+ .remove-button {
126
+ margin-top: 8px;
127
+ }
128
+
129
+ .add-section {
130
+ border-top: 1px solid #e0e0e0;
131
+ padding-top: 16px;
132
+ }
133
+
134
+ .available-locales {
135
+ margin-top: 16px;
136
+ }
137
+ `]
138
+ })
139
+ export default class CaptionDialogComponent implements OnInit {
140
+ form!: FormGroup;
141
+ availableLocales: string[] = [];
142
+
143
+ constructor(
144
+ private fb: FormBuilder,
145
+ public dialogRef: MatDialogRef<CaptionDialogComponent>,
146
+ @Inject(MAT_DIALOG_DATA) public data: {
147
+ captions: LocalizedCaption[];
148
+ supportedLocales: string[];
149
+ defaultLocale: string;
150
+ }
151
+ ) {}
152
+
153
+ ngOnInit() {
154
+ this.initializeForm();
155
+ this.updateAvailableLocales();
156
+ }
157
+
158
+ initializeForm() {
159
+ this.form = this.fb.group({
160
+ captions: this.fb.array([])
161
+ });
162
+
163
+ // Add existing captions
164
+ if (this.data.captions && this.data.captions.length > 0) {
165
+ this.data.captions.forEach(caption => {
166
+ this.addCaptionFormGroup(caption);
167
+ });
168
+ } else {
169
+ // Add default locale caption if no captions exist
170
+ this.addCaptionFormGroup({
171
+ locale_code: this.data.defaultLocale,
172
+ caption: ''
173
+ });
174
+ }
175
+ }
176
+
177
+ get captions() {
178
+ return this.form.get('captions') as FormArray;
179
+ }
180
+
181
+ addCaptionFormGroup(caption: LocalizedCaption) {
182
+ const group = this.fb.group({
183
+ locale_code: [caption.locale_code, Validators.required],
184
+ caption: [caption.caption, Validators.required]
185
+ });
186
+ this.captions.push(group);
187
+ }
188
+
189
+ addCaption() {
190
+ if (this.availableLocales.length > 0) {
191
+ this.addCaptionForLocale(this.availableLocales[0]);
192
+ }
193
+ }
194
+
195
+ addCaptionForLocale(locale: string) {
196
+ this.addCaptionFormGroup({
197
+ locale_code: locale,
198
+ caption: ''
199
+ });
200
+ this.updateAvailableLocales();
201
+ }
202
+
203
+ removeCaption(index: number) {
204
+ this.captions.removeAt(index);
205
+ this.updateAvailableLocales();
206
+ }
207
+
208
+ updateAvailableLocales() {
209
+ const usedLocales = this.captions.controls.map(c => c.get('locale_code')?.value);
210
+ this.availableLocales = this.data.supportedLocales.filter(
211
+ locale => !usedLocales.includes(locale)
212
+ );
213
+ }
214
+
215
+ getLocaleName(localeCode: string): string {
216
+ const localeNames: { [key: string]: string } = {
217
+ 'tr': 'Türkçe',
218
+ 'en': 'English',
219
+ 'de': 'Deutsch',
220
+ 'fr': 'Français',
221
+ 'es': 'Español',
222
+ 'ar': 'العربية',
223
+ 'ru': 'Русский',
224
+ 'zh': '中文',
225
+ 'ja': '日本語',
226
+ 'ko': '한국어'
227
+ };
228
+ return localeNames[localeCode] || localeCode;
229
+ }
230
+
231
+ save() {
232
+ if (this.form.valid) {
233
+ this.dialogRef.close(this.form.value.captions);
234
+ }
235
+ }
236
+
237
+ cancel() {
238
+ this.dialogRef.close();
239
+ }
240
+ }