-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunread-papers.html
More file actions
229 lines (212 loc) · 10.6 KB
/
unread-papers.html
File metadata and controls
229 lines (212 loc) · 10.6 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Unread Interesting Papers</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
<link rel="icon" type="image/png" href="scholarsphere-icon.png">
</head>
<body>
<div class="container">
<h1 class="text-center my-3">Unread Papers</h1>
<div class="mb-3 d-flex flex-wrap gap-2">
<a href="index.html" class="btn btn-secondary mr-2 mb-2">Read Papers</a>
<a href="unread-papers.html" class="btn btn-primary mr-2 mb-2 disabled" tabindex="-1" aria-disabled="true">Unread Papers</a>
<a href="datasets.html" class="btn btn-warning mr-2 mb-2">Datasets</a>
<a href="topics.html" class="btn btn-dark mr-2 mb-2">Topics</a>
<a href="useful-links.html" class="btn btn-info mb-2">Useful Links</a>
</div>
<div class="card mb-4 subitems-panel">
<div class="card-body">
<button type="button" id="unread-form-toggle" class="btn btn-primary btn-sm mb-3">Add to Unread</button>
<div id="unread-form-panel" style="display:none;">
<form id="unread-form" class="form-inline flex-wrap gap-2">
<div class="form-group mr-2 mb-2">
<label class="sr-only" for="unread-title">Title</label>
<input type="text" id="unread-title" class="form-control" placeholder="Paper title" required>
</div>
<div class="form-group mr-2 mb-2">
<label class="sr-only" for="unread-priority">Priority</label>
<select id="unread-priority" class="form-control">
<option value="3">3 - Highest</option>
<option value="2" selected>2 - Default</option>
<option value="1">1 - Lowest</option>
</select>
</div>
<div class="form-group mr-2 mb-2">
<label class="sr-only" for="unread-link">Link</label>
<input type="text" id="unread-link" class="form-control" placeholder="Link or file (optional)">
</div>
<button type="submit" class="btn btn-primary mb-2">Add to Unread</button>
</form>
</div>
</div>
</div>
<div class="card">
<div class="card-body">
<h3>To-Read Queue</h3>
<div id="priority-counts" class="mb-2 text-muted small"></div>
<div id="unread-list" class="list-group mt-3"></div>
</div>
</div>
</div>
<script>
const DEFAULT_RATING = 3;
const MIN_PRIORITY = 1;
const MAX_PRIORITY = 3;
const UNREAD_ENDPOINT = '/unread-list';
let unreadCache = [];
let isUnreadFormVisible = false;
function setUnreadFormVisible(visible, options = {}) {
const panel = document.getElementById('unread-form-panel');
const toggleBtn = document.getElementById('unread-form-toggle');
if (!panel || !toggleBtn) return;
isUnreadFormVisible = visible;
panel.style.display = visible ? 'block' : 'none';
toggleBtn.textContent = visible ? 'Hide Form' : 'Add to Unread';
if (visible && options.focusTitle) {
document.getElementById('unread-title')?.focus();
}
}
function renderStars(rating) {
const safeRating = Math.max(MIN_PRIORITY, Math.min(MAX_PRIORITY, parseInt(rating, 10) || DEFAULT_RATING));
return '⭐'.repeat(safeRating);
}
function normalizeRating(value) {
const parsed = parseInt(value, 10);
if (isNaN(parsed) || parsed < MIN_PRIORITY) return DEFAULT_RATING;
return Math.max(MIN_PRIORITY, Math.min(MAX_PRIORITY, parsed));
}
function coerceAddedTime(item) {
if (item.addedTime) return item.addedTime;
// Fallback: if id looks like a timestamp, reuse it
if (typeof item.id === 'number' && item.id > 1e12) {
return new Date(item.id).toISOString();
}
return new Date().toISOString();
}
function formatAddedTime(value) {
const d = new Date(value);
return isNaN(d.getTime()) ? 'Unknown' : d.toLocaleString();
}
async function fetchUnreadList() {
try {
const resp = await fetch(UNREAD_ENDPOINT, { cache: 'no-store' });
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
const data = await resp.json();
const items = Array.isArray(data.items) ? data.items.map(it => ({
...it,
priority: normalizeRating(it.priority),
addedTime: coerceAddedTime(it)
})) : [];
unreadCache = items;
return items;
} catch (e) {
console.error('Failed to load unread list:', e);
return unreadCache || [];
}
}
async function saveUnreadList(list) {
try {
const resp = await fetch(UNREAD_ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ items: list })
});
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
unreadCache = list;
return true;
} catch (e) {
console.error('Failed to save unread list:', e);
alert('Could not save unread list. Please try again.');
return false;
}
}
async function addUnreadPaper(event) {
event.preventDefault();
const titleInput = document.getElementById('unread-title');
const priorityInput = document.getElementById('unread-priority');
const linkInput = document.getElementById('unread-link');
const title = (titleInput.value || '').trim();
const priority = normalizeRating(priorityInput.value || DEFAULT_RATING);
const link = (linkInput.value || '').trim();
if (!title) return;
const now = new Date().toISOString();
const list = [...unreadCache, { id: Date.now(), title, priority, link, addedTime: now }];
const ok = await saveUnreadList(list);
if (!ok) return;
titleInput.value = '';
linkInput.value = '';
priorityInput.value = DEFAULT_RATING;
setUnreadFormVisible(false);
renderUnreadList();
}
async function deleteUnreadPaper(id) {
const list = unreadCache.filter(item => item.id !== id);
const ok = await saveUnreadList(list);
if (ok) renderUnreadList();
}
async function updatePriority(id, newPriority) {
const priority = normalizeRating(newPriority);
const list = unreadCache.map(item => item.id === id ? { ...item, priority } : item);
const ok = await saveUnreadList(list);
if (ok) renderUnreadList();
}
function renderUnreadList(listOverride) {
const container = document.getElementById('unread-list');
const list = (listOverride || unreadCache).slice().sort((a, b) => {
const prioDiff = b.priority - a.priority;
if (prioDiff !== 0) return prioDiff;
const aTime = new Date(a.addedTime || a.id || 0).getTime();
const bTime = new Date(b.addedTime || b.id || 0).getTime();
if (bTime !== aTime) return bTime - aTime;
return (a.title || '').localeCompare(b.title || '');
});
if (!container) return;
container.innerHTML = '';
const countsEl = document.getElementById('priority-counts');
const counts = {1:0,2:0,3:0};
list.forEach(item => { counts[item.priority] = (counts[item.priority] || 0) + 1; });
if (countsEl) {
const parts = [3,2,1].map(p => `${p}: ${counts[p] || 0}`).join(' | ');
countsEl.textContent = `Counts by priority — ${parts}`;
}
if (list.length === 0) {
container.innerHTML = '<div class="list-group-item text-muted">No unread papers yet.</div>';
return;
}
list.forEach(item => {
const row = document.createElement('div');
row.className = 'list-group-item d-flex justify-content-between align-items-center flex-wrap';
const left = document.createElement('div');
left.innerHTML = `
<div><strong>${item.title}</strong></div>
<div class="d-flex align-items-center flex-wrap">
<span class="mr-2">Priority:</span>
<select class="form-control form-control-sm w-auto mr-2" onchange="updatePriority(${item.id}, this.value)">
${[3,2,1].map(v => `<option value="${v}" ${v === item.priority ? 'selected' : ''}>${v}</option>`).join('')}
</select>
<span>${renderStars(item.priority)} (${item.priority}/${MAX_PRIORITY})</span>
</div>
<div class="text-muted small">Added: ${formatAddedTime(item.addedTime)}</div>
${item.link ? `<div><a href="${item.link}" target="_blank" rel="noopener">Open</a></div>` : ''}
`;
const btn = document.createElement('button');
btn.className = 'btn btn-sm btn-outline-success mt-2 mt-sm-0';
btn.textContent = 'Mark as Read (Delete)';
btn.onclick = () => deleteUnreadPaper(item.id);
row.appendChild(left);
row.appendChild(btn);
container.appendChild(row);
});
}
document.getElementById('unread-form-toggle').addEventListener('click', () => {
setUnreadFormVisible(!isUnreadFormVisible, { focusTitle: !isUnreadFormVisible });
});
document.getElementById('unread-form').addEventListener('submit', addUnreadPaper);
fetchUnreadList().then(renderUnreadList);
</script>
</body>
</html>