-
Notifications
You must be signed in to change notification settings - Fork 0
/
UiMiniPlayerCtrl.js
88 lines (75 loc) · 2.69 KB
/
UiMiniPlayerCtrl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
export class UiMiniPlayerCtrl {
constructor(spotify) {
this.spotify = spotify;
}
notifyUILoaded() {
this._installButtonCbs();
this.onTick();
}
onTick() {
this.updateAvailableDevices();
this.updatePlayingNow();
}
updateAvailableDevices = () => {
this.spotify.getAvailableDevices().then(devs => {
$('#playctrls_device').html('');
let device_active = false;
$.each(devs, (_, dev) => {
let selected = "";
if (dev.is_active) {
$('#playctrls_vol').val(dev.volume_percent);
selected = "selected";
device_active = true;
}
$('#playctrls_device').append(`<option value="${dev.id}" ${selected}>${dev.name}</option>`);
});
if (!device_active) {
$('#playctrls_device').append(`<option selected>NO DEVICE SELECTED</option>`);
}
});
}
updatePlayingNow = () => {
this.spotify.getPlayingNow().then(playingNow => {
const playCtrls = document.getElementById('playctrls');
if (!playingNow) {
playCtrls.classList.remove('somethingPlaying');
playCtrls.classList.add('nothingPlaying');
} else {
const shuffle_active = playingNow?.full_response?.shuffle_state;
$('#playctrls_shuffle_enabled').prop('checked', shuffle_active);
playCtrls.classList.add('somethingPlaying');
playCtrls.classList.remove('nothingPlaying');
$("#playingNow_StatusImg").attr("src", playingNow.album_img);
$("#playingNow_statusLine1").html(playingNow.songName);
$("#playingNow_statusLine2").html(`<a href="${playingNow.album_uri}">${playingNow.album}</a>| ` +
`<a href="${playingNow.album_uri}">${playingNow.artist}</a>`);
}
});
}
_installButtonCbs() {
const sp = this.spotify;
$('#playctrls_device').change(() => {
$('#playctrls_device option:selected').each((idx, opt) => {
const dev_id = opt.value;
const dev_name = opt.text;
sp.setActiveDevice(opt.value).then(_ => {
console.log("Selected new device", dev_name);
});
});
});
$('#playctrls_vol').change(_ => {
console.log("Set vol", $('#playctrls_vol').val());
sp.setVolume($('#playctrls_vol').val());
});
$('#playctrls_shuffle_enabled').change(_ => {
if ($('#playctrls_shuffle_enabled').is(":checked")) {
sp.setShuffleEnabled();
} else {
sp.setShuffleDisabled();
}
});
$('#playctrls_prev').click(_ => { sp.playPrev().then(this.updatePlayingNow); });
$('#playctrls_play').click(_ => { sp.playPause(); });
$('#playctrls_next').click(_ => { sp.playNext().then(this.updatePlayingNow); });
}
}