-
Notifications
You must be signed in to change notification settings - Fork 0
/
refract.js
159 lines (143 loc) · 6.15 KB
/
refract.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
(() => { 'use strict';
const createElement = (type, props, ...children) => {
if (props === null) props = {};
return {type, props, children};
};
const setAttribute = (dom, key, value) => {
if (typeof value == 'function' && key.startsWith('on')) {
const eventType = key.slice(2).toLowerCase();
dom.__refractHandlers = dom.__refractHandlers || {};
dom.removeEventListener(eventType, dom.__refractHandlers[eventType]);
dom.__refractHandlers[eventType] = value;
dom.addEventListener(eventType, dom.__refractHandlers[eventType]);
} else if (key == 'checked' || key == 'value' || key == 'className') {
dom[key] = value;
} else if (key == 'style' && typeof value == 'object') {
Object.assign(dom.style, value);
} else if (key == 'ref' && typeof value == 'function') {
value(dom);
} else if (key == 'key') {
dom.__refractKey = value;
} else if (typeof value != 'object' && typeof value != 'function') {
dom.setAttribute(key, value);
}
};
const render = (vdom, parent=null) => {
const mount = parent ? (el => parent.appendChild(el)) : (el => el);
if (typeof vdom == 'string' || typeof vdom == 'number') {
return mount(document.createTextNode(vdom));
} else if (typeof vdom == 'boolean' || vdom === null) {
return mount(document.createTextNode(''));
} else if (typeof vdom == 'object' && typeof vdom.type == 'function') {
return Component.render(vdom, parent);
} else if (typeof vdom == 'object' && typeof vdom.type == 'string') {
const dom = mount(document.createElement(vdom.type));
for (const child of [].concat(...vdom.children)) render(child, dom);
for (const prop in vdom.props) setAttribute(dom, prop, vdom.props[prop]);
return dom;
} else {
throw new Error(`Invalid VDOM: ${vdom}.`);
}
};
const patch = (dom, vdom, parent=dom.parentNode) => {
const replace = parent ? el => (parent.replaceChild(el, dom) && el) : (el => el);
if (typeof vdom == 'object' && typeof vdom.type == 'function') {
return Component.patch(dom, vdom, parent);
} else if (typeof vdom != 'object' && dom instanceof Text) {
return dom.textContent != vdom ? replace(render(vdom, parent)) : dom;
} else if (typeof vdom == 'object' && dom instanceof Text) {
return replace(render(vdom, parent));
} else if (typeof vdom == 'object' && dom.nodeName != vdom.type.toUpperCase()) {
return replace(render(vdom, parent));
} else if (typeof vdom == 'object' && dom.nodeName == vdom.type.toUpperCase()) {
const pool = {};
const active = document.activeElement;
[].concat(...dom.childNodes).map((child, index) => {
const key = child.__refractKey || `__index_${index}`;
pool[key] = child;
});
[].concat(...vdom.children).map((child, index) => {
const key = child.props && child.props.key || `__index_${index}`;
dom.appendChild(pool[key] ? patch(pool[key], child) : render(child, dom));
delete pool[key];
});
for (const key in pool) {
const instance = pool[key].__refractInstance;
if (instance) instance.componentWillUnmount();
pool[key].remove();
}
for (const attr of dom.attributes) dom.removeAttribute(attr.name);
for (const prop in vdom.props) setAttribute(dom, prop, vdom.props[prop]);
active.focus();
return dom;
}
};
class Component {
constructor(props) {
this.props = props || {};
this.state = null;
}
static render(vdom, parent=null) {
const props = Object.assign({}, vdom.props, {children: vdom.children});
if (Component.isPrototypeOf(vdom.type)) {
const instance = new (vdom.type)(props);
instance.componentWillMount();
instance.base = render(instance.render(), parent);
instance.base.__refractInstance = instance;
instance.base.__refractKey = vdom.props.key;
instance.componentDidMount();
return instance.base;
} else {
return render(vdom.type(props), parent);
}
}
static patch(dom, vdom, parent=dom.parentNode) {
const props = Object.assign({}, vdom.props, {children: vdom.children});
if (dom.__refractInstance && dom.__refractInstance.constructor == vdom.type) {
dom.__refractInstance.componentWillReceiveProps(props);
dom.__refractInstance.props = props;
return patch(dom, dom.__refractInstance.render(), parent);
} else if (Component.isPrototypeOf(vdom.type)) {
const ndom = Component.render(vdom, parent);
return parent ? (parent.replaceChild(ndom, dom) && ndom) : (ndom);
} else if (!Component.isPrototypeOf(vdom.type)) {
return patch(dom, vdom.type(props), parent);
}
}
setState(next) {
const compat = (a) => typeof this.state == 'object' && typeof a == 'object';
if (this.base && this.shouldComponentUpdate(this.props, next)) {
const prevState = this.state;
this.componentWillUpdate(this.props, next);
this.state = compat(next) ? Object.assign({}, this.state, next) : next;
patch(this.base, this.render());
this.componentDidUpdate(this.props, prevState);
} else {
this.state = compat(next) ? Object.assign({}, this.state, next) : next;
}
}
shouldComponentUpdate(nextProps, nextState) {
return nextProps != this.props || nextState != this.state;
}
componentWillReceiveProps(nextProps) {
return undefined;
}
componentWillUpdate(nextProps, nextState) {
return undefined;
}
componentDidUpdate(prevProps, prevState) {
return undefined;
}
componentWillMount() {
return undefined;
}
componentDidMount() {
return undefined;
}
componentWillUnmount() {
return undefined;
}
};
if (typeof module != 'undefined') module.exports = {createElement, render, Component};
if (typeof module == 'undefined') window.refract = {createElement, render, Component};
})();