Last active
September 25, 2019 08:30
-
-
Save IrisCZ/4e9fdc8518bfd9cccd4d2a917911f1c5 to your computer and use it in GitHub Desktop.
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
class Display extends HTMLElement { | |
constructor() { | |
super(); | |
const shadowRoot = this.attachShadow({ mode: 'open' }); | |
shadowRoot.innerHTML = ` | |
<div id="display" class="display">${this.setSelected()}</div> | |
`; | |
this.display = this.shadowRoot.querySelector('#display'); | |
} | |
static get observedAttributes() { | |
return ['selected']; | |
} | |
attributeChangedCallback() { | |
this.display.innerHTML = this.setSelected(); | |
} | |
setSelected() { | |
const key = this.getAttribute('selected'); | |
const dataLength = data.options[key] ? data.options[key].length : 0; | |
const randomNumber = Math.floor(Math.random() * Math.floor(dataLength)); | |
const selected = data.options[key] ? data.options[key][randomNumber] : ''; | |
return selected ? selected : 'Choose an author'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment