-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
330 lines (286 loc) · 13.2 KB
/
index.html
File metadata and controls
330 lines (286 loc) · 13.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sheet Mapper</title>
<script src='https://api.mapbox.com/mapbox-gl-js/v3.7.0/mapbox-gl.js'></script>
<link href='https://api.mapbox.com/mapbox-gl-js/v3.7.0/mapbox-gl.css' rel='stylesheet' />
<script src='https://unpkg.com/papaparse@5.3.0/papaparse.min.js'></script>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/@turf/turf@7/turf.min.js"></script>
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v5.0.3/mapbox-gl-geocoder.min.js'></script>
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v5.0.3/mapbox-gl-geocoder.css' type='text/css' />
<!-- Generate legend from Mapbox styles https://github.com/watergis/mapbox-gl-legend -->
<link href='https://watergis.github.io/mapbox-gl-legend/mapbox-gl-legend.css' rel='stylesheet' />
<script src="https://watergis.github.io/mapbox-gl-legend/mapbox-gl-legend.js"></script>
<style>
body { margin: 0; padding: 0; }
/* Mobile-first layout */
.layout-container {
display: flex;
flex-direction: column;
height: 100vh;
}
header {
flex-shrink: 0;
width: 100%;
/* Allow header to scroll horizontally on mobile */
overflow-x: auto;
}
.main-content {
flex: 1;
display: flex;
flex-direction: column;
position: relative;
}
#sidebar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 30vh;
width: 100%;
z-index: 10;
transform: translateY(70%);
transition: transform 0.3s ease-out;
}
#sidebar.expanded {
transform: translateY(0);
}
.sidebar-handle {
position: absolute;
top: -20px;
left: 50%;
transform: translateX(-50%);
width: 50px;
height: 5px;
background: #e5e7eb;
border-radius: 999px;
cursor: pointer;
}
#map {
height: 100%;
width: 100%;
}
/* Tablet and desktop styles */
@media (min-width: 768px) {
.main-content {
flex-direction: row;
}
#sidebar {
position: relative;
width: 300px;
height: calc(100vh - 120px);
transform: none;
}
.sidebar-handle {
display: none;
}
#map {
flex: 1;
}
}
#center-marker {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
pointer-events: none;
z-index: 1;
}
</style>
</head>
<body>
<div class="layout-container">
<!-- Header -->
<header class="bg-white shadow-md p-4">
<div class="container mx-auto">
<!-- Initial sheet input state -->
<div id="sheetInput" class="mb-4">
<div class="text-center mb-2 text-sm text-gray-600">Drop CSV anywhere on page or</div>
<div class="flex gap-2">
<input
type="text"
id="sheetUrl"
placeholder="Paste Google Sheet URL here"
class="flex-1 px-4 py-2 border rounded text-sm focus:outline-none focus:ring-2 focus:ring-blue-500"
>
</div>
</div>
<!-- Sheet buttons (initially hidden) -->
<div id="sheetButtons" class="mb-4 flex gap-2" style="display: none;">
<a id="viewSheetData" href="#" target="_blank" class="flex-1 px-4 py-2 bg-blue-500 text-white font-bold rounded hover:bg-blue-600 text-center text-sm md:text-base md:flex-none">
Open Sheet
<svg class="inline-block w-4 h-4 ml-1" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"></path>
</svg>
</a>
<button id="downloadGeoJSON" class="flex-1 px-4 py-2 bg-green-500 text-white font-bold rounded hover:bg-green-600 text-sm md:text-base md:flex-none">
Download GeoJSON
</button>
</div>
<!-- Filters row -->
<div id="filterContainer" class="flex flex-nowrap items-center gap-2 overflow-x-auto pb-2">
<!-- Filters will be dynamically added here -->
</div>
</div>
</header>
<!-- Main content -->
<div class="main-content">
<!-- Map container -->
<div id="map">
<div id="center-marker">
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<line x1="0" y1="10" x2="20" y2="10" stroke="red" stroke-width="2"/>
<line x1="10" y1="0" x2="10" y2="20" stroke="red" stroke-width="2"/>
</svg>
</div>
</div>
<!-- Sidebar -->
<div id="sidebar" class="bg-white shadow-lg overflow-y-auto">
<div class="sidebar-handle"></div>
<!-- Content will be dynamically inserted here -->
</div>
</div>
</div>
<!-- Add this script before your other scripts -->
<script>
document.addEventListener('DOMContentLoaded', () => {
// Add drag and drop handlers
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
document.body.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
// Handle file drop
document.body.addEventListener('drop', handleDrop, false);
function handleDrop(e) {
const dt = e.dataTransfer;
const file = dt.files[0];
if (file && file.type === 'text/csv') {
const reader = new FileReader();
reader.onload = function(event) {
const csvData = event.target.result;
// Parse CSV using Papa Parse
const parsedData = Papa.parse(csvData, {
header: true,
dynamicTyping: true,
skipEmptyLines: true
}).data;
// Hide input and show loading state
const sheetUrl = document.getElementById('sheetUrl');
sheetUrl.disabled = true;
sheetUrl.classList.add('bg-gray-100');
sheetUrl.value = 'Processing CSV file...';
// Trigger data loading with the parsed CSV
window.dispatchEvent(new CustomEvent('loadCSVData', {
detail: {
data: parsedData,
onError: (error) => {
// Reset input on error
sheetUrl.disabled = false;
sheetUrl.classList.remove('bg-gray-100');
sheetUrl.classList.add('text-red-500');
sheetUrl.value = `Error: ${error.message}`;
// Reset error state after 3 seconds
setTimeout(() => {
sheetUrl.classList.remove('text-red-500');
sheetUrl.value = '';
}, 3000);
},
onSuccess: () => {
// Hide input and show buttons on success
document.getElementById('sheetInput').style.display = 'none';
document.getElementById('sheetButtons').style.display = 'flex';
// Update download button text
document.getElementById('downloadGeoJSON').textContent = 'Download GeoJSON';
}
}
}));
};
reader.readAsText(file);
} else {
alert('Please drop a CSV file');
}
}
// Handle sidebar expansion on mobile
const sidebar = document.getElementById('sidebar');
const handle = document.querySelector('.sidebar-handle');
if (handle) {
handle.addEventListener('click', () => {
sidebar.classList.toggle('expanded');
});
}
// Close sidebar when clicking on map (mobile only)
document.getElementById('map').addEventListener('click', () => {
if (window.innerWidth < 768) {
sidebar.classList.remove('expanded');
}
});
// Sheet URL handling
const sheetInput = document.getElementById('sheetInput');
const sheetButtons = document.getElementById('sheetButtons');
const sheetUrl = document.getElementById('sheetUrl');
let loadingTimeout;
sheetUrl.addEventListener('input', (e) => {
clearTimeout(loadingTimeout);
loadingTimeout = setTimeout(() => {
const url = e.target.value;
const sheetId = extractSheetId(url);
if (sheetId) {
// Disable input and show loading state
sheetUrl.disabled = true;
sheetUrl.classList.add('bg-gray-100');
sheetUrl.value = 'Loading sheet data...';
// Update viewSheetData link
const viewSheetData = document.getElementById('viewSheetData');
viewSheetData.href = `https://docs.google.com/spreadsheets/d/${sheetId}`;
// Trigger data loading
window.dispatchEvent(new CustomEvent('loadSheetData', {
detail: {
sheetId,
onError: (error) => {
// Reset input on error
sheetUrl.disabled = false;
sheetUrl.classList.remove('bg-gray-100');
sheetUrl.classList.add('text-red-500');
sheetUrl.value = `Error: ${error.message}`;
// Reset error state after 3 seconds
setTimeout(() => {
sheetUrl.classList.remove('text-red-500');
sheetUrl.value = url;
}, 3000);
},
onSuccess: () => {
// Hide input and show buttons on success
sheetInput.style.display = 'none';
sheetButtons.style.display = 'flex';
}
}
}));
}
}, 500); // Wait 500ms after typing stops
});
function extractSheetId(url) {
// Extract sheet ID from various Google Sheets URL formats
const patterns = [
/\/spreadsheets\/d\/([a-zA-Z0-9-_]+)/,
/^([a-zA-Z0-9-_]+)$/
];
for (const pattern of patterns) {
const match = url.match(pattern);
if (match && match[1]) {
return match[1];
}
}
return null;
}
});
</script>
<!-- Add this before sheet-mapper.js -->
<script type="module" src="csv-map.js"></script>
</body>
</html>