-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdomComponent.js
57 lines (47 loc) · 1.41 KB
/
domComponent.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
var createElement = require('virtual-dom/create-element')
var diff = require('virtual-dom/diff')
var patch = require('virtual-dom/patch')
var toVdom = require('./toVdom')
var isVdom = require('./isVdom')
function DomComponent (options) {
this.document = options && options.document
}
function prepareVdom (object) {
var vdom = toVdom(object)
if (!isVdom(vdom)) {
throw new Error('expected render to return vdom')
} else {
return vdom
}
}
DomComponent.prototype.create = function (vdom) {
this.vdom = prepareVdom(vdom)
return (this.element = createElement(this.vdom, {document: this.document}))
}
DomComponent.prototype.merge = function (vdom, element) {
this.vdom = prepareVdom(vdom)
return (this.element = element)
}
DomComponent.prototype.update = function (vdom) {
var oldVdom = this.vdom
this.vdom = prepareVdom(vdom)
var patches = diff(oldVdom, this.vdom)
return (this.element = patch(this.element, patches))
}
DomComponent.prototype.destroy = function (options) {
function destroyWidgets (vdom) {
if (vdom.type === 'Widget') {
vdom.destroy()
} else if (vdom.children) {
vdom.children.forEach(destroyWidgets)
}
}
destroyWidgets(this.vdom)
if (options && options.removeElement && this.element.parentNode) {
this.element.parentNode.removeChild(this.element)
}
}
function domComponent (options) {
return new DomComponent(options)
}
exports.create = domComponent