forked from Rotonde/rotonde-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotonde.js
163 lines (138 loc) · 4.21 KB
/
rotonde.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
function Rotonde(client_url)
{
this.client_url = client_url;
this.client_version = "0.1.42";
// SETUP
this.requirements = {style:["reset","fonts","main"],script:["portal","feed","entry","operator","index"]};
this.includes = {script:[]};
this.is_owner = null;
this.install = function()
{
for(id in this.requirements.script){
var name = this.requirements.script[id];
this.install_script(name);
}
for(id in this.requirements.style){
var name = this.requirements.style[id];
this.install_style(name);
}
this.install_style("custom", true);
}
this.install_style = function(name, is_user_side)
{
var href = "links/"+name+'.css';
if(!is_user_side) href = this.client_url+href;
var s = document.createElement('link');
s.rel = 'stylesheet';
s.type = 'text/css';
s.href = href;
document.getElementsByTagName('head')[0].appendChild(s);
}
this.install_script = function(name)
{
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = this.client_url+"scripts/"+name+'.js';
document.getElementsByTagName('head')[0].appendChild(s);
}
this.confirm = function(type,name)
{
console.log("Included:",type,name)
this.includes[type].push(name);
this.verify();
}
this.verify = function()
{
var remaining = [];
for(id in this.requirements.script){
var name = this.requirements.script[id];
if(this.includes.script.indexOf(name) < 0){ remaining.push(name); }
}
if(remaining.length == 0){
this.start();
}
}
// START
this.el = document.createElement('div');
this.el.className = "rotonde";
this.portal = null;
this.feed = null;
this.operator = null;
this.index = null;
this.start = function()
{
console.info("Start")
document.body.appendChild(this.el);
document.addEventListener('mousedown',r.mouse_down, false);
this.index = new Index();
this.operator = new Operator();
this.operator.install(this.el);
this.load_account();
}
this.load_account = async function()
{
var dat = window.location.toString();
var archive = new DatArchive(dat);
var info = await archive.getInfo();
var portal_str;
var portal_data;
// Read or make file
try {
portal_str = await archive.readFile('/portal.json');
} catch (err) {
await archive.writeFile('/portal.json', JSON.stringify(r.create_portal(), null, 2));
portal_data = r.create_portal();
}
try {
portal_data = JSON.parse(portal_str);
// append slash to port entry so that .indexOf works correctly in other parts
portal_data.port = portal_data.port.map(function(portal_entry) {
if (portal_entry.slice(-1) !== "/") { portal_entry += "/";}
return portal_entry
})
} catch (err) {
console.error("Malformed JSON in portal.json")
}
portal_data.dat = dat;
this.portal = new Portal(portal_data);
this.portal.install(this.el);
this.is_owner = info.isOwner;
if(!info.isOwner){
this.operator.el.style.display = "none";
}
}
this.create_portal = async function(name = "new_name")
{
var archive = new DatArchive(window.location.toString())
var portal_str = await r.portal.archive.readFile('/dat.json');
var name = JSON.parse(portal_str).title.replace(/\W/g, '');
return {name: name,desc: "new_desc",port:[],feed:[],site:"",dat:""}
}
this.load_feed = async function(feed)
{
this.feed = new Feed(feed);
this.index.listeners.push(this.feed);
this.feed.install(this.el);
}
this.mouse_down = function(e)
{
if(!e.target.getAttribute("data-operation")){ return; }
e.preventDefault();
r.operator.inject(e.target.getAttribute("data-operation"));
window.scrollTo(0, 0);
}
this.reset = function()
{
this.reset_with_name();
}
this.reset_with_name = async function()
{
var archive = new DatArchive(window.location.toString())
var portal_str = await r.portal.archive.readFile('/dat.json');
var name = JSON.parse(portal_str).title.replace(/\W/g, '');
this.portal.data = {name: name,desc: "new_desc",port:[],feed:[],site:"",dat:""}
this.portal.save();
}
}
// Make this accessible for jest
window.Rotonde = Rotonde;