-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7044043
commit f962dfc
Showing
2 changed files
with
24 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,53 @@ | ||
/* eslint no-undef: 0 */ | ||
export class WCInclude extends HTMLElement { | ||
static get observedAttributes () { | ||
return ['src']; | ||
return ['src'] | ||
} | ||
|
||
attributeChangedCallback (name, oldValue, newValue) { | ||
if (!this.__initialized) { return; } | ||
if (!this.__initialized) { return } | ||
if (oldValue !== newValue) { | ||
this[name] = newValue; | ||
this[name] = newValue | ||
} | ||
} | ||
|
||
get src () { return this.getAttribute('src'); } | ||
get src () { return this.getAttribute('src') } | ||
set src (value) { | ||
this.setAttribute('src', value); | ||
this.setSrc(); | ||
this.setAttribute('src', value) | ||
this.setSrc() | ||
} | ||
|
||
constructor () { | ||
super(); | ||
this.__initialized = false; | ||
this.__element = null; | ||
const shadow = this.hasAttribute('shadow'); | ||
super() | ||
this.__initialized = false | ||
this.__element = null | ||
const shadow = this.hasAttribute('shadow') | ||
if (shadow) { | ||
this.attachShadow({ mode: 'open' }); | ||
this.attachShadow({ mode: 'open' }) | ||
} | ||
|
||
this.__element = shadow | ||
? this.shadowRoot | ||
: this; | ||
: this | ||
} | ||
|
||
async connectedCallback () { | ||
if (this.hasAttribute('src')) { | ||
this.setSrc(); | ||
this.setSrc() | ||
} | ||
this.__initialized = true; | ||
this.__initialized = true | ||
} | ||
|
||
async setSrc () { | ||
const src = this.getAttribute('src'); | ||
const contents = await this.fetchSrc(src); | ||
this.__element.innerHTML = contents; | ||
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(); | ||
const response = await fetch(src) | ||
return response.text() | ||
} | ||
} | ||
|
||
customElements.define('wc-include', WCInclude); | ||
customElements.define('wc-include', WCInclude) |