class WeatherWidget extends HTMLElement { constructor() { super(); this.state = window.WeatherState; this.attachShadow({ mode: 'open' }); } connectedCallback() { this.render(); this.state.addListener(() => this.render()); this.setupEventListeners(); } setupEventListeners() { this.shadowRoot.addEventListener('click', (e) => { if (e.target.closest('[data-action="search"]')) { this.handleSearch(); } if (e.target.closest('[data-action="refresh"]')) { window.loadCurrentLocationWeather(); } if (e.target.closest('[data-action="location"]')) { this.toggleLocationSearch(); } }); this.shadowRoot.addEventListener('input', (e) => { if (e.target.name === 'location-search') { this.handleSearchInput(e.target.value); } }); this.shadowRoot.addEventListener('keypress', (e) => { if (e.key === 'Enter' && e.target.name === 'location-search') { this.handleSearch(); } }); } handleSearchInput(value) { const searchBtn = this.shadowRoot.querySelector('[data-action="search"]'); searchBtn.disabled = !value.trim(); } handleSearch() { const input = this.shadowRoot.querySelector('[name="location-search"]'); if (input.value.trim()) { window.searchLocation(input.value.trim()); input.value = ''; this.toggleLocationSearch(); } } toggleLocationSearch() { const searchContainer = this.shadowRoot.querySelector('.location-search-container'); searchContainer.classList.toggle('hidden'); if (!searchContainer.classList.contains('hidden')) { const input = this.shadowRoot.querySelector('[name="location-search"]'); input.focus(); } } render() { this.shadowRoot.innerHTML = `
${this.state.currentLocation}