@@ -445,20 +445,29 @@ class PredictionList {
445
445
const self = this ;
446
446
const isAdm2 = predictions . some ( prediction => prediction . adm_2 !== null ) ;
447
447
448
+ storage . get ( "prediction_ids" ) . forEach ( id => {
449
+ if ( ! predictions . some ( prediction => prediction . id === id ) ) {
450
+ self . unselect ( id )
451
+ }
452
+ } )
453
+
448
454
let distinctAdm ;
455
+ let admLevel ;
449
456
let adm ;
450
457
if ( ! isAdm2 ) {
451
458
distinctAdm = [ ...new Set ( predictions . map ( prediction => parseInt ( prediction . adm_1 , 10 ) ) ) ] ;
452
459
if ( ! storage . get ( "adm_1" ) || ! distinctAdm . includes ( storage . get ( "adm_1" ) ) ) {
453
460
storage . set ( "adm_1" , distinctAdm [ 0 ] ) ;
454
461
}
455
462
adm = storage . get ( "adm_1" )
463
+ admLevel = 1 ;
456
464
} else {
457
465
distinctAdm = [ ...new Set ( predictions . map ( prediction => parseInt ( prediction . adm_2 , 10 ) ) ) ] ;
458
466
if ( ! storage . get ( "adm_2" ) || ! distinctAdm . includes ( storage . get ( "adm_2" ) ) ) {
459
467
storage . set ( "adm_2" , distinctAdm [ 0 ] ) ;
460
468
}
461
469
adm = storage . get ( "adm_2" )
470
+ admLevel = 2 ;
462
471
}
463
472
464
473
const adm_select = `
@@ -471,31 +480,39 @@ class PredictionList {
471
480
472
481
$ ( "#predictions-card .card-header .card-tools" ) . html ( adm_select ) ;
473
482
474
- const filterPredictions = ( selectedAdm ) => {
475
- if ( ! isAdm2 ) {
476
- storage . set ( "adm_1" , parseInt ( selectedAdm , 10 ) ) ;
477
- } else {
478
- storage . set ( "adm_2" , parseInt ( selectedAdm , 10 ) ) ;
479
- }
480
- update_casos ( this . dashboard ) ;
483
+ self . paginate ( self . filter ( predictions , admLevel , adm ) )
481
484
482
- let filter = selectedAdm
483
- ? predictions . filter (
484
- prediction => prediction . adm_1 == selectedAdm || prediction . adm_2 == selectedAdm
485
- )
486
- : predictions ;
485
+ $ ( "#adm-filter" ) . on ( "change" , function ( ) {
486
+ storage . set ( "prediction_ids" , [ ] ) ;
487
+ chart . clearPredictions ( ) ;
488
+ self . paginate ( self . filter ( predictions , admLevel , $ ( this ) . val ( ) ) ) ;
489
+ } ) ;
490
+ }
487
491
488
- self . predictions_map = filter . reduce ( ( acc , prediction ) => {
489
- const { description, ..._ } = prediction ;
490
- acc [ prediction . id ] = _ ;
491
- return acc ;
492
- } , { } ) ;
492
+ filter ( predictions , adm_level , adm ) {
493
+ let res ;
494
+ if ( adm_level === 1 ) {
495
+ storage . set ( "adm_1" , parseInt ( adm , 10 ) ) ;
496
+ res = predictions . filter ( prediction => prediction . adm_1 == adm ) ;
497
+ } else {
498
+ storage . set ( "adm_2" , parseInt ( adm , 10 ) ) ;
499
+ res = predictions . filter ( prediction => prediction . adm_2 == adm ) ;
500
+ }
501
+ update_casos ( this . dashboard ) ;
493
502
494
- return filter ;
495
- } ;
503
+ this . predictions_map = res . reduce ( ( acc , prediction ) => {
504
+ const { description, ..._ } = prediction ;
505
+ acc [ prediction . id ] = _ ;
506
+ return acc ;
507
+ } , { } ) ;
508
+
509
+ return res ;
510
+ } ;
496
511
512
+ paginate ( predictions ) {
513
+ const self = this ;
497
514
$ ( '#predictions-pagination' ) . pagination ( {
498
- dataSource : filterPredictions ( adm ) ,
515
+ dataSource : predictions ,
499
516
pageSize : 5 ,
500
517
callback : function ( data , pagination ) {
501
518
const body = data . map ( ( item ) => self . li ( item ) ) . join ( "" ) ;
@@ -519,21 +536,13 @@ class PredictionList {
519
536
} ) ;
520
537
521
538
$ ( "#select-all-checkbox" ) . on ( "click" , function ( ) {
522
- const isChecked = $ ( this ) . prop ( "checked" ) ;
523
- $ ( ".checkbox-prediction" ) . prop ( "checked" , isChecked ) . each ( function ( ) {
539
+ const checked = $ ( this ) . prop ( "checked" ) ;
540
+ $ ( ".checkbox-prediction" ) . prop ( "checked" , checked ) . each ( function ( ) {
524
541
const prediction_id = parseInt ( $ ( this ) . val ( ) , 10 ) ;
525
- const prediction = self . predictions_map [ prediction_id ] ;
526
- if ( isChecked ) {
527
- chart . addPrediction ( {
528
- id : prediction_id ,
529
- labels : prediction . chart . labels ,
530
- data : prediction . chart . data ,
531
- upper : prediction . chart . upper ,
532
- lower : prediction . chart . lower ,
533
- color : prediction . color
534
- } )
542
+ if ( checked ) {
543
+ self . select ( self . predictions_map [ prediction_id ] )
535
544
} else {
536
- chart . removePrediction ( prediction_id )
545
+ self . unselect ( prediction_id )
537
546
}
538
547
} ) ;
539
548
} ) ;
@@ -543,92 +552,34 @@ class PredictionList {
543
552
const prediction_id = parseInt ( event . target . value , 10 ) ;
544
553
545
554
if ( $ ( event . target ) . prop ( "checked" ) ) {
546
- const prediction = self . predictions_map [ prediction_id ] ;
547
- chart . addPrediction ( {
548
- id : prediction_id ,
549
- labels : prediction . chart . labels ,
550
- data : prediction . chart . data ,
551
- upper : prediction . chart . upper ,
552
- lower : prediction . chart . lower ,
553
- color : prediction . color
554
- } )
555
+ self . select ( self . predictions_map [ prediction_id ] )
555
556
} else {
556
- chart . removePrediction ( prediction_id )
557
+ self . unselect ( prediction_id )
557
558
}
558
559
} ) ;
559
560
} ,
560
561
} ) ;
562
+ }
561
563
562
- $ ( "#adm-filter" ) . on ( "change" , function ( ) {
563
- const selectedAdm = $ ( this ) . val ( ) ;
564
- const filteredPredictions = filterPredictions ( selectedAdm ) ;
565
- chart . clearPredictions ( ) ;
566
-
567
- $ ( '#predictions-pagination' ) . pagination ( {
568
- dataSource : filteredPredictions ,
569
- pageSize : 5 ,
570
- callback : function ( data , pagination ) {
571
- const body = data . map ( ( item ) => self . li ( item ) ) . join ( "" ) ;
572
- $ ( `#predictions-list` ) . html ( `
573
- <thead>
574
- <tr>
575
- <th style="width: 40px;">
576
- <input type="checkbox" id="select-all-checkbox">
577
- </th>
578
- <th>id</th>
579
- </tr>
580
- </thead>
581
- <tbody>${ body } </tbody>
582
- ` ) ;
583
-
584
- $ ( ".checkbox-prediction" ) . each ( function ( ) {
585
- const prediction_id = parseInt ( $ ( this ) . val ( ) , 10 ) ;
586
- if ( storage . get ( "prediction_ids" ) . includes ( prediction_id ) ) {
587
- $ ( `.checkbox-prediction[value="${ prediction_id } "]` ) . prop ( "checked" , true ) ;
588
- }
589
- } ) ;
590
-
591
- $ ( "#select-all-checkbox" ) . on ( "click" , function ( ) {
592
- const isChecked = $ ( this ) . prop ( "checked" ) ;
593
- $ ( ".checkbox-prediction" ) . prop ( "checked" , isChecked ) . each ( function ( ) {
594
- const prediction_id = parseInt ( $ ( this ) . val ( ) , 10 ) ;
595
- const prediction = self . predictions_map [ prediction_id ] ;
596
- if ( isChecked ) {
597
- chart . addPrediction ( {
598
- id : prediction_id ,
599
- labels : prediction . chart . labels ,
600
- data : prediction . chart . data ,
601
- upper : prediction . chart . upper ,
602
- lower : prediction . chart . lower ,
603
- color : prediction . color
604
- } )
605
- } else {
606
- chart . removePrediction ( prediction_id )
607
- }
608
- } ) ;
609
-
610
- } ) ;
564
+ select ( prediction ) {
565
+ let prediction_ids = new Set ( storage . get ( "prediction_ids" ) ) ;
566
+ prediction_ids . add ( prediction . id ) ;
567
+ chart . addPrediction ( {
568
+ id : prediction . id ,
569
+ labels : prediction . chart . labels ,
570
+ data : prediction . chart . data ,
571
+ upper : prediction . chart . upper ,
572
+ lower : prediction . chart . lower ,
573
+ color : prediction . color
574
+ } )
575
+ storage . set ( "prediction_ids" , Array . from ( prediction_ids ) ) ;
576
+ }
611
577
612
- $ ( ".checkbox-prediction" ) . on ( "click" , function ( event ) {
613
- event . stopPropagation ( ) ;
614
- const prediction_id = parseInt ( event . target . value , 10 ) ;
615
- const prediction = self . predictions_map [ prediction_id ] ;
616
- if ( $ ( event . target ) . prop ( "checked" ) ) {
617
- chart . addPrediction ( {
618
- id : prediction_id ,
619
- labels : prediction . chart . labels ,
620
- data : prediction . chart . data ,
621
- upper : prediction . chart . upper ,
622
- lower : prediction . chart . lower ,
623
- color : prediction . color
624
- } )
625
- } else {
626
- chart . removePrediction ( prediction_id )
627
- }
628
- } ) ;
629
- } ,
630
- } ) ;
631
- } ) ;
578
+ unselect ( prediction_id ) {
579
+ let prediction_ids = new Set ( storage . get ( "prediction_ids" ) ) ;
580
+ prediction_ids . delete ( prediction_id ) ;
581
+ chart . removePrediction ( prediction_id )
582
+ storage . set ( "prediction_ids" , Array . from ( prediction_ids ) ) ;
632
583
}
633
584
}
634
585
0 commit comments