-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.html
More file actions
820 lines (710 loc) · 39.2 KB
/
set.html
File metadata and controls
820 lines (710 loc) · 39.2 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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hướng dẫn lắp ráp - BrickDocs</title>
<meta name="description" content="Xem và tải hướng dẫn lắp ráp đồ chơi trực tuyến miễn phí trên BrickDocs. Kho manual Lego, Qman, Keeppley, Sluban chất lượng cao.">
<meta name="keywords" content="BrickDocs, hướng dẫn lắp ráp, Lego, Qman, Keeppley, Sluban, instruction manual">
<meta name="author" content="Dat">
<meta property="og:title" content="Hướng dẫn lắp ráp - BrickDocs">
<meta property="og:description" content="Xem và tải hướng dẫn lắp ráp đồ chơi trực tuyến miễn phí trên BrickDocs.">
<meta property="og:type" content="article">
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/@phosphor-icons/web"></script>
<!-- Viewers Libs -->
<script src="https://unpkg.com/jszip/dist/jszip.min.js"></script>
<script src="https://unpkg.com/docx-preview/dist/docx-preview.min.js"></script>
<script src="https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.full.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pptxjs/1.21.1/css/pptxjs.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pptxjs/1.21.1/js/pptxjs.js"></script>
<!-- PDF.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js"></script>
<script>
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js';
</script>
<style>
@import url('https://fonts.googleapis.com/css2?family=Be+Vietnam+Pro:wght@300;400;500;600;700&display=swap');
body {
font-family: 'Be Vietnam Pro', sans-serif;
background-color: #f8fafc;
}
.glass-panel {
background: rgba(255, 255, 255, 0.95);
border-right: 1px solid #e2e8f0;
}
.nav-item.active {
background-color: #e0e7ff;
color: #4338ca;
font-weight: 600;
}
/* Scrollbar */
::-webkit-scrollbar {
width: 6px;
}
::-webkit-scrollbar-thumb {
background: rgba(156, 163, 175, 0.5);
border-radius: 3px;
}
/* Modal Animation */
.modal-enter {
opacity: 0;
transform: scale(0.95);
}
.modal-enter-active {
opacity: 1;
transform: scale(1);
transition: opacity 300ms, transform 300ms;
}
.modal-exit {
opacity: 1;
transform: scale(1);
}
.modal-exit-active {
opacity: 0;
transform: scale(0.95);
transition: opacity 200ms, transform 200ms;
}
</style>
</head>
<body class="bg-slate-50 text-slate-800 h-screen flex overflow-hidden">
<!-- PREVIEW MODAL (Hidden by default) -->
<div id="filePreviewModal" class="fixed inset-0 z-50 hidden">
<!-- Backdrop -->
<div class="absolute inset-0 bg-slate-900/60 backdrop-blur-sm transition-opacity" onclick="closeDocModal()">
</div>
<!-- Content -->
<div
class="absolute inset-4 md:inset-10 bg-white rounded-2xl shadow-2xl flex flex-col overflow-hidden transform transition-all">
<!-- Modal Header -->
<div class="px-4 py-3 border-b border-slate-100 flex items-center justify-between bg-slate-50/50">
<div class="flex items-center gap-3 overflow-hidden">
<span class="bg-red-100 text-red-600 p-2 rounded-lg shrink-0">
<i id="modalFileIcon" class="ph-fill ph-file text-lg"></i>
</span>
<h3 id="modalFileName" class="font-semibold text-slate-800 truncate">Filename.pdf</h3>
</div>
<div class="flex items-center gap-2 shrink-0">
<a id="modalReadingModeBtn" href="#" target="_blank"
class="hidden p-2 text-indigo-600 hover:bg-indigo-50 rounded-lg transition"
title="Chế độ đọc sách">
<i class="ph-bold ph-book-open text-xl"></i>
</a>
<a id="modalDownloadBtn" href="#"
class="p-2 text-slate-500 hover:text-red-600 hover:bg-red-50 rounded-lg transition"
title="Tải xuống">
<i class="ph-bold ph-download-simple text-xl"></i>
</a>
<button onclick="closeDocModal()"
class="p-2 text-slate-500 hover:text-red-600 hover:bg-red-50 rounded-lg transition"
title="Đóng">
<i class="ph-bold ph-x text-xl"></i>
</button>
</div>
</div>
<!-- Modal Body (Viewer) -->
<div id="modalViewerContent" class="flex-1 overflow-auto bg-slate-100 relative p-4">
<!-- Content will be injected here -->
</div>
</div>
</div>
<!-- MOBILE OVERLAY BACKDROP -->
<div id="sidebarBackdrop" onclick="toggleSidebar()"
class="fixed inset-0 bg-slate-900/50 backdrop-blur-sm z-40 hidden min-[1400px]:hidden transition-opacity"></div>
<!-- SIDEBAR -->
<aside id="sidebar"
class="w-72 glass-panel h-full fixed min-[1400px]:static inset-y-0 left-0 z-50 flex flex-col transform -translate-x-full min-[1400px]:translate-x-0 transition-transform duration-300 shadow-xl min-[1400px]:shadow-lg bg-white">
<!-- Logo -->
<!-- Logo & Close Button -->
<div class="p-6 flex items-center justify-between">
<div class="flex items-center gap-3 cursor-pointer" onclick="window.location.href='index.html'">
<div class="w-10 h-10 bg-red-600 rounded-xl flex items-center justify-center text-white shadow-lg">
<i class="ph-bold ph-books text-xl"></i>
</div>
<span class="font-bold text-xl text-slate-800">BrickDocs</span>
</div>
<!-- Close Sidebar Button (Mobile) -->
<button onclick="toggleSidebar()"
class="min-[1400px]:hidden p-2 text-slate-400 hover:text-slate-600 hover:bg-slate-100 rounded-lg transition">
<i class="ph-bold ph-x text-xl"></i>
</button>
</div>
<!-- Danh sách môn (Load từ JSON) -->
<div id="sidebarScrollContainer" class="flex-1 overflow-y-auto px-4 py-2">
<div class="text-xs font-bold text-slate-400 uppercase tracking-wider mb-3 pl-2">Chọn bộ lắp ráp</div>
<nav id="sidebarList" class="space-y-1">
<div class="text-center text-sm p-4"><i class="ph ph-spinner animate-spin"></i> Đang tải menu...</div>
</nav>
</div>
<div class="p-4 border-t border-slate-200">
<a href="index.html"
class="flex items-center gap-3 px-4 py-3 text-slate-500 hover:text-red-600 transition rounded-xl">
<i class="ph ph-arrow-left text-lg"></i> Trang chủ
</a>
</div>
</aside>
<!-- CONTENT -->
<div class="flex-1 flex flex-col h-full relative z-10 overflow-hidden">
<!-- Header -->
<header
class="h-16 border-b border-slate-200 bg-white flex items-center justify-between px-4 md:px-6 shrink-0 shadow-sm gap-3">
<div class="flex items-center gap-3 overflow-hidden">
<button onclick="toggleSidebar()"
class="min-[1400px]:hidden p-2 -ml-2 text-slate-500 hover:bg-slate-100 rounded-lg">
<i class="ph-bold ph-list text-2xl"></i>
</button>
<h1 class="text-lg font-bold text-slate-800 flex items-center gap-2 truncate" id="pageTitle">
Loading...
</h1>
</div>
<button id="downloadAllBtn"
class="hidden bg-red-600 hover:bg-red-700 text-white text-sm font-medium px-4 py-2 rounded-lg transition items-center gap-2">
<i class="ph-bold ph-download-simple"></i> Tải trọn bộ (.zip)
</button>
</header>
<main class="flex-1 flex overflow-hidden">
<!-- File List (Left) -->
<div class="w-full md:w-1/3 max-w-md border-r border-slate-200 bg-white flex flex-col">
<div
class="p-3 border-b border-slate-50 bg-slate-50/50 flex justify-between text-xs font-semibold text-slate-500 uppercase">
<span>Tên file</span>
<span>Ngày</span>
</div>
<div class="flex-1 overflow-y-auto p-3 space-y-1" id="fileList">
<!-- Files will be loaded here -->
</div>
</div>
<!-- Preview (Right) -->
<div class="flex-1 bg-slate-100 relative flex flex-col">
<div class="hidden md:block">
<div id="emptyState"
class="absolute inset-0 flex flex-col items-center justify-center text-slate-400">
<i class="ph ph-files text-5xl mb-4 text-slate-300"></i>
<p>Chọn tài liệu để xem</p>
</div>
</div>
<div id="viewerContainer"
class="hidden fixed inset-0 z-[60] md:static md:inset-auto flex-1 flex flex-col h-full bg-white md:bg-slate-200">
<!-- Toolbar -->
<div
class="h-16 md:h-12 bg-white border-b flex items-center justify-between px-4 shrink-0 shadow-sm md:shadow-none">
<button onclick="closePreview()"
class="md:hidden p-2 -ml-2 mr-2 text-slate-500 hover:bg-slate-100 rounded-lg">
<i class="ph-bold ph-arrow-left text-xl"></i>
</button>
<span id="previewName" class="font-medium text-sm truncate max-w-xs">Filename</span>
<div class="flex gap-2">
<a id="readingModeBtn" href="#" target="_blank" class="hidden p-2 hover:bg-indigo-50 text-indigo-600 rounded" title="Chế độ đọc sách">
<i class="ph ph-book-open"></i>
</a>
<a id="downloadBtn" href="#" class="p-2 hover:bg-slate-100 rounded">
<i class="ph ph-download-simple"></i>
</a>
<button onclick="closePreview()" class="p-2 hover:bg-red-50 text-red-500 rounded">
<i class="ph ph-x"></i>
</button>
</div>
</div>
<!-- Viewers -->
<div class="flex-1 relative overflow-hidden">
<div id="pdfFrame" class="w-full h-full hidden bg-white overflow-y-auto relative"></div>
<div id="docxFrame" class="w-full h-full hidden bg-white overflow-auto p-8"></div>
<div id="excelFrame" class="w-full h-full hidden bg-white overflow-auto p-4"></div>
<div id="pptFrame" class="w-full h-full hidden bg-white"></div>
</div>
</div>
</div>
</main>
</div>
<!-- CONFIRMATION MODAL -->
<div id="confirmModal" class="fixed inset-0 z-50 hidden">
<div class="absolute inset-0 bg-slate-900/50 backdrop-blur-sm transition-opacity" onclick="closeConfirmModal()">
</div>
<div
class="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-full max-w-md bg-white rounded-2xl shadow-xl p-6 transition-all transform scale-100">
<div class="text-center">
<div
class="w-12 h-12 bg-amber-100 text-amber-600 rounded-full flex items-center justify-center mx-auto mb-4">
<i class="ph-bold ph-warning text-2xl"></i>
</div>
<h3 class="text-lg font-bold text-slate-800 mb-2">Xác nhận tải xuống</h3>
<p class="text-slate-600 text-sm mb-6 leading-relaxed">
Hệ thống chuẩn bị tải xuống trọn bộ hướng dẫn <br> <span id="confirmSubjectName"
class="font-bold text-red-600"></span>.
<br><br>
<span class="block bg-orange-50 text-orange-800 p-3 rounded-lg border border-orange-100 text-left">
<i class="ph-bold ph-info mr-1"></i> <strong>Lưu ý:</strong> Dung lượng file có thể lớn. Vui
lòng ưu tiên sử dụng <b>Wi-Fi</b> để tránh gián đoạn hoặc phát sinh cước phí dữ liệu di động
(3G/4G).
</span>
</p>
<div class="flex items-center justify-center gap-2 mb-6">
<input type="checkbox" id="dontShowAgain"
class="w-4 h-4 rounded border-slate-300 text-red-600 focus:ring-red-500">
<label for="dontShowAgain" class="text-sm text-slate-600 select-none">Không nhắc lại thông báo
này</label>
</div>
<div class="flex gap-3">
<button onclick="closeConfirmModal()"
class="flex-1 px-4 py-2.5 bg-slate-100 hover:bg-slate-200 text-slate-700 font-medium rounded-xl transition">
Hủy
</button>
<button id="confirmBtn"
class="flex-1 px-4 py-2.5 bg-red-600 hover:bg-red-700 text-white font-medium rounded-xl transition shadow-lg shadow-red-200">
Tải xuống
</button>
</div>
</div>
</div>
</div>
<!-- JAVASCRIPT LOGIC -->
<script>
document.addEventListener('DOMContentLoaded', async () => {
// 1. Lấy ID từ URL (Ví dụ: subject.html?id=web -> lấy được 'web')
const params = new URLSearchParams(window.location.search);
const subjectId = params.get('id');
// 2. Load Sidebar (Chỉ tải danh sách tên môn - Rất nhẹ)
await loadSidebar(subjectId);
// 3. Load File chi tiết (Chỉ tải đúng file json của môn này)
if (subjectId) {
loadSubjectDetails(subjectId);
} else {
document.getElementById('pageTitle').innerText = "Vui lòng chọn bộ lắp ráp";
document.getElementById('fileList').innerHTML = '<div class="p-4 text-center text-slate-400">Chọn bộ lắp ráp bên trái</div>';
}
});
// --- MOBILE LAYOUT LOGIC ---
function toggleSidebar() {
const sidebar = document.getElementById('sidebar');
const backdrop = document.getElementById('sidebarBackdrop');
const isClosed = sidebar.classList.contains('-translate-x-full');
if (isClosed) {
sidebar.classList.remove('-translate-x-full');
backdrop.classList.remove('hidden');
} else {
sidebar.classList.add('-translate-x-full');
backdrop.classList.add('hidden');
}
}
// --- LOAD SIDEBAR ---
async function loadSidebar(activeId) {
try {
// Tải file brands.json nhẹ hều
const res = await fetch('data/brands.json');
const subjects = await res.json();
const container = document.getElementById('sidebarList');
container.innerHTML = '';
subjects.forEach(sub => {
const isActive = sub.id === activeId;
// Chú ý: href trỏ về chính file này nhưng đổi tham số ?id=...
const link = document.createElement('a');
link.href = `set.html?id=${sub.id}`;
link.className = `flex items-center gap-3 px-4 py-3 rounded-xl transition mb-1 ${isActive ? 'bg-red-50 text-red-700 font-semibold' : 'text-slate-500 hover:bg-slate-50'}`;
// Close sidebar on mobile when clicked
link.onclick = (e) => {
if (window.innerWidth < 768) {
toggleSidebar();
}
};
link.innerHTML = `
<i class="${sub.icon || 'ph-folder'} text-lg"></i>
<span class="truncate">${sub.name}</span>
`;
container.appendChild(link);
});
// Restore scroll position
const savedScroll = sessionStorage.getItem('sidebarScrollPos');
if (savedScroll) {
document.getElementById('sidebarScrollContainer').scrollTop = parseInt(savedScroll);
}
// Add scroll listener
document.getElementById('sidebarScrollContainer').addEventListener('scroll', (e) => {
sessionStorage.setItem('sidebarScrollPos', e.target.scrollTop);
});
} catch (e) {
console.error("Lỗi tải sidebar:", e);
}
}
// --- LOAD CHI TIẾT MÔN HỌC ---
async function loadSubjectDetails(id) {
const list = document.getElementById('fileList');
list.innerHTML = '<div class="p-4 text-center"><i class="ph ph-spinner animate-spin"></i> Đang tải dữ liệu...</div>';
try {
// QUAN TRỌNG: Chỉ fetch file json của set đó
const res = await fetch(`data/sets/${id}.json`);
if (!res.ok) throw new Error("Không tìm thấy bộ lắp ráp");
const data = await res.json();
// Cập nhật giao diện
document.title = `${data.name} - BrickDocs`;
document.getElementById('pageTitle').innerHTML = `<i class="ph-fill ph-folder-open text-red-600"></i> ${data.name}`;
// Update meta description dynamically
let metaDesc = document.querySelector('meta[name="description"]');
if (metaDesc) metaDesc.setAttribute('content', `Hướng dẫn lắp ráp ${data.name} - Tải và xem trực tuyến miễn phí trên BrickDocs. ${data.description || ''}`);
let metaOgTitle = document.querySelector('meta[property="og:title"]');
if (metaOgTitle) metaOgTitle.setAttribute('content', `${data.name} - BrickDocs`);
let metaOgDesc = document.querySelector('meta[property="og:description"]');
if (metaOgDesc) metaOgDesc.setAttribute('content', `Hướng dẫn lắp ráp ${data.name}. ${data.description || 'Xem và tải miễn phí trên BrickDocs.'}`);
// --- SETUP DOWNLOAD ALL BUTTON ---
const dlBtn = document.getElementById('downloadAllBtn');
if (data.files && data.files.length > 0) {
dlBtn.classList.remove('hidden');
dlBtn.classList.add('flex');
dlBtn.onclick = () => handleDownloadClick(data);
} else {
dlBtn.classList.add('hidden');
dlBtn.classList.remove('flex');
}
// Render Files
list.innerHTML = '';
// --- NOTIFICATION FOR INTERNSHIP SUBJECTS ---
const internshipIds = ['internship', 'tsnn', 'ktcn'];
if (internshipIds.includes(id)) {
const alert = document.createElement('div');
alert.className = "mb-4 p-4 bg-amber-50 border border-amber-200 rounded-lg text-sm text-amber-800 flex gap-3";
alert.innerHTML = `
<i class="ph-fill ph-warning-circle text-lg shrink-0 mt-0.5"></i>
<div>
<strong>Lưu ý:</strong> Đây là biểu mẫu áp dụng cho <br><strong>HK2/2025-2026</strong>. <br/>
Các học kỳ khác vui lòng chỉ tham khảo, biểu mẫu có thể thay đổi tùy theo quy định của Khoa tại từng thời điểm.
</div>
`;
list.appendChild(alert);
}
// --- GENERIC NOTE/LINK NOTIFICATION ---
if (data.noteHtml) {
const note = document.createElement('div');
note.className = "mb-4 p-4 bg-red-50 border border-red-200 rounded-lg text-sm text-red-800 flex gap-3";
note.innerHTML = `
<i class="ph-fill ph-info text-lg shrink-0 mt-0.5"></i>
<div>${data.noteHtml}</div>
`;
list.appendChild(note);
}
if (data.files.length === 0) {
list.innerHTML = '<div class="p-4 text-center text-slate-400">Chưa có tài liệu.</div>';
return;
}
// --- GLOBAL STORAGE FOR ONCLICK HANDLERS ---
window.currentSubjectFiles = data.files;
data.files.forEach((file, index) => {
const icon = getIcon(file.type);
const el = document.createElement('div');
const sizeId = 'size-' + index;
el.className = "group flex items-center justify-between p-3 rounded-xl border border-slate-100 bg-white hover:border-red-200 hover:shadow-sm transition cursor-pointer";
// Add click handler to the main container
el.onclick = (e) => previewFile(window.currentSubjectFiles[index]);
el.innerHTML = `
<div class="flex items-center gap-3 overflow-hidden">
<div class="w-10 h-10 rounded-lg ${icon.bg} ${icon.text} flex items-center justify-center shrink-0">
<i class="${icon.class} text-xl"></i>
</div>
<div class="flex flex-col min-w-0">
<span class="font-medium text-slate-700 truncate text-sm">${file.name}</span>
<span class="text-xs text-slate-400"><span id="${sizeId}">${file.size || 'Checking...'}</span> - ${file.date || ''}</span>
</div>
</div>
<div class="flex items-center gap-2">
<!-- Preview Button (Popup) - Visible on small screens -->
<button onclick="event.stopPropagation(); openDocModal(window.currentSubjectFiles[${index}])" class="min-[1300px]:hidden p-2 text-slate-400 hover:bg-red-50 hover:text-red-600 rounded-lg transition" title="Xem trước">
<i class="ph-bold ph-eye text-lg"></i>
</button>
${file.type === 'pdf' ? `
<a href="reader.html?file=${encodeURIComponent(file.url)}&id=${id}" target="_blank" onclick="event.stopPropagation()" class="p-2 text-indigo-400 hover:bg-indigo-50 hover:text-indigo-600 rounded-lg transition" title="Chế độ đọc sách">
<i class="ph-bold ph-book-open text-lg"></i>
</a>
` : ''}
<a href="${file.url}" download onclick="event.stopPropagation()" class="p-2 text-slate-400 hover:bg-slate-100 hover:text-slate-600 rounded-lg transition" title="Tải xuống">
<i class="ph-bold ph-download-simple text-lg"></i>
</a>
</div>
`;
list.appendChild(el);
// --- TỰ ĐỘNG LẤY SIZE ---
if (file.size) {
const sizeEl = document.getElementById(sizeId);
if (sizeEl) sizeEl.innerText = file.size;
} else {
// Only fetch if no size provided
setTimeout(() => {
fetch(file.url, { method: 'HEAD' })
.then(response => {
const sizeEl = document.getElementById(sizeId);
if (!sizeEl) return;
if (response.ok) {
const bytes = response.headers.get('content-length');
sizeEl.innerText = bytes ? formatBytes(bytes) : 'Unknown';
} else {
sizeEl.innerText = 'Error';
}
})
.catch(() => {
const sizeEl = document.getElementById(sizeId);
if (sizeEl) sizeEl.innerText = 'Error';
});
}, 0);
}
});
} catch (e) {
list.innerHTML = `<div class="p-4 text-center text-red-400">Lỗi: Không tìm thấy dữ liệu cho set "${id}"</div>`;
}
}
// --- PREVIEW LOGIC (Refactored) ---
async function previewFile(file) {
// Desktop/Large Screen Preview
if (window.innerWidth >= 1300) {
const container = document.getElementById('viewerContainer');
const empty = document.getElementById('emptyState');
empty.classList.add('hidden');
container.classList.remove('hidden');
document.getElementById('previewName').innerText = file.name;
document.getElementById('downloadBtn').href = file.url;
// Reading Mode Button
const readingModeBtn = document.getElementById('readingModeBtn');
if (file.type === 'pdf') {
const params = new URLSearchParams(window.location.search);
const subjectId = params.get('id');
readingModeBtn.href = `reader.html?file=${encodeURIComponent(file.url)}&id=${subjectId}`;
readingModeBtn.classList.remove('hidden');
} else {
readingModeBtn.classList.add('hidden');
}
// Clear and Render
['pdfFrame', 'docxFrame', 'excelFrame', 'pptFrame'].forEach(id => {
const el = document.getElementById(id);
el.style.display = 'none';
el.innerHTML = '';
if (id === 'pdfFrame') el.src = '';
});
await renderFileContent(file, {
pdf: document.getElementById('pdfFrame'),
docx: document.getElementById('docxFrame'),
excel: document.getElementById('excelFrame'),
ppt: document.getElementById('pptFrame')
});
} else {
// Open Modal for < 1300px
openDocModal(file);
}
}
async function openDocModal(file) {
const modal = document.getElementById('filePreviewModal');
const content = document.getElementById('modalViewerContent');
document.getElementById('modalFileName').innerText = file.name;
document.getElementById('modalDownloadBtn').href = file.url;
// Reading Mode logic for Modal
const modalReadingBtn = document.getElementById('modalReadingModeBtn');
if (file.type === 'pdf') {
const params = new URLSearchParams(window.location.search);
const subjectId = params.get('id');
modalReadingBtn.href = `reader.html?file=${encodeURIComponent(file.url)}&id=${subjectId}`;
modalReadingBtn.classList.remove('hidden');
} else {
modalReadingBtn.classList.add('hidden');
}
const iconInfo = getIcon(file.type);
document.getElementById('modalFileIcon').className = `ph-fill ${iconInfo.class} text-lg`;
content.innerHTML = ''; // Clear previous content
modal.classList.remove('hidden');
// Create temporary containers for the generic renderer
const tempContainer = document.createElement('div');
tempContainer.className = 'w-full h-full bg-white rounded-xl shadow-sm overflow-hidden';
content.appendChild(tempContainer);
// Reuse generic renderer for consistent behavior (including PDF.js fix)
await renderFileContent(file, {
pdf: tempContainer,
docx: tempContainer,
excel: tempContainer,
ppt: tempContainer
});
}
function closeDocModal() {
document.getElementById('filePreviewModal').classList.add('hidden');
document.getElementById('modalViewerContent').innerHTML = ''; // Cleanup
}
// Helper to keep main logic clean (renamed original logic for desktop context)
async function renderFileContent(file, elements) {
const loading = `<div class="flex h-full items-center justify-center text-slate-400 gap-2"><i class="ph ph-spinner animate-spin"></i> Đang tải...</div>`;
// Track current file to prevent race conditions
window.currentRenderUrl = file.url;
if (file.type === 'pdf') {
elements.pdf.style.display = 'block';
elements.pdf.innerHTML = loading;
try {
const loadingTask = pdfjsLib.getDocument(file.url);
const pdf = await loadingTask.promise;
// If user switched file while loading document
if (window.currentRenderUrl !== file.url) return;
elements.pdf.innerHTML = '';
elements.pdf.className = 'w-full h-full overflow-y-auto bg-slate-100 flex flex-col items-center gap-4 p-4';
// Enable momentum scrolling for iOS
elements.pdf.style.webkitOverflowScrolling = 'touch';
for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
// Stop if user switched file
if (window.currentRenderUrl !== file.url) break;
const page = await pdf.getPage(pageNum);
// Double check after await
if (window.currentRenderUrl !== file.url) break;
// Scale based on container width
const viewport = page.getViewport({ scale: 1.5 });
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
canvas.className = 'shadow-lg bg-white rounded-lg max-w-full';
const renderContext = {
canvasContext: context,
viewport: viewport
};
await page.render(renderContext).promise;
// Final check before appending
if (window.currentRenderUrl === file.url) {
elements.pdf.appendChild(canvas);
}
}
} catch (error) {
console.error('PDF Render Error:', error);
elements.pdf.innerHTML = `
<div class="flex flex-col items-center justify-center h-full text-slate-500">
<i class="ph-duotone ph-warning-circle text-4xl mb-2"></i>
<p>Không thể xem trước file này.</p>
<a href="${file.url}" class="text-red-600 hover:underline mt-2">Tải xuống để xem</a>
</div>
`;
}
}
else if (file.type === 'word' || file.type === 'docx') {
elements.docx.style.display = 'block';
elements.docx.innerHTML = loading;
// Fix for iOS scrolling
elements.docx.style.webkitOverflowScrolling = 'touch';
elements.docx.classList.add('overflow-y-auto');
try {
const blob = await (await fetch(file.url)).blob();
elements.docx.innerHTML = '';
await docx.renderAsync(blob, elements.docx, null, { inWrapper: false });
} catch (e) { elements.docx.innerHTML = 'Lỗi tải Word'; }
}
else if (file.type === 'excel') {
elements.excel.style.display = 'block';
elements.excel.innerHTML = loading;
// Fix for iOS scrolling
elements.excel.style.webkitOverflowScrolling = 'touch';
elements.excel.classList.add('overflow-y-auto');
try {
const ab = await (await fetch(file.url)).arrayBuffer();
const wb = XLSX.read(ab);
const html = XLSX.utils.sheet_to_html(wb.Sheets[wb.SheetNames[0]]);
elements.excel.innerHTML = html;
elements.excel.querySelector('table').style.borderCollapse = 'collapse';
} catch (e) { elements.excel.innerHTML = 'Lỗi tải Excel'; }
}
else if (file.type === 'ppt') {
elements.ppt.style.display = 'block';
elements.ppt.innerHTML = `
<div class="p-3 text-center text-amber-700 bg-amber-50 border-b border-amber-100 text-sm">
<i class="ph-bold ph-warning-circle"></i> Trình duyệt không hỗ trợ hiển thị trực tiếp file pptx, vui lòng tải xuống để xem nội dung chi tiết
</div>
<div id="pptx-target"></div>
`;
$("#pptx-target").pptxToHtml({
pptxFileUrl: file.url,
slidesScale: "100%",
slideMode: false,
keyBoardShortCut: false
});
}
}
function closePreview() {
document.getElementById('viewerContainer').classList.add('hidden');
document.getElementById('emptyState').classList.remove('hidden');
document.getElementById('pdfFrame').src = 'about:blank';
}
function handleDownloadClick(data) {
const dontShow = localStorage.getItem('hideDownloadConfirm');
if (dontShow === 'true') {
performDownload(data);
} else {
showConfirmModal(data);
}
}
function showConfirmModal(data) {
const modal = document.getElementById('confirmModal');
const nameSpan = document.getElementById('confirmSubjectName');
const confirmBtn = document.getElementById('confirmBtn');
nameSpan.innerText = data.name;
modal.classList.remove('hidden');
// Reset checkbox
document.getElementById('dontShowAgain').checked = false;
confirmBtn.onclick = () => {
const isChecked = document.getElementById('dontShowAgain').checked;
if (isChecked) {
localStorage.setItem('hideDownloadConfirm', 'true');
}
closeConfirmModal();
performDownload(data);
};
}
function closeConfirmModal() {
document.getElementById('confirmModal').classList.add('hidden');
}
async function performDownload(data) {
const btn = document.getElementById('downloadAllBtn');
const originalText = btn.innerHTML;
btn.innerHTML = `<i class="ph ph-spinner animate-spin"></i> Đang nén...`;
btn.disabled = true;
try {
const zip = new JSZip();
// Create a folder inside the zip with the subject name
const folder = zip.folder(data.name);
// Helper to fetch valid files
const fetchFile = async (file) => {
try {
const response = await fetch(file.url);
if (!response.ok) throw new Error('Network error');
const blob = await response.blob();
folder.file(file.name, blob);
} catch (e) {
console.error(`Lỗi tải file ${file.name}:`, e);
// Optional: Write an error log file in the zip
folder.file(`ERROR_${file.name}.txt`, `Không thể tải file này: ${e.message}`);
}
};
// Execute all fetches in parallel
await Promise.all(data.files.map(fetchFile));
// Generate ZIP
const content = await zip.generateAsync({ type: "blob" });
// Trigger Download
const a = document.createElement("a");
a.href = URL.createObjectURL(content);
a.download = `${data.name}.zip`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(a.href);
} catch (e) {
console.error("Zip Error:", e);
alert("Có lỗi xảy ra khi tạo file nén. Vui lòng thử lại.");
} finally {
btn.innerHTML = originalText;
btn.disabled = false;
}
}
function getIcon(type) {
if (type === 'pdf') return { class: 'ph-fill ph-file-pdf', bg: 'bg-rose-100', text: 'text-rose-600' };
if (type === 'word') return { class: 'ph-fill ph-file-doc', bg: 'bg-blue-100', text: 'text-blue-600' };
if (type === 'excel') return { class: 'ph-fill ph-file-xls', bg: 'bg-emerald-100', text: 'text-emerald-600' };
if (type === 'ppt') return { class: 'ph-fill ph-file-ppt', bg: 'bg-orange-100', text: 'text-orange-600' };
return { class: 'ph-fill ph-file', bg: 'bg-slate-100', text: 'text-slate-600' };
}
function formatBytes(bytes, decimals = 2) {
if (!+bytes) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
}
</script>
</body>
</html>