forked from caracal-js/Incognito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
81 lines (71 loc) · 2.5 KB
/
app.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
import EventEmitter from "./events.js";
import { createComponent } from './component.js';
class App extends EventEmitter {
constructor(hashes = []) {
super();
this.header = createComponent(document.querySelector('header'));
this.search = createComponent(document.querySelector('header .search'));
this.nav = createComponent(document.querySelector('nav'));
this.main = createComponent(document.querySelector('main'));
this.hashes = hashes;
};
init() {
window.addEventListener('hashchange', event => {
const ancestor = new URL(event.oldURL);
this.emit('exit', ancestor, location);
(this.emit(location.hash, this) || this.emit('default', this))
this.emit('after', null);
});
this.emit('init', this);
this.emit(location.hash, this) || this.emit('default', this);
};
createLink(href = null, content = '', config = {}) {
const elem = this.createElement('a', content, config);
if (href) elem.href = href;
return elem;
};
createElement(elemName, content = '', config = {}) {
const element = document.createElement(elemName);
if ('events' in config) {
for (const name in config.events) {
element.addEventListener(name, config.events[name]);
};
};
if ('attrs' in config) {
for (const name in config.attrs) {
element.setAttribute(name, config.attrs[name])
};
};
if ('style' in config && 'style' in element) {
for (const name in config.style) {
element.style.setProperty(name, config.style[name])
};
};
if ('id' in config) {
element.id = config.id;
};
if ('class' in config) {
if (Array.isArray(config.class)) {
element.classList.add(...config.class);
} else {
element.className = config.class;
};
};
if (typeof content === 'object') {
if (Array.isArray(content)) {
element.append(
...content.filter(node => !!node)
);
} else {
element.append(content);
}
} else {
element.innerHTML = content;
};
if ('decorate' in config) {
config.decorate(element);
};
return element;
};
};
export { App };