-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
255 lines (219 loc) · 7.79 KB
/
app.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
let app = {
init() {
this.setupEventListeners();
this.loadInitialContent();
this.setupRouting();
this.loadUserData();
},
theme: {
init() {
const themeToggle = document.getElementById('themeToggle');
const moonIcon = themeToggle.querySelector('.moon-icon');
const sunIcon = themeToggle.querySelector('.sun-icon');
// Check for saved theme preference or system preference
const savedTheme = localStorage.getItem('theme');
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
// Set initial theme
if (savedTheme === 'dark' || (!savedTheme && prefersDark)) {
document.documentElement.setAttribute('data-theme', 'dark');
moonIcon.classList.add('hidden');
sunIcon.classList.remove('hidden');
}
// Toggle theme
themeToggle.addEventListener('click', () => {
const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
if (isDark) {
document.documentElement.removeAttribute('data-theme');
localStorage.setItem('theme', 'light');
moonIcon.classList.remove('hidden');
sunIcon.classList.add('hidden');
} else {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark');
moonIcon.classList.add('hidden');
sunIcon.classList.remove('hidden');
}
});
}
},
config: {
account: {
id: 1,
name: "Better Living"
},
user: {
id: 1,
name: "Chris Robison",
username: "cdr",
pic: "/img/profilepics/cdr.png"
},
navUrls: {
'overview': '/dashboard.html',
'campaigns': '/browse.html',
'donors': '/donors.html',
'settings': '/settings.html'
},
api: {
baseUrl: 'https://app.thegivehub.com/api',
endpoints: {
campaigns: '/campaign',
donors: '/donors',
user: '/user'
}
}
},
elements: {
menuTrigger: document.getElementById('menuTrigger'),
sidebar: document.getElementById('sidebar'),
overlay: document.getElementById('overlay'),
contentFrame: document.getElementById('contentFrame'),
frameLoader: document.getElementById('frameLoader'),
frameError: document.getElementById('frameError'),
userAvatar: document.querySelector('.user-avatar'),
navLinks: document.querySelectorAll('.nav-link')
},
state: {
loaded: false,
currentPage: null,
sidebarOpen: false,
lastError: null
},
data: {
campaigns: [],
donors: [],
userProfile: null
},
setupEventListeners() {
// Navigation
this.elements.navLinks.forEach(link => {
link.addEventListener('click', (e) => this.handleNavigation(e));
});
// Sidebar
this.elements.menuTrigger.addEventListener('click', () => this.toggleSidebar());
this.elements.overlay.addEventListener('click', () => this.toggleSidebar());
// Iframe events
this.elements.contentFrame.addEventListener('load', () => this.handleFrameLoad());
window.addEventListener('message', (event) => this.handleFrameMessage(event));
// Responsive handling
window.addEventListener('resize', () => this.handleResize());
// Error handling
window.addEventListener('error', (event) => this.handleError(event));
},
setupRouting() {
window.addEventListener('popstate', (event) => {
if (event.state && event.state.url) {
this.loadContent(event.state.url, false);
}
});
},
async loadUserData() {
try {
const response = await fetch(`${this.config.api.baseUrl}${this.config.api.endpoints.user}`);
const userData = await response.json();
this.data.userProfile = userData;
this.updateUserInterface();
} catch (error) {
console.error('Failed to load user data:', error);
}
},
updateUserInterface() {
// Update user avatar
if (this.data.userProfile) {
this.elements.userAvatar.textContent = this.getInitials(this.data.userProfile.name);
}
},
handleNavigation(event) {
event.preventDefault();
const link = event.currentTarget;
this.elements.navLinks.forEach(l => l.classList.remove('active'));
link.classList.add('active');
if (window.innerWidth <= 768) {
this.toggleSidebar(false);
}
const url = link.getAttribute('href');
if (url && url !== '#') {
this.loadContent(url, true);
this.state.currentPage = url;
}
},
handleFrameLoad() {
this.elements.frameLoader.classList.remove('active');
this.state.loaded = true;
try {
// Attempt to communicate with the frame
this.elements.contentFrame.contentWindow.postMessage({
type: 'app-ready',
config: this.config
}, '*');
} catch (error) {
console.warn('Frame communication failed:', error);
}
},
handleFrameMessage(event) {
// Handle messages from iframe content
const { type, data } = event.data;
switch (type) {
case 'content-height':
this.adjustFrameHeight(data.height);
break;
case 'navigation':
this.loadContent(data.url, true);
break;
case 'error':
this.handleError(data.error);
break;
}
},
handleResize() {
if (window.innerWidth > 768 && this.state.sidebarOpen) {
this.toggleSidebar(false);
}
},
handleError(error) {
this.state.lastError = error;
this.elements.frameLoader.classList.remove('active');
this.elements.frameError.classList.add('active');
console.error('Application error:', error);
},
toggleSidebar(show = null) {
const newState = show === null ? !this.state.sidebarOpen : show;
this.state.sidebarOpen = newState;
this.elements.sidebar.classList.toggle('active', newState);
this.elements.overlay.classList.toggle('active', newState);
},
loadContent(url, addToHistory = true) {
this.elements.frameLoader.classList.add('active');
this.elements.frameError.classList.remove('active');
if (addToHistory) {
history.pushState({ url }, '', url);
}
try {
this.elements.contentFrame.src = url;
} catch (error) {
this.handleError(error);
}
},
loadInitialContent() {
const defaultPage = this.config.navUrls.overview;
this.loadContent(defaultPage, true);
},
reloadFrame() {
if (this.elements.contentFrame.src !== 'about:blank') {
this.loadContent(this.elements.contentFrame.src, false);
}
},
// Utility functions
getInitials(name) {
return name
.split(' ')
.map(part => part.charAt(0))
.join('')
.toUpperCase();
},
adjustFrameHeight(height) {
this.elements.contentFrame.style.height = `${height}px`;
}
};
// Initialize the application
window.app = app;
document.addEventListener('DOMContentLoaded', () => app.init());