(function(window) { 'use strict'; const HIDE_OVERLAY_TIMEOUT = 5000; const REPEAT_VALUES = ['off', 'list', 'song']; const SHUFFLE_VALUES = ['off', 'on']; var proto = Object.create(HTMLElement.prototype); proto.createdCallback = function() { var shadowRoot = this.createShadowRoot(); shadowRoot.innerHTML = `

`; var $ = shadowRoot.querySelector.bind(shadowRoot); this.els = { container: $('#container'), backImage: $('img[data-layer="back"]'), frontImage: $('img[data-layer="front"]'), caption: $('#caption'), controls: $('#controls'), artist: $('#artist'), album: $('#album'), share: $('[data-action="share"]'), repeat: $('[data-action="repeat"]'), shuffle: $('[data-action="shuffle"]'), rating: $('#rating') }; var onImageLoad = (evt) => { const image = evt.target; window.requestAnimationFrame(() => { var newActiveImage = image.closest('img:not(.active)'); if (!newActiveImage) { return; } var oldActiveImage = this.els.container.querySelector('img.active'); newActiveImage.classList.add('active'); oldActiveImage.classList.remove('active'); }); }; [].forEach.call(this.els.container.querySelectorAll('img'), (img) => { img.addEventListener('load', onImageLoad); }); this.els.container.addEventListener('transitionend', (evt) => { if (!evt.target.matches('img:not(.active)')) { return; } var oldActiveImage = evt.target; oldActiveImage.src = null; }); this.els.container.addEventListener('click', (evt) => { var button = evt.target.closest('button'); if (!button) { if (evt.target.closest('#rating')) { this.overlayVisible = true; return; } this.overlayVisible = !this.overlayVisible; return; } var action = button.dataset.action; switch (action) { case 'repeat': this.nextRepeat(); break; case 'shuffle': this.nextShuffle(); break; } this.overlayVisible = true; this.dispatchEvent(new CustomEvent(action)); }); this.els.rating.addEventListener('change', (evt) => { this.dispatchEvent(new CustomEvent('ratingchange', { detail: evt.detail })); }); this.onDOMRetranslated = () => { document.l10n.translateFragment(shadowRoot); }; this.repeat = this.getAttribute('repeat'); this.shuffle = this.getAttribute('shuffle'); this.overlayVisible = true; }; 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 'artist': this.els.artist.textContent = newVal; break; case 'album': this.els.album.textContent = newVal; break; case 'repeat': this.els.repeat.dataset.value = REPEAT_VALUES.indexOf(newVal) !== -1 ? newVal : REPEAT_VALUES[0]; break; case 'shuffle': this.els.shuffle.dataset.value = SHUFFLE_VALUES.indexOf(newVal) !== -1 ? newVal : SHUFFLE_VALUES[0]; break; case 'src': this.els.container.querySelector('img:not(.active)').src = newVal; break; } }; proto.nextRepeat = function() { this.repeat = REPEAT_VALUES[REPEAT_VALUES.indexOf(this.repeat) + 1]; }; proto.nextShuffle = function() { this.shuffle = SHUFFLE_VALUES[SHUFFLE_VALUES.indexOf(this.shuffle) + 1]; }; ['artist', 'album', 'src'].forEach(function(prop) { Object.defineProperty(proto, prop, { get: function() { return this.getAttribute(prop); }, set: function(value) { this.setAttribute(prop, value || ''); } }); }); Object.defineProperty(proto, 'repeat', { get: function() { return this.getAttribute('repeat') || REPEAT_VALUES[0]; }, set: function(value) { value = REPEAT_VALUES.indexOf(value) !== -1 ? value : REPEAT_VALUES[0]; this.setAttribute('repeat', value); this.els.repeat.dataset.l10nId = 'repeat-' + value; this.onDOMRetranslated(); } }); Object.defineProperty(proto, 'shuffle', { get: function() { return this.getAttribute('shuffle') || SHUFFLE_VALUES[0]; }, set: function(value) { value = SHUFFLE_VALUES.indexOf(value) !== -1 ? value : SHUFFLE_VALUES[0]; this.setAttribute('shuffle', value); } }); Object.defineProperty(proto, 'overlayVisible', { get: function() { return this.els.container.classList.contains('show-overlay'); }, set: function(value) { var overlayVisible = !!value; clearTimeout(this._hideOverlayTimeout); if (overlayVisible) { this.els.container.classList.add('show-overlay'); this._hideOverlayTimeout = setTimeout(() => { this.overlayVisible = false; }, HIDE_OVERLAY_TIMEOUT); } else { this.els.container.classList.remove('show-overlay'); } } }); try { window.MusicArtwork = document.registerElement('music-artwork', { prototype: proto }); } catch (e) { if (e.name !== 'NotSupportedError') { throw e; } } })(window);