4 function GetFilename(url)
7 return url.split('/').pop().split('#')[0].split('?')[0];
10 function EscapeStr(str) {
11 res = str.replace(/'/g,"\\'");
18 e = d.documentElement,
19 g = d.getElementsByTagName('body')[0],
20 body_h = g.clientHeight,
21 window_h = w.innerHeight|| e.clientHeight|| g.clientHeight,
22 items = d.getElementById('items'),
23 current_h = items.clientHeight,
24 new_h=(window_h-body_h)+current_h;
25 if (new_h>minScrollHeight) {
26 items.style.height=new_h+"px";
30 function RefreshPageStatus() {
32 var req = new XMLHttpRequest();
34 req.onreadystatechange = function () {
35 if (this.readyState != 4 || this.status != 200) return;
36 var returnedData = JSON.parse(this.responseText);
37 trackName = GetFilename(returnedData['current_playing']);
38 trackNo = returnedData['song'];
39 document.title='MPD Player: '+trackName;
40 nowPlaying = (1+Number(trackNo)) + '/' + returnedData['playlistlength'] + ' '+trackName;
41 if (returnedData['state']=='stop') {
42 nowPlaying = '<font color="gray">' + nowPlaying+ '</font>'
44 document.getElementById('nowplaying_content').innerHTML=nowPlaying;
45 if (returnedData["state"]=="play") {
46 document.getElementById('playpausebutton').innerHTML="<span onclick=\"Command('pause')\"><img class=\"button\" src=\"images/pause.png\"></span>";
48 document.getElementById('playpausebutton').innerHTML="<span onclick=\"Command('play')\"><img class=\"button\" src=\"images/play.png\"></span>";
50 if (returnedData["repeat"]=="1") {
51 document.getElementById('repeatstate').innerHTML="<img src=\"images/repeaton.png\"></a>";
53 document.getElementById('repeatstate').innerHTML="<img src=\"images/repeatoff.png\"></a>";
55 document.getElementById('volume_total').innerHTML="<div id=\"volume_actual\" style=\"width:"+returnedData["volume"]+"%\">";
57 var items = document.getElementById('items');
58 var table = items.getElementsByClassName('track');
59 var current_track="track_"+trackNo;
60 for (var i = 0; i < table.length; i++) {
61 console.log(table[i].id);
62 if (table[i].id==current_track) {
63 table[i].classList.add("itemActive");
65 table[i].classList.remove("itemActive")
71 req.open("GET", urlbase+"status", true);
76 function RefreshPlaylist() {
78 var req = new XMLHttpRequest();
80 req.onreadystatechange = function () {
81 if (this.readyState != 4 || this.status != 200) return;
82 var returnedData = JSON.parse(this.responseText);
84 var playlistMenuText = "<table>\
86 <td><span class=\"button\" onclick=\"EditPlayList()\"><img width=\"20\" src=\"images/songs.png\"></span><td>\
87 <td><span class=\"button\" onclick=\"LoadPlayList()\"><img width=\"20\" src=\"images/lists.png\"></span><td>\
88 <td><span class=\"button\" onclick=\"SavePlayList()\"><img width=\"20\" src=\"images/save.png\"></span><td>\
89 <td><span class=\"button\" onclick=\"return confirm('Clear current playlist, are you sure?') ? PlaylistCommand('clear') : false;\" ><img width=\"20\" src=\"images/removeall.png\"></span><td>\
93 var itemsText="<table>\
94 <tr id=\"items_heading\">\
95 <td></td class=\"track_number\"><td class=\"file\">Title</td><td class=\"button\" colspan=\"3\">Controls</td>\
99 for (var key in returnedData) {
100 var rec=returnedData[key];
101 var name=GetFilename(rec["name"]);
112 itemsText = itemsText + "<tr id=\"track_"+id+"\" class=\"track "+evText+"\">\
113 <td class=\"track_number\">\
114 <a name=\""+id+"\">"+(Number(id)+1)+"</a></td>\
117 <span class=\"button\" onclick=\"PlaylistCommand('playitem',"+id+")\">"+name+"</span>\
120 <span class=\"button\" onclick=\"PlaylistCommand('moveup',"+id+")\"><img width=\"15\" src=\"images/up.png\"></span>\
123 <span class=\"button\" onclick=\"PlaylistCommand('movedown',"+id+")\"><img width=\"15\" src=\"images/down.png\"></span>\
125 <td class=\"remove\">\
126 <span class=\"button\" onclick=\"PlaylistCommand('remove',"+id+")\"><img width=\"15\" src=\"images/remove.png\"></span>\
131 itemsText = itemsText + "</table>";
133 document.getElementById('items').innerHTML=itemsText;
134 document.getElementById('playlist_menu_top').innerHTML=playlistMenuText;
135 document.getElementById('playlist_menu_bottom').innerHTML=playlistMenuText;
138 req.open("GET", urlbase+"playlist", true);
143 function EditPlayList(dir) {
145 var req = new XMLHttpRequest();
147 req.onreadystatechange = function () {
148 if (this.readyState != 4 || this.status != 200) return;
149 var returnedData = JSON.parse(this.responseText);
151 var playlistMenuText = "<table>\
153 <td><span class=\"button\" onclick=\"RefreshPlaylist()\"><img width=\"20\" src=\"images/playlist.png\"></span><td>\
154 <td><span class=\"button\" onclick=\"return confirm('Add all to the list, are you sure?') ? PlaylistEditCommand('add','"+EscapeStr(dir)+"') : false;\" ><img width=\"20\" src=\"images/addall.png\"></span><td>\
158 var itemsText= "<table>\
159 <tr id=\"items_heading\">\
160 <td></td class=\"track_number\"><td class=\"file\">Title</td><td class=\"button\" colspan=\"2\">Controls</td>\
165 var lastSlash=dir.lastIndexOf("/");
167 var upperLevel=dir.slice(0,lastSlash);
172 var itemsText = itemsText + "<tr class=\"itemOdd\">\
173 <td class=\"track_number\"></td>\
174 <td class=\"file\"><span class=\"button\" onclick=\"EditPlayList('"+upperLevel+"')\">..</span></td>\
179 for (var key in returnedData) {
180 var rec=returnedData[key];
181 var type=rec["type"];
182 var name=rec["name"];
183 var lastSlash=name.lastIndexOf("/");
185 var tailName=name.slice(lastSlash+1);
190 if (type == "directory" || type == "file") {
200 itemsText = itemsText + "<tr class=\""+evText+"\">\
201 <td class=\"track_number\">\
202 <a name=\""+i+"\"></a></td>\
205 if (type == "directory") {
206 itemsText = itemsText + "<td class=\"file\">\
207 <span class=\"button\" onclick=\"EditPlayList('"+EscapeStr(name)+"')\">"+tailName+"</span></td><td>\
208 <span class=\"button\" onclick=\"PlaylistEditCommand('add','"+EscapeStr(name)+"')\"><img width=\"15\" src=\"images/plus.png\"></span></td>";
211 if (type == "file") {
212 itemsText = itemsText + "<td class=\"file\">\
213 <span class=\"button\" onclick=\"PlaylistEditCommand('add','"+EscapeStr(name)+"')\">"+tailName+"</span></td><td>\
214 <span class=\"button\" onclick=\"PlaylistEditCommand('add','"+EscapeStr(name)+"')\"><img width=\"15\" src=\"images/plus.png\"></span></td>";
217 itemsText = itemsText + "</tr>";
223 var itemsText = itemsText+"</table>";
224 document.getElementById('items').innerHTML=itemsText;
225 document.getElementById('playlist_menu_top').innerHTML=playlistMenuText;
226 document.getElementById('playlist_menu_bottom').innerHTML=playlistMenuText;
229 if (!dir) { dir = ''; };
231 req.open("GET", urlbase+"lists|edit|"+dir, true);
236 function LoadPlayList() {
238 var req = new XMLHttpRequest();
240 req.onreadystatechange = function () {
241 if (this.readyState != 4 || this.status != 200) return;
242 var returnedData = JSON.parse(this.responseText);
243 playlistMenuText="<table>\
245 <td><span class=\"button\" onclick=\"RefreshPlaylist()\"><img width=\"20\" src=\"images/playlist.png\"></span><td>\
246 <td><span class=\"button\" onclick=\"confirm('Clear current playlist, are you sure?') ? PlaylistCommandRefStatus('clear') : false;\" ><img width=\"20\" src=\"images/removeall.png\"></span><td>\
250 <tr id=\"items_heading\">\
251 <td class=\"track_number\"></td><td class=\"file\">Name</td><td class=\"button\">Controls</td>\
255 for (var key in returnedData) {
256 var name=returnedData[key];
266 itemsText = itemsText + "<tr class=\""+evText+"\">\
267 <td class=\"track_number\"><a name=\"0\"></a></td>\
268 <td class=\"file\"><span class=\"button\" onclick=\"PlaylistEditCommandRefFull('load','"+EscapeStr(name)+"')\">"+name+"</td>\
269 <td class=\"controls\"><span class=\"button\" onclick=\"confirm('Delete playlist "+name+", are you sure?') ? DelPlayList('"+EscapeStr(name)+"') : false;\"><img width=\"20\" src=\"images/minus.png\"></span></td>\
273 itemsText=itemsText+"</table>";
274 document.getElementById('items').innerHTML=itemsText;
275 document.getElementById('playlist_menu_top').innerHTML=playlistMenuText;
276 document.getElementById('playlist_menu_bottom').innerHTML=playlistMenuText;
279 req.open("GET", urlbase+"lists|load", true);
284 function SavePlayList() {
286 var name=window.prompt('List name','');
288 var req = new XMLHttpRequest();
290 req.onreadystatechange = function () {
291 if (this.readyState != 4 || this.status != 200) return;
292 if (this.responseText != 'OK') {
293 window.alert(this.responseText);
297 req.open("GET", urlbase+"lists|save|"+name, true);
302 function DelPlayList(item) {
304 var req = new XMLHttpRequest();
306 req.onreadystatechange = function () {
307 if (this.readyState != 4 || this.status != 200) return;
312 req.open("GET", urlbase+"lists|delete|"+item, true);
317 function RefreshPageContent() {
322 function Command(cmd) {
324 var req = new XMLHttpRequest();
326 req.onreadystatechange = function () {
327 if (this.readyState != 4 || this.status != 200) return;
331 req.open("GET", urlbase+cmd, true);
336 function PlaylistCommand(cmd,item) {
338 var req = new XMLHttpRequest();
340 req.onreadystatechange = function () {
341 if (this.readyState != 4 || this.status != 200) return;
342 RefreshPageContent();
345 req.open("GET", urlbase+"cpl|"+cmd+"|"+item, true);
350 function PlaylistCommandRefStatus(cmd,item) {
352 var req = new XMLHttpRequest();
354 req.onreadystatechange = function () {
355 if (this.readyState != 4 || this.status != 200) return;
359 req.open("GET", urlbase+"cpl|"+cmd+"|"+item, true);
364 function PlaylistEditCommand(cmd,item) {
366 var req = new XMLHttpRequest();
368 req.onreadystatechange = function () {
369 if (this.readyState != 4 || this.status != 200) return;
373 req.open("GET", urlbase+"lists|"+cmd+"|"+item, true);
378 function PlaylistEditCommandRefFull(cmd,item) {
380 var req = new XMLHttpRequest();
382 req.onreadystatechange = function () {
383 if (this.readyState != 4 || this.status != 200) return;
384 RefreshPageContent();
387 req.open("GET", urlbase+"lists|"+cmd+"|"+item, true);
392 function subscribe_status() {
393 var xhr = new XMLHttpRequest();
395 xhr.onreadystatechange = function() {
396 if (this.readyState != 4) return;
397 if (this.status == 200) {
399 setTimeout(subscribe_status,1000)
401 setTimeout(subscribe_status,15000)
404 xhr.open("GET", urlbase+"idle", true);
408 setTimeout(subscribe_status,5000)