|
| 1 | +// floating-panel-settings.js |
| 2 | +// Version: 1.0 |
| 3 | +// |
| 4 | +// Documentation: |
| 5 | +// This file handles settings persistence and profile management for the floating panel. |
| 6 | +// It includes methods for loading/saving panel settings, debouncing saves, and profile switching. |
| 7 | +// These functions extend the window.MaxExtensionFloatingPanel namespace. |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | + |
| 12 | +/** |
| 13 | + * Debounced version of savePanelSettings. |
| 14 | + * Waits 150ms after the last call before actually saving. |
| 15 | + */ |
| 16 | +window.MaxExtensionFloatingPanel.debouncedSavePanelSettings = function() { |
| 17 | + if (this.savePositionTimer) { |
| 18 | + clearTimeout(this.savePositionTimer); |
| 19 | + } |
| 20 | + this.savePositionTimer = setTimeout(() => { |
| 21 | + this.savePanelSettings(); |
| 22 | + this.savePositionTimer = null; |
| 23 | + }, 150); |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * Loads panel settings from Chrome's storage via the config service worker. |
| 28 | + * If no settings are found, default settings are used. |
| 29 | + */ |
| 30 | +window.MaxExtensionFloatingPanel.loadPanelSettings = function() { |
| 31 | + try { |
| 32 | + // Initialize with default settings immediately to avoid null references. |
| 33 | + this.currentPanelSettings = { ...this.defaultPanelSettings }; |
| 34 | + const hostname = window.location.hostname; |
| 35 | + |
| 36 | + // Request settings from the service worker. |
| 37 | + chrome.runtime.sendMessage({ |
| 38 | + type: 'getFloatingPanelSettings', |
| 39 | + hostname: hostname |
| 40 | + }, (response) => { |
| 41 | + if (response && response.settings) { |
| 42 | + this.currentPanelSettings = response.settings; |
| 43 | + logConCgp('[floating-panel] Loaded panel settings for ' + hostname); |
| 44 | + } else { |
| 45 | + logConCgp('[floating-panel] Using default panel settings for ' + hostname); |
| 46 | + } |
| 47 | + |
| 48 | + // Apply settings to panel if it exists. |
| 49 | + if (this.panelElement) { |
| 50 | + this.updatePanelFromSettings(); |
| 51 | + } |
| 52 | + |
| 53 | + // Restore panel visibility state after settings are loaded. |
| 54 | + this.restorePanelState(); |
| 55 | + }); |
| 56 | + } catch (error) { |
| 57 | + logConCgp('[floating-panel] Error loading panel settings: ' + error.message); |
| 58 | + this.currentPanelSettings = { ...this.defaultPanelSettings }; |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * Saves panel settings to Chrome's storage via the config service worker. |
| 64 | + */ |
| 65 | +window.MaxExtensionFloatingPanel.savePanelSettings = function() { |
| 66 | + try { |
| 67 | + const hostname = window.location.hostname; |
| 68 | + chrome.runtime.sendMessage({ |
| 69 | + type: 'saveFloatingPanelSettings', |
| 70 | + hostname: hostname, |
| 71 | + settings: this.currentPanelSettings |
| 72 | + }, (response) => { |
| 73 | + if (response && response.success) { |
| 74 | + logConCgp('[floating-panel] Saved panel settings for ' + hostname); |
| 75 | + } else { |
| 76 | + logConCgp('[floating-panel] Failed to save panel settings for ' + hostname); |
| 77 | + } |
| 78 | + }); |
| 79 | + } catch (error) { |
| 80 | + logConCgp('[floating-panel] Error saving panel settings: ' + error.message); |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +/** |
| 85 | + * Loads available profiles from the service worker. |
| 86 | + */ |
| 87 | +window.MaxExtensionFloatingPanel.loadAvailableProfiles = function() { |
| 88 | + chrome.runtime.sendMessage( |
| 89 | + { type: 'listProfiles' }, |
| 90 | + (response) => { |
| 91 | + if (response.profiles && Array.isArray(response.profiles)) { |
| 92 | + this.availableProfiles = response.profiles; |
| 93 | + console.log('[floating-panel] Available profiles loaded:', this.availableProfiles); |
| 94 | + // After loading profiles, get the current profile. |
| 95 | + this.getCurrentProfile(); |
| 96 | + } |
| 97 | + } |
| 98 | + ); |
| 99 | +}; |
| 100 | + |
| 101 | +/** |
| 102 | + * Gets the current active profile from the service worker. |
| 103 | + */ |
| 104 | +window.MaxExtensionFloatingPanel.getCurrentProfile = function() { |
| 105 | + chrome.runtime.sendMessage( |
| 106 | + { type: 'getConfig' }, |
| 107 | + (response) => { |
| 108 | + if (response.config) { |
| 109 | + // Retrieve current profile name from storage. |
| 110 | + chrome.storage.local.get(['currentProfile'], (result) => { |
| 111 | + if (result.currentProfile) { |
| 112 | + this.currentProfileName = result.currentProfile; |
| 113 | + console.log('[floating-panel] Current profile:', this.currentProfileName); |
| 114 | + // Update the profile switcher UI. |
| 115 | + this.createProfileSwitcher(); |
| 116 | + } |
| 117 | + }); |
| 118 | + } |
| 119 | + } |
| 120 | + ); |
| 121 | +}; |
| 122 | + |
| 123 | +/** |
| 124 | + * Handles switching to a different profile. |
| 125 | + */ |
| 126 | +window.MaxExtensionFloatingPanel.switchToProfile = function(profileName) { |
| 127 | + // Prevent switching to the same profile. |
| 128 | + if (profileName === this.currentProfileName) return; |
| 129 | + chrome.runtime.sendMessage( |
| 130 | + { type: 'switchProfile', profileName: profileName }, |
| 131 | + (response) => { |
| 132 | + if (response.error) { |
| 133 | + console.error(`[floating-panel] Error switching to profile ${profileName}:`, response.error); |
| 134 | + return; |
| 135 | + } |
| 136 | + if (response.config) { |
| 137 | + // Update the current profile name and global config. |
| 138 | + this.currentProfileName = profileName; |
| 139 | + window.globalMaxExtensionConfig = response.config; |
| 140 | + // Refresh buttons in the panel. |
| 141 | + this.refreshButtonsInPanel(); |
| 142 | + console.log(`[floating-panel] Successfully switched to profile: ${profileName}`); |
| 143 | + } |
| 144 | + } |
| 145 | + ); |
| 146 | +}; |
| 147 | + |
| 148 | +/** |
| 149 | + * Initializes the floating panel functionality. |
| 150 | + * (This method calls both UI creation and settings loading.) |
| 151 | + */ |
| 152 | +window.MaxExtensionFloatingPanel.initialize = function() { |
| 153 | + // Set default panel settings. |
| 154 | + this.currentPanelSettings = { ...this.defaultPanelSettings }; |
| 155 | + // Create the floating panel (UI). |
| 156 | + this.createFloatingPanel(); |
| 157 | + // Load saved settings. |
| 158 | + this.loadPanelSettings(); |
| 159 | + // Load available profiles. |
| 160 | + this.loadAvailableProfiles(); |
| 161 | + logConCgp('[floating-panel] Floating panel initialized.'); |
| 162 | +}; |
0 commit comments