2017-12-04 16:14:46 +01:00
|
|
|
import { withComponent, props } from 'skatejs';
|
|
|
|
|
|
|
|
function renderTime(duration) {
|
2017-12-04 16:35:06 +01:00
|
|
|
const time = parseInt(duration, 10);
|
|
|
|
let seconds = parseInt((time) % 60, 10);
|
|
|
|
let minutes = parseInt((time / (60)) % 60, 10);
|
|
|
|
let hours = parseInt(time / (60 * 60), 10);
|
2017-12-04 16:14:46 +01:00
|
|
|
|
2017-12-04 16:35:06 +01:00
|
|
|
hours = (hours < 10) ? `0${hours}` : hours;
|
|
|
|
minutes = (minutes < 10) ? `0${minutes}` : minutes;
|
|
|
|
seconds = (seconds < 10) ? `0${seconds}` : seconds;
|
2017-12-04 16:14:46 +01:00
|
|
|
|
2017-12-04 16:35:06 +01:00
|
|
|
return `${hours}:${minutes}:${seconds}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderProgress(elapsed, total) {
|
|
|
|
if (!parseInt(total, 10)) {
|
|
|
|
return '<progress></progress>';
|
|
|
|
}
|
|
|
|
return `<progress min="0" max="${total}" value="${elapsed}"></progress>`;
|
2017-12-04 16:14:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const Component = withComponent();
|
|
|
|
class MPD extends Component {
|
|
|
|
static props = {
|
|
|
|
mpd: props.string,
|
|
|
|
data: props.object,
|
|
|
|
interval: props.number,
|
|
|
|
};
|
|
|
|
|
|
|
|
connected() {
|
|
|
|
if (!this.mpd) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.fetchData();
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnecting() {
|
2018-02-07 15:59:35 +01:00
|
|
|
if (this.timeout) {
|
|
|
|
window.clearTimeout(this.timeout);
|
|
|
|
delete this.timeout;
|
2017-12-04 16:14:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchData() {
|
2018-02-07 15:59:35 +01:00
|
|
|
delete this.timeout;
|
2017-12-04 16:35:06 +01:00
|
|
|
const url = `https://c-beam.cbrp3.c-base.org/mpd/${this.mpd}/status/?_=${Date.now()}`;
|
2017-12-04 16:14:46 +01:00
|
|
|
fetch(url, {
|
|
|
|
headers: {
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
2017-12-04 16:35:06 +01:00
|
|
|
},
|
2017-12-04 16:14:46 +01:00
|
|
|
})
|
|
|
|
.then(data => data.json())
|
|
|
|
.then((data) => {
|
|
|
|
this.data = data.content;
|
2018-02-07 15:59:35 +01:00
|
|
|
if (!this.interval) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.timeout = window.setTimeout(() => {
|
|
|
|
this.fetchData();
|
|
|
|
}, this.interval * 1000);
|
2017-12-04 16:14:46 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
renderer(renderRoot, render) {
|
|
|
|
const root = renderRoot;
|
|
|
|
while (root.firstChild) {
|
|
|
|
root.removeChild(root.firstChild);
|
|
|
|
}
|
|
|
|
root.appendChild(render());
|
2017-12-04 17:38:46 +01:00
|
|
|
const styles = document.createElement('style');
|
|
|
|
styles.appendChild(document.createTextNode(`
|
|
|
|
.artist, .song {
|
|
|
|
color: #fff;
|
|
|
|
}
|
|
|
|
progress {
|
|
|
|
-webkit-appearance: none;
|
|
|
|
appearance: none;
|
|
|
|
width: 40%;
|
|
|
|
}
|
|
|
|
progress::-webkit-progress-bar {
|
|
|
|
background-color: #2e3436;
|
|
|
|
}
|
|
|
|
progress[value]::-webkit-progress-value {
|
|
|
|
background-color: hsl(35, 98%, 46%);
|
|
|
|
}
|
|
|
|
`));
|
|
|
|
root.appendChild(styles);
|
2017-12-04 16:14:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
render({ data }) {
|
|
|
|
const el = document.createElement('div');
|
|
|
|
if (!data.state) {
|
|
|
|
return el;
|
|
|
|
}
|
2017-12-04 16:35:06 +01:00
|
|
|
el.className = data.state;
|
2017-12-04 16:14:46 +01:00
|
|
|
let status = 'Stopped';
|
2017-12-04 16:35:06 +01:00
|
|
|
if (data.state === 'pause') {
|
|
|
|
status = `${renderProgress(data.elapsed, data.total)} paused`;
|
|
|
|
}
|
2017-12-04 16:14:46 +01:00
|
|
|
if (data.state === 'play') {
|
2017-12-04 16:35:06 +01:00
|
|
|
status = `${renderProgress(data.elapsed, data.total)} ${renderTime(data.elapsed)}/${renderTime(data.total)}`;
|
2017-12-04 16:14:46 +01:00
|
|
|
}
|
2017-12-04 16:35:06 +01:00
|
|
|
let artist = data.current_song.artist ? `${data.current_song.artist} - ${data.current_song.album}` : data.current_song.name;
|
2017-12-04 16:14:46 +01:00
|
|
|
if (!artist) {
|
|
|
|
artist = 'Unknown';
|
|
|
|
}
|
2017-12-04 16:35:06 +01:00
|
|
|
let volume = parseInt(data.volume, 10);
|
|
|
|
if (volume < 0) {
|
|
|
|
volume = 0;
|
|
|
|
}
|
2017-12-04 16:14:46 +01:00
|
|
|
el.innerHTML = `
|
2017-12-04 17:38:46 +01:00
|
|
|
<div class="artist">${artist}</div>
|
|
|
|
<div class="song">${data.current_song.title}</div>
|
2017-12-04 16:35:06 +01:00
|
|
|
<div>${status}</div>
|
2017-12-04 17:38:46 +01:00
|
|
|
<div><progress min="0" max="100" value="${volume}"></progress> volume ${volume}</div>
|
2017-12-04 16:14:46 +01:00
|
|
|
`;
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define('cbase-mpd', MPD);
|