6 function GetFilename(url)
9 return url.split('/').pop().split('#')[0].split('?')[0];
12 function EscapeStr(str) {
13 res = str.replace(/'/g,"\\'");
20 e = d.documentElement,
21 g = d.getElementsByTagName('body')[0],
22 body_h = g.clientHeight,
23 window_h = w.innerHeight|| e.clientHeight|| g.clientHeight,
24 items = d.getElementById('items'),
25 current_h = items.clientHeight,
26 new_h=(window_h-body_h)+current_h;
27 if (new_h>minScrollHeight) {
28 items.style.height=new_h+"px";
32 function toHHMMSS(seconds) {
33 var hours = Math.floor(seconds / 3600);
34 seconds -= hours*3600;
35 var minutes = Math.floor(seconds / 60);
36 seconds -= minutes*60;
38 if (hours < 10) {hours = "0"+hours;}
39 if (minutes < 10) {minutes = "0"+minutes;}
40 if (seconds < 10) {seconds = "0"+seconds;}
42 return minutes+':'+seconds;
44 return hours+':'+minutes+':'+seconds;
48 function RefreshTime() {
50 currentTime = toHHMMSS(currentSeconds)
51 trackTime = toHHMMSS(trackSeconds)
52 nowPlayingTime = currentTime+"/"+trackTime
54 nowPlayingTime = "-:--/-:--"
56 document.getElementById('nowplaying_tracklen').innerHTML=nowPlayingTime;
59 function PeriodicRefreshTime() {
60 if (currentState == "play") {
62 delta = (nowTime - updateTime)/1000
63 currentSeconds = updateSeconds + Math.round(delta)
64 if (currentSeconds > trackSeconds) {
65 currentSeconds = trackSeconds
71 function RefreshPageStatus() {
73 var req = new XMLHttpRequest();
75 req.onreadystatechange = function () {
76 if (this.readyState != 4 || this.status != 200) return;
77 var returnedData = JSON.parse(this.responseText);
78 trackName = GetFilename(returnedData['current_playing']);
79 trackNo = returnedData['song'];
80 currentState = returnedData['state'];
81 document.title='MPD Player: '+trackName;
83 var nowPlayingTrackNo=String(1+Number(trackNo))
85 var nowPlayingTrackNo="-"
87 nowPlayingTrack = nowPlayingTrackNo + '/' + returnedData['playlistlength'];
88 nowPlayingName = trackName;
89 playingTime = returnedData['time']
91 var splits = playingTime.split(":")
92 updateTime = Date.now()
93 currentSeconds = Number(splits[0])
94 updateSeconds = currentSeconds
95 trackSeconds = Number(splits[1])
99 if (currentState=='stop') {
100 nowPlayingName = '<font color="gray">' + nowPlayingName+ '</font>'
102 document.getElementById('nowplaying_trackno').innerHTML=nowPlayingTrack;
103 document.getElementById('nowplaying_trackname').innerHTML=nowPlayingName;
105 if (currentState=="play") {
106 document.getElementById('playpausebutton').innerHTML="<span onclick=\"Command('pause')\"><img class=\"button\" title=\"Pause\" src=\"images/pause.svg\"></span>";
108 document.getElementById('playpausebutton').innerHTML="<span onclick=\"Command('play')\"><img class=\"button\" title=\"Play\" src=\"images/play-button.svg\"></span>";
110 if (currentState=="stop") {
111 document.getElementById('stopbutton').innerHTML="<span><img class=\"button\" src=\"images/stopoff.svg\"></span>";
113 document.getElementById('stopbutton').innerHTML="<span onclick=\"Command('stop')\"><img class=\"button\" title=\"Stop\" src=\"images/stop.svg\"></span>";
115 if (returnedData["repeat"]=="1") {
116 document.getElementById('repeatstate').innerHTML="<img class=\"button\" title=\"Replay is on\" src=\"images/replay.svg\"></a>";
118 document.getElementById('repeatstate').innerHTML="<img class=\"button\" title=\"Replay is off\" src=\"images/replayoff.svg\"></a>";
120 document.getElementById('volume_total').innerHTML="<div id=\"volume_actual\" style=\"width:"+returnedData["volume"]+"%\">";
122 var items = document.getElementById('items');
123 var table = items.getElementsByClassName('track');
124 var current_track="track_"+trackNo;
125 for (var i = 0; i < table.length; i++) {
126 if (table[i].id==current_track) {
127 table[i].classList.add("itemActive");
129 table[i].classList.remove("itemActive")
135 req.open("GET", urlbase+"status", true);
140 function RefreshPlaylist() {
142 var req = new XMLHttpRequest();
144 req.onreadystatechange = function () {
145 if (this.readyState != 4 || this.status != 200) return;
146 var returnedData = JSON.parse(this.responseText);
148 var playlistMenuText = "<table>\
150 <td><span onclick=\"EditPlayList()\"><img class=\"medium-button\" title=\"Files\" src=\"images/folder.svg\"></span><td>\
151 <td><span onclick=\"LoadPlayList()\"><img class=\"medium-button\" title=\"Lists\" src=\"images/list.svg\"></span><td>\
152 <td><span onclick=\"SavePlayList()\"><img class=\"medium-button\" title=\"Save current list\" src=\"images/download.svg\"></span><td>\
153 <td><span onclick=\"return confirm('Clear current playlist, are you sure?') ? PlaylistCommand('clear') : false;\" ><img class=\"medium-button\" title=\"Clear current list\" src=\"images/cancel.svg\"></span><td>\
157 var itemsText="<table>\
158 <tr id=\"items_heading\">\
159 <td></td class=\"track_number\"><td class=\"file\">Title</td><td class=\"medium-button\" colspan=\"3\">Controls</td>\
163 for (var key in returnedData) {
164 var rec=returnedData[key];
165 var name=GetFilename(rec["name"]);
176 itemsText = itemsText + "<tr id=\"track_"+id+"\" class=\"track "+evText+"\">\
177 <td class=\"track_number\">\
178 <a name=\""+id+"\">"+(Number(id)+1)+"</a></td>\
181 <span class=\"link\" onclick=\"PlaylistCommand('playitem',"+id+")\">"+name+"</span>\
184 <span onclick=\"PlaylistCommand('moveup',"+id+")\"><img class=\"small-button\" title=\"Move up\" src=\"images/up-arrow.svg\"></span>\
187 <span onclick=\"PlaylistCommand('movedown',"+id+")\"><img class=\"small-button\" title=\"Move down\" src=\"images/down-arrow.svg\"></span>\
189 <td class=\"remove\">\
190 <span onclick=\"PlaylistCommand('remove',"+id+")\"><img class=\"small-button\" title=\"Remove\" src=\"images/cancel.svg\"></span>\
195 itemsText = itemsText + "</table>";
197 document.getElementById('items').innerHTML=itemsText;
198 document.getElementById('playlist_menu_top').innerHTML=playlistMenuText;
199 document.getElementById('playlist_menu_bottom').innerHTML=playlistMenuText;
202 req.open("GET", urlbase+"playlist", true);
207 function EditPlayList(dir) {
209 var req = new XMLHttpRequest();
211 req.onreadystatechange = function () {
212 if (this.readyState != 4 || this.status != 200) return;
213 var returnedData = JSON.parse(this.responseText);
215 var playlistMenuText = "<table>\
217 <td><span onclick=\"RefreshPlaylist()\"><img class=\"medium-button\" title=\"Home\" src=\"images/list.svg\"></span><td>\
218 <td><span onclick=\"return confirm('Add all to the list, are you sure?') ? PlaylistEditCommand('add','"+EscapeStr(dir)+"') : false;\" ><img class=\"medium-button\" title=\"Add all\" src=\"images/plus.svg\"></span><td>\
222 var itemsText= "<table>\
223 <tr id=\"items_heading\">\
224 <td></td class=\"track_number\"><td class=\"file\">Title</td><td colspan=\"2\">Controls</td>\
229 var lastSlash=dir.lastIndexOf("/");
231 var upperLevel=dir.slice(0,lastSlash);
236 var itemsText = itemsText + "<tr class=\"itemOdd\">\
237 <td class=\"track_number\"></td>\
238 <td class=\"file\"><span class=\"link\" onclick=\"EditPlayList('"+upperLevel+"')\">..</span></td>\
243 for (var key in returnedData) {
244 var rec=returnedData[key];
245 var type=rec["type"];
246 var name=rec["name"];
247 var lastSlash=name.lastIndexOf("/");
249 var tailName=name.slice(lastSlash+1);
254 if (type == "directory" || type == "file") {
264 itemsText = itemsText + "<tr class=\""+evText+"\">\
265 <td class=\"track_number\">\
266 <a name=\""+i+"\"></a></td>\
269 if (type == "directory") {
270 itemsText = itemsText + "<td class=\"file\">\
271 <span class=\"link\" onclick=\"EditPlayList('"+EscapeStr(name)+"')\">"+tailName+"</span></td><td>\
272 <span onclick=\"PlaylistEditCommand('add','"+EscapeStr(name)+"')\"><img class=\"small-button\" title=\"Add\" src=\"images/plus.svg\"></span></td>";
275 if (type == "file") {
276 itemsText = itemsText + "<td class=\"file\">\
277 <span class=\"link\" onclick=\"PlaylistEditCommand('add','"+EscapeStr(name)+"')\">"+tailName+"</span></td><td>\
278 <span onclick=\"PlaylistEditCommand('add','"+EscapeStr(name)+"')\"><img class=\"small-button\" title=\"Add\" src=\"images/plus.svg\"></span></td>";
281 itemsText = itemsText + "</tr>";
287 var itemsText = itemsText+"</table>";
288 document.getElementById('items').innerHTML=itemsText;
289 document.getElementById('playlist_menu_top').innerHTML=playlistMenuText;
290 document.getElementById('playlist_menu_bottom').innerHTML=playlistMenuText;
293 if (!dir) { dir = ''; };
295 req.open("GET", urlbase+"lists|edit|"+dir, true);
300 function LoadPlayList() {
302 var req = new XMLHttpRequest();
304 req.onreadystatechange = function () {
305 if (this.readyState != 4 || this.status != 200) return;
306 var returnedData = JSON.parse(this.responseText);
307 playlistMenuText="<table>\
309 <td><span onclick=\"RefreshPlaylist()\"><img class=\"medium-button\" title=\"Home\" src=\"images/list.svg\"></span><td>\
310 <td><span onclick=\"confirm('Clear current playlist, are you sure?') ? PlaylistCommandRefStatus('clear') : false;\" ><img class=\"medium-button\" title=\"Clear all\" src=\"images/cancel.svg\"></span><td>\
314 <tr id=\"items_heading\">\
315 <td class=\"track_number\"></td><td class=\"file\">Name</td><td>Controls</td>\
319 for (var key in returnedData) {
320 var name=returnedData[key];
330 itemsText = itemsText + "<tr class=\""+evText+"\">\
331 <td class=\"track_number\"><a name=\"0\"></a></td>\
332 <td class=\"file\"><span class=\"link\" onclick=\"PlaylistEditCommandRefFull('load','"+EscapeStr(name)+"')\">"+name+"</td>\
333 <td class=\"controls\"><span onclick=\"confirm('Delete playlist "+name+", are you sure?') ? DelPlayList('"+EscapeStr(name)+"') : false;\"><img class=\"small-button\" title=\"Delete\" src=\"images/minus.svg\"></span></td>\
337 itemsText=itemsText+"</table>";
338 document.getElementById('items').innerHTML=itemsText;
339 document.getElementById('playlist_menu_top').innerHTML=playlistMenuText;
340 document.getElementById('playlist_menu_bottom').innerHTML=playlistMenuText;
343 req.open("GET", urlbase+"lists|load", true);
348 function SavePlayList() {
350 var name=window.prompt('List name','');
352 var req = new XMLHttpRequest();
354 req.onreadystatechange = function () {
355 if (this.readyState != 4 || this.status != 200) return;
356 if (this.responseText != 'OK') {
357 window.alert(this.responseText);
361 req.open("GET", urlbase+"lists|save|"+name, true);
366 function DelPlayList(item) {
368 var req = new XMLHttpRequest();
370 req.onreadystatechange = function () {
371 if (this.readyState != 4 || this.status != 200) return;
376 req.open("GET", urlbase+"lists|delete|"+item, true);
381 function RefreshPageContent() {
386 function Command(cmd) {
388 var req = new XMLHttpRequest();
390 req.onreadystatechange = function () {
391 if (this.readyState != 4 || this.status != 200) return;
395 req.open("GET", urlbase+cmd, true);
400 function PlaylistCommand(cmd,item) {
402 var req = new XMLHttpRequest();
404 req.onreadystatechange = function () {
405 if (this.readyState != 4 || this.status != 200) return;
406 RefreshPageContent();
409 req.open("GET", urlbase+"cpl|"+cmd+"|"+item, true);
414 function PlaylistCommandRefStatus(cmd,item) {
416 var req = new XMLHttpRequest();
418 req.onreadystatechange = function () {
419 if (this.readyState != 4 || this.status != 200) return;
423 req.open("GET", urlbase+"cpl|"+cmd+"|"+item, true);
428 function PlaylistEditCommand(cmd,item) {
430 var req = new XMLHttpRequest();
432 req.onreadystatechange = function () {
433 if (this.readyState != 4 || this.status != 200) return;
437 req.open("GET", urlbase+"lists|"+cmd+"|"+item, true);
442 function PlaylistEditCommandRefFull(cmd,item) {
444 var req = new XMLHttpRequest();
446 req.onreadystatechange = function () {
447 if (this.readyState != 4 || this.status != 200) return;
448 RefreshPageContent();
451 req.open("GET", urlbase+"lists|"+cmd+"|"+item, true);
456 function subscribe_status() {
457 var xhr = new XMLHttpRequest();
459 xhr.onreadystatechange = function() {
460 if (this.readyState != 4) return;
461 if (this.status == 200) {
463 setTimeout(subscribe_status,1000)
465 setTimeout(subscribe_status,15000)
468 xhr.open("GET", urlbase+"idle", true);
472 setTimeout(subscribe_status,5000)
473 setInterval(PeriodicRefreshTime, 1000);