|
| 1 | +import { Component, inject, signal, computed } from '@angular/core'; |
| 2 | +import { CommonModule } from '@angular/common'; |
| 3 | +import { FormsModule } from '@angular/forms'; |
| 4 | +import { |
| 5 | + MAT_DIALOG_DATA, |
| 6 | + MatDialogRef, |
| 7 | + MatDialogModule, |
| 8 | +} from '@angular/material/dialog'; |
| 9 | +import { MatButtonModule } from '@angular/material/button'; |
| 10 | +import { TemperatureWidgetState } from './temperature-widget.component'; |
| 11 | +import { MatFormFieldModule } from '@angular/material/form-field'; |
| 12 | +import { MatSelectModule } from '@angular/material/select'; |
| 13 | +import { MatInputModule } from '@angular/material/input'; |
| 14 | +import { MatSlideToggleModule } from '@angular/material/slide-toggle'; |
| 15 | + |
| 16 | +@Component({ |
| 17 | + selector: 'demo-temperature-state-dialog', |
| 18 | + standalone: true, |
| 19 | + imports: [ |
| 20 | + CommonModule, |
| 21 | + FormsModule, |
| 22 | + MatDialogModule, |
| 23 | + MatButtonModule, |
| 24 | + MatFormFieldModule, |
| 25 | + MatSelectModule, |
| 26 | + MatInputModule, |
| 27 | + MatSlideToggleModule, |
| 28 | + ], |
| 29 | + template: ` |
| 30 | + <h2 mat-dialog-title i18n="@@demo.widgets.temperature.dialog.title"> |
| 31 | + Temperature Settings |
| 32 | + </h2> |
| 33 | + <mat-dialog-content> |
| 34 | + <!-- Temperature Value Input --> |
| 35 | + <mat-form-field appearance="outline" class="temperature-field"> |
| 36 | + <mat-label i18n="@@demo.widgets.temperature.dialog.temperatureValue" |
| 37 | + >Temperature Value (°C)</mat-label |
| 38 | + > |
| 39 | + <input |
| 40 | + matInput |
| 41 | + type="number" |
| 42 | + step="0.1" |
| 43 | + [ngModel]="temperature()" |
| 44 | + (ngModelChange)="onTemperatureChange($event)" |
| 45 | + placeholder="{{ |
| 46 | + temperaturePlaceholder |
| 47 | + }}" |
| 48 | + i18n-placeholder="@@demo.widgets.temperature.dialog.temperatureValuePlaceholder" |
| 49 | + /> |
| 50 | + <mat-hint i18n="@@demo.widgets.temperature.dialog.temperatureHint" |
| 51 | + >Value stored in Celsius and converted for display</mat-hint |
| 52 | + > |
| 53 | + </mat-form-field> |
| 54 | +
|
| 55 | + <!-- Unit Selection --> |
| 56 | + <mat-form-field appearance="outline" class="unit-field"> |
| 57 | + <mat-label i18n="@@demo.widgets.temperature.dialog.unit" |
| 58 | + >Temperature Unit</mat-label |
| 59 | + > |
| 60 | + <mat-select |
| 61 | + [value]="unit()" |
| 62 | + (selectionChange)="unit.set($any($event.value))" |
| 63 | + > |
| 64 | + <mat-option |
| 65 | + value="C" |
| 66 | + i18n="@@demo.widgets.temperature.dialog.unitCelsius" |
| 67 | + >Celsius (°C)</mat-option |
| 68 | + > |
| 69 | + <mat-option |
| 70 | + value="F" |
| 71 | + i18n="@@demo.widgets.temperature.dialog.unitFahrenheit" |
| 72 | + >Fahrenheit (°F)</mat-option |
| 73 | + > |
| 74 | + <mat-option |
| 75 | + value="K" |
| 76 | + i18n="@@demo.widgets.temperature.dialog.unitKelvin" |
| 77 | + >Kelvin (K)</mat-option |
| 78 | + > |
| 79 | + </mat-select> |
| 80 | + </mat-form-field> |
| 81 | +
|
| 82 | + <!-- Label Input --> |
| 83 | + <mat-form-field appearance="outline" class="label-field"> |
| 84 | + <mat-label i18n="@@demo.widgets.temperature.dialog.label" |
| 85 | + >Label (optional)</mat-label |
| 86 | + > |
| 87 | + <input |
| 88 | + matInput |
| 89 | + type="text" |
| 90 | + maxlength="20" |
| 91 | + [(ngModel)]="label" |
| 92 | + placeholder="{{ |
| 93 | + labelPlaceholder |
| 94 | + }}" |
| 95 | + i18n-placeholder="@@demo.widgets.temperature.dialog.labelPlaceholder" |
| 96 | + /> |
| 97 | + </mat-form-field> |
| 98 | +
|
| 99 | + <!-- Background Toggle --> |
| 100 | + <div class="toggle-field"> |
| 101 | + <mat-slide-toggle |
| 102 | + [checked]="hasBackground()" |
| 103 | + (change)="hasBackground.set($event.checked)" |
| 104 | + i18n="@@demo.widgets.temperature.dialog.background" |
| 105 | + > |
| 106 | + Background |
| 107 | + </mat-slide-toggle> |
| 108 | + <span |
| 109 | + class="toggle-hint" |
| 110 | + i18n="@@demo.widgets.temperature.dialog.backgroundDescription" |
| 111 | + >Adds a background behind the temperature</span |
| 112 | + > |
| 113 | + </div> |
| 114 | + </mat-dialog-content> |
| 115 | +
|
| 116 | + <mat-dialog-actions align="end"> |
| 117 | + <button |
| 118 | + mat-button |
| 119 | + (click)="onCancel()" |
| 120 | + i18n="@@demo.common.cancel" |
| 121 | + > |
| 122 | + Cancel |
| 123 | + </button> |
| 124 | + <button |
| 125 | + mat-flat-button |
| 126 | + (click)="save()" |
| 127 | + [disabled]="!hasChanged()" |
| 128 | + i18n="@@demo.common.save" |
| 129 | + > |
| 130 | + Save |
| 131 | + </button> |
| 132 | + </mat-dialog-actions> |
| 133 | + `, |
| 134 | + styles: [ |
| 135 | + ` |
| 136 | + mat-dialog-content { |
| 137 | + display: block; |
| 138 | + overflow-y: auto; |
| 139 | + overflow-x: hidden; |
| 140 | + } |
| 141 | +
|
| 142 | + mat-form-field { |
| 143 | + width: 100%; |
| 144 | + display: block; |
| 145 | + margin-bottom: 1rem; |
| 146 | + } |
| 147 | +
|
| 148 | + .temperature-field { |
| 149 | + margin-top: 1rem; |
| 150 | + } |
| 151 | +
|
| 152 | + /* Toggle section */ |
| 153 | + .toggle-field { |
| 154 | + display: flex; |
| 155 | + align-items: center; |
| 156 | + gap: 0.75rem; |
| 157 | + margin-bottom: 0.5rem; |
| 158 | + } |
| 159 | +
|
| 160 | + .toggle-hint { |
| 161 | + margin: 0; |
| 162 | + } |
| 163 | + `, |
| 164 | + ], |
| 165 | +}) |
| 166 | +export class TemperatureStateDialogComponent { |
| 167 | + private readonly data = inject<TemperatureWidgetState>(MAT_DIALOG_DATA); |
| 168 | + private readonly dialogRef = inject(MatDialogRef<TemperatureStateDialogComponent>); |
| 169 | + |
| 170 | + // Placeholders for i18n extraction |
| 171 | + readonly temperaturePlaceholder = $localize`:@@demo.widgets.temperature.dialog.temperatureValuePlaceholder:Enter temperature`; |
| 172 | + readonly labelPlaceholder = $localize`:@@demo.widgets.temperature.dialog.labelPlaceholder:e.g., Room Temp, CPU`; |
| 173 | + |
| 174 | + // State signals |
| 175 | + readonly temperature = signal<number | null>(this.data.temperature ?? null); |
| 176 | + readonly unit = signal<'C' | 'F' | 'K'>(this.data.unit ?? 'C'); |
| 177 | + readonly label = signal<string>(this.data.label ?? ''); |
| 178 | + readonly hasBackground = signal<boolean>(this.data.hasBackground ?? true); |
| 179 | + |
| 180 | + // Store original values for comparison |
| 181 | + private readonly originalTemperature = this.data.temperature ?? null; |
| 182 | + private readonly originalUnit = this.data.unit ?? 'C'; |
| 183 | + private readonly originalLabel = this.data.label ?? ''; |
| 184 | + private readonly originalHasBackground = this.data.hasBackground ?? true; |
| 185 | + |
| 186 | + // Computed change detection |
| 187 | + readonly hasChanged = computed( |
| 188 | + () => |
| 189 | + this.temperature() !== this.originalTemperature || |
| 190 | + this.unit() !== this.originalUnit || |
| 191 | + this.label() !== this.originalLabel || |
| 192 | + this.hasBackground() !== this.originalHasBackground |
| 193 | + ); |
| 194 | + |
| 195 | + onTemperatureChange(value: string | number | null): void { |
| 196 | + if (value === '' || value === null) { |
| 197 | + this.temperature.set(null); |
| 198 | + } else { |
| 199 | + const numValue = typeof value === 'string' ? parseFloat(value) : value; |
| 200 | + if (!isNaN(numValue)) { |
| 201 | + this.temperature.set(numValue); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + onCancel(): void { |
| 207 | + this.dialogRef.close(); |
| 208 | + } |
| 209 | + |
| 210 | + save(): void { |
| 211 | + this.dialogRef.close({ |
| 212 | + temperature: this.temperature(), |
| 213 | + unit: this.unit(), |
| 214 | + label: this.label(), |
| 215 | + hasBackground: this.hasBackground(), |
| 216 | + } as TemperatureWidgetState); |
| 217 | + } |
| 218 | +} |
0 commit comments