-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.diff
More file actions
563 lines (550 loc) · 18.1 KB
/
patch.diff
File metadata and controls
563 lines (550 loc) · 18.1 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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
diff --git a/index.html b/index.html
index 1234567..abcdef0 100644
--- a/index.html
+++ b/index.html
@@ -13,6 +13,7 @@
<!-- LIBRARIES (CDN) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/lucide@latest/dist/umd/lucide.min.js"></script>
<!-- FONTS -->
@@ -732,6 +733,15 @@
.block {
display: block;
}
+
+ /* Capture and Export Enhancements */
+ #print-capture-area {
+ position: absolute;
+ left: -9999px;
+ top: -9999px;
+ max-width: 900px;
+ margin: 0 auto;
+ background: white;
+ display: none;
+ }
+
+ .no-capture {
+ display: none !important;
+ }
/* Print Styles */
@media print {
@@ -1615,7 +1625,7 @@
document.getElementById('print-header-subtitle').textContent = `Daily Schedule for ${selectedDay}`;
- const tableHeader = '<th>Class</th>' + periodHeaders.map((h, idx) =>
+ const tableHeader = '<th role="columnheader">Class</th>' + periodHeaders.map((h, idx) =>
- `<th${idx === currentPeriodIdx ? ' class="highlight-period"' : ''}>${h.name}<br/><span style="font-weight:400; font-size: 0.75rem;">${h.time}</span></th>`
+ `<th role="columnheader"${idx === currentPeriodIdx ? ' class="highlight-period"' : ''}>${h.name}<br/><span style="font-weight:400; font-size: 0.75rem;">${h.time}</span></th>`
).join('');
const tableBody = classNames.map(cName => {
@@ -1656,6 +1666,12 @@
<button onclick="handleShareScreenshot()" class="button button-whatsapp" style="width: auto;">
<i data-lucide="share-2"></i>
</button>
+ <button onclick="handleShareAsPDF()" class="button button-primary no-capture" style="width: auto; font-size: 0.75rem; padding: 0.5rem 0.75rem;">
+ <i data-lucide="file-text"></i> PDF
+ </button>
+ <button onclick="handleExportCSV()" class="button button-secondary no-capture" style="width: auto; font-size: 0.75rem; padding: 0.5rem 0.75rem;">
+ <i data-lucide="download"></i> CSV
+ </button>
</div>
</div>
<div class="flex flex-wrap gap-2" style="border-bottom: 1px solid var(--gray-200);
@@ -1664,7 +1680,7 @@
</div>
<div class="table-container">
- <table class="responsive">
+ <table class="responsive" role="table" aria-label="Daily timetable for ${selectedDay}">
- <thead><tr>${tableHeader}</tr></thead>
+ <thead><tr role="row">${tableHeader}</tr></thead>
<tbody>${tableBody}</tbody>
</table>
</div>
@@ -1745,6 +1761,12 @@
<button onclick="handleShareScreenshot()" class="button button-whatsapp" style="max-width: 200px;">
<i data-lucide="share-2"></i> Share
</button>
+ <button onclick="handleShareAsPDF()" class="button button-primary no-capture" style="max-width: 200px;">
+ <i data-lucide="file-text"></i> Share as PDF
+ </button>
+ <button onclick="handleExportCSV()" class="button button-secondary no-capture" style="max-width: 200px;">
+ <i data-lucide="download"></i> Export CSV
+ </button>
</div>
`;
}
@@ -1833,6 +1855,12 @@
<button onclick="handleShareScreenshot()" class="button button-whatsapp" style="max-width: 200px;">
<i data-lucide="share-2"></i> Share
</button>
+ <button onclick="handleShareAsPDF()" class="button button-primary no-capture" style="max-width: 200px;">
+ <i data-lucide="file-text"></i> Share as PDF
+ </button>
+ <button onclick="handleExportCSV()" class="button button-secondary no-capture" style="max-width: 200px;">
+ <i data-lucide="download"></i> Export CSV
+ </button>
</div>
`;
}
@@ -2078,6 +2106,42 @@
});
}
+ // Ensure fonts are loaded before capturing
+ async function ensureFontsLoaded() {
+ try {
+ if (document.fonts && document.fonts.ready) {
+ await document.fonts.ready;
+ }
+ // Additional wait for font rendering
+ await new Promise(resolve => setTimeout(resolve, 100));
+ } catch (error) {
+ console.warn('Font loading check failed:', error);
+ }
+ }
+
+ // Helper function to get device pixel ratio with cap
+ function getOptimalScale() {
+ const devicePixelRatio = window.devicePixelRatio || 1;
+ return Math.min(devicePixelRatio, 2); // Cap at 2 for performance
+ }
+
+ // Pure function: Convert table data to 2D array for CSV export
+ function tableTo2DArray(tableElement) {
+ if (!tableElement) return [];
+
+ const rows = [];
+ const headerRow = tableElement.querySelector('thead tr');
+ const bodyRows = tableElement.querySelectorAll('tbody tr');
+
+ // Extract headers
+ if (headerRow) {
+ const headers = Array.from(headerRow.querySelectorAll('th')).map(th => {
+ return th.textContent.replace(/\n/g, ' ').replace(/\s+/g, ' ').trim();
+ });
+ rows.push(headers);
+ }
+
+ // Extract body rows
+ bodyRows.forEach(row => {
+ const cells = Array.from(row.querySelectorAll('td')).map(td => {
+ // Clean up cell content - remove line breaks and extra spaces
+ return td.textContent.replace(/\n/g, ' ').replace(/\s+/g, ' ').trim();
+ });
+ rows.push(cells);
+ });
+
+ return rows;
+ }
+
async function handlePrint() {
try {
// Check if mobile and offer alternative
@@ -2129,12 +2193,12 @@
try {
- // 3. Wait for rendering to complete
+ // 3. Ensure fonts are loaded and wait for rendering
+ await ensureFontsLoaded();
await waitForRender();
- // 4. Capture with high-quality settings optimized for tables
+ // 4. Capture with optimal settings using device pixel ratio
const canvas = await html2canvas(captureElement, {
backgroundColor: '#fff',
- scale: 2,
+ scale: getOptimalScale(),
useCORS: true,
allowTaint: false,
foreignObjectRendering: false,
@@ -2190,6 +2254,136 @@
}
}
+ // New function: Share as PDF
+ async function handleShareAsPDF() {
+ try {
+ // Check if jsPDF is available
+ if (typeof window.jsPDF === 'undefined') {
+ showToast('❌ PDF library not loaded', 3000, 'error');
+ return;
+ }
+
+ const captureElement = document.getElementById('print-capture-area');
+ if (!captureElement) {
+ showToast('❌ Required elements not found', 3000, 'error');
+ return;
+ }
+
+ // Prepare print content
+ if (!preparePrintContent()) {
+ showToast('❌ Failed to prepare print content', 3000, 'error');
+ return;
+ }
+
+ showToast('📄 Generating PDF...', 2000, 'info');
+
+ const originalDisplay = captureElement.style.display;
+ captureElement.style.display = 'block';
+
+ try {
+ await ensureFontsLoaded();
+ await waitForRender();
+
+ // Capture with html2canvas
+ const canvas = await html2canvas(captureElement, {
+ backgroundColor: '#fff',
+ scale: getOptimalScale(),
+ useCORS: true,
+ allowTaint: false,
+ foreignObjectRendering: false,
+ imageTimeout: 15000
+ });
+
+ // Create PDF
+ const { jsPDF } = window.jsPDF;
+ const pdf = new jsPDF({
+ orientation: 'landscape',
+ unit: 'mm',
+ format: 'a4'
+ });
+
+ // Calculate dimensions
+ const pdfWidth = pdf.internal.pageSize.getWidth();
+ const pdfHeight = pdf.internal.pageSize.getHeight();
+ const canvasWidth = canvas.width;
+ const canvasHeight = canvas.height;
+ const ratio = Math.min(pdfWidth / canvasWidth, pdfHeight / canvasHeight);
+ const imgWidth = canvasWidth * ratio;
+ const imgHeight = canvasHeight * ratio;
+ const x = (pdfWidth - imgWidth) / 2;
+ const y = (pdfHeight - imgHeight) / 2;
+
+ // Add image to PDF
+ const imgData = canvas.toDataURL('image/png');
+ pdf.addImage(imgData, 'PNG', x, y, imgWidth, imgHeight);
+
+ // Generate filename
+ const now = new Date();
+ const dateStr = now.toISOString().split('T')[0];
+ const filename = `vpps-timetable-${dateStr}.pdf`;
+
+ // Save PDF
+ pdf.save(filename);
+ showToast('✅ PDF saved successfully!', 3000, 'success');
+
+ } finally {
+ captureElement.style.display = originalDisplay;
+ }
+
+ } catch (error) {
+ console.error('PDF generation error:', error);
+ showToast('❌ Failed to generate PDF', 3000, 'error');
+ }
+ }
+
+ // New function: Export CSV
+ function handleExportCSV() {
+ try {
+ // Find the current visible table
+ const visibleTable = document.querySelector('.table-container table:not(#print-capture-area table)');
+ if (!visibleTable) {
+ showToast('❌ No table found to export', 3000, 'error');
+ return;
+ }
+
+ // Convert table to 2D array
+ const data = tableTo2DArray(visibleTable);
+ if (data.length === 0) {
+ showToast('❌ No data found in table', 3000, 'error');
+ return;
+ }
+
+ // Convert to CSV format
+ const csvContent = data.map(row =>
+ row.map(cell => `"${cell.replace(/"/g, '""')}"`)
+ .join(',')
+ ).join('\n');
+
+ // Create and download CSV file
+ const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
+ const now = new Date();
+ const dateStr = now.toISOString().split('T')[0];
+ const filename = `vpps-timetable-${dateStr}.csv`;
+
+ const link = document.createElement('a');
+ const url = URL.createObjectURL(blob);
+ link.setAttribute('href', url);
+ link.setAttribute('download', filename);
+ link.style.visibility = 'hidden';
+ document.body.appendChild(link);
+ link.click();
+ document.body.removeChild(link);
+ URL.revokeObjectURL(url);
+
+ showToast('✅ CSV exported successfully!', 3000, 'success');
+
+ } catch (error) {
+ console.error('CSV export error:', error);
+ showToast('❌ Failed to export CSV', 3000, 'error');
+ }
+ }
+
function downloadImage(blob, filename) {
try {
const url = URL.createObjectURL(blob);
@@ -2290,6 +2484,16 @@
}
}
+ // Pre-render print content and keep updated
+ function updatePrintContent() {
+ try {
+ // Always keep the print content updated in background
+ preparePrintContent();
+ } catch (error) {
+ console.error('Error updating print content:', error);
+ }
+ }
+
async function handlePrint() {
try {
// Check if mobile and offer alternative
@@ -2649,6 +2853,9 @@
default:
renderDashboard();
}
+
+ // Update print content when view changes
+ setTimeout(() => updatePrintContent(), 100);
}
// --- SUBSTITUTION MANAGEMENT ---
@@ -3177,6 +3384,19 @@
showToast('🔄 View refreshed!', 2000, 'success');
}
+ // Register service worker for offline functionality
+ if ('serviceWorker' in navigator) {
+ navigator.serviceWorker.register('./sw.js')
+ .then(registration => {
+ console.log('Service Worker registered successfully:', registration.scope);
+ })
+ .catch(error => {
+ console.log('Service Worker registration failed:', error);
+ });
+ }
+
+ // Pre-render print content on load
+ updatePrintContent();
+
showToast('📱 Mobile-optimized Timetable Command Center loaded!', 2500, 'success');
}, 300);
diff --git a/sw.js b/sw.js
new file mode 100644
index 0000000..1234567
--- /dev/null
+++ b/sw.js
@@ -0,0 +1,169 @@
+// Service Worker for Veer Patta Public School Timetable
+// Provides offline-first caching for the page shell and timetable data
+
+const CACHE_NAME = 'vpps-timetable-v1';
+const STATIC_CACHE_NAME = 'vpps-static-v1';
+
+// Resources to cache on install
+const STATIC_ASSETS = [
+ './',
+ './index.html',
+ 'https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js',
+ 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js',
+ 'https://cdn.jsdelivr.net/npm/lucide@latest/dist/umd/lucide.min.js',
+ 'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap'
+];
+
+// Install event - cache static assets
+self.addEventListener('install', event => {
+ console.log('Service Worker: Installing...');
+
+ event.waitUntil(
+ caches.open(STATIC_CACHE_NAME)
+ .then(cache => {
+ console.log('Service Worker: Caching static assets');
+ return cache.addAll(STATIC_ASSETS);
+ })
+ .then(() => {
+ console.log('Service Worker: Static assets cached successfully');
+ return self.skipWaiting();
+ })
+ .catch(error => {
+ console.error('Service Worker: Failed to cache static assets:', error);
+ })
+ );
+});
+
+// Activate event - clean up old caches
+self.addEventListener('activate', event => {
+ console.log('Service Worker: Activating...');
+
+ event.waitUntil(
+ caches.keys()
+ .then(cacheNames => {
+ return Promise.all(
+ cacheNames.map(cacheName => {
+ if (cacheName !== CACHE_NAME && cacheName !== STATIC_CACHE_NAME) {
+ console.log('Service Worker: Deleting old cache:', cacheName);
+ return caches.delete(cacheName);
+ }
+ })
+ );
+ })
+ .then(() => {
+ console.log('Service Worker: Old caches cleaned up');
+ return self.clients.claim();
+ })
+ );
+});
+
+// Fetch event - serve from cache, fallback to network
+self.addEventListener('fetch', event => {
+ const { request } = event;
+ const url = new URL(request.url);
+
+ // Handle different types of requests
+ if (request.method !== 'GET') {
+ return; // Only handle GET requests
+ }
+
+ // For timetable data requests (if any API calls are made)
+ if (url.pathname.includes('timetable') || url.pathname.includes('api')) {
+ event.respondWith(
+ networkFirstWithCache(request, CACHE_NAME)
+ );
+ return;
+ }
+
+ // For static assets and main page
+ event.respondWith(
+ cacheFirstWithNetworkFallback(request)
+ );
+});
+
+// Cache-first strategy with network fallback (for static assets)
+async function cacheFirstWithNetworkFallback(request) {
+ try {
+ // Try to get from cache first
+ const cachedResponse = await caches.match(request);
+ if (cachedResponse) {
+ console.log('Service Worker: Serving from cache:', request.url);
+ return cachedResponse;
+ }
+
+ // If not in cache, fetch from network
+ console.log('Service Worker: Fetching from network:', request.url);
+ const networkResponse = await fetch(request);
+
+ // Cache the response for future use
+ if (networkResponse.ok) {
+ const cache = await caches.open(STATIC_CACHE_NAME);
+ cache.put(request, networkResponse.clone());
+ }
+
+ return networkResponse;
+ } catch (error) {
+ console.error('Service Worker: Fetch failed for:', request.url, error);
+
+ // Return a basic offline page or fallback
+ if (request.destination === 'document') {
+ return new Response(
+ '<!DOCTYPE html><html><head><title>Offline</title></head><body><h1>You are offline</h1><p>Please check your internet connection.</p></body></html>',
+ { headers: { 'Content-Type': 'text/html' } }
+ );
+ }
+
+ return new Response('Network error occurred', { status: 503 });
+ }
+}
+
+// Network-first strategy with cache fallback (for dynamic data)
+async function networkFirstWithCache(request, cacheName) {
+ try {
+ // Try network first
+ console.log('Service Worker: Trying network first for:', request.url);
+ const networkResponse = await fetch(request);
+
+ if (networkResponse.ok) {
+ // Cache the fresh response
+ const cache = await caches.open(cacheName);
+ cache.put(request, networkResponse.clone());
+ console.log('Service Worker: Cached fresh data:', request.url);
+ return networkResponse;
+ }
+
+ throw new Error('Network response not ok');
+ } catch (error) {
+ console.log('Service Worker: Network failed, trying cache for:', request.url);
+
+ // Fallback to cache
+ const cachedResponse = await caches.match(request);
+ if (cachedResponse) {
+ console.log('Service Worker: Serving stale data from cache:', request.url);
+ return cachedResponse;
+ }
+
+ // No cache available
+ console.error('Service Worker: No cache available for:', request.url);
+ return new Response('No cached data available', { status: 503 });
+ }
+}
+
+// Background sync for future enhancements
+self.addEventListener('sync', event => {
+ console.log('Service Worker: Background sync triggered:', event.tag);
+
+ if (event.tag === 'background-sync-timetable') {
+ event.waitUntil(
+ // Future: Sync timetable data in background
+ console.log('Service Worker: Background timetable sync completed')
+ );
+ }
+});
+
+// Handle push notifications (for future enhancements)
+self.addEventListener('push', event => {
+ console.log('Service Worker: Push notification received');
+
+ const options = {
+ body: event.data ? event.data.text() : 'Timetable update available',
+ icon: '/favicon.ico',
+ badge: '/favicon.ico',
+ vibrate: [100, 50, 100],
+ data: {
+ dateOfArrival: Date.now(),
+ primaryKey: 1
+ },
+ actions: [
+ {
+ action: 'explore',
+ title: 'View Timetable',
+ icon: '/favicon.ico'
+ },
+ {
+ action: 'close',
+ title: 'Close',
+ icon: '/favicon.ico'
+ }
+ ]
+ };
+
+ event.waitUntil(
+ self.registration.showNotification('VPPS Timetable', options)
+ );
+});
+
+// Handle notification clicks
+self.addEventListener('notificationclick', event => {
+ console.log('Service Worker: Notification clicked');
+
+ event.notification.close();
+
+ if (event.action === 'explore') {
+ event.waitUntil(
+ clients.openWindow('/')
+ );
+ }
+});
+
+console.log('Service Worker: Script loaded successfully');