|
78 | 78 | {% for route in sorted_routes %} |
79 | 79 | <tr> |
80 | 80 | <td> |
81 | | - <a href="{{ route.id }}" title="{{ route.id }}"> |
| 81 | + <a href="{{ route.id }}/" title="{{ route.id }}"> |
82 | 82 | <span class="name">{{ route.name }}</span> |
83 | 83 | </a> |
84 | 84 | {% if route.gpx %} |
|
117 | 117 |
|
118 | 118 | Tabulator.registerModule([EditModule, FormatModule, InteractionModule, MutatorModule, ResizeColumnsModule, ResizeTableModule, SortModule, SelectRowModule, FilterModule]); |
119 | 119 |
|
| 120 | + let searched = false |
120 | 121 | document.addEventListener("DOMContentLoaded", () => { |
121 | 122 | let table = new Tabulator("#routes table", { |
122 | 123 | columns: |
|
217 | 218 | let previousTimeout = null; |
218 | 219 | document.getElementById("filter-search").addEventListener("input", (event) => { |
219 | 220 | if (previousTimeout) { |
220 | | - clearTimeout(previousTimeout); |
| 221 | + clearTimeout(previousTimeout); |
221 | 222 | } |
222 | | - // Debounce 100ms |
223 | | - previousTimeout = setTimeout(() => { |
224 | | - // Update query parameter |
225 | | - const url = new URL(window.location); |
226 | | - url.searchParams.set("q", event.target.value); |
227 | | - window.history.replaceState({}, "", url); |
228 | | - table.setFilter("name", "like", event.target.value); |
229 | | - }, 100); |
| 223 | + |
| 224 | + // Debounce |
| 225 | + previousTimeout = setTimeout(() => { |
| 226 | + if (window.goatcounter && !searched) { |
| 227 | + window.goatcounter.count({path: "searched", event: true}); |
| 228 | + searched = true; |
| 229 | + } |
| 230 | + const query = event.target.value; |
| 231 | + const url = new URL(window.location); |
| 232 | + |
| 233 | + // Update query parameter |
| 234 | + url.searchParams.set("q", query); |
| 235 | + window.history.replaceState({}, "", url); |
| 236 | + |
| 237 | + const startMatch = query.match(/start:([^ ]+)/); |
| 238 | + const endMatch = query.match(/end:([^ ]+)/); |
| 239 | + const distanceMatches = [...query.matchAll(/distance:(<=|>=|<|>|)?([0-9.]+)/g)]; |
| 240 | + const neighborhoodMatches = [...query.matchAll(/neighborhood:(['"]([^'"]+)['"]|[^ ]+)/g)]; |
| 241 | + const nameQuery = query |
| 242 | + .replace(/start:[^ ]+/, "") |
| 243 | + .replace(/end:[^ ]+/, "") |
| 244 | + .replace(/distance:(<=|>=|<|>|)?[0-9.]+/, "") |
| 245 | + .replace(/neighborhood:(['"]([^'"]+)['"]|[^ ]+)/g, "") |
| 246 | + .trim(); |
| 247 | + |
| 248 | + // Remove only relevant filters |
| 249 | + table.getFilters().forEach((filter) => { |
| 250 | + console.log(filter); |
| 251 | + if ( |
| 252 | + ["start", "end", "distance_mi", "name", "neighborhoods"].includes(filter.field) |
| 253 | + ) { |
| 254 | + table.removeFilter(filter.field, filter.type, filter.value); |
| 255 | + } |
| 256 | + }); |
| 257 | + |
| 258 | + if (startMatch) { |
| 259 | + table.addFilter("start", "starts", startMatch[1]); |
| 260 | + } |
| 261 | + |
| 262 | + if (endMatch) { |
| 263 | + table.addFilter("end", "starts", endMatch[1]); |
| 264 | + } |
| 265 | + |
| 266 | + if (neighborhoodMatches.length > 0) { |
| 267 | + table.addFilter("neighborhoods", "keywords", neighborhoodMatches.map((match) => match[2] || match[1]).join(","), {matchAll: true, separator: ","}); |
| 268 | + } |
| 269 | + |
| 270 | + |
| 271 | + // FIXME: Only one actually works |
| 272 | + distanceMatches.forEach((match) => { |
| 273 | + const operator = match[1] || "="; // Default operator is "=" if not provided |
| 274 | + const value = Number.parseFloat(match[2]); |
| 275 | + table.addFilter("distance_mi", operator, value); |
| 276 | + }); |
| 277 | + |
| 278 | + if (nameQuery) { |
| 279 | + table.addFilter("name", "like", nameQuery); |
| 280 | + } |
| 281 | + }, 150); |
230 | 282 |
|
231 | 283 | }); |
232 | 284 | document.getElementById("filter-deprecated").addEventListener("change", (event) => { |
233 | 285 | if (event.target.checked) { |
234 | 286 | table.removeFilter("deprecated", "!=", true); |
235 | 287 | } else { |
236 | | - table.setFilter("deprecated", "!=", true); |
| 288 | + table.addFilter("deprecated", "!=", true); |
237 | 289 | } |
238 | 290 | table.redraw(); |
239 | 291 | }); |
|
0 commit comments