-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme-preview.js
More file actions
137 lines (118 loc) · 4.78 KB
/
Copy paththeme-preview.js
File metadata and controls
137 lines (118 loc) · 4.78 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Theme Preview Component
* A JavaScript component that shows a preview of each theme in the theme selector
*/
class ThemePreview {
constructor(options = {}) {
this.options = Object.assign({
selectorId: 'themeSelector',
previewContainerId: 'themePreviewContainer',
quickSwitchId: 'themeQuickSwitch',
mobileToggleId: 'themeToggleFloating',
mobileSelectorId: 'mobileThemeSelector',
themes: []
}, options);
this.currentTheme = localStorage.getItem('selectedTheme') || (this.options.themes.length > 0 ? this.options.themes[0].name : null);
this.init();
}
init() {
// Initialize theme preview on load
document.addEventListener('DOMContentLoaded', () => {
this.createPreviewElements();
this.bindEvents();
this.updateVisualElements();
});
}
createPreviewElements() {
// Create visual preview elements for themes (if container exists)
const previewContainer = document.getElementById(this.options.previewContainerId);
if (previewContainer) {
previewContainer.innerHTML = '';
this.options.themes.forEach(theme => {
const previewElement = document.createElement('div');
previewElement.className = 'theme-preview-item';
previewElement.setAttribute('data-theme', theme.name);
previewElement.style.backgroundColor = theme['--container-bg'];
previewElement.style.borderColor = theme['--border-color'];
const themeIcon = document.createElement('i');
themeIcon.className = `fas ${theme.icon || 'fa-circle'}`;
themeIcon.style.color = theme['--button-bg'];
const themeName = document.createElement('span');
themeName.textContent = theme.name;
themeName.style.color = theme['--text-color'];
previewElement.appendChild(themeIcon);
previewElement.appendChild(themeName);
previewContainer.appendChild(previewElement);
// Mark active theme
if (theme.name === this.currentTheme) {
previewElement.classList.add('active');
}
});
}
}
bindEvents() {
// Bind quick switch buttons
const quickSwitch = document.getElementById(this.options.quickSwitchId);
if (quickSwitch) {
quickSwitch.addEventListener('click', (e) => {
if (e.target.closest('.theme-quick-btn')) {
const themeBtn = e.target.closest('.theme-quick-btn');
const themeName = themeBtn.getAttribute('data-theme-name');
if (themeName) {
this.changeTheme(themeName);
}
}
});
}
// Bind preview container clicks
const previewContainer = document.getElementById(this.options.previewContainerId);
if (previewContainer) {
previewContainer.addEventListener('click', (e) => {
if (e.target.closest('.theme-preview-item')) {
const themeItem = e.target.closest('.theme-preview-item');
const themeName = themeItem.getAttribute('data-theme');
if (themeName) {
this.changeTheme(themeName);
}
}
});
}
// Mobile theme toggle
const mobileToggle = document.getElementById(this.options.mobileToggleId);
const mobileSelector = document.getElementById(this.options.mobileSelectorId);
if (mobileToggle && mobileSelector) {
mobileToggle.addEventListener('click', () => {
mobileSelector.classList.toggle('show');
});
// Close when clicking outside
document.addEventListener('click', (e) => {
if (!e.target.closest(`#${this.options.mobileToggleId}, #${this.options.mobileSelectorId}`)) {
mobileSelector.classList.remove('show');
}
});
}
}
changeTheme(themeName) {
// This is just a stub - the actual theme change is handled by the main script
// We'll dispatch a custom event that the main script can listen for
const event = new CustomEvent('themeChange', { detail: { themeName } });
document.dispatchEvent(event);
this.currentTheme = themeName;
this.updateVisualElements();
}
updateVisualElements() {
// Update active states in UI
const previewItems = document.querySelectorAll('.theme-preview-item');
previewItems.forEach(item => {
item.classList.toggle('active', item.getAttribute('data-theme') === this.currentTheme);
});
const quickButtons = document.querySelectorAll('.theme-quick-btn');
quickButtons.forEach(btn => {
btn.classList.toggle('active', btn.getAttribute('data-theme-name') === this.currentTheme);
});
}
}
// Usage:
// const themePreview = new ThemePreview({
// themes: themes, // Pass in the themes array from script.js
// });