forked from vanillawc/wc-include
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (50 loc) · 1.12 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
// src/wc-include.js
var WCInclude = class extends HTMLElement {
static get observedAttributes() {
return ["src"];
}
attributeChangedCallback(name, oldValue, newValue) {
if (!this.__initialized) {
return;
}
if (oldValue !== newValue) {
this[name] = newValue;
}
}
get src() {
return this.getAttribute("src");
}
set src(value) {
this.setAttribute("src", value);
this.setSrc();
}
constructor() {
super();
this.__initialized = false;
this.__element = null;
const shadow = this.hasAttribute("shadow");
if (shadow) {
this.attachShadow({mode: "open"});
}
this.__element = shadow ? this.shadowRoot : this;
}
async connectedCallback() {
if (this.hasAttribute("src")) {
this.setSrc();
}
this.__initialized = true;
}
async setSrc() {
const src = this.getAttribute("src");
const contents = await this.fetchSrc(src);
this.__element.innerHTML = contents;
}
async fetchSrc(src) {
const response = await fetch(src);
return response.text();
}
};
customElements.define("wc-include", WCInclude);
export {
WCInclude
};