-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcustom-element-v0.js
55 lines (48 loc) · 1.52 KB
/
custom-element-v0.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
var assert = require('assert')
module.exports = toCustomElement
function toCustomElement (Nanocomponent, attrs) {
if (!attrs) { attrs = [] }
assert.equal(typeof Nanocomponent, 'function', 'nanocomponent-adapters/custom-elements: component should be type object')
assert.equal(typeof attrs, 'object', 'nanocomponent-adapters/custom-elements-v1: attrs should be type Array')
var createdCallback = function () {
this.props = {}
this.comp = new Nanocomponent()
}
var attachedCallback = function () {
var newProps = {}
if (this.hasAttributes()) {
var mattrs = this.attributes
for (var i = 0, len = mattrs.length; i < len; i++) {
if (attrs.includes(mattrs[i].name)) {
newProps[mattrs[i].name] = mattrs[i].value
}
}
}
this.props = newProps
if (!this.comp.element) {
var el = this.comp.render(this.props)
this.appendChild(el)
}
}
var detachedCallback = function () {
this.props = null
this.comp = null
}
var attributeChangedCallback = function (attr, oldVal, newVal) {
if (attrs.includes(attr)) {
this.props[attr] = newVal
if (this.comp.element) this.comp.render(this.props)
}
}
return {
prototype: Object.create(
window.HTMLElement.prototype,
{
createdCallback: { value: createdCallback },
attachedCallback: { value: attachedCallback },
detachedCallback: { value: detachedCallback },
attributeChangedCallback: { value: attributeChangedCallback }
}
)
}
}