|
27 | 27 | } |
28 | 28 | } |
29 | 29 |
|
| 30 | + // Pre-fill the form from URL parameters. |
| 31 | + // Supported parameters: data_source, signal, start_day, end_day, geo_type, geo_value, as_of |
| 32 | + const urlParams = new URLSearchParams(window.location.search); |
| 33 | +
|
| 34 | + // Overwrite default source & sensor if these are present in the URL |
| 35 | + if (urlParams.has('data_source')) { |
| 36 | + sourceValue = urlParams.get('data_source'); |
| 37 | +
|
| 38 | + if (urlParams.has('signal')) { |
| 39 | + sensorValue = urlParams.get('signal'); |
| 40 | + } |
| 41 | + } |
| 42 | +
|
30 | 43 | $: isWeekly = sensor ? sensor.isWeeklySignal : false; |
31 | 44 | $: formatDate = isWeekly ? (d) => formatWeek(d).replace('W', '') : formatDateISO; |
32 | 45 |
|
33 | | - let geoType = 'county'; |
| 46 | + let geoType = urlParams.has('geo_type') ? urlParams.get('geo_type') : 'county'; |
34 | 47 |
|
35 | 48 | let startDate = new Date(); |
36 | 49 | let endDate = new Date(); |
37 | 50 |
|
38 | 51 | function initDate(date) { |
39 | 52 | const param = new DateParam(date); |
40 | | - endDate = param.sparkLineTimeFrame.max; |
41 | | - startDate = param.sparkLineTimeFrame.min; |
| 53 | +
|
| 54 | + // Populate date based on URL params or, if absent, the current date |
| 55 | + startDate = urlParams.has('start_day') ? new Date(urlParams.get('start_day')) : param.sparkLineTimeFrame.min; |
| 56 | + endDate = urlParams.has('end_day') ? new Date(urlParams.get('end_day')) : param.sparkLineTimeFrame.max; |
| 57 | +
|
| 58 | + // Also normalize the dates to the current timezone |
| 59 | + startDate = new Date(startDate.getTime() - startDate.getTimezoneOffset() * -60000); |
| 60 | + endDate = new Date(endDate.getTime() - endDate.getTimezoneOffset() * -60000); |
42 | 61 | } |
43 | 62 | $: initDate($currentDateObject); |
44 | 63 |
|
|
59 | 78 |
|
60 | 79 | let geoValuesMode = 'all'; |
61 | 80 | let geoValues = []; |
| 81 | + let geoURLSet = false; |
62 | 82 | $: geoItems = [...(infosByLevel[geoType] || []), ...(geoType === 'county' ? infosByLevel.state : [])]; |
63 | 83 | $: { |
64 | 84 | if (geoItems != null) { |
65 | 85 | geoValues = []; |
66 | 86 | geoValuesMode = guessMode(geoItems.length); |
67 | 87 | } |
| 88 | +
|
| 89 | + // Populate region based on URL params... but let the user override this later |
| 90 | + if (urlParams.has('geo_value') && !geoURLSet) { |
| 91 | + let infos = infosByLevel[geoType].filter((d) => d.propertyId == urlParams.get('geo_value')); |
| 92 | + addRegion(infos[0]); |
| 93 | + geoURLSet = true; |
| 94 | + } |
68 | 95 | } |
69 | 96 |
|
70 | 97 | function flatIds(geoValues) { |
|
90 | 117 | } |
91 | 118 | } |
92 | 119 | $: usesAsOf = asOfMode !== 'latest' && asOfDate instanceof Date; |
| 120 | + // set as_of based on URL params, if it's a valid date |
| 121 | + if (urlParams.has('as_of') && !isNaN(new Date(urlParams.get('as_of')))) { |
| 122 | + asOfMode = 'single'; |
| 123 | + asOfDate = new Date(urlParams.get('as_of')); |
| 124 | + // Also normalize the dates to the current timezone |
| 125 | + asOfDate = new Date(asOfDate.getTime() - asOfDate.getTimezoneOffset() * -60000); |
| 126 | + } |
93 | 127 |
|
94 | 128 | let form = null; |
95 | 129 |
|
|
104 | 138 | ); |
105 | 139 | }); |
106 | 140 | } |
| 141 | + // Fix up the UI if we got an inactive sensor from the URL parameters. |
| 142 | + if (sensor && !sensor.active) { |
| 143 | + showInActive = true; |
| 144 | + // Force an update to sourceValue to set related reactive statements correctly. |
| 145 | + let temp = sourceValue; |
| 146 | + sourceValue = ''; |
| 147 | + sourceValue = temp; |
| 148 | + } |
107 | 149 | }); |
108 | 150 |
|
109 | 151 | function addRegion(detail) { |
|
0 commit comments