Commit d40293ce authored by Michał Woźniak's avatar Michał Woźniak
Browse files

working on sanely processing URL hashes

parent 5fcb6d96
......@@ -996,21 +996,6 @@ let calculateAverageRate = (data) => {
}
/*
* assemble and set the url hash
* without firing a hashchange event
*/
let setUrlHash = () => {
var selected_sites = []
for (let sselect of sitesSelects) {
selected_sites.push(sselect.value.toLowerCase().replace(/[^a-z0-9]/g, '-'))
}
// using History.pushState() because we don't want the onhashchange event to fire
history.pushState({}, '', '#' + selected_sites.join(','))
}
/*
* handling a .sites-select change
*/
......@@ -1032,7 +1017,7 @@ let selectSite = (e) => {
var site = theSelect.value
// set the URL hash without firing the hashchange event
setUrlHash()
updateUrlHash()
// using a timeout to give the user a chance to change their mind
theSelect.selectTimeout = setTimeout(function(){
......@@ -1671,31 +1656,55 @@ let testAllSites = () => {
/*
* fun with data
* let's get the sites with negative new active cases
* helper: get all the chart settings as an object
*/
let sitesWithNegativeNewActiveCases = () => {
Object
.keys(siteData)
.forEach((site)=>{
var active = 0
var new_active = 0
try {
if ("active" in siteData[site].data[1]) {
active = siteData[site].data.slice(-1)[0].active
new_active = siteData[site].data.slice(-1)[0].new_active
} else {
sd = siteData[site].data.slice(-2)
active = sd[1].confirmed - sd[1].recovered - sd[1].deaths
new_active = active - (sd[0].confirmed - sd[0].recovered - sd[0].deaths)
}
if (new_active < 0) {
console.log(`${site} :: ${active} :: ${new_active}`)
}
} catch (e){
console.log(`${site} :: skipped, no data`)
}
})
let getChartSettings = () => {
return {
}
}
/*
* helper: set all the chart settings from an object
*/
let setChartSettings = (settingsObj) => {
}
/*
* get an array of sites from a string
* used mainly in context of the URL hash
*
* takes a comma-separated string of URL-normalized country names
* and returns the country names as used in siteData
*/
let getSitesFromUrlString = (sites_string) => {
// get the non-empty site names
hash_sites = sites_string
.split(',') // split by ',' in case we have a number of sites to handle
// filter out empty strings
.reduce((acc, el)=>{
if (el) {
acc.push(el);
}
return acc;
}, [])
// find the sites that match those strings
return Object.keys(siteData)
.filter(site => hash_sites.includes(site.toLowerCase().replace(/[^a-z]/g, '-')))
}
/*
* assemble and set the url hash
* without firing a hashchange event
*/
let updateUrlHash = () => {
var selected_sites = []
for (let sselect of sitesSelects) {
selected_sites.push(sselect.value.toLowerCase().replace(/[^a-z0-9]/g, '-'))
}
// using History.pushState() because we don't want the onhashchange event to fire
history.pushState({}, '', '#' + selected_sites.join(','))
}
......@@ -2018,7 +2027,7 @@ document.addEventListener('DOMContentLoaded', (e)=>{
sitesSelects[0].value = "select one"
}
// using History.pushState() because we don't want the onhashchange event to fire
setUrlHash()
updateUrlHash()
// update the chart
updateChartSettings()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment