@@ -4,6 +4,11 @@ <h5>Plot / Export data</h5>
4
4
< div class ="card ">
5
5
< div class ="card-body ">
6
6
< 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
+
7
12
< div class ="row ">
8
13
< div class ="col-2 ">
9
14
< label class ="form-label "
@@ -99,70 +104,46 @@ <h5>Plot / Export data</h5>
99
104
</ div >
100
105
</ section >
101
106
< 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 ;
104
122
if ( mode === 'epivis' ) {
105
- maxSelectionLength = 1 ;
123
+ maximumSelectionLength = 1 ;
124
+ } else {
125
+ maximumSelectionLength = 10 ;
126
+ }
127
+
128
+ if ( geographicType ) {
129
+ var data = getFilteredGeographicValues ( geographicType ) ;
106
130
} else {
107
- maxSelectionLength = 10 ;
131
+ data = [ ] ;
108
132
}
133
+
134
+
109
135
$ ( '#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 ,
139
137
minimumInputLength : 0 ,
140
- maximumSelectionLength : maxSelectionLength ,
138
+ maximumSelectionLength : maximumSelectionLength ,
141
139
} ) ;
142
140
}
143
141
144
- $ ( document ) . ready ( function ( ) {
145
- initGeographicValueSelect ( 'epivis' ) ;
146
- } ) ;
147
-
148
142
document . getElementById ( 'geographic_type' ) . addEventListener ( "change" , ( event ) => {
149
- $ ( '#geographic_value' ) . val ( null ) . trigger ( 'change' ) ;
143
+ $ ( '#geographic_value' ) . empty ( ) ;
144
+ initGeographicValueSelect ( currentMode , event . target . value ) ;
150
145
} )
151
146
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
-
166
147
function handleModeChange ( mode ) {
167
148
document . getElementById ( "epivis-form" ) . reset ( ) ;
168
149
@@ -185,17 +166,49 @@ <h5>Plot / Export data</h5>
185
166
} ) ;
186
167
}
187
168
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
+ }
188
201
189
202
function submitMode ( event ) {
190
203
event . preventDefault ( ) ;
191
-
204
+
192
205
var dataSource = document . getElementById ( 'source' ) . value ;
193
206
var dataSignal = document . getElementById ( 'signal' ) . value ;
194
207
var geographicType = document . getElementById ( 'geographic_type' ) . value ;
195
208
var geographicValue = $ ( '#geographic_value' ) . select2 ( 'data' ) . map ( ( el ) => el . id ) . join ( ',' ) ;
196
209
197
210
if ( geographicType === 'Choose...' || geographicValue === '' ) {
198
- alert ( 'Please select Geographic Type and Geographic Value!' ) ;
211
+ showWarningAlert ( " Geographic Type or Geographic Value is not selected." ) ;
199
212
return ;
200
213
}
201
214
@@ -205,20 +218,20 @@ <h5>Plot / Export data</h5>
205
218
206
219
epiVisUrl += `#${ urlParamsEncoded } ` ;
207
220
window . open ( epiVisUrl , '_blank' ) . focus ( ) ;
208
-
209
221
} else {
210
222
var startDate = document . getElementById ( 'start_date' ) . value ;
211
223
var endDate = document . getElementById ( 'end_date' ) . value ;
212
224
213
225
var dataExportUrl = "{{ data_export_url }}" ;
214
226
215
227
if ( startDate === '' || endDate === '' ) {
216
- alert ( 'Please select start and end date' ) ;
228
+ showWarningAlert ( "Start Date or End Date is not selected." )
217
229
return ;
218
230
}
219
231
220
232
dataExportUrl += `?signal=${ dataSource } :${ dataSignal } &start_day=${ startDate } &end_day=${ endDate } &geo_type=${ geographicType } &geo_values=${ geographicValue } ` ;
221
233
window . open ( dataExportUrl , '_blank' ) . focus ( ) ;
222
234
}
223
235
}
236
+
224
237
</ script >
0 commit comments