on d0f4bbb help sign in
webring/scripts/portal.js
68 lines 2.7 kB history raw
1'use strict'
3function Portal (sites) {
4 this.el = document.createElement('div')
5 this.sites = sites
7 // Templates
9 function _readme () {
10 return `<p class='readme'>This webring is an attempt to inspire artists & developers to build their own website and share traffic among each other. The ring welcomes personalized websites such as <b>diaries, wikis & portfolios</b>.</p><p>To add yourself to the ring, submit a <a href='https://github.com/XXIIVV/webring/edit/master/scripts/sites.js' target='_blank'>Pull Request</a>.<br />If you found a broken link, please <a href='https://github.com/XXIIVV/webring/issues/new' target='_blank'>report it</a>.</p>`
11 }
13 function _buttons () {
14 return `<p class='buttons'><a href='#random' onClick="portal.reload('random')">Random</a> | <a href='https://github.com/XXIIVV/webring'>Information</a> <a id='icon' href='#random' onClick="portal.reload('random')"></a></p>`
15 }
17 function _directory (sites) {
18 return `
19 <ul>${sites.reduce((acc, site, id) => { return `${acc}<li class='${site.type}'>${id}) <a href='${site.url}'>${_name(site)}</a></li>` }, '')}</ul>\n${_readme()}${_buttons()}`
20 }
22 function _name(site) {
23 return site.title ? site.title : `${site.url.split('//')[1]}`
24 }
26 function _redirect (site) {
27 return `<p>Redirecting to <a href="${site.url}">${site.url}</a></p><meta http-equiv="refresh" content="3; url=${site.url}">
28 <p class='buttons'><a href='#' onClick="portal.reload('')">Directory</a> | <a href='#${site.url}' onClick="portal.reload('random')">Skip</a> | <a href='#random' onClick="portal.reload('random')">Random</a> | <a href='https://github.com/XXIIVV/webring'>Information</a> <a id='icon' href='#random' onClick="portal.reload('random')"></a></p>`
29 }
31 //
33 this.install = function (host) {
34 host.appendChild(this.el)
35 }
37 this.start = function () {
38 this.el.innerHTML = window.location.hash && window.location.hash.length > 4 ? _redirect(this.next()) : _directory(this.sites)
39 }
41 this.reload = function () {
42 setTimeout(() => { window.location.reload() }, 500)
43 }
45 this.navigate = function (target) {
46 setTimeout(() => { window.location.href = target }, 3000)
47 }
49 this.locate = function () {
50 const hash = window.location.hash.replace('#', '').trim()
52 if (hash == 'random') {
53 return Math.floor(Math.random() * this.sites.length)
54 }
56 for (const id in this.sites) {
57 const site = this.sites[id]
58 if (site.url.indexOf(hash) > -1) {
59 return parseInt(id)
60 }
61 }
62 return -1
63 }
65 this.next = function (loc = this.locate()) {
66 return loc == this.sites.length - 1 ? this.sites[0] : this.sites[loc + 1]
67 }