Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
amethystnetwork-dev
GitHub Repository: amethystnetwork-dev/Incognito
Path: blob/main/static/script/app.js
917 views
1
/**
2
* Incognito
3
*
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 3 of the License, or
7
* (at your option) any later version.
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16
*/
17
18
/*
19
_____ _ _ _
20
| __ \ | | | | | |
21
| |__) | ___ _ __ | |_ ___ __| | | |__ _ _
22
| ___/ / _ \ | '__| | __| / _ \ / _` | | '_ \ | | | |
23
| | | (_) | | | | |_ | __/ | (_| | | |_) | | |_| |
24
|_| \___/ |_| \__| \___| \__,_| |_.__/ \__, |
25
__/ |
26
|___/
27
_ _ _ _ _ _ _
28
/\ | | | | | | | \ | | | | | |
29
/ \ _ __ ___ ___ | |_ | |__ _ _ ___ | |_ | \| | ___ | |_ __ __ ___ _ __ | | __
30
/ /\ \ | '_ ` _ \ / _ \ | __| | '_ \ | | | | / __| | __| | . ` | / _ \ | __| \ \ /\ / / / _ \ | '__| | |/ /
31
/ ____ \ | | | | | | | __/ | |_ | | | | | |_| | \__ \ | |_ | |\ | | __/ | |_ \ V V / | (_) | | | | <
32
/_/ \_\ |_| |_| |_| \___| \__| |_| |_| \__, | |___/ \__| |_| \_| \___| \__| \_/\_/ \___/ |_| |_|\_\
33
__/ |
34
|___/
35
*/
36
37
import EventEmitter from "./events.js";
38
import { createComponent } from './component.js';
39
40
class App extends EventEmitter {
41
constructor(hashes = []) {
42
super();
43
this.bare = new BareMux.BareClient();
44
this.header = createComponent(document.querySelector('header'));
45
this.search = createComponent(document.querySelector('header .search'));
46
this.nav = createComponent(document.querySelector('nav'));
47
this.main = createComponent(document.querySelector('main'));
48
this.hashes = hashes;
49
};
50
init() {
51
window.addEventListener('hashchange', event => {
52
const ancestor = new URL(event.oldURL);
53
this.emit('exit', ancestor, location);
54
(this.emit(location.hash, this) || this.emit('default', this))
55
this.emit('after', null);
56
});
57
this.emit('init', this);
58
this.emit(location.hash, this) || this.emit('default', this);
59
};
60
createLink(href = null, content = '', config = {}) {
61
const elem = this.createElement('a', content, config);
62
if (href) elem.href = href;
63
return elem;
64
};
65
createElement(elemName, content = '', config = {}) {
66
const element = document.createElement(elemName);
67
68
if ('events' in config) {
69
for (const name in config.events) {
70
element.addEventListener(name, config.events[name]);
71
};
72
};
73
74
if ('attrs' in config) {
75
for (const name in config.attrs) {
76
element.setAttribute(name, config.attrs[name])
77
};
78
};
79
80
if ('style' in config && 'style' in element) {
81
for (const name in config.style) {
82
element.style.setProperty(name, config.style[name])
83
};
84
};
85
86
if ('id' in config) {
87
element.id = config.id;
88
};
89
90
if ('class' in config) {
91
if (Array.isArray(config.class)) {
92
element.classList.add(...config.class);
93
} else {
94
element.className = config.class;
95
};
96
};
97
98
if (typeof content === 'object') {
99
if (Array.isArray(content)) {
100
element.append(
101
...content.filter(node => !!node)
102
);
103
} else {
104
element.append(content);
105
}
106
} else {
107
element.innerHTML = content;
108
};
109
110
if ('decorate' in config) {
111
config.decorate(element);
112
};
113
114
return element;
115
};
116
117
#registerSW() {
118
return navigator.serviceWorker.getRegistration('./uv.sw-handler.js').then((worker) => {
119
if(worker) return worker;
120
return navigator.serviceWorker.register('./uv.sw-handler.js');
121
});
122
}
123
124
async registerSW() {
125
const worker = await this.#registerSW();
126
this.setTransport();
127
return worker;
128
}
129
130
setTransport() {
131
BareMux.SetTransport("CurlMod.LibcurlClient", { wisp: `${window.location.protocol === 'https:' ? 'wss' : 'ws'}://${window.location.host}/wisp/` });
132
}
133
};
134
135
export { App };
136