-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
132 lines (112 loc) · 3.09 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
var app = require('hyperapp').app
var CEV0Component = require('ce-v0/comp')
function hyperappCustomElement (options) {
var view = options.view
var state = options.state
var ctor = options.constructor
var mapAttrsToState = 'mapAttrsToState' in options
? !!options.mapAttrsToState
: true
var observedAttributes = options.observedAttributes
var hasObservedAttributes = Array.isArray(observedAttributes)
var actions = Object.assign(mapAttrsToState ? {
__applyState: (item) => (state) => {
return item
}
} : {}, options.actions)
var opts = {}
var converters = {}
if (hasObservedAttributes) {
opts.observedAttributes = []
observedAttributes.forEach(function (attr) {
if (typeof attr === 'string') {
opts.observedAttributes.push(attr)
} else {
var name = Object.keys(attr)[0]
opts.observedAttributes.push(name)
if (typeof attr[name] === 'function') {
converters[name] = attr[name]
}
}
})
}
function convert (name, value) {
var converter = converters[name]
if (converter) {
if (converter === Boolean) {
return value !== 'false' && value !== '0'
}
return converters[name](value)
}
return value
}
function mapToState (el) {
if (hasObservedAttributes) {
opts.observedAttributes.forEach(function (name) {
if (el.hasAttribute(name)) {
var item = {}
item[name] = convert(name, el.getAttribute(name))
el.actions.__applyState(item)
}
})
}
}
if (typeof ctor === 'function') {
opts.constructor = function () {
this.actions = app(state, actions, view, this)
ctor.call(this)
if (mapAttrsToState) {
mapToState(this)
}
}
} else {
opts.constructor = function () {
this.actions = app(state, actions, view, this)
if (mapAttrsToState) {
mapToState(this)
}
}
}
// Only fire attributeChangedCallback
// if it's an observed attribute as per CEV1
var onattribute
var attributeChangedCallback = options.attributeChangedCallback
if (hasObservedAttributes) {
onattribute = function (name, oldValue, newValue) {
if (observedAttributes.indexOf(name) < 0) {
return
}
if (attributeChangedCallback) {
attributeChangedCallback.call(this, name, oldValue, newValue)
}
if (mapAttrsToState) {
var partial = {}
partial[name] = convert(name, newValue)
this.actions.__applyState(partial)
}
}
opts.onattribute = onattribute
}
for (var key in options) {
switch (key) {
case 'connectedCallback':
opts.onconnected = options[key]
break
case 'disconnectedCallback':
opts.ondisconnected = options[key]
break
case 'view':
case 'state':
case 'actions':
case 'constructor':
case 'observedAttributes':
case 'attributeChangedCallback':
break
default:
opts[key] = options[key]
break
}
}
return new CEV0Component(opts)
}
module.exports = hyperappCustomElement