on 4e103fa help sign in
webring/scripts/portal.js
69 lines 2.9 kB history raw
1'use strict'
3function Portal (sites) {
4 this.el = document.createElement('div')
5 this.el.id = 'portal'
6 this.sites = sites
8 // Templates
10 function _readme () {
11 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>`
12 }
14 function _buttons () {
15 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> | <a href="hallway.html">The Hallway</a> | <a href="wiki.html">The Wiki</a> | <a id="opml">OPML</a></p>`
16 }
18 function _directory (sites) {
19 const listItems = sites.reduce((acc, site, id) => `${acc}<li ${_type(site)} id='${id}'><a href='${site.url}'>${_name(site)}</a></li>`, '')
20 return `<main><ul>${listItems}</ul></main><footer>${_readme()}${_buttons()}</footer>`
21 }
23 function _name (site) {
24 return site.title || `${site.url.split('//')[1]}`
25 }
27 function _type (site) {
28 return site.type ? `data-type="${site.type}"` : ''
29 }
31 function _redirect (site) {
32 return `<main><p>Redirecting to <a href="${site.url}">${site.url}</a></p></main>
33 <meta http-equiv="refresh" content="3; url=${site.url}">
34 <footer><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>`
35 }
37 //
39 this.install = function (host) {
40 host.appendChild(this.el)
41 }
43 this.start = function () {
44 this.el.innerHTML = window.location.hash && window.location.hash.length > 4 ? _redirect(this.next()) : _directory(this.sites)
45 }
47 this.reload = function () {
48 setTimeout(() => { window.location.reload() }, 500)
49 }
51 this.navigate = function (target) {
52 setTimeout(() => { window.location.href = target }, 3000)
53 }
55 this.locate = function () {
56 const hash = window.location.hash.replace('#', '').trim()
58 if (hash === 'random') {
59 return Math.floor(Math.random() * this.sites.length)
60 } else {
61 const id = sites.findIndex(site => site.url.indexOf(hash) > -1)
62 return id > -1 ? parseInt(id) : -1
63 }
64 }
66 this.next = function (loc = this.locate()) {
67 return loc == this.sites.length - 1 ? this.sites[0] : this.sites[loc + 1]
68 }