(function(window) {
/*jshint maxlen:false*/
'use strict';
var proto = Object.create(HTMLElement.prototype);
proto.createdCallback = function() {
var shadowRoot = this.createShadowRoot();
shadowRoot.innerHTML =
`
`;
var $id = shadowRoot.getElementById.bind(shadowRoot);
this.els = {
container: $id('container')
};
this.els.container.addEventListener('click', (evt) => {
var button = evt.target.closest('button');
if (!button) {
return;
}
var value = parseInt(button.value, 10) || 0;
if (value === this.value) {
value--;
}
this.value = value;
this.dispatchEvent(new CustomEvent('change', {
detail: value
}));
});
this.onDOMRetranslated = () => {
document.l10n.translateFragment(shadowRoot);
};
};
proto.attachedCallback = function() {
document.addEventListener('DOMRetranslated', this.onDOMRetranslated);
this.onDOMRetranslated();
};
proto.detachedCallback = function() {
document.removeEventListener('DOMRetranslated', this.onDOMRetranslated);
};
proto.attributeChangedCallback = function(attr, oldVal, newVal) {
switch (attr) {
case 'value':
this.els.container.dataset.value = newVal;
break;
}
};
Object.defineProperty(proto, 'value', {
get: function() {
return parseInt(this.getAttribute('value'), 10) || 0;
},
set: function(value) {
this.setAttribute('value', clamp(0, 5, parseInt(value, 10) || 0));
}
});
function clamp(min, max, value) {
return Math.min(Math.max(min, value), max);
}
try {
window.MusicRating = document.registerElement('music-rating', {
prototype: proto
});
} catch (e) {
if (e.name !== 'NotSupportedError') {
throw e;
}
}
})(window);