File size: 2,420 Bytes
452f691
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class SettingsView {
    constructor(section) {
        this.section = section;
        this.themeToggle = section.querySelector('[data-theme-toggle]');
        this.marketIntervalInput = section.querySelector('[data-market-interval]');
        this.newsIntervalInput = section.querySelector('[data-news-interval]');
        this.layoutToggle = section.querySelector('[data-layout-toggle]');
    }

    init() {
        this.loadPreferences();
        this.bindEvents();
    }

    loadPreferences() {
        const theme = localStorage.getItem('dashboard-theme') || 'dark';
        document.body.dataset.theme = theme;
        if (this.themeToggle) {
            this.themeToggle.checked = theme === 'light';
        }
        const marketInterval = localStorage.getItem('market-interval') || 60;
        const newsInterval = localStorage.getItem('news-interval') || 120;
        if (this.marketIntervalInput) this.marketIntervalInput.value = marketInterval;
        if (this.newsIntervalInput) this.newsIntervalInput.value = newsInterval;
        const layout = localStorage.getItem('layout-density') || 'spacious';
        document.body.dataset.layout = layout;
        if (this.layoutToggle) {
            this.layoutToggle.checked = layout === 'compact';
        }
    }

    bindEvents() {
        if (this.themeToggle) {
            this.themeToggle.addEventListener('change', () => {
                const theme = this.themeToggle.checked ? 'light' : 'dark';
                document.body.dataset.theme = theme;
                localStorage.setItem('dashboard-theme', theme);
            });
        }
        if (this.marketIntervalInput) {
            this.marketIntervalInput.addEventListener('change', () => {
                localStorage.setItem('market-interval', this.marketIntervalInput.value);
            });
        }
        if (this.newsIntervalInput) {
            this.newsIntervalInput.addEventListener('change', () => {
                localStorage.setItem('news-interval', this.newsIntervalInput.value);
            });
        }
        if (this.layoutToggle) {
            this.layoutToggle.addEventListener('change', () => {
                const layout = this.layoutToggle.checked ? 'compact' : 'spacious';
                document.body.dataset.layout = layout;
                localStorage.setItem('layout-density', layout);
            });
        }
    }
}

export default SettingsView;