import './btn.mjs'; class JukeboxComponent extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }); this.navi = window.navi; this.navi.init(); this.unsubscribeWorld = this.navi.on('world:data', (data) => { console.log(data, this.navi.world.data); this.render() }); this.playlist = []; this.currentTrackIndex = 0; this.bgMusic = null; this.isMuted = false; this.hasInteracted = false; this.isPlaylistMode = false; } disconnectedCallback() { if(this.unsubscribeWorld) this.unsubscribeWorld(); } render() { this.shadowRoot.innerHTML = ` `; this.shadowRoot.getElementById('simple-mute-btn').addEventListener('click', () => this.toggleMute()); this.shadowRoot.getElementById('mute-btn').addEventListener('click', () => this.toggleMute()); this.shadowRoot.getElementById('prev-btn').addEventListener('click', () => this.previousTrack()); this.shadowRoot.getElementById('next-btn').addEventListener('click', () => this.nextTrack()); this.loadMusic(this.navi.world.data.theme.music); } loadMusic(musicConfig) { if(!musicConfig) return; this.isPlaylistMode = Array.isArray(musicConfig); this.playlist = Array.isArray(musicConfig) ? musicConfig : [musicConfig]; this.currentTrackIndex = 0; if (this.isPlaylistMode) { this.shadowRoot.getElementById('simple-mute-btn').classList.add('hidden'); this.shadowRoot.getElementById('playlist-controls').classList.remove('hidden'); } else { this.shadowRoot.getElementById('simple-mute-btn').classList.remove('hidden'); this.shadowRoot.getElementById('playlist-controls').classList.add('hidden'); } this.loadTrack(this.currentTrackIndex); } loadTrack(index) { if (this.bgMusic) { this.bgMusic.pause(); this.bgMusic = null; } if (index >= 0 && index < this.playlist.length) { this.bgMusic = new Audio(this.playlist[index]); this.bgMusic.volume = 0.5; if (this.isPlaylistMode) { this.bgMusic.addEventListener('ended', () => { this.currentTrackIndex = (this.currentTrackIndex + 1) % this.playlist.length; this.loadTrack(this.currentTrackIndex); }); this.updateTrackDisplay(); } else { this.bgMusic.loop = true; } this.setupAutoplayHandler(); if (this.hasInteracted && !this.isMuted) { this.bgMusic.play(); } } } updateTrackDisplay() { const trackName = this.shadowRoot.getElementById('track-name'); const fileName = this.playlist[this.currentTrackIndex].split('/').pop(); const trackNum = String(this.currentTrackIndex + 1).padStart(2, '0'); trackName.textContent = `[${trackNum}] ${fileName}`; } setupAutoplayHandler() { const startMusic = () => { if (!this.hasInteracted && !this.isMuted && this.bgMusic) { this.hasInteracted = true; this.bgMusic.play(); } }; const interactionEvents = ['click', 'keydown', 'touchstart']; interactionEvents.forEach(event => { document.addEventListener(event, startMusic, { once: true }); }); } previousTrack() { if (!this.isPlaylistMode) return; this.currentTrackIndex = (this.currentTrackIndex - 1 + this.playlist.length) % this.playlist.length; this.loadTrack(this.currentTrackIndex); } nextTrack() { if (!this.isPlaylistMode) return; this.currentTrackIndex = (this.currentTrackIndex + 1) % this.playlist.length; this.loadTrack(this.currentTrackIndex); } toggleMute() { this.isMuted = !this.isMuted; const simpleMuteBtn = this.shadowRoot.getElementById('simple-mute-btn'); const muteBtn = this.shadowRoot.getElementById('mute-btn'); const mutedSVG = ''; const unmutedSVG = ''; if (this.bgMusic) { if (this.isMuted) { this.bgMusic.pause(); simpleMuteBtn.innerHTML = mutedSVG; muteBtn.innerHTML = mutedSVG; } else { if (!this.hasInteracted) this.hasInteracted = true; this.bgMusic.play(); simpleMuteBtn.innerHTML = unmutedSVG; muteBtn.innerHTML = unmutedSVG; } } } } customElements.define('jukebox-component', JukeboxComponent);