export class StreamList { constructor() { this.listContainer = document.getElementById('streams-list'); this.streams = []; this.onUseCallback = null; this.expandedIndex = null; } render(streams, onUseCallback) { this.streams = streams; this.onUseCallback = onUseCallback; // Render stream items this.listContainer.innerHTML = streams.map((stream, index) => this.renderItem(stream, index)).join(''); // Attach event listeners this.attachEventListeners(); } renderItem(stream, index) { const icon = this.getStreamIcon(stream.type); const isExpanded = this.expandedIndex === index; const truncatedUrl = this.truncateURL(stream.url, 60); return `
`; } truncateURL(url, maxLength = 60) { if (url.length <= maxLength) { return url; } return url.substring(0, maxLength) + '...'; } getStreamIcon(type) { const icons = { 'FFMPEG': '', 'ONVIF': '', 'JPEG': '', 'MJPEG': '', 'HLS': '', 'HTTP_VIDEO': '' }; return icons[type] || icons['FFMPEG']; } attachEventListeners() { // Click on header to toggle this.listContainer.querySelectorAll('.stream-item-header').forEach(header => { header.addEventListener('click', (e) => { // Don't toggle if clicking "Use Stream" button if (e.target.closest('.btn-use-stream')) { return; } const index = parseInt(header.dataset.index); this.toggleExpand(index); }); }); // Use Stream buttons this.listContainer.querySelectorAll('.btn-use-stream').forEach(btn => { btn.addEventListener('click', (e) => { e.stopPropagation(); // Prevent toggle const index = parseInt(e.target.dataset.index); if (this.onUseCallback) { this.onUseCallback(this.streams[index], index); } }); }); } toggleExpand(index) { if (this.expandedIndex === index) { // Collapse if already expanded this.expandedIndex = null; } else { // Expand new item this.expandedIndex = index; } // Re-render to update state this.render(this.streams, this.onUseCallback); } }