Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
465 lines (423 loc) · 15.8 KB
/
Copy pathscript.js
File metadata and controls
465 lines (423 loc) · 15.8 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
// ========== State ==========
let map;
let markers = {};
let selectedLatLng = null;
let isAddingMode = false;
let echoes = [];
let activeEchoId = null;
let editingEchoId = null;
let draftPhoto = '';
const STORAGE_KEY = 'localecho_echoes_v1';
// Sample seeds so the map isn't empty on first visit
const SEED_ECHOES = [
{
id: 'seed1',
lat: 31.5204,
lng: 74.3587,
title: 'The first monsoon I remember',
story: 'I was seven. We ran outside the house on Jail Road when the rain started. My father held a newspaper over our heads and we still got soaked. The smell of wet earth still brings me back.',
author: 'Ayesha',
date: '2023-07-12'
},
{
id: 'seed2',
lat: 31.5497,
lng: 74.3436,
title: 'Late-night chai after exams',
story: 'Every final week we ended up here around 1 a.m. The owner never asked us to leave. He just kept the kettle going. That table near the window still feels like ours.',
author: 'Anonymous',
date: '2024-11-03'
},
{
id: 'seed3',
lat: 31.5100,
lng: 74.3450,
title: 'Where I learned to ride',
story: 'My uncle held the back of the bicycle the whole time and pretended he was still holding on long after he had let go. I only found out years later.',
author: 'Hassan',
date: '2022-03-18'
},
{
id: 'seed4',
lat: 24.8607,
lng: 67.0011,
title: 'Sea breeze and silence',
story: 'Came here alone after a hard conversation. Watched the waves for an hour. Somehow the noise of the city felt farther away than the water.',
author: 'Sana',
date: '2025-01-09'
},
{
id: 'seed5',
lat: 33.6844,
lng: 73.0479,
title: 'The tree that survived everything',
story: 'This old tree outside the school gate has been here longer than any of us. We carved initials once. They are almost gone now, but the memory isn’t.',
author: 'Omar',
date: '2024-05-22'
}
];
// ========== Init ==========
function init() {
loadEchoes();
initBoard();
bindEvents();
}
function loadEchoes() {
const saved = localStorage.getItem(STORAGE_KEY);
if (saved) {
try {
echoes = JSON.parse(saved);
if (!Array.isArray(echoes) || echoes.length === 0) {
echoes = [...SEED_ECHOES];
saveEchoes();
}
} catch (e) {
echoes = [...SEED_ECHOES];
saveEchoes();
}
} else {
echoes = [...SEED_ECHOES];
saveEchoes();
}
}
function saveEchoes() {
localStorage.setItem(STORAGE_KEY, JSON.stringify(echoes));
}
function initBoard() {
map = L.map('map', { zoomControl: false }).setView([31.5204, 74.3587], 5);
L.control.zoom({ position: 'bottomleft' }).addTo(map);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© OpenStreetMap contributors'
}).addTo(map);
window.echoIcon = L.divIcon({ className: 'echo-pin', html: '<span class="pin-core"></span>', iconSize: [22, 22], iconAnchor: [11, 11] });
document.getElementById('mapLoading').style.display = 'none';
renderAllMarkers();
renderSidebar();
// Try geolocation
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(pos) => {
const { latitude, longitude } = pos.coords;
map.setView([latitude, longitude], 12);
},
() => {},
{ timeout: 4000 }
);
}
map.on('click', (event) => {
if (isAddingMode) {
selectedLatLng = event.latlng;
document.getElementById('locationHint').innerHTML =
`📌 Location selected: ${selectedLatLng.lat.toFixed(5)}, ${selectedLatLng.lng.toFixed(5)}`;
openModal();
}
});
}
// ========== Markers & List ==========
function renderAllMarkers() {
Object.values(markers).forEach(marker => map.removeLayer(marker));
markers = {};
echoes.forEach(echo => {
const marker = L.marker([echo.lat, echo.lng], { icon: window.echoIcon, zIndexOffset: 1000 })
.addTo(map)
.bindPopup(createPopupContent(echo));
const markerElement = marker.getElement();
if (markerElement) {
markerElement.style.setProperty('--pin-delay', `${Math.min(Object.keys(markers).length, 8) * 70}ms`);
}
marker.on('click', () => showEchoDetails(echo));
markers[echo.id] = marker;
});
}
function createPopupContent(echo) {
return `<strong>${escapeHtml(echo.title)}</strong><br><small>${escapeHtml(echo.author || 'Anonymous')} · ${formatDate(echo.date)}</small>`;
}
function showEchoDetails(echo) {
activeEchoId = echo.id;
document.querySelectorAll('.echo-pin-selected').forEach(marker => marker.classList.remove('echo-pin-selected'));
markers[echo.id]?.getElement()?.classList.add('echo-pin-selected');
document.getElementById('detailTitle').textContent = echo.title;
document.getElementById('detailMeta').textContent = `${echo.author || 'Anonymous'} · ${formatDate(echo.date)}`;
document.getElementById('detailBody').textContent = echo.story;
const detailPhoto = document.getElementById('detailPhoto');
detailPhoto.hidden = !echo.photo;
detailPhoto.src = echo.photo || '';
detailPhoto.alt = echo.photo ? `Photo for ${echo.title}` : '';
document.getElementById('favoriteDetail').textContent = echo.favorite ? '★ Favorited' : '☆ Favorite';
document.getElementById('echoDetail').classList.add('open');
}
function renderSidebar() {
const list = document.getElementById('echoList');
const countEl = document.getElementById('echoCount');
countEl.textContent = echoes.length;
if (echoes.length === 0) {
list.innerHTML = `
<div class="empty-state">
<div style="font-size:2rem;">🗺️</div>
<p>No echoes yet.<br>Be the first to leave a memory here.</p>
</div>`;
return;
}
const searchTerm = document.getElementById('storySearch')?.value.trim().toLowerCase() || '';
const filter = document.getElementById('storyFilter')?.value || 'all';
let sorted = [...echoes].sort((a, b) => new Date(b.date) - new Date(a.date));
if (filter === 'recent') sorted = sorted.slice(0, 5);
if (filter === 'favorites') sorted = sorted.filter(echo => echo.favorite);
if (searchTerm) sorted = sorted.filter(echo => `${echo.title} ${echo.story} ${echo.author}`.toLowerCase().includes(searchTerm));
if (sorted.length === 0) {
list.innerHTML = '<div class="empty-state"><div style="font-size:2rem;">⌕</div><p>No matching echoes yet.</p></div>';
return;
}
list.innerHTML = sorted.map((echo, index) => `
<div class="echo-card" data-id="${echo.id}" style="--card-delay: ${Math.min(index, 8) * 45}ms">
<div class="meta">
<span class="author">${escapeHtml(echo.author || 'Anonymous')}</span>
<span>${echo.favorite ? '★ ' : ''}${formatDate(echo.date)}</span>
</div>
<div class="title">${escapeHtml(echo.title)}</div>
<div class="preview">${escapeHtml(echo.story)}</div>
</div>
`).join('');
// Click handlers
list.querySelectorAll('.echo-card').forEach(card => {
card.addEventListener('click', () => {
const id = card.dataset.id;
const echo = echoes.find(e => e.id === id);
if (echo) {
map.setView([echo.lat, echo.lng], Math.max(map.getZoom(), 12));
markers[id]?.openPopup();
// highlight
list.querySelectorAll('.echo-card').forEach(c => c.classList.remove('active'));
card.classList.add('active');
}
});
});
}
// ========== Events ==========
function bindEvents() {
document.getElementById('addEchoBtn').addEventListener('click', startAddMode);
document.getElementById('locateBtn').addEventListener('click', locateMe);
document.getElementById('closeModal').addEventListener('click', closeModal);
document.getElementById('cancelModal').addEventListener('click', closeModal);
document.getElementById('submitEcho').addEventListener('click', submitEcho);
document.getElementById('exportBtn').addEventListener('click', exportEchoes);
document.getElementById('importBtn').addEventListener('click', () => document.getElementById('importFile').click());
document.getElementById('importFile').addEventListener('change', importEchoes);
document.getElementById('closeDetail').addEventListener('click', () => document.getElementById('echoDetail').classList.remove('open'));
document.getElementById('editDetail').addEventListener('click', editActiveEcho);
document.getElementById('useLocationBtn').addEventListener('click', useCurrentLocationForDraft);
document.getElementById('echoPhoto').addEventListener('change', handlePhotoChange);
document.getElementById('storySearch').addEventListener('input', renderSidebar);
document.getElementById('storyFilter').addEventListener('change', renderSidebar);
document.getElementById('randomEchoBtn').addEventListener('click', showRandomEcho);
document.getElementById('favoriteDetail').addEventListener('click', toggleActiveFavorite);
document.getElementById('deleteDetail').addEventListener('click', deleteActiveEcho);
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape') {
document.querySelectorAll('.overlay.open').forEach(overlay => overlay.classList.remove('open'));
closeModal();
}
});
// Mobile sidebar toggle
const toggleBtn = document.getElementById('toggleSidebar');
if (window.innerWidth <= 800) {
toggleBtn.style.display = 'inline-block';
toggleBtn.addEventListener('click', () => {
document.getElementById('sidebar').classList.toggle('open');
});
}
}
function showRandomEcho() {
if (!echoes.length) return showToast('Add an echo first');
const echo = echoes[Math.floor(Math.random() * echoes.length)];
map.setView([echo.lat, echo.lng], Math.max(map.getZoom(), 12));
showEchoDetails(echo);
markers[echo.id]?.openPopup();
}
function toggleActiveFavorite() {
const echo = echoes.find(item => item.id === activeEchoId);
if (!echo) return;
echo.favorite = !echo.favorite;
saveEchoes();
renderSidebar();
showEchoDetails(echo);
}
function deleteActiveEcho() {
const echo = echoes.find(item => item.id === activeEchoId);
if (!echo || !confirm(`Delete “${echo.title}”?`)) return;
echoes = echoes.filter(item => item.id !== activeEchoId);
saveEchoes();
renderAllMarkers();
renderSidebar();
document.getElementById('echoDetail').classList.remove('open');
showToast('Echo deleted');
}
function startAddMode() {
isAddingMode = true;
editingEchoId = null;
draftPhoto = '';
selectedLatLng = null;
document.getElementById('locationHint').innerHTML =
'📌 Click anywhere on the map to place your echo';
showToast('Click on the map to choose a location');
// visual cue
document.getElementById('map').style.cursor = 'crosshair';
}
function openModal(echo = null) {
if (echo) {
editingEchoId = echo.id;
document.getElementById('echoTitle').value = echo.title;
document.getElementById('echoStory').value = echo.story;
document.getElementById('echoAuthor').value = echo.author === 'Anonymous' ? '' : echo.author;
selectedLatLng = { lat: echo.lat, lng: echo.lng };
draftPhoto = echo.photo || '';
document.getElementById('locationHint').textContent = `📌 Location: ${echo.lat.toFixed(5)}, ${echo.lng.toFixed(5)}`;
updatePhotoPreview();
}
document.getElementById('addModal').classList.add('open');
document.getElementById('echoTitle').focus();
}
function closeModal() {
document.getElementById('addModal').classList.remove('open');
isAddingMode = false;
selectedLatLng = null;
editingEchoId = null;
draftPhoto = '';
document.getElementById('map').style.cursor = '';
document.getElementById('echoForm').reset();
document.getElementById('photoPreview').innerHTML = '';
}
function submitEcho() {
const title = document.getElementById('echoTitle').value.trim();
const story = document.getElementById('echoStory').value.trim();
const author = document.getElementById('echoAuthor').value.trim() || 'Anonymous';
if (!title || !story) {
showToast('Please fill in title and story');
return;
}
if (!selectedLatLng) {
showToast('Please click on the map to choose a location');
return;
}
const isEditing = Boolean(editingEchoId);
const existingEcho = echoes.find(echo => echo.id === editingEchoId);
const newEcho = {
id: editingEchoId || 'e_' + Date.now(),
lat: selectedLatLng.lat,
lng: selectedLatLng.lng,
title,
story,
author,
date: isEditing ? existingEcho.date : new Date().toISOString().slice(0, 10),
photo: draftPhoto,
favorite: isEditing ? existingEcho.favorite : false
};
if (editingEchoId) {
echoes = echoes.map(echo => echo.id === editingEchoId ? newEcho : echo);
} else {
echoes.push(newEcho);
}
saveEchoes();
renderAllMarkers();
renderSidebar();
closeModal();
setTimeout(() => showEchoDetails(newEcho), 300);
showToast(isEditing ? 'Echo updated' : 'Echo left successfully ✨');
}
function editActiveEcho() {
const echo = echoes.find(item => item.id === activeEchoId);
if (!echo) return;
document.getElementById('echoDetail').classList.remove('open');
isAddingMode = false;
openModal(echo);
}
function useCurrentLocationForDraft() {
if (!navigator.geolocation) return showToast('Location is not supported');
showToast('Finding your location…');
navigator.geolocation.getCurrentPosition(
(pos) => {
selectedLatLng = { lat: pos.coords.latitude, lng: pos.coords.longitude };
map.setView([selectedLatLng.lat, selectedLatLng.lng], 14);
document.getElementById('locationHint').textContent = `📌 Location selected: ${selectedLatLng.lat.toFixed(5)}, ${selectedLatLng.lng.toFixed(5)}`;
showToast('Location added');
},
() => showToast('Could not get your location')
);
}
function handlePhotoChange(event) {
const file = event.target.files[0];
if (!file) return;
if (file.size > 2 * 1024 * 1024) return showToast('Please choose a photo under 2 MB');
const reader = new FileReader();
reader.onload = () => {
draftPhoto = reader.result;
updatePhotoPreview();
};
reader.readAsDataURL(file);
}
function updatePhotoPreview() {
document.getElementById('photoPreview').innerHTML = draftPhoto ? `<img src="${draftPhoto}" alt="Selected photo preview">` : '';
}
function locateMe() {
if (!navigator.geolocation) {
showToast('Geolocation not supported');
return;
}
showToast('Finding you…');
navigator.geolocation.getCurrentPosition(
(pos) => {
map.setView([pos.coords.latitude, pos.coords.longitude], 14);
showToast('You are here');
},
() => showToast('Could not get your location')
);
}
function exportEchoes() {
const file = new Blob([JSON.stringify(echoes, null, 2)], { type: 'application/json' });
const link = document.createElement('a');
link.href = URL.createObjectURL(file);
link.download = 'echo-stories.json';
link.click();
URL.revokeObjectURL(link.href);
showToast('Your stories were downloaded');
}
function importEchoes(event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = () => {
try {
const imported = JSON.parse(reader.result);
if (!Array.isArray(imported) || imported.some(echo => !echo.title || !echo.story)) throw new Error();
if (echoes.length && !confirm('Replace your current stories with this backup?')) return;
echoes = imported;
saveEchoes();
renderAllMarkers();
renderSidebar();
showToast(`${echoes.length} stories restored`);
} catch (error) {
showToast('That file is not an Echo backup');
}
event.target.value = '';
};
reader.readAsText(file);
}
// ========== Helpers ==========
function escapeHtml(str) {
const div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
}
function formatDate(dateStr) {
const d = new Date(dateStr);
return d.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' });
}
function showToast(msg) {
const t = document.getElementById('toast');
t.textContent = msg;
t.classList.add('show');
setTimeout(() => t.classList.remove('show'), 2800);
}
// Start
init();