|
| 1 | +import { |
| 2 | + Component, |
| 3 | + Input, |
| 4 | + Output, |
| 5 | + EventEmitter, |
| 6 | + OnInit, |
| 7 | + OnDestroy, |
| 8 | + OnChanges, |
| 9 | + SimpleChanges, |
| 10 | + ElementRef, |
| 11 | + ViewChild, |
| 12 | + ChangeDetectionStrategy, |
| 13 | + inject, |
| 14 | + effect, |
| 15 | + signal, |
| 16 | +} from '@angular/core'; |
| 17 | +import { Gracket } from '../core/Gracket'; |
| 18 | +import type { GracketOptions, TournamentData } from '../types'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Angular 18+ Component wrapper for Gracket |
| 22 | + * Uses modern Angular features: signals, inject, standalone components |
| 23 | + */ |
| 24 | +@Component({ |
| 25 | + selector: 'gracket', |
| 26 | + standalone: true, |
| 27 | + template: ` |
| 28 | + @if (error()) { |
| 29 | + <div class="gracket-error" [style]="{ color: 'red', padding: '1rem' }"> |
| 30 | + Error initializing Gracket: {{ error()?.message }} |
| 31 | + </div> |
| 32 | + } @else { |
| 33 | + <div #container [class]="className" [ngStyle]="style"></div> |
| 34 | + } |
| 35 | + `, |
| 36 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 37 | +}) |
| 38 | +export class GracketComponent implements OnInit, OnDestroy, OnChanges { |
| 39 | + @ViewChild('container', { static: false }) containerRef?: ElementRef<HTMLDivElement>; |
| 40 | + |
| 41 | + @Input({ required: true }) data!: TournamentData; |
| 42 | + @Input() options?: Omit<GracketOptions, 'src'>; |
| 43 | + @Input() className?: string; |
| 44 | + @Input() style?: Record<string, string>; |
| 45 | + |
| 46 | + @Output() init = new EventEmitter<Gracket>(); |
| 47 | + @Output() errorOccurred = new EventEmitter<Error>(); |
| 48 | + @Output() dataUpdated = new EventEmitter<TournamentData>(); |
| 49 | + |
| 50 | + // Using signals for reactive state - Angular 18+ best practice |
| 51 | + protected error = signal<Error | null>(null); |
| 52 | + private gracketInstance = signal<Gracket | null>(null); |
| 53 | + private elementRef = inject(ElementRef); |
| 54 | + |
| 55 | + ngOnInit(): void { |
| 56 | + // Initialize will happen after view init when container is available |
| 57 | + } |
| 58 | + |
| 59 | + ngAfterViewInit(): void { |
| 60 | + this.initializeGracket(); |
| 61 | + } |
| 62 | + |
| 63 | + ngOnChanges(changes: SimpleChanges): void { |
| 64 | + if (changes['data'] && !changes['data'].firstChange) { |
| 65 | + this.updateData(); |
| 66 | + } |
| 67 | + |
| 68 | + if (changes['options'] && !changes['options'].firstChange) { |
| 69 | + this.reinitializeGracket(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + ngOnDestroy(): void { |
| 74 | + this.destroy(); |
| 75 | + } |
| 76 | + |
| 77 | + private initializeGracket(): void { |
| 78 | + const container = this.containerRef?.nativeElement; |
| 79 | + if (!container || !this.data?.length) return; |
| 80 | + |
| 81 | + try { |
| 82 | + const instance = new Gracket(container, { |
| 83 | + ...this.options, |
| 84 | + src: this.data, |
| 85 | + }); |
| 86 | + |
| 87 | + this.gracketInstance.set(instance); |
| 88 | + this.error.set(null); |
| 89 | + this.init.emit(instance); |
| 90 | + } catch (err) { |
| 91 | + const error = err as Error; |
| 92 | + this.error.set(error); |
| 93 | + this.errorOccurred.emit(error); |
| 94 | + console.error('Gracket initialization error:', err); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private reinitializeGracket(): void { |
| 99 | + this.destroy(); |
| 100 | + this.initializeGracket(); |
| 101 | + } |
| 102 | + |
| 103 | + private updateData(): void { |
| 104 | + const instance = this.gracketInstance(); |
| 105 | + if (instance && this.data?.length) { |
| 106 | + try { |
| 107 | + instance.update(this.data); |
| 108 | + this.error.set(null); |
| 109 | + this.dataUpdated.emit(this.data); |
| 110 | + } catch (err) { |
| 111 | + const error = err as Error; |
| 112 | + this.error.set(error); |
| 113 | + this.errorOccurred.emit(error); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** Update a team's score */ |
| 119 | + public updateScore( |
| 120 | + roundIndex: number, |
| 121 | + gameIndex: number, |
| 122 | + teamIndex: number, |
| 123 | + score: number |
| 124 | + ): void { |
| 125 | + try { |
| 126 | + this.gracketInstance()?.updateScore(roundIndex, gameIndex, teamIndex, score); |
| 127 | + this.error.set(null); |
| 128 | + } catch (err) { |
| 129 | + this.error.set(err as Error); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** Advance to next round */ |
| 134 | + public advanceRound(fromRound?: number): TournamentData | undefined { |
| 135 | + try { |
| 136 | + const result = this.gracketInstance()?.advanceRound(fromRound); |
| 137 | + this.error.set(null); |
| 138 | + return result; |
| 139 | + } catch (err) { |
| 140 | + this.error.set(err as Error); |
| 141 | + return undefined; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /** Get the Gracket instance */ |
| 146 | + public getInstance(): Gracket | null { |
| 147 | + return this.gracketInstance(); |
| 148 | + } |
| 149 | + |
| 150 | + /** Destroy the instance */ |
| 151 | + public destroy(): void { |
| 152 | + const instance = this.gracketInstance(); |
| 153 | + if (instance) { |
| 154 | + instance.destroy(); |
| 155 | + this.gracketInstance.set(null); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * Angular Service for programmatic Gracket control |
| 162 | + * Can be injected into components |
| 163 | + */ |
| 164 | +export class GracketService { |
| 165 | + private instances = new Map<string, Gracket>(); |
| 166 | + |
| 167 | + /** Create a new Gracket instance */ |
| 168 | + create( |
| 169 | + id: string, |
| 170 | + container: HTMLElement, |
| 171 | + data: TournamentData, |
| 172 | + options?: GracketOptions |
| 173 | + ): Gracket { |
| 174 | + // Destroy existing instance if any |
| 175 | + this.destroy(id); |
| 176 | + |
| 177 | + const instance = new Gracket(container, { |
| 178 | + ...options, |
| 179 | + src: data, |
| 180 | + }); |
| 181 | + |
| 182 | + this.instances.set(id, instance); |
| 183 | + return instance; |
| 184 | + } |
| 185 | + |
| 186 | + /** Get an existing instance */ |
| 187 | + get(id: string): Gracket | undefined { |
| 188 | + return this.instances.get(id); |
| 189 | + } |
| 190 | + |
| 191 | + /** Update data for an instance */ |
| 192 | + update(id: string, data: TournamentData): void { |
| 193 | + this.instances.get(id)?.update(data); |
| 194 | + } |
| 195 | + |
| 196 | + /** Update score */ |
| 197 | + updateScore( |
| 198 | + id: string, |
| 199 | + roundIndex: number, |
| 200 | + gameIndex: number, |
| 201 | + teamIndex: number, |
| 202 | + score: number |
| 203 | + ): void { |
| 204 | + this.instances.get(id)?.updateScore(roundIndex, gameIndex, teamIndex, score); |
| 205 | + } |
| 206 | + |
| 207 | + /** Advance round */ |
| 208 | + advanceRound(id: string, fromRound?: number): TournamentData | undefined { |
| 209 | + return this.instances.get(id)?.advanceRound(fromRound); |
| 210 | + } |
| 211 | + |
| 212 | + /** Destroy an instance */ |
| 213 | + destroy(id: string): void { |
| 214 | + const instance = this.instances.get(id); |
| 215 | + if (instance) { |
| 216 | + instance.destroy(); |
| 217 | + this.instances.delete(id); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + /** Destroy all instances */ |
| 222 | + destroyAll(): void { |
| 223 | + this.instances.forEach((instance) => instance.destroy()); |
| 224 | + this.instances.clear(); |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +export default GracketComponent; |
0 commit comments