-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopics.html
More file actions
291 lines (251 loc) · 12.2 KB
/
Copy pathtopics.html
File metadata and controls
291 lines (251 loc) · 12.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Topics</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">Topics</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">Unread Papers</a>
<a href="datasets.html" class="btn btn-warning mr-2 mb-2">Datasets</a>
<a href="topics.html" class="btn btn-secondary mr-2 mb-2 disabled" tabindex="-1" aria-disabled="true">Topics</a>
<a href="useful-links.html" class="btn btn-info mr-2 mb-2">Useful Links</a>
</div>
<div class="card mb-4 subitems-panel">
<div class="card-body">
<button type="button" id="topic-form-toggle" class="btn btn-success btn-sm mb-2">Add Topic</button>
<div id="topic-form-panel" style="display:none;">
<form id="topic-form" class="form-inline flex-wrap gap-2">
<div class="form-group mr-2 mb-2">
<label class="sr-only" for="topic-name">Topic Name</label>
<input type="text" id="topic-name" class="form-control" placeholder="e.g., Robotics, World Models" required>
</div>
<div class="form-group mr-2 mb-2 flex-grow-1">
<label class="sr-only" for="topic-description">Description</label>
<input type="text" id="topic-description" class="form-control w-100" placeholder="Brief description">
</div>
<button type="submit" id="topic-submit-btn" class="btn btn-success mb-2">Add Topic</button>
<button type="button" id="topic-cancel-edit" class="btn btn-outline-secondary mb-2 ml-2" style="display:none;">Cancel</button>
</form>
<small id="topic-editing-hint" class="text-muted" style="display:none;">Editing topic. Click Add Topic to save or Cancel.</small>
</div>
</div>
</div>
<div class="card">
<div class="card-body">
<h3>Your Topics</h3>
<div id="topics-count" class="mb-2 text-muted small"></div>
<div id="topics-list" class="list-group mt-3"></div>
</div>
</div>
</div>
<script>
const TOPICS_ENDPOINT = '/topics';
let topics = [];
let editingId = null;
let isTopicFormVisible = false;
function setTopicFormVisible(visible, options = {}) {
const panel = document.getElementById('topic-form-panel');
const toggleBtn = document.getElementById('topic-form-toggle');
if (!panel || !toggleBtn) return;
isTopicFormVisible = visible;
panel.style.display = visible ? 'block' : 'none';
toggleBtn.textContent = visible ? 'Hide Form' : 'Add Topic';
if (visible && options.focusName) {
document.getElementById('topic-name')?.focus();
}
}
function slugify(text) {
return text
.toString()
.toLowerCase()
.trim()
.replace(/\s+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-');
}
function normalizeTopic(raw) {
return {
id: raw.id || Date.now(),
name: (raw.name || '').trim() || 'Untitled Topic',
description: (raw.description || '').trim(),
slug: raw.slug || slugify(raw.name || 'untitled'),
createdAt: raw.createdAt || new Date().toISOString(),
updatedAt: raw.updatedAt
};
}
function formatDateTime(value) {
if (!value) return 'Never';
const d = new Date(value);
return isNaN(d.getTime()) ? 'Unknown' : d.toLocaleString();
}
async function fetchTopics() {
try {
const resp = await fetch(TOPICS_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(normalizeTopic) : [];
topics = items;
return items;
} catch (e) {
console.error('Failed to load topics:', e);
return topics || [];
}
}
async function saveTopics(list) {
try {
const resp = await fetch(TOPICS_ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ items: list })
});
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
topics = list;
return true;
} catch (e) {
console.error('Failed to save topics:', e);
alert('Could not save topics. Please ensure the server is running and try again.');
return false;
}
}
async function addTopic(event) {
event.preventDefault();
const nameInput = document.getElementById('topic-name');
const descInput = document.getElementById('topic-description');
const name = (nameInput.value || '').trim();
const description = (descInput.value || '').trim();
if (!name) {
alert('Please provide a topic name.');
return;
}
const now = new Date().toISOString();
const slug = editingId ? topics.find(t => t.id === editingId)?.slug : slugify(name);
const updatedItem = {
id: editingId || Date.now(),
name,
description,
slug,
createdAt: editingId ? (topics.find(t => t.id === editingId)?.createdAt || now) : now,
updatedAt: editingId ? now : undefined
};
const list = editingId
? topics.map(item => item.id === editingId ? updatedItem : item)
: [...topics, updatedItem];
const ok = await saveTopics(list);
if (!ok) return;
editingId = null;
document.getElementById('topic-submit-btn').textContent = 'Add Topic';
document.getElementById('topic-cancel-edit').style.display = 'none';
document.getElementById('topic-editing-hint').style.display = 'none';
nameInput.value = '';
descInput.value = '';
setTopicFormVisible(false);
renderTopics();
}
async function deleteTopic(id) {
if (!confirm('Are you sure you want to delete this topic?')) return;
const list = topics.filter(item => item.id !== id);
const ok = await saveTopics(list);
if (ok) renderTopics();
}
function startEditTopic(item) {
editingId = item.id;
setTopicFormVisible(true);
document.getElementById('topic-name').value = item.name || '';
document.getElementById('topic-description').value = item.description || '';
document.getElementById('topic-submit-btn').textContent = 'Update Topic';
document.getElementById('topic-cancel-edit').style.display = 'inline-block';
document.getElementById('topic-editing-hint').style.display = 'inline';
window.scrollTo({ top: 0, behavior: 'smooth' });
}
function cancelEditTopic() {
editingId = null;
document.getElementById('topic-form').reset();
document.getElementById('topic-submit-btn').textContent = 'Add Topic';
document.getElementById('topic-cancel-edit').style.display = 'none';
document.getElementById('topic-editing-hint').style.display = 'none';
setTopicFormVisible(false);
}
function viewTopic(slug) {
window.location.href = `topic-view.html?slug=${encodeURIComponent(slug)}`;
}
function renderTopics() {
const list = topics
.slice()
.sort((a, b) => {
const aTime = new Date(a.updatedAt || a.createdAt).getTime();
const bTime = new Date(b.updatedAt || b.createdAt).getTime();
if (bTime !== aTime) return bTime - aTime;
return (a.name || '').localeCompare(b.name || '');
});
const container = document.getElementById('topics-list');
if (!container) return;
container.innerHTML = '';
const countEl = document.getElementById('topics-count');
if (countEl) {
countEl.textContent = `Total topics: ${list.length}`;
}
if (list.length === 0) {
container.innerHTML = '<div class="list-group-item text-muted">No topics yet. Create one above!</div>';
return;
}
list.forEach(item => {
const row = document.createElement('div');
row.className = 'list-group-item d-flex justify-content-between align-items-start flex-wrap';
const left = document.createElement('div');
left.className = 'flex-grow-1';
const titleLink = document.createElement('a');
titleLink.href = '#';
titleLink.className = 'font-weight-bold';
titleLink.style.fontSize = '1.1rem';
titleLink.textContent = item.name;
titleLink.onclick = (e) => {
e.preventDefault();
viewTopic(item.slug);
};
const desc = document.createElement('div');
desc.className = 'text-muted';
desc.textContent = item.description || 'No description';
const meta = document.createElement('div');
meta.className = 'text-muted small mt-1';
const modifiedTime = item.updatedAt || item.createdAt;
meta.innerHTML = `
<span>Created: ${formatDateTime(item.createdAt)}</span>
${item.updatedAt ? ` | <span>Modified: ${formatDateTime(item.updatedAt)}</span>` : ''}
`;
left.appendChild(titleLink);
left.appendChild(desc);
left.appendChild(meta);
const actions = document.createElement('div');
actions.className = 'd-flex flex-wrap gap-2 mt-2 mt-sm-0';
const editBtn = document.createElement('button');
editBtn.className = 'btn btn-sm btn-outline-secondary mr-2';
editBtn.textContent = 'Edit';
editBtn.onclick = () => startEditTopic(item);
const deleteBtn = document.createElement('button');
deleteBtn.className = 'btn btn-sm btn-outline-danger';
deleteBtn.textContent = 'Delete';
deleteBtn.onclick = () => deleteTopic(item.id);
actions.appendChild(editBtn);
actions.appendChild(deleteBtn);
row.appendChild(left);
row.appendChild(actions);
container.appendChild(row);
});
}
document.getElementById('topic-form-toggle').addEventListener('click', () => {
setTopicFormVisible(!isTopicFormVisible, { focusName: !isTopicFormVisible });
});
document.getElementById('topic-form').addEventListener('submit', addTopic);
document.getElementById('topic-cancel-edit').addEventListener('click', cancelEditTopic);
fetchTopics().then(renderTopics);
</script>
</body>
</html>