-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforest.html
More file actions
252 lines (219 loc) · 10.6 KB
/
forest.html
File metadata and controls
252 lines (219 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Yearly Forest & Stats</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<link rel="stylesheet" href="style.css">
</head>
<body>
<nav class="top-nav">
<a href="index.html" class="nav-back">← Timeline</a>
</nav>
<header>
<h1 class="page-title" id="page-title">Forest Map</h1>
<p class="subtitle page-subtitle" id="page-subtitle"></p>
</header>
<main>
<section class="dashboard">
<div class="stat-card compact">
<h3>Trees This Year</h3>
<p id="yearly-trees">0</p>
</div>
<div class="stat-card compact">
<h3>Green Cover</h3>
<p><span id="yearly-cover">0</span> <span class="unit">sq m</span></p>
</div>
</section>
<section class="content-split">
<div class="tree-list-container" id="list-container">
<div class="list-header">
<h3>Saplings Planted</h3>
<span class="list-count-badge" id="list-count">0</span>
</div>
<div id="tree-list"></div>
</div>
<div id="map">
<button class="locate-btn" id="locate-btn">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="3"/><path d="M12 2v3M12 19v3M2 12h3M19 12h3"/></svg>
Find My Location
</button>
</div>
</section>
</main>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script src="utils.js"></script>
<script src="data.js"></script>
<script>
const urlParams = new URLSearchParams(window.location.search);
const yearParam = parseInt(urlParams.get('year')) || new Date().getFullYear();
document.getElementById('page-title').textContent = `${yearParam} Forest`;
const map = initForestMap('map', [28.4595, 77.0266], 13);
const currentYear = new Date().getFullYear();
const localTrees = getLocalTrees();
const localTreeIds = new Set(localTrees.map(t => t.id));
const combinedData = [...allTreeData, ...localTrees];
const yearData = combinedData.filter(tree => tree.year === yearParam);
let totalTrees = 0;
let totalGreenCover = 0;
let cardCount = 0;
const markers = {};
const listContainer = document.getElementById('tree-list');
function updateDashboard() {
document.getElementById('yearly-trees').textContent = totalTrees.toLocaleString();
document.getElementById('yearly-cover').textContent = totalGreenCover.toLocaleString();
document.getElementById('list-count').textContent = cardCount;
}
function renderTree(tree, isLocal) {
const impact = getTreeImpact(tree, currentYear);
totalTrees += tree.count;
totalGreenCover += impact.greenCover;
cardCount += 1;
const iconSizePx = 20 + (tree.age * 4);
const treeIcon = L.divIcon({
className: 'custom-tree-icon',
html: `<div style="font-size:${iconSizePx}px; line-height:1; filter:drop-shadow(0 2px 3px rgba(0,0,0,0.35));">🌳</div>`,
iconSize: [iconSizePx, iconSizePx],
iconAnchor: [iconSizePx / 2, iconSizePx]
});
const marker = L.marker([tree.lat, tree.lng], { icon: treeIcon }).addTo(map);
const safeType = escapeHTML(tree.type);
marker.bindPopup(`<b style="color:#111;">${safeType}</b><br><span style="color:#555;">Count: ${tree.count}</span>`);
markers[tree.id] = marker;
const card = document.createElement('div');
card.className = 'tree-card';
const strong = document.createElement('strong');
strong.textContent = `${tree.type} · ${tree.count}`;
const span = document.createElement('span');
span.textContent = `${tree.month} — ${tree.age} yr sapling`;
card.appendChild(strong);
card.appendChild(span);
if (isLocal) {
const delBtn = document.createElement('button');
delBtn.className = 'delete-btn';
delBtn.title = 'Remove';
delBtn.textContent = '✕';
delBtn.addEventListener('click', (e) => {
e.stopPropagation();
saveLocalTrees(getLocalTrees().filter(t => t.id !== tree.id));
map.removeLayer(markers[tree.id]);
delete markers[tree.id];
totalTrees -= tree.count;
totalGreenCover -= impact.greenCover;
cardCount -= 1;
card.remove();
updateDashboard();
if (cardCount === 0) {
listContainer.innerHTML = '';
listContainer.appendChild(emptyStateEl());
document.getElementById('page-subtitle').textContent = 'Click anywhere on the map to log your first sapling.';
} else if (Object.keys(markers).length > 0) {
map.flyToBounds(new L.featureGroup(Object.values(markers)).getBounds(), { padding: [50, 50], duration: 1.2 });
}
});
card.appendChild(delBtn);
}
card.addEventListener('click', () => {
map.flyTo([tree.lat, tree.lng], 16, { duration: 1.2 });
markers[tree.id].openPopup();
});
listContainer.appendChild(card);
}
function emptyStateEl() {
const div = document.createElement('div');
div.className = 'empty-state';
div.innerHTML = `<div class="empty-icon">🌱</div><p>No saplings logged for ${yearParam}.<br>Click anywhere on the map to add one.</p>`;
return div;
}
yearData.forEach(tree => renderTree(tree, localTreeIds.has(tree.id)));
updateDashboard();
if (yearData.length === 0) {
listContainer.appendChild(emptyStateEl());
document.getElementById('page-subtitle').textContent = 'Click anywhere on the map to log your first sapling.';
}
if (Object.keys(markers).length > 0) {
map.fitBounds(new L.featureGroup(Object.values(markers)).getBounds(), { padding: [50, 50] });
} else if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
pos => map.setView([pos.coords.latitude, pos.coords.longitude], 13),
() => {}
);
}
map.on('click', function(e) {
const formDiv = document.createElement('div');
formDiv.className = 'glass-form';
formDiv.innerHTML = `
<h4>Log a Sapling</h4>
<div class="form-error" id="plant-error">Please fill in tree type and count.</div>
<div class="form-field">
<label>Tree Type</label>
<input type="text" id="new-type" placeholder="e.g. Mango">
</div>
<div class="form-field">
<label>Number of Trees</label>
<input type="number" id="new-count" placeholder="e.g. 5" min="1">
</div>
<div class="form-field">
<label>Sapling Age (years)</label>
<input type="number" id="new-age" placeholder="e.g. 1" min="0">
</div>
<button id="plant-btn">Plant Here 🌱</button>
`;
const errorEl = formDiv.querySelector('#plant-error');
const typeInp = formDiv.querySelector('#new-type');
const countInp = formDiv.querySelector('#new-count');
const ageInp = formDiv.querySelector('#new-age');
const btn = formDiv.querySelector('#plant-btn');
btn.addEventListener('click', function() {
const type = typeInp.value.trim();
const count = parseInt(countInp.value);
const age = parseInt(ageInp.value);
if (!type || !count) {
errorEl.classList.add('visible');
return;
}
errorEl.classList.remove('visible');
// Generate safe random UUID-like ID
const safeId = crypto.randomUUID ? crypto.randomUUID() : Date.now() + Math.random().toString().slice(2,8);
const newTree = {
id: safeId,
lat: e.latlng.lat,
lng: e.latlng.lng,
type,
count,
age: age || 0,
month: new Date().toLocaleString('default', { month: 'long' }),
year: yearParam
};
const savedData = getLocalTrees();
savedData.push(newTree);
saveLocalTrees(savedData);
map.closePopup();
const emptyState = listContainer.querySelector('.empty-state');
if (emptyState) emptyState.remove();
document.getElementById('page-subtitle').textContent = '';
renderTree(newTree, true);
updateDashboard();
});
L.popup({ minWidth: 220 }).setLatLng(e.latlng).setContent(formDiv).openOn(map);
});
document.getElementById('locate-btn').addEventListener('click', function(e) {
e.stopPropagation();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
const { latitude: lat, longitude: lng } = position.coords;
map.flyTo([lat, lng], 15);
L.popup()
.setLatLng([lat, lng])
.setContent("<b style='color:#111;'>You are here</b><br><span style='color:#555; font-size:0.85em;'>Click nearby to log a sapling.</span>")
.openOn(map);
}, () => {
console.warn('Location access denied.');
});
}
});
</script>
</body>
</html>