-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
87 lines (75 loc) · 2.34 KB
/
index.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
var PicoComponent = require('picocomponent')
var viperHTML = require('viperhtml')
function extend (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i]
for (var key in source) {
target[key] = source[key]
}
}
return target
}
function HyperComponent (props) {
if (this.connectedCallback) this.connect = this.connectedCallback
if (this.disconnectedCallback) this.disconnect = this.disconnectedCallback
PicoComponent.call(this)
this.props = props || this.defaultProps || {}
this.state = this.defaultState || {}
}
extend((HyperComponent.prototype = Object.create(PicoComponent.prototype)), {
constructor: HyperComponent,
handleEvent: handleEvent,
render: render,
setState: setState
})
function handleEvent (event) {
this['on' + event.type](event)
}
function render (node) {
var self = this
this.el = this.renderCallback(
this._wire || (self._wire = function wire () {
var args = arguments
var value = args[0]
var isStatic = value && value.raw
if (args.length > 1) {
if (isStatic) {
return (node ? viperHTML.bind(node) : viperHTML.wire(self)).apply(viperHTML, args)
}
return viperHTML.wire.apply(viperHTML, args)
}
if (typeof value === 'object') {
if (isStatic) {
return viperHTML.wire(self).apply(viperHTML, args)
}
return viperHTML.wire(value)
}
}),
this._component || (function (componentCache) {
// leverage this closure as an in-memory store for child component instances
return (self._component = function component (Component, props, children) {
var key = props && props.key ? Component.name + ':' + props.key : Component.name
var instance = componentCache[key]
if (instance === undefined) {
instance = componentCache[key] = new Component(
extend(
Component.defaultProps || {},
props || {},
children ? { children: children } : {}
)
)
} else {
instance.props = extend(instance.props, props, { children: children })
}
return instance.render()
})
})({})
)
return this.el
}
function setState (state) {
extend(this.state, state)
this.render()
}
module.exports = HyperComponent
module.exports.default = module.exports