/** * Dock Desktop Integration - Fixed Version * Direct approach to integrate the dock with desktop environment */ const DockDesktopIntegration = (function() { // Private state const state = { initialized: false, dockButtons: [] }; // Create desktop dock const createDesktopDock = function() { console.log("Creating desktop dock..."); // Find all dock buttons in the original dock const originalButtons = document.querySelectorAll('.dock-button'); if (!originalButtons || originalButtons.length === 0) { console.warn('No dock buttons found'); return null; } // Store button references state.dockButtons = Array.from(originalButtons); console.log(`Found ${state.dockButtons.length} dock buttons`); // Create desktop dock container const desktopDock = document.createElement('div'); desktopDock.id = 'desktop-dock'; desktopDock.className = 'desktop-dock'; // Clone dock buttons state.dockButtons.forEach((button, index) => { // Clone the button const clonedButton = button.cloneNode(true); // Ensure it has an ID if (!clonedButton.id) { clonedButton.id = `desktop-dock-btn-${index}`; } // Store the original button's ID or data const buttonId = button.id || `button-${index}`; const buttonTitle = button.getAttribute('title') || button.textContent || `Module ${index + 1}`; const moduleName = buttonId.replace('-dock-btn', ''); clonedButton.setAttribute('data-original-id', buttonId); clonedButton.setAttribute('data-module-name', moduleName); // Add click handler clonedButton.addEventListener('click', function(e) { e.preventDefault(); e.stopPropagation(); handleDockButtonClick(clonedButton, buttonTitle, moduleName); }); desktopDock.appendChild(clonedButton); console.log(`Added button for module: ${moduleName}`); }); return desktopDock; }; // Direct override of togglePanel function const overrideTogglePanel = function() { console.log("Overriding togglePanel function..."); // Check if window.togglePanel exists if (window.togglePanel && typeof window.togglePanel === 'function') { console.log("Found existing togglePanel function, saving reference"); // Store the original function window._originalTogglePanel = window.togglePanel; // Override with our version window.togglePanel = function(panelId) { // In desktop mode, handle with desktop system if (document.getElementById('desktop-container') !== null) { console.log(`togglePanel called in desktop mode for: ${panelId}`); openModuleInDesktop(panelId); } else { // Use original function in normal mode console.log(`Using original togglePanel for: ${panelId}`); window._originalTogglePanel(panelId); } }; return true; } else { console.warn("togglePanel function not found"); return false; } }; // Handle dock button clicks const handleDockButtonClick = function(buttonElement, title, moduleName) { console.log(`Dock button clicked: ${moduleName}`); openModuleInDesktop(moduleName, title); }; // Open module in desktop window const openModuleInDesktop = function(moduleName, customTitle) { console.log(`Opening module in desktop: ${moduleName}`); // Normalize moduleName (remove any -panel suffix) moduleName = moduleName.replace('-panel', ''); // Default title based on module name let title = customTitle || moduleName.charAt(0).toUpperCase() + moduleName.slice(1).replace(/-/g, ' '); // Find if window already exists const existingWindows = document.querySelectorAll('.window-container'); let windowExists = false; existingWindows.forEach(win => { if (win.getAttribute('data-module') === moduleName) { console.log(`Window for ${moduleName} already exists, activating it`); if (window.DesktopIntegration && window.DesktopIntegration.windowManager) { window.DesktopIntegration.windowManager.activateWindow(win); windowExists = true; } } }); if (windowExists) return; // Create content for window let content; // Look for existing panel first const existingPanel = document.getElementById(`${moduleName}-panel`); if (existingPanel) { console.log(`Found existing panel for ${moduleName}, cloning content`); content = existingPanel.cloneNode(true); content.style.display = 'block'; // Ensure it's visible content.classList.remove('hidden'); } else { // Create new content if no panel exists console.log(`No existing panel found for ${moduleName}, creating new content`); content = createDefaultContent(moduleName, title); } // Special handling for WordPress connector if (moduleName === 'wordpress-connector') { title = 'WordPress Connector'; content = createWordPressConnectorContent(); } // Create window if DesktopIntegration is available if (window.DesktopIntegration && window.DesktopIntegration.createWindow) { console.log(`Creating window for ${moduleName}`); const win = window.DesktopIntegration.createWindow(title, content, { width: 800, height: 600, x: 50 + Math.random() * 100, y: 50 + Math.random() * 100 }); // Store module name in window element if (win) { win.setAttribute('data-module', moduleName); // Initialize WordPress connector if needed if (moduleName === 'wordpress-connector') { initializeWordPressConnector(win); } } } else { console.error("DesktopIntegration not available"); } }; // Create WordPress connector content const createWordPressConnectorContent = function() { const container = document.createElement('div'); container.id = 'wp-connector-container'; container.className = 'module-container wp-connector-container'; container.innerHTML = `
Module: ${title}
This module is initializing...