-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
173 lines (170 loc) · 5.44 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const { h, render, Component, Fragment } = require('preact')
const components = {}
let OID = 0
const buildCreateElement = function (component) {
return (input, props, ...kids) => {
if (input === Fragment) return h(Fragment, props, kids)
let isComponentClass = typeof input !== 'string'
let tagName = isComponentClass ? input.tagName : input
tagName = tagName.toUpperCase()
if (tagName === 'SLOT' && component.props) {
let childs = component.props.children || []
for (let i = 0; i < childs.length; i++) {
if (childs[i] && childs[i].props && childs[i].props.slot === props.name) {
return childs[i]
}
}
}
if (components[tagName] || isComponentClass) {
let custom_el_props = {}
if (props) {
custom_el_props.key = props.key
custom_el_props.slot = props.slot
for (let key in props) {
if (key.indexOf('prop-') === 0) {
continue // pass implicitly as component prop
}
let isPropFunction = typeof props[key] === 'function'
let isDOMEvent = key.indexOf('on') === 0
if (isPropFunction && isDOMEvent) {
custom_el_props[key] = props[key]
delete props[key]
}
if (typeof props[key] === 'string') {
custom_el_props[key] = props[key]
delete props[key]
}
}
for (let key in props) {
if (key.indexOf('prop-') === 0) {
props[key.replace('prop-', '')] = props[key]
delete props[key]
}
}
}
let renderTagName = custom_el_props.as || tagName
return h(renderTagName, custom_el_props, h(components[tagName] || input, props, kids))
} else {
return h(tagName, props, kids)
}
}
}
module.exports.upgrade = function (el, props) {
if (!components[el.tagName]) throw new Error('oval component ' + el.tagName + ' not found')
let ComponentClass = components[el.tagName]
el.preactRenderRef = render(h(ComponentClass, props), el, el.preactRenderRef)
return el.component
}
module.exports.define = function (options) {
if (!options.tagName) throw new Error('options.tagName required')
options.tagName = options.tagName.toUpperCase()
if (components[options.tagName]) return components[options.tagName]
let isGlobal = true
if (options.tagLine.indexOf('not-global') !== -1) {
isGlobal = false
}
const OvalComponent = class extends Component {
constructor () {
super()
this.createElement = buildCreateElement(this)
this.oid = OID++
this.handlers = {}
this.shouldRender = true
this.unmountCalled = false
Object.defineProperty(this, 'el', {
get: () => {
if (!this.base) {
throw new Error('Component base not found to provide `el` reference. Is the component ' + options.tagName + ' mounted?')
}
return this.base.parentNode
}
})
if (options.onconstruct) options.onconstruct.call(this)
}
on (eventName, eventHandler) {
this.handlers[eventName] = this.handlers[eventName] || []
this.handlers[eventName].push(eventHandler)
}
off (eventName, eventHandler) {
let index = this.handlers[eventName].indexOf(eventHandler)
if (index === -1) return
this.handlers[eventName].splice(index, 1)
}
emit (eventName, eventData) {
if (this.handlers[eventName]) {
this.handlers[eventName].forEach(f => f(eventData))
}
}
componentWillMount () {
this.emit('mount')
this.emit('update')
}
componentWillUpdate () {
}
componentDidUpdate () {
this.emit('updated')
}
componentDidMount () {
this.el.component = this
this.el.state = this.state
this.el.on = this.on.bind(this)
this.el.off = this.off.bind(this)
this.el.emit = this.emit.bind(this)
this.el.unmount = this.unmount.bind(this)
this.el.update = this.update.bind(this)
for (let key in this.props) {
if (typeof this.props[key] === 'function') {
this.el.on(key, this.props[key])
}
}
this.emit('updated')
this.emit('mounted')
}
async update () {
if (!this.shouldRender) return
await this.forceUpdate()
}
unmount () {
let rootNode = this.el
render(null, rootNode, rootNode.preactRenderRef)
rootNode.parentNode.removeChild(rootNode)
}
componentWillUnmount () {
this.emit('unmount')
this.unmountCalled = true
}
shouldComponentUpdate () {
return this.shouldRender
}
render (props, state) {
if (this.defaultProps) {
props = Object.assign({}, this.defaultProps, props)
}
this.emit('update')
return this.template(Fragment, props, state)
}
static appendAt (container, props) {
let el = document.createElement(options.tagName)
for (let key in props) {
if (typeof props[key] === 'string') {
el.setAttribute(key, props[key])
}
}
container.appendChild(el)
el.preactRenderRef = render(h(this, props), el, el.preactRenderRef)
return el
}
static get tagName () {
return options.tagName
}
static get defaultProps () {
return options.tagProps || {}
}
}
if (isGlobal) {
components[options.tagName] = OvalComponent
let existingElements = document.body.querySelectorAll(options.tagName)
existingElements.forEach(module.exports.upgrade)
}
return OvalComponent
}