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

slowly cleaning up the url hash handling code

parent d40293ce
......@@ -1691,7 +1691,17 @@ let getSitesFromUrlString = (sites_string) => {
}, [])
// find the sites that match those strings
return Object.keys(siteData)
.filter(site => hash_sites.includes(site.toLowerCase().replace(/[^a-z]/g, '-')))
.reduce((acc, site) => {
// index of the site name in hash_sites
var i = hash_sites.indexOf(site.toLowerCase().replace(/[^a-z]/g, '-'))
if (i>=0) {
acc[i] = site
}
return acc;
}, new Array(hash_sites.length))
// filter out any undefined elements,
// which are simply sites not found in siteData
.filter(site => (site !== undefined))
}
/*
......@@ -2043,18 +2053,10 @@ document.addEventListener('DOMContentLoaded', (e)=>{
window.addEventListener("hashchange", (e)=>{
console.log(`+-- manual hash change! '${window.location.hash}'`)
console.log(` got hash: ${window.location.hash}`)
hash_sites = window
.location
.hash
.substr(1) // remove the '#'
.split(',') // split by ',' in case we have a number of sites to handle
.reduce((acc, el)=>{ // filter out empty strings
if (el) {
acc.push(el);
}
return acc;
}, [])
// get the actual site names from the URL hash (sans '#')
hash_sites = getSitesFromUrlString(window.location.hash.substr(1))
// any site data containers to be removed?
if (sitesSelects.length - hash_sites.length > 0) {
console.log(` +-- removing ${sitesSelects.length - hash_sites.length} sites...`)
......@@ -2065,19 +2067,19 @@ document.addEventListener('DOMContentLoaded', (e)=>{
}
}
// do we have any hash to work with?
if (window.location.hash) {
// do we have any sites to work with?
if (hash_sites.length > 0) {
// handle each hash site separately, adding site data containers if need be
hash_sites.forEach((hash_site, index)=>{
var hash_site = sites.filter(site => (site.toLowerCase().replace(/[^a-z]/g, '-') === hash_site))
if ( ( index < 6 ) && ( hash_site.length === 1 ) ) {
// we only allow 6 sites
if ( index < 6 ) {
if ( ( index >= sitesSelects.length ) ) {
addSite.dispatchEvent(new Event('click'))
}
console.log(` +-- got site from hash: ${hash_site[0]}`)
console.log(` +-- got site from hash: ${hash_site}`)
// this only handles the first sitesSelect
// all others are not currently addressable in hash
sitesSelects[index].value = hash_site[0]
sitesSelects[index].value = hash_site
sitesSelects[index].dispatchEvent(new Event('change'));
}
})
......
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