Spaces:
Sleeping
Sleeping
Update static/script.js
#2
by
Hanan-Alnakhal - opened
- static/script.js +485 -385
static/script.js
CHANGED
|
@@ -1,385 +1,485 @@
|
|
| 1 |
-
// ===== GLOBAL STATE =====
|
| 2 |
-
let currentResults = null;
|
| 3 |
-
let explanations = null;
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
});
|
| 12 |
-
|
| 13 |
-
// ===== NAVIGATION
|
| 14 |
-
function
|
| 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 |
-
uploadArea.addEventListener('
|
| 76 |
-
e.preventDefault();
|
| 77 |
-
uploadArea.classList.
|
| 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 |
-
document.getElementById('
|
| 183 |
-
document.getElementById('
|
| 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 |
-
|
| 293 |
-
|
| 294 |
-
}
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
//
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
}
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// ===== GLOBAL STATE =====
|
| 2 |
+
let currentResults = null;
|
| 3 |
+
let explanations = null;
|
| 4 |
+
let sessionId = null; // Store session ID
|
| 5 |
+
|
| 6 |
+
// ===== INITIALIZATION =====
|
| 7 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 8 |
+
initNavigation();
|
| 9 |
+
initFileUpload();
|
| 10 |
+
initScrollAnimations();
|
| 11 |
+
});
|
| 12 |
+
|
| 13 |
+
// ===== NAVIGATION =====
|
| 14 |
+
function initNavigation() {
|
| 15 |
+
const navLinks = document.querySelectorAll('.nav-link');
|
| 16 |
+
|
| 17 |
+
navLinks.forEach(link => {
|
| 18 |
+
link.addEventListener('click', (e) => {
|
| 19 |
+
e.preventDefault();
|
| 20 |
+
const target = link.getAttribute('href');
|
| 21 |
+
|
| 22 |
+
// Update active state
|
| 23 |
+
navLinks.forEach(l => l.classList.remove('active'));
|
| 24 |
+
link.classList.add('active');
|
| 25 |
+
|
| 26 |
+
// Smooth scroll
|
| 27 |
+
document.querySelector(target).scrollIntoView({
|
| 28 |
+
behavior: 'smooth',
|
| 29 |
+
block: 'start'
|
| 30 |
+
});
|
| 31 |
+
});
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
// Mobile menu toggle
|
| 35 |
+
const mobileToggle = document.querySelector('.mobile-menu-toggle');
|
| 36 |
+
const navMenu = document.querySelector('.nav-menu');
|
| 37 |
+
|
| 38 |
+
if (mobileToggle) {
|
| 39 |
+
mobileToggle.addEventListener('click', () => {
|
| 40 |
+
navMenu.classList.toggle('active');
|
| 41 |
+
});
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
// ===== SCROLL ANIMATIONS =====
|
| 46 |
+
function initScrollAnimations() {
|
| 47 |
+
const observer = new IntersectionObserver((entries) => {
|
| 48 |
+
entries.forEach(entry => {
|
| 49 |
+
if (entry.isIntersecting) {
|
| 50 |
+
entry.target.style.opacity = '1';
|
| 51 |
+
entry.target.style.transform = 'translateY(0)';
|
| 52 |
+
}
|
| 53 |
+
});
|
| 54 |
+
}, { threshold: 0.1 });
|
| 55 |
+
|
| 56 |
+
document.querySelectorAll('.about-card, .feature-card').forEach(card => {
|
| 57 |
+
card.style.opacity = '0';
|
| 58 |
+
card.style.transform = 'translateY(20px)';
|
| 59 |
+
card.style.transition = 'all 0.6s ease-out';
|
| 60 |
+
observer.observe(card);
|
| 61 |
+
});
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
// ===== FILE UPLOAD =====
|
| 65 |
+
function initFileUpload() {
|
| 66 |
+
const fileInput = document.getElementById('fileInput');
|
| 67 |
+
const uploadArea = document.getElementById('uploadArea');
|
| 68 |
+
|
| 69 |
+
if (!fileInput || !uploadArea) return;
|
| 70 |
+
|
| 71 |
+
// Click to upload
|
| 72 |
+
fileInput.addEventListener('change', handleFileSelect);
|
| 73 |
+
|
| 74 |
+
// Drag and drop
|
| 75 |
+
uploadArea.addEventListener('dragover', (e) => {
|
| 76 |
+
e.preventDefault();
|
| 77 |
+
uploadArea.classList.add('dragover');
|
| 78 |
+
});
|
| 79 |
+
|
| 80 |
+
uploadArea.addEventListener('dragleave', () => {
|
| 81 |
+
uploadArea.classList.remove('dragover');
|
| 82 |
+
});
|
| 83 |
+
|
| 84 |
+
uploadArea.addEventListener('drop', (e) => {
|
| 85 |
+
e.preventDefault();
|
| 86 |
+
uploadArea.classList.remove('dragover');
|
| 87 |
+
|
| 88 |
+
const file = e.dataTransfer.files[0];
|
| 89 |
+
if (file && file.type === 'application/pdf') {
|
| 90 |
+
uploadFile(file);
|
| 91 |
+
} else {
|
| 92 |
+
showToast('Please upload a PDF file', 'error');
|
| 93 |
+
}
|
| 94 |
+
});
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
function handleFileSelect(e) {
|
| 98 |
+
const file = e.target.files[0];
|
| 99 |
+
if (file) {
|
| 100 |
+
uploadFile(file);
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
async function uploadFile(file) {
|
| 105 |
+
const formData = new FormData();
|
| 106 |
+
formData.append('file', file);
|
| 107 |
+
|
| 108 |
+
// Show progress
|
| 109 |
+
document.getElementById('uploadArea').classList.add('hidden');
|
| 110 |
+
document.getElementById('uploadProgress').classList.remove('hidden');
|
| 111 |
+
|
| 112 |
+
try {
|
| 113 |
+
const response = await fetch('/api/upload', {
|
| 114 |
+
method: 'POST',
|
| 115 |
+
body: formData
|
| 116 |
+
});
|
| 117 |
+
|
| 118 |
+
const data = await response.json();
|
| 119 |
+
|
| 120 |
+
if (!response.ok) {
|
| 121 |
+
throw new Error(data.error || 'Upload failed');
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
// Store session ID and results
|
| 125 |
+
sessionId = data.session_id;
|
| 126 |
+
currentResults = data.results;
|
| 127 |
+
|
| 128 |
+
console.log('Session ID:', sessionId);
|
| 129 |
+
console.log('Results:', currentResults);
|
| 130 |
+
|
| 131 |
+
showToast(`✓ Found ${data.count} lab results!`, 'success');
|
| 132 |
+
|
| 133 |
+
// Generate explanations
|
| 134 |
+
await generateExplanations();
|
| 135 |
+
|
| 136 |
+
// Display results
|
| 137 |
+
displayResults();
|
| 138 |
+
|
| 139 |
+
// Scroll to results
|
| 140 |
+
setTimeout(() => {
|
| 141 |
+
document.getElementById('results').scrollIntoView({
|
| 142 |
+
behavior: 'smooth'
|
| 143 |
+
});
|
| 144 |
+
}, 500);
|
| 145 |
+
|
| 146 |
+
} catch (error) {
|
| 147 |
+
showToast(error.message, 'error');
|
| 148 |
+
resetUploadArea();
|
| 149 |
+
}
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
async function generateExplanations() {
|
| 153 |
+
showLoading('Generating AI explanations... This may take 30-60 seconds.');
|
| 154 |
+
|
| 155 |
+
try {
|
| 156 |
+
const response = await fetch('/api/explain', {
|
| 157 |
+
method: 'POST',
|
| 158 |
+
headers: {
|
| 159 |
+
'Content-Type': 'application/json'
|
| 160 |
+
},
|
| 161 |
+
body: JSON.stringify({ session_id: sessionId })
|
| 162 |
+
});
|
| 163 |
+
|
| 164 |
+
const data = await response.json();
|
| 165 |
+
|
| 166 |
+
if (!response.ok) {
|
| 167 |
+
throw new Error(data.error || 'Failed to generate explanations');
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
explanations = data.explanations;
|
| 171 |
+
console.log('Explanations:', explanations);
|
| 172 |
+
|
| 173 |
+
} catch (error) {
|
| 174 |
+
showToast('Error generating explanations: ' + error.message, 'error');
|
| 175 |
+
// Continue anyway to show results
|
| 176 |
+
} finally {
|
| 177 |
+
hideLoading();
|
| 178 |
+
}
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
function displayResults() {
|
| 182 |
+
const resultsSection = document.getElementById('results');
|
| 183 |
+
const container = document.getElementById('resultsContainer');
|
| 184 |
+
|
| 185 |
+
if (!currentResults || currentResults.length === 0) {
|
| 186 |
+
container.innerHTML = '<p class="empty-message">No results found</p>';
|
| 187 |
+
return;
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
// Show results section
|
| 191 |
+
resultsSection.classList.remove('hidden');
|
| 192 |
+
|
| 193 |
+
// Update summary stats
|
| 194 |
+
updateSummaryStats();
|
| 195 |
+
|
| 196 |
+
// Clear container
|
| 197 |
+
container.innerHTML = '';
|
| 198 |
+
|
| 199 |
+
// Create result cards
|
| 200 |
+
currentResults.forEach((result, index) => {
|
| 201 |
+
const card = createResultCard(result, index);
|
| 202 |
+
container.appendChild(card);
|
| 203 |
+
});
|
| 204 |
+
|
| 205 |
+
// Reset upload area
|
| 206 |
+
resetUploadArea();
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
function updateSummaryStats() {
|
| 210 |
+
const stats = {
|
| 211 |
+
normal: 0,
|
| 212 |
+
high: 0,
|
| 213 |
+
low: 0
|
| 214 |
+
};
|
| 215 |
+
|
| 216 |
+
currentResults.forEach(result => {
|
| 217 |
+
if (result.status in stats) {
|
| 218 |
+
stats[result.status]++;
|
| 219 |
+
}
|
| 220 |
+
});
|
| 221 |
+
|
| 222 |
+
document.getElementById('normalCount').textContent = stats.normal;
|
| 223 |
+
document.getElementById('highCount').textContent = stats.high;
|
| 224 |
+
document.getElementById('lowCount').textContent = stats.low;
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
function createResultCard(result, index) {
|
| 228 |
+
const card = document.createElement('div');
|
| 229 |
+
card.className = 'result-card';
|
| 230 |
+
card.style.setProperty('--i', index + 1);
|
| 231 |
+
|
| 232 |
+
const statusClass = `status-${result.status}`;
|
| 233 |
+
const explanation = explanations && explanations[result.test_name]
|
| 234 |
+
? explanations[result.test_name]
|
| 235 |
+
: 'Explanation is being generated. Click "Ask Questions" to learn more about this result.';
|
| 236 |
+
|
| 237 |
+
card.innerHTML = `
|
| 238 |
+
<div class="result-header">
|
| 239 |
+
<div>
|
| 240 |
+
<h3 class="result-name">${escapeHtml(result.test_name)}</h3>
|
| 241 |
+
<p class="result-range">Reference: ${escapeHtml(result.reference_range || 'N/A')}</p>
|
| 242 |
+
</div>
|
| 243 |
+
<div class="result-value-container">
|
| 244 |
+
<div class="result-value">${escapeHtml(result.value)} ${escapeHtml(result.unit)}</div>
|
| 245 |
+
<span class="result-status ${statusClass}">${result.status}</span>
|
| 246 |
+
</div>
|
| 247 |
+
</div>
|
| 248 |
+
<div class="result-explanation">
|
| 249 |
+
<strong>💡 What does this mean?</strong>
|
| 250 |
+
<p>${escapeHtml(explanation)}</p>
|
| 251 |
+
</div>
|
| 252 |
+
`;
|
| 253 |
+
|
| 254 |
+
return card;
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
function resetUploadArea() {
|
| 258 |
+
document.getElementById('uploadArea').classList.remove('hidden');
|
| 259 |
+
document.getElementById('uploadProgress').classList.add('hidden');
|
| 260 |
+
document.getElementById('fileInput').value = '';
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
// ===== SUMMARY GENERATION =====
|
| 264 |
+
async function generateSummary() {
|
| 265 |
+
if (!sessionId) {
|
| 266 |
+
showToast('Please upload a lab report first', 'error');
|
| 267 |
+
return;
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
showLoading('Generating comprehensive summary...');
|
| 271 |
+
|
| 272 |
+
try {
|
| 273 |
+
const response = await fetch('/api/summary', {
|
| 274 |
+
method: 'POST',
|
| 275 |
+
headers: {
|
| 276 |
+
'Content-Type': 'application/json'
|
| 277 |
+
},
|
| 278 |
+
body: JSON.stringify({ session_id: sessionId })
|
| 279 |
+
});
|
| 280 |
+
|
| 281 |
+
const data = await response.json();
|
| 282 |
+
|
| 283 |
+
if (!response.ok) {
|
| 284 |
+
throw new Error(data.error || 'Failed to generate summary');
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
// Show summary modal
|
| 288 |
+
showSummaryModal(data.summary, data.stats);
|
| 289 |
+
|
| 290 |
+
} catch (error) {
|
| 291 |
+
showToast('Error: ' + error.message, 'error');
|
| 292 |
+
} finally {
|
| 293 |
+
hideLoading();
|
| 294 |
+
}
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
function showSummaryModal(summary, stats) {
|
| 298 |
+
const modal = document.createElement('div');
|
| 299 |
+
modal.className = 'chat-modal';
|
| 300 |
+
modal.innerHTML = `
|
| 301 |
+
<div class="chat-modal-content">
|
| 302 |
+
<div class="chat-header">
|
| 303 |
+
<h3>📊 Complete Summary</h3>
|
| 304 |
+
<button class="chat-close" onclick="this.closest('.chat-modal').remove()">×</button>
|
| 305 |
+
</div>
|
| 306 |
+
<div class="chat-messages">
|
| 307 |
+
<div class="chat-message assistant">
|
| 308 |
+
<div class="chat-bubble">
|
| 309 |
+
<p>${escapeHtml(summary).replace(/\n/g, '<br><br>')}</p>
|
| 310 |
+
</div>
|
| 311 |
+
</div>
|
| 312 |
+
</div>
|
| 313 |
+
</div>
|
| 314 |
+
`;
|
| 315 |
+
document.body.appendChild(modal);
|
| 316 |
+
|
| 317 |
+
// Close on click outside
|
| 318 |
+
modal.addEventListener('click', (e) => {
|
| 319 |
+
if (e.target === modal) {
|
| 320 |
+
modal.remove();
|
| 321 |
+
}
|
| 322 |
+
});
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
// ===== CHAT FUNCTIONALITY =====
|
| 326 |
+
function openChat() {
|
| 327 |
+
if (!sessionId) {
|
| 328 |
+
showToast('Please upload a lab report first', 'error');
|
| 329 |
+
return;
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
const modal = document.getElementById('chatModal');
|
| 333 |
+
modal.classList.remove('hidden');
|
| 334 |
+
document.getElementById('chatInput').focus();
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
function closeChat() {
|
| 338 |
+
document.getElementById('chatModal').classList.add('hidden');
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
function handleChatKeypress(event) {
|
| 342 |
+
if (event.key === 'Enter') {
|
| 343 |
+
sendChatMessage();
|
| 344 |
+
}
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
async function sendChatMessage() {
|
| 348 |
+
const input = document.getElementById('chatInput');
|
| 349 |
+
const question = input.value.trim();
|
| 350 |
+
|
| 351 |
+
if (!question) return;
|
| 352 |
+
|
| 353 |
+
if (!sessionId) {
|
| 354 |
+
showToast('Session expired. Please upload your report again.', 'error');
|
| 355 |
+
return;
|
| 356 |
+
}
|
| 357 |
+
|
| 358 |
+
// Clear input
|
| 359 |
+
input.value = '';
|
| 360 |
+
|
| 361 |
+
// Add user message
|
| 362 |
+
addChatMessage(question, 'user');
|
| 363 |
+
|
| 364 |
+
// Show loading message
|
| 365 |
+
const loadingId = addChatMessage('Thinking...', 'assistant', true);
|
| 366 |
+
|
| 367 |
+
try {
|
| 368 |
+
const response = await fetch('/api/ask', {
|
| 369 |
+
method: 'POST',
|
| 370 |
+
headers: {
|
| 371 |
+
'Content-Type': 'application/json'
|
| 372 |
+
},
|
| 373 |
+
body: JSON.stringify({
|
| 374 |
+
question: question,
|
| 375 |
+
session_id: sessionId
|
| 376 |
+
})
|
| 377 |
+
});
|
| 378 |
+
|
| 379 |
+
const data = await response.json();
|
| 380 |
+
|
| 381 |
+
if (!response.ok) {
|
| 382 |
+
throw new Error(data.error || 'Failed to get answer');
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
// Remove loading message
|
| 386 |
+
document.getElementById(loadingId).remove();
|
| 387 |
+
|
| 388 |
+
// Add assistant response
|
| 389 |
+
addChatMessage(data.answer, 'assistant');
|
| 390 |
+
|
| 391 |
+
} catch (error) {
|
| 392 |
+
document.getElementById(loadingId).remove();
|
| 393 |
+
addChatMessage(`Sorry, I encountered an error: ${error.message}`, 'assistant');
|
| 394 |
+
showToast(error.message, 'error');
|
| 395 |
+
}
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
function addChatMessage(text, sender, isLoading = false) {
|
| 399 |
+
const messagesContainer = document.getElementById('chatMessages');
|
| 400 |
+
|
| 401 |
+
// Remove welcome message if exists
|
| 402 |
+
const welcome = messagesContainer.querySelector('.chat-welcome');
|
| 403 |
+
if (welcome) {
|
| 404 |
+
welcome.remove();
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
const messageId = `msg-${Date.now()}`;
|
| 408 |
+
const messageDiv = document.createElement('div');
|
| 409 |
+
messageDiv.id = messageId;
|
| 410 |
+
messageDiv.className = `chat-message ${sender}`;
|
| 411 |
+
|
| 412 |
+
const bubbleClass = isLoading ? 'chat-bubble loading' : 'chat-bubble';
|
| 413 |
+
messageDiv.innerHTML = `<div class="${bubbleClass}">${escapeHtml(text)}</div>`;
|
| 414 |
+
|
| 415 |
+
messagesContainer.appendChild(messageDiv);
|
| 416 |
+
messagesContainer.scrollTop = messagesContainer.scrollHeight;
|
| 417 |
+
|
| 418 |
+
return messageId;
|
| 419 |
+
}
|
| 420 |
+
|
| 421 |
+
// ===== LOADING OVERLAY =====
|
| 422 |
+
function showLoading(message = 'Processing...') {
|
| 423 |
+
const overlay = document.getElementById('loadingOverlay');
|
| 424 |
+
if (overlay) {
|
| 425 |
+
overlay.querySelector('p').textContent = message;
|
| 426 |
+
overlay.classList.remove('hidden');
|
| 427 |
+
}
|
| 428 |
+
}
|
| 429 |
+
|
| 430 |
+
function hideLoading() {
|
| 431 |
+
const overlay = document.getElementById('loadingOverlay');
|
| 432 |
+
if (overlay) {
|
| 433 |
+
overlay.classList.add('hidden');
|
| 434 |
+
}
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
// ===== TOAST NOTIFICATIONS =====
|
| 438 |
+
function showToast(message, type = 'info') {
|
| 439 |
+
const toast = document.getElementById('toast');
|
| 440 |
+
toast.textContent = message;
|
| 441 |
+
toast.className = `toast ${type} show`;
|
| 442 |
+
|
| 443 |
+
setTimeout(() => {
|
| 444 |
+
toast.classList.remove('show');
|
| 445 |
+
}, 4000);
|
| 446 |
+
}
|
| 447 |
+
|
| 448 |
+
// ===== UTILITY FUNCTIONS =====
|
| 449 |
+
function escapeHtml(text) {
|
| 450 |
+
const div = document.createElement('div');
|
| 451 |
+
div.textContent = text;
|
| 452 |
+
return div.innerHTML;
|
| 453 |
+
}
|
| 454 |
+
|
| 455 |
+
// ===== SMOOTH SCROLL FOR ALL LINKS =====
|
| 456 |
+
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
|
| 457 |
+
anchor.addEventListener('click', function (e) {
|
| 458 |
+
e.preventDefault();
|
| 459 |
+
const target = document.querySelector(this.getAttribute('href'));
|
| 460 |
+
if (target) {
|
| 461 |
+
target.scrollIntoView({
|
| 462 |
+
behavior: 'smooth',
|
| 463 |
+
block: 'start'
|
| 464 |
+
});
|
| 465 |
+
}
|
| 466 |
+
});
|
| 467 |
+
});
|
| 468 |
+
|
| 469 |
+
// ===== CLOSE MODALS ON ESC =====
|
| 470 |
+
document.addEventListener('keydown', (e) => {
|
| 471 |
+
if (e.key === 'Escape') {
|
| 472 |
+
const chatModal = document.getElementById('chatModal');
|
| 473 |
+
if (chatModal && !chatModal.classList.contains('hidden')) {
|
| 474 |
+
closeChat();
|
| 475 |
+
}
|
| 476 |
+
}
|
| 477 |
+
});
|
| 478 |
+
|
| 479 |
+
// ===== CLOSE MODALS ON OUTSIDE CLICK =====
|
| 480 |
+
document.addEventListener('click', (e) => {
|
| 481 |
+
const chatModal = document.getElementById('chatModal');
|
| 482 |
+
if (e.target === chatModal) {
|
| 483 |
+
closeChat();
|
| 484 |
+
}
|
| 485 |
+
});
|