on dd2d4e5 help sign in
webring/scripts/portal.js
87 lines 2.8 kB history raw
1'use strict';
3function Portal(sites)
4{
5 this.el = document.createElement("div");
6 this.sites = sites;
7
8 this.install = function()
9 {
10 document.body.appendChild(this.el);
11 }
13 this.start = function()
14 {
15 this.install();
16 this.el.innerHTML = window.location.hash && window.location.hash.length > 4 ? this.redirect() : this.directory();
17 }
19 this.readme = function()
20 {
21 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/index.html' 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>`
22 }
24 this.buttons = function()
25 {
26 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>`
27 }
29 this.directory = function()
30 {
31 let html = ""
33 for(let id in this.sites){
34 let site = this.sites[id]
35 html += `<ln>${id}) <a href='${site}'>${site.split("//")[1]}</a></ln>`
36 }
37 return `<list>${html}</list>\n${this.readme()}${this.buttons()}`
38 }
40 this.reload = function()
41 {
42 setTimeout(()=>{ window.location.reload() },500)
43 }
45 this.navigate = function(target)
46 {
47 setTimeout(() => {
48 window.location.href = target
49 },3000)
50 }
52 this.location = function()
53 {
54 return window.location.hash.replace("#","").trim();
55 }
57 this.locate = function()
58 {
59 let hash = this.location();
61 if(hash == "random"){
62 return Math.floor(Math.random()*this.sites.length)
63 }
65 for(let id in this.sites){
66 let site = this.sites[id];
67 if(site.indexOf(hash) >-1){
68 return parseInt(id)
69 }
70 }
71 return -1
72 }
74 this.next = function(loc)
75 {
76 return loc == this.sites.length-1 ? this.sites[0] : this.sites[loc+1];
77 }
79 this.redirect = function()
80 {
81 let location = this.locate();
82 let target = this.next(location);
83 this.navigate(target)
84 return `<p>Redirecting to <b>${target}</b></p><meta http-equiv="refresh" content="3; url=${target}">
85 <p class='buttons'><a href='#' onClick="portal.reload('')">Directory</a> | <a href='#${target}' 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>`
86 }