1+ const epiVisUrl = localStorage . getItem ( "epivis_url" ) ;
2+ const dataExportUrl = localStorage . getItem ( "data_export_url" ) ;
3+ const covidCastUrl = localStorage . getItem ( "covidcast_url" ) ;
4+
15function initSelect2 ( elementId , data ) {
26 $ ( `#${ elementId } ` ) . select2 ( {
37 data : data ,
@@ -35,7 +39,7 @@ document.getElementById('geographic_type').addEventListener("change", (event) =>
3539
3640let checkedSignalMembers = [ ]
3741
38- function plotData ( epivisUrl ) {
42+ function plotData ( ) {
3943 var dataSets = { } ;
4044
4145 var geographicType = document . getElementById ( 'geographic_type' ) . value ;
@@ -70,7 +74,7 @@ function plotData(epivisUrl) {
7074
7175 var urlParamsEncoded = btoa ( `{"datasets":${ JSON . stringify ( requestParams ) } }` ) ;
7276
73- var linkToEpivis = `${ epivisUrl } #${ urlParamsEncoded } `
77+ var linkToEpivis = `${ epiVisUrl } #${ urlParamsEncoded } `
7478 window . open ( linkToEpivis , '_blank' ) . focus ( ) ;
7579}
7680
@@ -96,7 +100,7 @@ function addSelectedSignal(element) {
96100 _endpoint : element . dataset . endpoint ,
97101 data_source : element . dataset . datasource ,
98102 signal : element . dataset . signal ,
99- time_type : element . dataset . timetype ,
103+ time_type : element . dataset . timeType ,
100104 } ) ;
101105 updateSelectedSignals ( element . dataset . datasource , element . dataset . signalDisplayname , element . dataset . signalSet , element . dataset . signal ) ;
102106 } else {
@@ -200,4 +204,145 @@ function format (signalSetId, relatedSignals) {
200204 tableMarkup = "<p>No available signals yet.</p>"
201205 }
202206 return tableMarkup ;
207+ }
208+
209+
210+ function exportData ( ) {
211+ var geographicType = document . getElementById ( 'geographic_type' ) . value ;
212+ var geographicValues = $ ( '#geographic_value' ) . select2 ( 'data' ) . map ( ( el ) => ( typeof el . id === 'string' ) ? el . id . toLowerCase ( ) : el . id ) ;
213+ if ( geographicType === 'Choose...' || geographicValues . length === 0 ) {
214+ showWarningAlert ( "Geographic Type or Geographic Value is not selected." ) ;
215+ return ;
216+ }
217+ var startDate = document . getElementById ( 'start_date' ) . value ;
218+ var endDate = document . getElementById ( 'end_date' ) . value ;
219+
220+ var manualDataExport = "To download data, please click on the link or copy/paste command into your terminal: \n\n"
221+
222+ checkedSignalMembers . forEach ( ( signal ) => {
223+ if ( signal [ "time_type" ] === "week" ) {
224+ startDate = getDateYearWeek ( new Date ( startDate ) ) ;
225+ endDate = getDateYearWeek ( new Date ( endDate ) ) ;
226+ } ;
227+ var exportUrl = `${ dataExportUrl } ?signal=${ signal [ "data_source" ] } :${ signal [ "signal" ] } &start_day=${ startDate } &end_day=${ endDate } &geo_type=${ geographicType } &geo_values=${ geographicValues } ` ;
228+ manualDataExport += `wget --content-disposition <a href="${ exportUrl } ">${ exportUrl } </a>\n`
229+ } ) ;
230+ $ ( '#modeSubmitResult' ) . html ( manualDataExport ) ;
231+ }
232+
233+ function previewData ( ) {
234+ var geographicType = document . getElementById ( 'geographic_type' ) . value ;
235+ var geographicValues = $ ( '#geographic_value' ) . select2 ( 'data' ) . map ( ( el ) => ( typeof el . id === 'string' ) ? el . id . toLowerCase ( ) : el . id ) . join ( ',' ) ;
236+ if ( geographicType === 'Choose...' || geographicValues . length === 0 ) {
237+ showWarningAlert ( "Geographic Type or Geographic Value is not selected." ) ;
238+ return ;
239+ }
240+ var previewExample = [ ] ;
241+ checkedSignalMembers . forEach ( ( signal ) => {
242+ var startDate = document . getElementById ( "start_date" ) . value ;
243+ var endDate = document . getElementById ( "end_date" ) . value ;
244+ if ( signal [ "time_type" ] === "week" ) {
245+ startDate = getDateYearWeek ( new Date ( startDate ) ) ;
246+ endDate = getDateYearWeek ( new Date ( endDate ) ) ;
247+ } ;
248+ var requestSent = false ;
249+ if ( ! requestSent ) {
250+ $ . ajax ( {
251+ url : covidCastUrl ,
252+ type : 'GET' ,
253+ async : false ,
254+ data : {
255+ 'time_type' : signal [ "time_type" ] ,
256+ 'time_values' : `${ startDate } --${ endDate } ` ,
257+ 'data_source' : signal [ "data_source" ] ,
258+ 'signal' : signal [ "signal" ] ,
259+ 'geo_type' : geographicType ,
260+ 'geo_values' : geographicValues
261+ } ,
262+ success : function ( result ) {
263+ if ( result [ "epidata" ] . length != 0 ) {
264+ previewExample . push ( { epidata : result [ "epidata" ] [ 0 ] , result : result [ "result" ] , message : result [ "message" ] } )
265+ } else {
266+ previewExample . push ( { epidata : result [ "epidata" ] , result : result [ "result" ] , message : result [ "message" ] } )
267+ }
268+ }
269+ } )
270+ }
271+ } )
272+ $ ( '#modeSubmitResult' ) . html ( JSON . stringify ( previewExample , null , 2 ) ) ;
273+ requestSent = true ;
274+ }
275+
276+
277+ // Plot/Export/Preview data block
278+
279+ var currentMode = 'preview' ;
280+
281+ function handleModeChange ( mode ) {
282+ document . getElementById ( "dataForm" ) . reset ( ) ;
283+ $ ( '#geographic_value' ) . empty ( ) ;
284+ $ ( '#modeSubmitResult' ) . html ( '' ) ;
285+
286+ var choose_dates = document . getElementsByName ( 'choose_date' ) ;
287+
288+ if ( mode === 'epivis' ) {
289+ currentMode = 'epivis' ;
290+ choose_dates . forEach ( ( el ) => {
291+ el . style . display = 'none' ;
292+ } ) ;
293+ $ ( '#modeSubmitResult' ) . html ( '' ) ;
294+ } else if ( mode === 'export' ) {
295+ currentMode = 'export' ;
296+ choose_dates . forEach ( ( el ) => {
297+ el . style . display = 'flex' ;
298+ } ) ;
299+ $ ( '#modeSubmitResult' ) . html ( '' ) ;
300+ } else {
301+ currentMode = 'preview' ;
302+ choose_dates . forEach ( ( el ) => {
303+ el . style . display = 'flex' ;
304+ } ) ;
305+ }
306+ document . getElementsByName ( "modes" ) . forEach ( ( el ) => {
307+ if ( currentMode === el . value ) {
308+ el . checked = true ;
309+ }
310+ } ) ;
311+ }
312+
313+ function getDateYearWeek ( date ) {
314+ const currentDate =
315+ ( typeof date === 'object' ) ? date : new Date ( ) ;
316+ const januaryFirst =
317+ new Date ( currentDate . getFullYear ( ) , 0 , 1 ) ;
318+ const daysToNextMonday =
319+ ( januaryFirst . getDay ( ) === 1 ) ? 0 :
320+ ( 7 - januaryFirst . getDay ( ) ) % 7 ;
321+ const nextMonday =
322+ new Date ( currentDate . getFullYear ( ) , 0 ,
323+ januaryFirst . getDate ( ) + daysToNextMonday ) ;
324+
325+ var weekNumber = ( currentDate < nextMonday ) ? 52 :
326+ ( currentDate > nextMonday ? Math . ceil (
327+ ( currentDate - nextMonday ) / ( 24 * 3600 * 1000 ) / 7 ) : 1 ) ;
328+
329+ if ( weekNumber < 10 ) {
330+ weekNumber = `0${ weekNumber } ` ;
331+ }
332+
333+ const year = currentDate . getFullYear ( )
334+
335+ return `${ year } ${ weekNumber } ` ;
336+ }
337+
338+ function submitMode ( event ) {
339+ event . preventDefault ( ) ;
340+
341+ if ( currentMode === 'epivis' ) {
342+ plotData ( ) ;
343+ } else if ( currentMode === 'export' ) {
344+ exportData ( ) ;
345+ } else {
346+ previewData ( ) ;
347+ }
203348}
0 commit comments