class TopBar extends HTMLElement { connectedCallback() { this.attachShadow({ mode: 'open' }); // Initial Render this.render(); // Bind Events this.bindEvents(); } render() { const isIndic = window.appState.model === 'indictrans2-en-indic-1B'; const isM2M = window.appState.model === 'm2m100-1.2b'; // Generate Options for M2M const m2mOptions = window.LANGUAGES_M2M100.map(l => `` ).join(''); // Generate Options for Indic Target const indicOptions = window.LANGUAGES_INDIC.map(l => `` ).join(''); // Generate Options for M2M Target const m2mTargetOptions = window.LANGUAGES_M2M100.map(l => `` ).join(''); this.shadowRoot.innerHTML = `
Bhaasha - AI Translator
English (Hardcoded)
`; this.replaceIcons(); } bindEvents() { const radios = this.shadowRoot.querySelectorAll('input[name="model"]'); const sourceSelect = this.shadowRoot.getElementById('source-m2m'); const targetSelect = this.shadowRoot.getElementById('target-lang'); const swapBtn = this.shadowRoot.getElementById('swap-btn'); radios.forEach(radio => { radio.addEventListener('change', (e) => { window.appState.model = e.target.value; // Reset langs based on model if (window.appState.model === 'indictrans2-en-indic-1B') { window.appState.sourceLang = 'en'; window.appState.targetLang = 'hin_Deva'; } else { window.appState.sourceLang = 'en'; window.appState.targetLang = window.appState.targetLang || 'es'; // Keep or default } this.render(); // Re-render to show/hide dropdowns this.bindEvents(); // Re-bind events window.dispatchEvent(new CustomEvent('stateChanged', { detail: window.appState })); }); }); if(sourceSelect) { sourceSelect.addEventListener('change', (e) => { window.appState.sourceLang = e.target.value; window.dispatchEvent(new CustomEvent('stateChanged', { detail: window.appState })); }); } if(targetSelect) { targetSelect.addEventListener('change', (e) => { window.appState.targetLang = e.target.value; window.dispatchEvent(new CustomEvent('stateChanged', { detail: window.appState })); }); } if(swapBtn) { swapBtn.addEventListener('click', () => { // Swap values const temp = window.appState.sourceLang; window.appState.sourceLang = window.appState.targetLang; window.appState.targetLang = temp; this.render(); this.bindEvents(); window.dispatchEvent(new CustomEvent('stateChanged', { detail: window.appState })); window.dispatchEvent(new CustomEvent('swapLanguages')); }); } } replaceIcons() { const icons = this.shadowRoot.querySelectorAll('[data-feather]'); icons.forEach(icon => { const name = icon.getAttribute('data-feather'); // Simple SVG replacement for shadow DOM context // Using feather's internal replace logic if available or manually rendering // Feather.replace() doesn't scope to shadow DOM automatically. // We'll rely on inline SVGs in innerHTML for this component to be safe and simple, // or assume we can run feather.replace() on the root if we include the script. // Since innerHTML has raw SVGs now for the logo and swap button, we are good. }); } } customElements.define('top-bar', TopBar);