on master help sign in
add back in ability to go to the next site in the ring that matches langs
commit67981cd063f3a9ccb55e451145d17fbeb134a33b authorckipp01 <ckipp@pm.me> dateWed 9 Oct 2019, 21:25 (7 years ago) parent69897f6
1 file changed+11−8
scripts/portal.js+11−8
scripts/portal.js+11−8
@@ -53,8 +53,9 @@ function Portal (sites) {
5353 }
5454
5555 this.start = function () {
56− if (window.location.hash && window.location.hash.length > 4) {
57− const site = this.next()
56+ const hash = window.location.hash
57+ if (hash && hash.length > 4) {
58+ const site = this.getSite(hash.replace('#', '').trim())
5859 this.el.innerHTML = _redirectView(site)
5960 setTimeout(() => { window.location = site.url }, 3000)
6061 } else {
@@ -73,15 +74,17 @@ function Portal (sites) {
7374 this.sitesMatchingLangs = this.sites
7475 .filter(site => site.langs && site.langs.some(lang => navigator.languages.includes(lang)))
7576
76− this.next = function () {
77− const hash = window.location.hash.replace('#', '').trim()
77+ this.nextSiteIndex = (ceiling, index) => index === ceiling ? 0 : index + 1
78+ this.randomSite = this.sitesMatchingLangs[Math.floor(Math.random() * this.sitesMatchingLangs.length)]
7879
80+ this.getSite = function (hash) {
7981 if (hash === 'random') {
80− return this.sitesMatchingLangs[Math.floor(Math.random() * this.sitesMatchingLangs.length)]
82+ return this.randomSite
8183 } else {
82− return sites.find(site => site.url.includes(hash))
83− ? sites.find(site => site.url.includes(hash))
84− : this.sitesMatchingLangs[Math.floor(Math.random() * this.sitesMatchingLangs.length)]
84+ const index = this.sitesMatchingLangs.findIndex(site => site.url.includes(hash))
85+ return (index > -1)
86+ ? this.sitesMatchingLangs[this.nextSiteIndex(this.sitesMatchingLangs.length, index)]
87+ : this.randomSite
8588 }
8689 }
8790}