Slide 24
Slide 24 text
class BlogStar extends HTMLElement {
static get observedAttributes() { return ['counter']; }
constructor() {
super();
this._counter = 0;
this.attachShadow({mode: 'open'});
}
connectedCallback() {
if (!this._readied)
this.render();
}
display() {
this.output.innerHTML = '★'.repeat(this.counter);
}
get counter() { return this._counter; }
set counter(value) {
if (value != this._counter) {
this._counter = parseInt(value);
this.setAttribute('counter', value);
}
if (this._readied)
this.display();
}
attributeChangedCallback(attr, oldValue, newValue) {
if (oldValue !== newValue) {
this[attr] = newValue;
}
}
render() {
this._readied = true;
var button = document.createElement('star-selector');
button.addEventListener('click', this.increment.bind(this));
this.shadowRoot.appendChild(button);
this.output = document.createElement('span');
this.shadowRoot.appendChild(this.output);
// Some styles for pretty.
this.style.display = 'block';
this.style.fontSize = '30px';
this.output.style.marginLeft = '10px';
}
}
customElements.define('blog-star', MyHTMLElement);