|
| 1 | +import { FASTElement, attr, observable } from '@microsoft/fast-element'; |
| 2 | +import { RenderableFASTElement } from '@microsoft/fast-html'; |
| 3 | +import type { CalcState, ButtonDef } from '../modes/engine.js'; |
| 4 | +import { createInitialState, getMode, getModeKeys } from '../modes/engine.js'; |
| 5 | +import '../modes/standard.js'; |
| 6 | +import '../modes/scientific.js'; |
| 7 | + |
| 8 | +interface ButtonData { |
| 9 | + label: string; |
| 10 | + value: string; |
| 11 | + type: string; |
| 12 | + span: string; |
| 13 | +} |
| 14 | + |
| 15 | +export class CalcApp extends RenderableFASTElement(FASTElement) { |
| 16 | + @attr mode!: string; |
| 17 | + @attr({ attribute: 'display-value' }) displayValue!: string; |
| 18 | + @attr expression!: string; |
| 19 | + @attr({ attribute: 'columns' }) gridColumns!: string; |
| 20 | + |
| 21 | + @observable buttons!: ButtonData[]; |
| 22 | + |
| 23 | + private state!: CalcState; |
| 24 | + |
| 25 | + async prepare(): Promise<void> { |
| 26 | + this.mode = this.getAttribute('mode') || 'standard'; |
| 27 | + this.displayValue = this.getAttribute('display-value') || '0'; |
| 28 | + this.expression = this.getAttribute('expression') || ''; |
| 29 | + this.gridColumns = this.getAttribute('columns') || '4'; |
| 30 | + this.state = createInitialState(); |
| 31 | + |
| 32 | + // Read button data from pre-rendered DOM (mirrors todo-fast pattern) |
| 33 | + const buttons: ButtonData[] = []; |
| 34 | + if (this.shadowRoot) { |
| 35 | + for (const el of this.shadowRoot.querySelectorAll('calc-button')) { |
| 36 | + buttons.push({ |
| 37 | + label: el.getAttribute('label') || '', |
| 38 | + value: el.getAttribute('value') || '', |
| 39 | + type: el.getAttribute('btn-type') || '', |
| 40 | + span: el.getAttribute('btn-span') || '1', |
| 41 | + }); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if (buttons.length > 0) { |
| 46 | + this.buttons = buttons; |
| 47 | + } else { |
| 48 | + this.loadButtonsFromEngine(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private boundKeydown = this.onKeydown.bind(this); |
| 53 | + |
| 54 | + connectedCallback(): void { |
| 55 | + super.connectedCallback(); |
| 56 | + document.addEventListener('keydown', this.boundKeydown); |
| 57 | + this.updateActiveModeTab(); |
| 58 | + } |
| 59 | + |
| 60 | + disconnectedCallback(): void { |
| 61 | + super.disconnectedCallback(); |
| 62 | + document.removeEventListener('keydown', this.boundKeydown); |
| 63 | + } |
| 64 | + |
| 65 | + onButtonPress(e: CustomEvent<{ value: string }>): void { |
| 66 | + this.handleInput(e.detail.value); |
| 67 | + } |
| 68 | + |
| 69 | + onModeSelect(e: MouseEvent): void { |
| 70 | + const target = e.composedPath()[0] as HTMLElement; |
| 71 | + const btn = target.closest('[data-mode]') as HTMLElement | null; |
| 72 | + if (!btn) return; |
| 73 | + |
| 74 | + const newMode = btn.getAttribute('data-mode'); |
| 75 | + if (newMode && newMode !== this.mode) { |
| 76 | + this.mode = newMode; |
| 77 | + this.state = { |
| 78 | + ...this.state, |
| 79 | + expression: '', |
| 80 | + resetOnNext: true, |
| 81 | + error: null, |
| 82 | + }; |
| 83 | + this.expression = ''; |
| 84 | + this.loadButtonsFromEngine(); |
| 85 | + this.updateActiveModeTab(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private onKeydown(e: KeyboardEvent): void { |
| 90 | + const keyMap: Record<string, string> = { |
| 91 | + '0': '0', '1': '1', '2': '2', '3': '3', '4': '4', |
| 92 | + '5': '5', '6': '6', '7': '7', '8': '8', '9': '9', |
| 93 | + '.': '.', '+': '+', '-': '-', '*': '*', '/': '/', |
| 94 | + Enter: '=', '=': '=', Escape: 'clear', Backspace: 'clear', |
| 95 | + '%': 'percent', |
| 96 | + }; |
| 97 | + |
| 98 | + const input = keyMap[e.key]; |
| 99 | + if (input) { |
| 100 | + e.preventDefault(); |
| 101 | + this.handleInput(input); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private handleInput(input: string): void { |
| 106 | + const engine = getMode(this.mode); |
| 107 | + if (!engine) return; |
| 108 | + |
| 109 | + this.state = engine.processInput(input, this.state); |
| 110 | + this.displayValue = this.state.display; |
| 111 | + this.expression = this.state.expression; |
| 112 | + } |
| 113 | + |
| 114 | + private loadButtonsFromEngine(): void { |
| 115 | + const engine = getMode(this.mode); |
| 116 | + if (!engine) return; |
| 117 | + |
| 118 | + this.gridColumns = String(engine.columns); |
| 119 | + this.buttons = engine.buttons.map((b: ButtonDef) => ({ |
| 120 | + label: b.label, |
| 121 | + value: b.value, |
| 122 | + type: b.type, |
| 123 | + span: String(b.span ?? 1), |
| 124 | + })); |
| 125 | + } |
| 126 | + |
| 127 | + private updateActiveModeTab(): void { |
| 128 | + if (!this.shadowRoot) return; |
| 129 | + for (const tab of this.shadowRoot.querySelectorAll('[data-mode]')) { |
| 130 | + if (tab.getAttribute('data-mode') === this.mode) { |
| 131 | + tab.setAttribute('data-active', ''); |
| 132 | + } else { |
| 133 | + tab.removeAttribute('data-active'); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +CalcApp.defineAsync({ |
| 140 | + name: 'calc-app', |
| 141 | + templateOptions: 'defer-and-hydrate', |
| 142 | +}); |
0 commit comments