@@ -5,9 +5,6 @@ const BASE_URL = "https://data.opentimes.org/times/version=0.0.1/mode=auto/year=
5
5
const BIG_STATES = [ "06" , "36" ] ;
6
6
const ZOOM_THRESHOLDS = [ 6 , 8 ] ;
7
7
8
- const protocol = new pmtiles . Protocol ( ) ;
9
- maplibregl . addProtocol ( "pmtiles" , protocol . tile ) ;
10
-
11
8
class ColorScale {
12
9
constructor ( zoomLower , zoomUpper ) {
13
10
this . scaleContainer = this . createScaleContainer ( ) ;
@@ -143,6 +140,9 @@ class Map {
143
140
}
144
141
145
142
async init ( ) {
143
+ const protocol = new pmtiles . Protocol ( ) ;
144
+ maplibregl . addProtocol ( "pmtiles" , protocol . tile ) ;
145
+
146
146
this . map = new maplibregl . Map ( {
147
147
style : "https://tiles.openfreemap.org/styles/positron" ,
148
148
center : [ - 74.0 , 40.75 ] ,
@@ -396,6 +396,9 @@ class ParquetProcessor {
396
396
397
397
async updateMapOnQuery ( map , urls , id ) {
398
398
const results = [ ] ;
399
+ if ( ! this . validIdInput ( id ) ) {
400
+ return results ;
401
+ }
399
402
400
403
const dataPromises = urls . map ( async ( url ) => {
401
404
const metadata = await this . fetchAndCacheMetadata ( url ) ;
@@ -438,6 +441,15 @@ class ParquetProcessor {
438
441
await Promise . all ( dataPromises ) ;
439
442
return results ;
440
443
}
444
+
445
+ validIdInput ( id ) {
446
+ if ( id && / ^ \d { 11 } $ / . test ( id ) ) {
447
+ return true ;
448
+ } else {
449
+ console . warn ( "Invalid ID input. Please enter a valid 11-digit tract ID." )
450
+ return false ;
451
+ }
452
+ }
441
453
}
442
454
443
455
function debounce ( func , wait ) {
@@ -449,26 +461,28 @@ function debounce(func, wait) {
449
461
}
450
462
451
463
( async ( ) => {
452
- let idParam = new URLSearchParams ( window . location . search ) . get ( "id" ) ;
453
464
const spinner = new Spinner ( ) ;
454
- const colorScale = new ColorScale ( ZOOM_THRESHOLDS [ 0 ] , ZOOM_THRESHOLDS [ 1 ] ) ;
455
- const processor = new ParquetProcessor ( BASE_URL ) ;
456
-
457
465
spinner . show ( ) ;
458
466
467
+ const colorScale = new ColorScale ( ZOOM_THRESHOLDS [ 0 ] , ZOOM_THRESHOLDS [ 1 ] ) ;
468
+ const processor = new ParquetProcessor ( BASE_URL ) ;
459
469
const map = new Map ( colorScale , spinner , processor ) ;
460
470
colorScale . draw ( map . map ) ;
461
471
462
- // Load the previous map click if there was one
463
- if ( idParam ) {
464
- await processor . runQuery ( map , idParam . substring ( 0 , 2 ) , idParam ) ;
465
- }
472
+ // Wait for the map to fully load before running a query
473
+ map . map . on ( "load" , async ( ) => {
474
+ // Load the previous map click if there was one
475
+ const idParam = new URLSearchParams ( window . location . search ) . get ( "id" ) ;
476
+ if ( idParam ) {
477
+ await processor . runQuery ( map , idParam . substring ( 0 , 2 ) , idParam ) ;
478
+ }
466
479
467
- // Remove the hash if map is at starting location
468
- if ( window . location . hash === "#10/40.75/-74" ) {
469
- console . log ( window . location . hash ) ;
470
- window . history . replaceState ( { } , "" , window . location . pathname + window . location . search ) ;
471
- }
480
+ // Remove the hash if map is at starting location
481
+ if ( window . location . hash === "#10/40.75/-74" ) {
482
+ console . log ( window . location . hash ) ;
483
+ window . history . replaceState ( { } , "" , window . location . pathname + window . location . search ) ;
484
+ }
472
485
473
- spinner . hide ( ) ;
486
+ spinner . hide ( ) ;
487
+ } ) ;
474
488
} ) ( ) ;
0 commit comments