Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
292 lines (249 loc) · 9.49 KB
/
Copy pathmain.js
File metadata and controls
292 lines (249 loc) · 9.49 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Main application entry point
import { ExamApplication } from './exam.js';
// Initialize the exam application
const examApp = new ExamApplication();
// Make examApp globally available for onclick handlers
window.examApp = examApp;
// Initialize the welcome screen
examApp.renderWelcome();
// Add some utility functions for enhanced user experience
document.addEventListener('DOMContentLoaded', () => {
// Add loading indicator
const showLoading = () => {
document.getElementById('loading').style.display = 'flex';
};
const hideLoading = () => {
document.getElementById('loading').style.display = 'none';
};
// Intercept navigation to show loading
const originalMethods = {
startExam: examApp.startExam.bind(examApp),
goToQuestion: examApp.goToQuestion.bind(examApp),
nextQuestion: examApp.nextQuestion.bind(examApp),
previousQuestion: examApp.previousQuestion.bind(examApp),
submitExam: examApp.submitExam.bind(examApp)
};
// Wrap methods with loading indicator
examApp.startExam = function() {
showLoading();
setTimeout(() => {
originalMethods.startExam();
hideLoading();
}, 500);
};
examApp.goToQuestion = function(index) {
showLoading();
setTimeout(() => {
originalMethods.goToQuestion(index);
hideLoading();
}, 300);
};
examApp.nextQuestion = function() {
// Skip loading indicator for mobile performance
if (/Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
originalMethods.nextQuestion();
} else {
showLoading();
setTimeout(() => {
originalMethods.nextQuestion();
hideLoading();
}, 300);
}
};
examApp.previousQuestion = function() {
// Skip loading indicator for mobile performance
if (/Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
originalMethods.previousQuestion();
} else {
showLoading();
setTimeout(() => {
originalMethods.previousQuestion();
hideLoading();
}, 300);
}
};
examApp.submitExam = function() {
// Always show loading for submit to give user feedback
showLoading();
// Debug log for mobile
console.log('Submit exam called', {
userAgent: navigator.userAgent,
isMobile: /Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
});
setTimeout(() => {
try {
originalMethods.submitExam();
} catch (error) {
console.error('Error submitting exam:', error);
hideLoading();
alert('Error submitting exam. Please try again.');
}
hideLoading();
}, 800);
};
// Add keyboard shortcuts info
const keyboardShortcuts = `
Keyboard Shortcuts:
• Arrow Left/Right: Navigate between questions
• 1-4: Select answer options
• Esc: Pause exam (if implemented)
`;
// Add keyboard shortcuts to console for developers
console.log(keyboardShortcuts);
// Add accessibility improvements
document.body.setAttribute('tabindex', '0');
// Focus management for accessibility
const focusManagement = {
currentFocus: 0,
setFocus(index) {
this.currentFocus = index;
const focusableElements = document.querySelectorAll(
'button, input[type="radio"], a, [tabindex]:not([tabindex="-1"])'
);
if (focusableElements[index]) {
focusableElements[index].focus();
}
},
moveFocus(direction) {
const focusableElements = document.querySelectorAll(
'button, input[type="radio"], a, [tabindex]:not([tabindex="-1"])'
);
let newIndex = this.currentFocus + direction;
if (newIndex < 0) newIndex = focusableElements.length - 1;
if (newIndex >= focusableElements.length) newIndex = 0;
this.setFocus(newIndex);
}
};
// Handle keyboard navigation for accessibility
document.addEventListener('keydown', (e) => {
if (e.altKey || e.ctrlKey || e.metaKey) return;
switch (e.key) {
case 'Tab':
// Handle tab navigation normally
break;
case 'ArrowUp':
e.preventDefault();
focusManagement.moveFocus(-1);
break;
case 'ArrowDown':
e.preventDefault();
focusManagement.moveFocus(1);
break;
}
});
// Add touch/swipe support for mobile devices
let touchStartX = 0;
let touchEndX = 0;
document.addEventListener('touchstart', (e) => {
touchStartX = e.changedTouches[0].screenX;
});
document.addEventListener('touchend', (e) => {
touchEndX = e.changedTouches[0].screenX;
handleSwipe();
});
function handleSwipe() {
const swipeThreshold = 50;
const diff = touchStartX - touchEndX;
if (Math.abs(diff) > swipeThreshold) {
if (diff > 0) {
// Swipe left - next question
if (!examApp.isCompleted && examApp.questions.length > 0) {
examApp.nextQuestion();
}
} else {
// Swipe right - previous question
if (!examApp.isCompleted && examApp.questions.length > 0) {
examApp.previousQuestion();
}
}
}
}
// Add beforeunload warning when exam is in progress
window.addEventListener('beforeunload', (e) => {
if (examApp.questions.length > 0 && !examApp.isCompleted) {
e.preventDefault();
e.returnValue = 'Are you sure you want to leave? Your exam progress will be lost.';
return e.returnValue;
}
});
// Add service worker for offline support (optional)
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js')
.then((registration) => {
console.log('SW registered: ', registration);
})
.catch((registrationError) => {
console.log('SW registration failed: ', registrationError);
});
});
}
// Add performance monitoring
const performanceObserver = new PerformanceObserver((list) => {
list.getEntries().forEach((entry) => {
if (entry.entryType === 'measure') {
console.log(`Performance: ${entry.name} took ${entry.duration}ms`);
}
});
});
if ('PerformanceObserver' in window) {
performanceObserver.observe({ entryTypes: ['measure'] });
}
// Add error handling
window.addEventListener('error', (e) => {
console.error('Application error:', e.error);
// Show user-friendly error message
const errorContainer = document.createElement('div');
errorContainer.className = 'error-notification';
errorContainer.innerHTML = `
<div class="error-content">
<h3>Oops! Something went wrong</h3>
<p>Please refresh the page and try again.</p>
<button onclick="location.reload()" class="btn btn-primary">Refresh Page</button>
</div>
`;
// Add error notification styles if not already present
if (!document.querySelector('#error-styles')) {
const style = document.createElement('style');
style.id = 'error-styles';
style.textContent = `
.error-notification {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
padding: 2rem;
border-radius: 10px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
z-index: 10000;
text-align: center;
}
.error-content h3 {
color: #e74c3c;
margin-bottom: 1rem;
}
.error-content p {
color: #7f8c8d;
margin-bottom: 1.5rem;
}
`;
document.head.appendChild(style);
}
document.body.appendChild(errorContainer);
// Remove error notification after 5 seconds
setTimeout(() => {
if (errorContainer.parentNode) {
errorContainer.parentNode.removeChild(errorContainer);
}
}, 5000);
});
// Add unhandled promise rejection handling
window.addEventListener('unhandledrejection', (e) => {
console.error('Unhandled promise rejection:', e.reason);
e.preventDefault();
});
console.log('CEH v13 Mock Exam Application initialized successfully');
});
// Export for potential testing or external access
export { examApp };