aboutsummaryrefslogtreecommitdiff
path: root/modules/core/module-config.js
blob: fb5199e03c697cbad0235d60ba6a1c97dcbc8a62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Extracted from newOS4 index.html — ConfigManagerModule
        class ConfigManagerModule {
            constructor() {
                this.commands = {}; this.terminal = null;
                this.configPrefix = 'webx11_config_';
            }
            getHelpInfo() {
                return {
                    'config': 'config [key] — Show all config or a specific key',
                    'config-set': 'config-set <key> <value> — Set a configuration value',
                    'config-list': 'config-list — List all configuration keys',
                    'config-delete': 'config-delete <key> — Delete a configuration key',
                    'config-export': 'config-export — Export all config as JSON',
                    'config-reset': 'config-reset — Reset all configuration to defaults',
                };
            }
            registerCommands() {
                this.commands['config'] = (args) => this.getConfig(args);
                this.commands['config-set'] = (args) => this.setConfig(args);
                this.commands['config-list'] = () => this.listConfig();
                this.commands['config-delete'] = (args) => this.deleteConfig(args);
                this.commands['config-export'] = () => this.exportConfig();
                this.commands['config-reset'] = () => this.resetConfig();
            }
            getConfig(args) {
                if (!args[0]) {
                    this.terminal.print("Usage: config <key>", "warning"); return;
                }
                const value = localStorage.getItem(this.configPrefix + args[0]);
                if (value) {
                    this.terminal.print(`${args[0]}: ${value}`, "info");
                } else {
                    this.terminal.print(`Key '${args[0]}' not found`, "error");
                }
            }
            setConfig(args) {
                if (args.length < 2) {
                    this.terminal.print("Usage: config-set <key> <value>", "warning"); return;
                }
                const key = args[0], value = args.slice(1).join(' ');
                localStorage.setItem(this.configPrefix + key, value);
                this.terminal.print(`Config saved: ${key} = ${value}`, "success");
            }
            listConfig() {
                const configs = {};
                for (let i = 0; i < localStorage.length; i++) {
                    const key = localStorage.key(i);
                    if (key.startsWith(this.configPrefix)) {
                        configs[key.replace(this.configPrefix, '')] = localStorage.getItem(key);
                    }
                }
                if (Object.keys(configs).length === 0) {
                    this.terminal.print("No configuration saved", "info"); return;
                }
                this.terminal.print("=== Configuration ===", "info");
                Object.entries(configs).forEach(([k, v]) => { this.terminal.print(`${k}: ${v}`); });
            }
            deleteConfig(args) {
                if (!args[0]) {
                    this.terminal.print("Usage: config-delete <key>", "warning"); return;
                }
                localStorage.removeItem(this.configPrefix + args[0]);
                this.terminal.print(`Config '${args[0]}' deleted`, "success");
            }
            exportConfig() {
                const configs = {};
                for (let i = 0; i < localStorage.length; i++) {
                    const key = localStorage.key(i);
                    if (key.startsWith(this.configPrefix)) {
                        configs[key] = localStorage.getItem(key);
                    }
                }
                const json = JSON.stringify(configs, null, 2);
                const blob = new Blob([json], { type: 'application/json' });
                const url = URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url; a.download = 'webx11-config.json'; a.click();
                URL.revokeObjectURL(url);
                this.terminal.print("Configuration exported", "success");
            }
            resetConfig() {
                if (confirm("Reset all configuration to defaults?")) {
                    const keysToRemove = [];
                    for (let i = 0; i < localStorage.length; i++) {
                        const key = localStorage.key(i);
                        if (key.startsWith(this.configPrefix)) { keysToRemove.push(key); }
                    }
                    keysToRemove.forEach(k => localStorage.removeItem(k));
                    this.terminal.print("Configuration reset to defaults", "success");
                }
            }
        }