-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
180 lines (159 loc) · 5.07 KB
/
script.js
File metadata and controls
180 lines (159 loc) · 5.07 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
let resources = {};
let favorites = JSON.parse(localStorage.getItem('favorites')) || [];
const subjectsNav = document.getElementById('subjects');
const contentSection = document.getElementById('content');
/* SUBJECT BUTTONS */
function createSubjectButtons(subjects) {
subjectsNav.innerHTML = '';
subjects.forEach(subject => {
const btn = document.createElement('button');
btn.textContent = subject;
btn.classList.add('subject-btn');
btn.onclick = () => {
document.querySelectorAll('.subject-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
showSubjectContent(subject);
};
subjectsNav.appendChild(btn);
});
}
/* SHOW SUBJECT CONTENT */
function showSubjectContent(subject) {
const data = resources[subject];
if (!data) {
contentSection.innerHTML = '<p>No data available.</p>';
return;
}
let html = `
<div class="top-bar">
<input type="text" id="searchInput" placeholder="Search papers..." oninput="filterPapers()" />
<button onclick="toggleDarkMode()">🌙 Dark Mode</button>
</div>
`;
/* NOTES */
if (data.notes?.length) {
html += `
<div class="resource-section">
<h2>Notes</h2>
<div class="resource-list">
${data.notes.map(n => `<a href="${n.url}" target="_blank">${n.title}</a>`).join('')}
</div>
</div>
`;
}
/* PAPERS */
if (data.papers?.length) {
html += `
<div class="resource-section">
<h2>Past Papers</h2>
<div id="papersList">
`;
data.papers.forEach(paper => {
const fav = favorites.includes(paper.url);
html += `
<div class="paper-item" data-title="${paper.title.toLowerCase()}">
<span class="paper-title">${paper.title}</span>
<div class="paper-actions">
<button onclick="viewPaper('${paper.url}')">View</button>
<a href="${paper.url}" download>Download</a>
<button onclick="toggleFavorite('${paper.url}')">
${fav ? '⭐' : '☆'}
</button>
</div>
</div>
`;
});
html += `
</div>
<div id="paper-viewer">
<p>Select a paper to view it.</p>
</div>
</div>
`;
}
/* VIDEOS */
if (data.videos?.length) {
html += `
<div class="resource-section">
<h2>Videos</h2>
${data.videos.map(v => `
<div class="video-item">
<p>${v.title}</p>
<iframe src="${v.url}" allowfullscreen></iframe>
</div>
`).join('')}
</div>
`;
}
contentSection.innerHTML = html;
}
/* VIEW PDF */
function viewPaper(url) {
document.getElementById('paper-viewer').innerHTML =
`<iframe src="${url}" class="pdf-frame"></iframe>`;
}
/* SEARCH */
function filterPapers() {
const q = document.getElementById('searchInput').value.toLowerCase();
document.querySelectorAll('.paper-item').forEach(item => {
item.style.display = item.dataset.title.includes(q) ? 'flex' : 'none';
});
}
/* FAVORITES */
function toggleFavorite(url) {
if (favorites.includes(url)) {
favorites = favorites.filter(f => f !== url);
} else {
favorites.push(url);
}
localStorage.setItem('favorites', JSON.stringify(favorites));
document.querySelector('.subject-btn.active')?.click();
}
/* DARK MODE */
function toggleDarkMode() {
document.body.classList.toggle('dark');
localStorage.setItem('darkMode', document.body.classList.contains('dark'));
}
/* LOAD DARK MODE */
if (localStorage.getItem('darkMode') === 'true') {
document.body.classList.add('dark');
}
/* LOAD DATA */
fetch('resources.json')
.then(res => res.json())
.then(data => {
resources = data;
createSubjectButtons(Object.keys(data).sort());
})
.catch(() => contentSection.innerHTML = '<p>Error loading data.</p>');
// PWA Install Button
let deferredPrompt;
const installBtn = document.getElementById("installBtn");
window.addEventListener("beforeinstallprompt", (e) => {
e.preventDefault(); // prevent auto prompt
deferredPrompt = e; // save the event
installBtn.style.display = "inline-block"; // show button above footer
});
installBtn.addEventListener("click", async () => {
installBtn.style.display = "none"; // hide button immediately
deferredPrompt.prompt(); // show install prompt
const choice = await deferredPrompt.userChoice; // optional: can check accept/decline
deferredPrompt = null; // reset
});
// Optional: hide button automatically if app is already installed
window.addEventListener("appinstalled", () => {
installBtn.style.display = "none";
});
const cacheStatus = document.getElementById("cacheStatus");
// Update when service worker is ready
if ("serviceWorker" in navigator) {
navigator.serviceWorker.ready.then(reg => {
cacheStatus.textContent = "Caching in progress… your content will be available offline soon.";
});
// Listen for messages from service worker (optional)
navigator.serviceWorker.addEventListener("message", event => {
if (event.data === "CACHE_COMPLETE") {
cacheStatus.textContent = "All content is now available offline!";
}
});
}