@@ -4,6 +4,11 @@ <h5>Plot / Export data</h5>
44 < div class ="card ">
55 < div class ="card-body ">
66 < form class ="margin-top-1rem " onsubmit ="submitMode(event) " id ="epivis-form ">
7+
8+ < div class ="alert alert-warning alert-dismissible fade show " role ="alert " id ="warning-alert ">
9+ < strong > Holy guacamole!</ strong > You should check in on some of those fields below.
10+ </ div >
11+
712 < div class ="row ">
813 < div class ="col-2 ">
914 < label class ="form-label "
@@ -99,70 +104,46 @@ <h5>Plot / Export data</h5>
99104 </ div >
100105</ section >
101106< script >
102- function initGeographicValueSelect ( mode ) {
103- var maxSelectionLength = 1 ;
107+ var geoValues = [ ] ;
108+ var currentMode = 'epivis' ;
109+
110+ function getFilteredGeographicValues ( geographicType ) {
111+ var data = geoValues . reduce ( ( data , geoValue ) => {
112+ if ( geoValue . geoType === geographicType ) {
113+ data . push ( geoValue ) ;
114+ }
115+ return data ;
116+ } , [ ] ) ;
117+ return data ;
118+ }
119+
120+ function initGeographicValueSelect ( mode , geographicType = null ) {
121+ var maximumSelectionLength = 1 ;
104122 if ( mode === 'epivis' ) {
105- maxSelectionLength = 1 ;
123+ maximumSelectionLength = 1 ;
124+ } else {
125+ maximumSelectionLength = 10 ;
126+ }
127+
128+ if ( geographicType ) {
129+ var data = getFilteredGeographicValues ( geographicType ) ;
106130 } else {
107- maxSelectionLength = 10 ;
131+ data = [ ] ;
108132 }
133+
134+
109135 $ ( '#geographic_value' ) . select2 ( {
110- ajax : {
111- url : '{% url ' geography_units_api ' %}' ,
112- dataType : 'json' ,
113- data : function ( params ) {
114- var geographic_type = document . getElementById ( 'geographic_type' ) ;
115- return {
116- geography__name : geographic_type . value ,
117- term : params . term ,
118- limit : 50
119- } ;
120- } ,
121- processResults : function ( data ) {
122- return {
123- results : $ . map ( data . results , function ( item ) {
124- if ( typeof item . geo_id === 'string' ) {
125- return {
126- text : item . display_name ,
127- id : item . geo_id . toLowerCase ( )
128- }
129- } else {
130- return {
131- text : item . display_name ,
132- id : item . geo_id
133- }
134- }
135- } )
136- } ;
137- }
138- } ,
136+ data : data ,
139137 minimumInputLength : 0 ,
140- maximumSelectionLength : maxSelectionLength ,
138+ maximumSelectionLength : maximumSelectionLength ,
141139 } ) ;
142140 }
143141
144- $ ( document ) . ready ( function ( ) {
145- initGeographicValueSelect ( 'epivis' ) ;
146- } ) ;
147-
148142 document . getElementById ( 'geographic_type' ) . addEventListener ( "change" , ( event ) => {
149- $ ( '#geographic_value' ) . val ( null ) . trigger ( 'change' ) ;
143+ $ ( '#geographic_value' ) . empty ( ) ;
144+ initGeographicValueSelect ( currentMode , event . target . value ) ;
150145 } )
151146
152- document . getElementsByName ( 'modes' ) . forEach ( ( el ) => {
153- el . addEventListener ( 'change' , ( event ) => {
154- $ ( '#geographic_value' ) . val ( null ) . trigger ( 'change' ) ;
155- if ( event . target . value === 'epivis' ) {
156- initGeographicValueSelect ( event . target . value ) ;
157- } else {
158- initGeographicValueSelect ( 'export' ) ;
159- }
160- handleModeChange ( event . target . value ) ;
161- } ) ;
162- } ) ;
163-
164- var currentMode = 'epivis' ;
165-
166147 function handleModeChange ( mode ) {
167148 document . getElementById ( "epivis-form" ) . reset ( ) ;
168149
@@ -185,17 +166,49 @@ <h5>Plot / Export data</h5>
185166 } ) ;
186167 }
187168
169+ document . getElementsByName ( 'modes' ) . forEach ( ( el ) => {
170+ el . addEventListener ( 'change' , ( event ) => {
171+ $ ( '#geographic_value' ) . empty ( ) ;
172+ currentMode = event . target . value ;
173+ initGeographicValueSelect ( event . target . value )
174+ handleModeChange ( event . target . value ) ;
175+ } ) ;
176+ } ) ;
177+
178+
179+ $ ( document ) . ready ( function ( ) {
180+ { % for geography in signal . available_geography . all % }
181+ { % for unit in geography . geography_units . all % }
182+ geoValues . push ( { 'id' : '{{ unit.geo_id }}' , 'geoType' : '{{ unit.geography }}' , 'text' : '{{ unit.display_name }}' } ) ;
183+ { % endfor % }
184+ { % endfor % }
185+
186+ $ ( '#geographic_value' ) . select2 ( {
187+ data : [ ] ,
188+ minimumInputLength : 0 ,
189+ maximumSelectionLength : 1 ,
190+ } ) ;
191+ } ) ;
192+
193+ $ ( "#warning-alert" ) . hide ( ) ;
194+
195+ function showWarningAlert ( warningMessage ) {
196+ $ ( "#warning-alert" ) . html ( warningMessage ) ;
197+ $ ( "#warning-alert" ) . fadeTo ( 2000 , 500 ) . slideUp ( 1000 , function ( ) {
198+ $ ( "#warning-alert" ) . slideUp ( 1000 ) ;
199+ } ) ;
200+ }
188201
189202 function submitMode ( event ) {
190203 event . preventDefault ( ) ;
191-
204+
192205 var dataSource = document . getElementById ( 'source' ) . value ;
193206 var dataSignal = document . getElementById ( 'signal' ) . value ;
194207 var geographicType = document . getElementById ( 'geographic_type' ) . value ;
195208 var geographicValue = $ ( '#geographic_value' ) . select2 ( 'data' ) . map ( ( el ) => el . id ) . join ( ',' ) ;
196209
197210 if ( geographicType === 'Choose...' || geographicValue === '' ) {
198- alert ( 'Please select Geographic Type and Geographic Value!' ) ;
211+ showWarningAlert ( " Geographic Type or Geographic Value is not selected." ) ;
199212 return ;
200213 }
201214
@@ -205,20 +218,20 @@ <h5>Plot / Export data</h5>
205218
206219 epiVisUrl += `#${ urlParamsEncoded } ` ;
207220 window . open ( epiVisUrl , '_blank' ) . focus ( ) ;
208-
209221 } else {
210222 var startDate = document . getElementById ( 'start_date' ) . value ;
211223 var endDate = document . getElementById ( 'end_date' ) . value ;
212224
213225 var dataExportUrl = "{{ data_export_url }}" ;
214226
215227 if ( startDate === '' || endDate === '' ) {
216- alert ( 'Please select start and end date' ) ;
228+ showWarningAlert ( "Start Date or End Date is not selected." )
217229 return ;
218230 }
219231
220232 dataExportUrl += `?signal=${ dataSource } :${ dataSignal } &start_day=${ startDate } &end_day=${ endDate } &geo_type=${ geographicType } &geo_values=${ geographicValue } ` ;
221233 window . open ( dataExportUrl , '_blank' ) . focus ( ) ;
222234 }
223235 }
236+
224237</ script >
0 commit comments