-
Notifications
You must be signed in to change notification settings - Fork 0
/
ivan.js
79 lines (72 loc) · 1.64 KB
/
ivan.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
var elements = {};
function div(name, props, children) {
let state = elements[name];
if (!state) {
state = elements[name] =
{ elem: document.createElement("div"), props: {}, name: name, children: [], content: null};
}
for (let prop in props) {
if (Reflect.getOwnPropertyDescriptor(props, prop)) {
if (state.props[prop] != props[prop]) {
state.props[prop] = props[prop];
state.elem[prop] = props[prop];
}
}
}
if (typeof(children) == "string") {
if (state.content != children) {
state.content = children;
state.elem.innerHTML = children;
}
} else {
for (let child of children) {
if (!state.children.find(x => x == child)) {
state.children.push(child);
state.elem.appendChild(child);
}
}
for (let child of state.children) {
if (!children.find(x => x == child)) {
state.children.splice(state.children.indexOf(child), 1);
state.elem.remove(child);
}
}
}
return state.elem;
}
function mount(container, elem) {
let found = false;
for (let child of container.children) {
if (child == elem) {
found = true;
}
}
if (!found) {
container.appendChild(elem);
}
}
class MyComponent {
constructor() {
setInterval(()=> this.render(), 1000);
this.onClick = this.onClick.bind(this);
}
onClick(){
this.flag = !this.flag;
this.render();
}
render() {
return (
div("my-div", {}, [
div("text", {className: "date"},
"Current time: " + (new Date()).toString()
),
div("class-change", {className: this.flag ? "foo" : "bar", onclick: this.onClick },
"Click me!"
)
])
);
}
}
mount(document.getElementById("container"),
(new MyComponent()).render()
);