-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
85 lines (73 loc) · 2.46 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
var Nanocomponent = require('nanocomponent')
var nanologger = require('nanologger')
var nanomorph = require('nanomorph')
var assert = require('assert')
var shallowEqual = require('juliangruber-shallow-equal/objects')
var rootLabelRegex = /^data-onloadid/
module.exports = Microcomponent
function Microcomponent (opts) {
if (!(this instanceof Microcomponent)) return new Microcomponent(opts)
Nanocomponent.call(this)
opts = opts || {}
this.name = opts.name || 'component'
this.props = opts.props || {}
this.state = opts.state || {}
this._log = nanologger(this.name)
this._log.debug('initialized')
if (opts.pure) {
this._update = function (props) {
return !shallowEqual(props, this.props)
}
}
}
Microcomponent.prototype = Object.create(Nanocomponent.prototype)
Microcomponent.prototype.on = function (eventname, handler) {
assert.equal(typeof eventname, 'string', 'microcomponent.on eventname should be type string')
assert.equal(typeof handler, 'function', 'microcomponent.on handler should be type function')
if (eventname === 'render') {
this._render = function () {
return handler.call(this)
}
var render = this.render
this.render = function (props) {
this._log.debug(eventname, props)
var oldElement = this._element
var ret = render.call(this, props)
var newElement = this._element
if (oldElement) {
var oldAttrs = oldElement.attributes
var attr, name
for (var i = 0, len = oldAttrs.length; i < len; i++) {
attr = oldAttrs[i]
name = attr.name
if (rootLabelRegex.test(name)) {
newElement.setAttribute(name, attr.value)
break
}
}
nanomorph(oldElement, newElement)
this._element = oldElement
}
return ret
}
} else {
this['_' + eventname] = function () {
var len = arguments.length
var args = new Array(len)
for (var i = 0; i < len; i++) args[i] = arguments[i]
args.length
? this._log.debug(eventname, args)
: this._log.debug(eventname)
var res = handler.apply(this, args)
return res
}
}
return this
}
Microcomponent.prototype.emit = function (eventname) {
assert.equal(typeof eventname, 'string', 'microcomponent.emit eventname should be type string')
var len = arguments.length - 1
var args = new Array(len)
for (var i = 0; i < len; i++) args[i] = arguments[i + 1]
return this['_' + eventname].apply(this, args)
}