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