-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.mjs
100 lines (87 loc) · 2.54 KB
/
index.mjs
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
import MorphdomMixin from '@enhance/morphdom-mixin'
import TemplateMixin from '@enhance/template-mixin'
import ShadowElementMixin from '@enhance/shadow-element-mixin'
import CustomElementMixin from '@enhance/custom-element-mixin'
import BaseElement from '@enhance/base-element'
import EventHandlerMixin from '@enhance/event-handler-mixin'
export default function enhance(tagName, opts) {
const shadow = opts.shadow
const _observedAttributes = opts.observedAttributes ||
opts.attrs
delete opts.observedAttributes
delete opts.attrs
const _formAssociated = opts.formAssociated
delete opts.formAssociated
const _adoptedCallback = opts.adoptedCallback ||
opts.adopted
delete opts.adoptedCallback
delete opts.adopted
const _connectedCallback = opts.connectedCallback ||
opts.connected
delete opts.connectedCallback
delete opts.connected
const _disconnectedCallback = opts.disconnectedCallback ||
opts.disconnected
delete opts.disconnectedCallback
delete opts.disconnected
class Base extends BaseElement {
constructor() {
super()
Object.keys(opts)
.map(k => Object.defineProperty(
this,
k,
{
value: opts[k],
writable: true
})
)
}
static get observedAttributes() {
return _observedAttributes
}
static get formAssociated() {
return _formAssociated
}
adoptedCallback() {
if (typeof _adoptedCallback === 'function') {
_adoptedCallback.call(this, this)
}
}
connectedCallback() {
if (this.isConnected && typeof _connectedCallback === 'function') {
_connectedCallback.call(this, this)
}
}
disconnectedCallback() {
if (this.api) {
this.api.unsubscribe(this.process)
}
if (typeof _disconnectedCallback === 'function') {
_disconnectedCallback.call(this, this)
}
}
}
const elementMixin = (shadow === 'open' || shadow === 'closed')
? ShadowElementMixin
: CustomElementMixin
class EnhanceElement extends MorphdomMixin(EventHandlerMixin(elementMixin(TemplateMixin(Base)))) {
constructor() {
super({ mode: shadow })
if (this.api && this.keys) {
this.api.subscribe(this.process, this.keys)
this.store = this.api.store
}
else if (this.store && this.keys) {
this.store.subscribe(this.process, this.keys)
}
this.init && this.init(this)
}
}
if (!customElements.get(tagName)) {
customElements.define(
tagName,
EnhanceElement
)
}
}