forked from Raynos/mercury
-
Notifications
You must be signed in to change notification settings - Fork 0
/
real-dom.js
59 lines (52 loc) · 1.6 KB
/
real-dom.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
'use strict';
var document = require('global/document');
var hg = require('../index.js');
var h = require('../index.js').h;
var setTimeout = require('timers').setTimeout;
function Hook() {}
// Hook to be called when DOM node has been rendered
Hook.prototype.hook = function hook(node) {
// Note that you should only modify DOM node attributes in virtual-dom
// hooks, anything else is unsafe
node.style.color = 'red';
};
// Hook to be called when DOM node is removed
Hook.prototype.unhook = function unhook(node) {
node.style.color = null;
};
function Widget() {
this.type = 'Widget';
}
Widget.prototype.init = function init() {
var elem = document.createElement('div');
elem.innerHTML = 'Content set directly on real DOM node, by widget ' +
'<em>before</em> update.';
return elem;
};
Widget.prototype.update = function update(prev, elem) {
elem.innerHTML = 'Content set directly on real DOM node, by widget ' +
'<em>after</em> update.';
};
function App() {
var state = hg.state({
updated: hg.value(false)
});
setTimeout(function timer() {
state.updated.set(true);
}, 2000);
return state;
}
App.render = function App(state) {
return h('div', [
// Create virtual node with hook if !state.updated
h('div', !state.updated ? {hook: new Hook()} : {}, [
'Style attribute of real DOM node set by ',
h('em', 'hook'),
' and unset by ',
h('em', 'unhook')
]),
// Create widget controlled node
new Widget()
]);
};
hg.app(document.body, App(), App.render);