All Downloads are FREE. Search and download functionalities are using the official Maven repository.

application.main.js Maven / Gradle / Ivy

There is a newer version: 1.2.21
Show newest version
import {connect} from './chat.js';
import {applyToAllSvg, getSessionId, refreshReplyForms, refreshVerbose} from './functions.js';
import {updateTabs} from './tabs.js';
import {setupUIHandlers} from './uiHandlers.js';
import {onWebSocketText} from './messageHandling.js';
import {setupFormSubmit, setupMessageInput, setupUserInfo} from './uiSetup.js';
import {fetchAppConfig} from './appConfig.js';

console.log('Main script started');

function debounce(func, wait) {
    let timeout;
    return function executedFunction(...args) {
        const later = () => {
            clearTimeout(timeout);
            func(...args);
        };
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
    };
}

const updateDocumentComponents = debounce(function () {
    try {
        updateTabs();
    } catch (e) {
        console.error("Error updating tabs:", e);
    }
    try {
        if (typeof Prism !== 'undefined') Prism.highlightAll();
    } catch (e) {
        console.error("Error highlighting code:", e);
    }
    try {
        refreshVerbose();
    } catch (e) {
        console.error("Error refreshing verbose:", e);
    }
    try {
        refreshReplyForms()
    } catch (e) {
        console.error("Error refreshing reply forms:", e);
    }
    try {
        if (typeof mermaid !== 'undefined') {
            const mermaidDiagrams = document.querySelectorAll('.mermaid:not(.mermaid-processed)');
            if (mermaidDiagrams.length > 0) {
                mermaid.run();
                mermaidDiagrams.forEach(diagram => diagram.classList.add('mermaid-processed'));
            }
        }
    } catch (e) {
        console.error("Error running mermaid:", e);
    }
    // try {
    //     applyToAllSvg();
    // } catch (e) {
    //     console.error("Error applying SVG pan zoom:", e);
    // }
}, 250);

document.addEventListener('DOMContentLoaded', () => {
    console.log('DOM content loaded');
    if (typeof mermaid !== 'undefined') mermaid.run();

    updateTabs();
    setupUIHandlers();

    const loginLink = document.getElementById('login');
    const usernameLink = document.getElementById('username');
    const userSettingsLink = document.getElementById('user-settings');
    const userUsageLink = document.getElementById('user-usage');
    const logoutLink = document.getElementById('logout');
    const form = document.getElementById('main-input');
    const messageInput = document.getElementById('chat-input');
    const sessionId = getSessionId();
    const messages = document.getElementById('messages');

    if (sessionId) {
        console.log(`Connecting with session ID: ${sessionId}`);
        connect(sessionId, (event) => onWebSocketText(event, messages, updateDocumentComponents));
    } else {
        console.log('Connecting without session ID');
        connect(undefined, (event) => onWebSocketText(event, messages, updateDocumentComponents));
    }

    setupMessageInput(form, messageInput);
    setupFormSubmit(form, messageInput);
    setupUserInfo(loginLink, usernameLink, userSettingsLink, userUsageLink, logoutLink);

    fetchAppConfig(sessionId)
        .then(({singleInput, stickyInput, loadImages, showMenubar}) => {
            // Use the config values as needed
            console.log('App config loaded:', {singleInput, stickyInput, loadImages, showMenubar});
        })
        .catch(error => {
            console.error('There was a problem with the fetch operation:', error);
        });

    updateTabs();

    document.querySelectorAll('.tabs-container').forEach(tabsContainer => {
        console.log('Restoring tabs for container:', tabsContainer.id);
        const savedTab = localStorage.getItem(`selectedTab_${tabsContainer.id}`);
        if (savedTab) {
            const savedButton = tabsContainer.querySelector(`.tab-button[data-for-tab="${savedTab}"]`);
            console.log('Main script finished loading');
            if (savedButton) {
                savedButton.click();
                console.log(`Restored saved tab: ${savedTab}`);
            }
        }
    });
});




© 2015 - 2024 Weber Informatics LLC | Privacy Policy