-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixins.js
130 lines (110 loc) · 2.82 KB
/
mixins.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
const templateHelper = base =>
class extends base {
constructor(...args) {
super(...args);
}
static get templateHTML() {
return "";
}
static get template() {
if (this.__templateEl) return this.__templateEl;
this.__templateEl = document.createElement("template");
this.__templateEl.innerHTML = this.templateHTML;
return this.__templateEl;
}
get templateContent() {
if (window.ShadyCSS)
window.ShadyCSS.prepareTemplate(
this.constructor.template,
this.tagName.toLocaleLowerCase()
);
return document.importNode(this.constructor.template.content, true);
}
};
const refs = base =>
class extends base {
constructor(...args) {
super(...args);
this.refs = new Proxy(
{},
{
get: this.__getFromShadowRoot.bind(this)
}
);
}
__getFromShadowRoot(target, name) {
return this.shadowRoot.querySelector('[ref="' + name + '"]');
}
};
const emitEvent = base =>
class extends base {
constructor(...args) {
super(...args);
}
emitEvent(name, detail) {
this.dispatchEvent(new CustomEvent(name, { detail, bubbles: true }));
}
};
const allAttributesChanged = base =>
class extends base {
constructor(...args) {
super(...args);
// Gets populated by attributeChangedCallback
this.__attributesMap = {};
this.__waitingOnAttr = (this.constructor.observedAttributes
? this.constructor.observedAttributes
: []
).filter(name => {
if (!this.attributes.getNamedItem(name)) {
this.__attributesMap[name] = this.constructor.defaultAttributeValue(
name
);
}
return !!this.attributes.getNamedItem(name);
});
// No attributes so update attribute never called.
// So fire this anyway.
if (this.__waitingOnAttr.length === 0) {
this.allAttributesChangedCallback(this.__attributesMap);
}
}
static defaultAttributeValue() {
/* the name of the attribute is parsed in as a parameter */
return;
}
static parseAttributeValue(name, value) {
return value;
}
attributeChangedCallback(attr, oldValue, newValue, ns) {
if (super.attributeChangedCallback) {
super.attributeChangedCallback(attr, oldValue, newValue, ns);
}
this.__attributesMap[attr] = this.constructor.parseAttributeValue.call(
this,
attr,
newValue
);
if (this.__waitingOnAttr.length) {
const index = this.__waitingOnAttr.indexOf(attr);
if (index !== -1) {
// Remove it from array.
this.__waitingOnAttr.splice(index, 1);
}
}
// All attributes parsed
if (this.__waitingOnAttr.length === 0) {
this.allAttributesChangedCallback(this.__attributesMap);
}
}
allAttributesChangedCallback() {}
};
const HTMLElementPlus = refs(
allAttributesChanged(templateHelper(emitEvent(HTMLElement)))
);
export {
allAttributesChanged,
emitEvent,
templateHelper,
refs,
HTMLElementPlus
};