// // SPDX-FileCopyrightText: Hadad // SPDX-License-Identifier: Apache-2.0 // (function () { 'use strict'; var ws = null; var sessionId = ''; var reconnectTimer = null; var isConnecting = false; var currentImages = []; if (document && document.body) { sessionId = document.body.dataset.sessionId || ''; } var connectWebSocket = function() { if (isConnecting || (ws && ws.readyState === 1)) return; isConnecting = true; var protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; var wsUrl = protocol + '//' + window.location.host; ws = new WebSocket(wsUrl); ws.onopen = function() { isConnecting = false; if (reconnectTimer) { clearTimeout(reconnectTimer); reconnectTimer = null; } ws.send(JSON.stringify({ type: 'register', sessionId: sessionId })); }; ws.onmessage = function(event) { try { var data = JSON.parse(event.data); if (!data.sessionId || data.sessionId !== sessionId) { return; } handleWebSocketMessage(data); } catch (e) {} }; ws.onclose = function() { isConnecting = false; reconnectWebSocket(); }; ws.onerror = function() { isConnecting = false; if (ws) ws.close(); }; }; var reconnectWebSocket = function() { if (reconnectTimer) return; reconnectTimer = setTimeout(function() { reconnectTimer = null; connectWebSocket(); }, 1000); }; var handleWebSocketMessage = function(data) { if (!data || !data.type) return; var handlers = { 'progressUpdate': function() { updateProgressUI(data.progress); }, 'generationStarted': showGeneratingUI, 'generationComplete': function() { handleGenerationComplete(data.images); }, 'generationError': function() { handleGenerationError(data.error); }, 'generationCancelled': handleGenerationCancelled, 'imageDeleted': function() { handleImageDeleted(data.images); } }; if (handlers[data.type]) { handlers[data.type](); } }; var updateProgressUI = function(progress) { var progressFill = document.querySelector( '.progress-fill' ); var progressText = document.querySelector( '.progress-text' ); if (progressFill) { progressFill.style.width = progress + '%'; } if (progressText) { progressText.textContent = Math.floor(progress) + '% Complete'; } }; var toggleFormInputs = function(disabled) { var form = document.getElementById('generateForm'); var inputs = form ? form.querySelectorAll( 'input, select, textarea' ) : []; Array.prototype.forEach.call(inputs, function(input) { input.disabled = disabled; }); }; var showGeneratingUI = function() { var outputSection = document.querySelector( '.image-output-section' ); toggleFormInputs(true); if (outputSection) { outputSection.classList.remove('has-images'); outputSection.innerHTML = [ '
', '
', '

', 'Generating your image...

', '
', '
', '
', '

0% Complete

', '
' ].join(''); } updateButtonsForGeneration(true); }; var hideGeneratingUI = function() { toggleFormInputs(false); updateButtonsForGeneration(false); if (window.validateInputs) { window.validateInputs(); } }; var resetToInitialState = function() { hideGeneratingUI(); if (currentImages && currentImages.length > 0) { displayImages(currentImages); } else { showPlaceholder(); } }; var showPlaceholder = function() { var outputSection = document.querySelector( '.image-output-section' ); if (!outputSection) return; outputSection.classList.remove('has-images'); outputSection.innerHTML = [ '', '', '', '', '', '

', 'No images generated yet. ', 'Start creating amazing visuals!', '

' ].join(''); }; var createButton = function(type, isGenerating) { var icons = { stop: '', play: '' }; if (isGenerating) { return [ '' ].join(''); } return [ '' ].join(''); }; var updateButtonsForGeneration = function(isGenerating) { var buttonsContainer = document.querySelector( '.flex.justify-center.gap-4' ); if (!buttonsContainer) return; buttonsContainer.innerHTML = createButton( isGenerating ? 'stop' : 'play', isGenerating ); }; var handleGenerationComplete = function(images) { currentImages = images || []; hideGeneratingUI(); displayImages(currentImages); }; var handleGenerationError = function(error) { resetToInitialState(); showErrorModal(error); }; var handleGenerationCancelled = function() { resetToInitialState(); }; var handleImageDeleted = function(images) { currentImages = images || []; displayImages(currentImages); }; var createImageCard = function(image, index) { var downloadIcon = [ '', '' ].join(''); var deleteIcon = [ '' ].join(''); return [ '
', '', image.prompt, '', '
', '', '', downloadIcon, '', '', '', '
', '
', '

', image.prompt, '

', '

', '', image.model.toUpperCase(), ' | ', image.size, '

', '
', '
' ].join(''); }; var displayImages = function(images) { var outputSection = document.querySelector( '.image-output-section' ); if (!outputSection) return; if (!images || images.length === 0) { showPlaceholder(); } else { outputSection.classList.add('has-images'); var html = ['
']; images.forEach(function(image, index) { html.push(createImageCard(image, index)); }); html.push('
'); outputSection.innerHTML = html.join(''); } }; var showErrorModal = function(error) { var existingModal = document.getElementById( 'errorModal' ); if (existingModal) existingModal.remove(); var modal = document.createElement('div'); modal.id = 'errorModal'; modal.className = 'modal-overlay'; modal.innerHTML = [ '' ].join(''); document.body.appendChild(modal); }; window.deleteImage = function(index) { var xhr = new XMLHttpRequest(); xhr.open('POST', '/', true); xhr.setRequestHeader( 'Content-Type', 'application/json' ); xhr.onload = function() { try { var response = JSON.parse(xhr.responseText); if (!response.success && response.error) { showErrorModal(response.error); } } catch (e) {} }; xhr.send(JSON.stringify({ action: 'delete', sessionId: sessionId, imageIndex: index })); }; var initializeImages = function() { var outputSection = document.querySelector( '.image-output-section' ); if (!outputSection || !outputSection.classList.contains('has-images')) { return; } var imageCards = outputSection.querySelectorAll( '.image-card' ); if (!imageCards || imageCards.length === 0) return; currentImages = []; imageCards.forEach(function(card) { var img = card.querySelector('img'); var prompt = card.querySelector('.image-prompt'); var model = card.querySelector('.image-model'); var meta = card.querySelector('.image-meta'); if (img && img.src && img.src.includes('base64,')) { var base64 = img.src.split('base64,')[1]; var size = meta ? meta.textContent.split('|')[1] : ''; currentImages.push({ id: 'existing-' + Math.random() .toString(36).substring(2, 15), base64: base64, prompt: prompt ? prompt.textContent : '', model: model ? model.textContent.toLowerCase() : '', size: size ? size.trim() : '' }); } }); }; connectWebSocket(); initializeImages(); })();