3 function GetFilename(url)
6 return url.split('/').pop().split('#')[0].split('?')[0];
9 function EscapeStr(str) {
10 res = str.replace(/'/g,"\\'");
14 function RefreshPageStatus() {
16 var req = new XMLHttpRequest();
18 req.onreadystatechange = function () {
19 if (this.readyState != 4 || this.status != 200) return;
20 var returnedData = JSON.parse(this.responseText);
21 nowPlaying = GetFilename(returnedData['current_playing'])
22 document.title='MPD Player: '+nowPlaying;
23 nowPlaying = (1+Number(returnedData['song'])) + '/' + returnedData['playlistlength'] + ' '+nowPlaying;
24 if (returnedData['state']=='stop') {
25 nowPlaying = '<font color="gray">' + nowPlaying+ '</font>'
27 document.getElementById('nowplaying_content').innerHTML=nowPlaying;
28 if (returnedData["state"]=="play") {
29 document.getElementById('playpausebutton').innerHTML="<span onclick=\"Command('pause')\"><img class=\"button\" src=\"images/pause.png\"></span>";
31 document.getElementById('playpausebutton').innerHTML="<span onclick=\"Command('play')\"><img class=\"button\" src=\"images/play.png\"></span>";
33 if (returnedData["repeat"]=="1") {
34 document.getElementById('repeatstate').innerHTML="<img src=\"images/repeaton.png\"></a>";
36 document.getElementById('repeatstate').innerHTML="<img src=\"images/repeatoff.png\"></a>";
38 document.getElementById('volume_total').innerHTML="<div id=\"volume_actual\" style=\"width:"+returnedData["volume"]+"%\">";
41 req.open("GET", urlbase+"status", true);
47 function RefreshPlaylist() {
49 var req = new XMLHttpRequest();
51 req.onreadystatechange = function () {
52 if (this.readyState != 4 || this.status != 200) return;
53 var returnedData = JSON.parse(this.responseText);
55 var text = "<div id=\"playlist_menu\">\
58 <td><span class=\"button\" onclick=\"EditPlayList()\"><img width=\"20\" src=\"images/songs.png\"></span><td>\
59 <td><span class=\"button\" onclick=\"LoadPlayList()\"><img width=\"20\" src=\"images/lists.png\"></span><td>\
60 <td><span class=\"button\" onclick=\"SavePlayList()\"><img width=\"20\" src=\"images/save.png\"></span><td>\
61 <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>\
67 <tr id=\"items_heading\">\
68 <td></td><td>Title</td><th colspan=\"4\">Controls</th>\
72 for (var key in returnedData) {
73 var rec=returnedData[key];
74 var name=GetFilename(rec["name"]);
85 text = text + "<tr id=\""+evText+"\">\
86 <td id=\"track_number\">\
87 <a name=\""+id+"\"></a></td>\
90 <span class=\"button\" onclick=\"PlaylistCommand('playitem',"+id+")\">"+name+"</span>\
93 <span class=\"button\" onclick=\"PlaylistCommand('moveup',"+id+")\"><img width=\"15\" src=\"images/up.png\"></span>\
96 <span class=\"button\" onclick=\"PlaylistCommand('movedown',"+id+")\"><img width=\"15\" src=\"images/down.png\"></span>\
99 <span class=\"button\" onclick=\"PlaylistCommand('remove',"+id+")\"><img width=\"15\" src=\"images/remove.png\"></span>\
104 text = text + "</table>\
106 <div id=\"playlist_menu\">\
109 <td><span class=\"button\" onclick=\"EditPlayList()\"><img width=\"20\" src=\"images/songs.png\"></span><td>\
110 <td><span class=\"button\" onclick=\"LoadPlayList()\"><img width=\"20\" src=\"images/lists.png\"></span><td>\
111 <td><span class=\"button\" onclick=\"SavePlayList()\"><img width=\"20\" src=\"images/save.png\"></span><td>\
112 <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>\
117 document.getElementById('playlist').innerHTML=text;
120 req.open("GET", urlbase+"playlist", true);
125 function EditPlayList(dir) {
127 var req = new XMLHttpRequest();
129 req.onreadystatechange = function () {
130 if (this.readyState != 4 || this.status != 200) return;
131 var returnedData = JSON.parse(this.responseText);
133 var text = "<div id=\"playlist_menu\">\
136 <td><span class=\"button\" onclick=\"RefreshPlaylist()\"><img width=\"20\" src=\"images/playlist.png\"></span><td>\
137 <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>\
143 <tr id=\"items_heading\">\
144 <td></td><td>Title</td><th colspan=\"2\">Controls</th>\
148 var lastSlash=dir.lastIndexOf("/");
150 var upperLevel=dir.slice(0,lastSlash);
154 var text = text + "<tr id=\"home\">\
156 <td><span class=\"button\" onclick=\"EditPlayList('"+upperLevel+"')\"><img width=\"20\" src=\"images/left.png\"></span></td>\
162 for (var key in returnedData) {
163 var rec=returnedData[key];
164 var type=rec["type"];
165 var name=rec["name"];
166 var lastSlash=name.lastIndexOf("/");
168 var tailName=name.slice(lastSlash+1);
173 if (type == "directory" || type == "file") {
183 text = text + "<tr id=\""+evText+"\">\
184 <td id=\"track_number\">\
185 <a name=\""+i+"\"></a></td>\
188 if (type == "directory") {
189 text = text + "<td id=\"file\">\
190 <span class=\"button\" onclick=\"EditPlayList('"+EscapeStr(name)+"')\">"+tailName+"</span></td><td>\
191 <span class=\"button\" onclick=\"PlaylistEditCommand('add','"+EscapeStr(name)+"')\"><img width=\"15\" src=\"images/plus.png\"></span></td>";
194 if (type == "file") {
195 text = text + "<td id=\"file\">\
196 <span class=\"button\" onclick=\"PlaylistEditCommand('add','"+EscapeStr(name)+"')\">"+tailName+"</span></td><td>\
197 <span class=\"button\" onclick=\"PlaylistEditCommand('add','"+EscapeStr(name)+"')\"><img width=\"15\" src=\"images/plus.png\"></span></td>";
200 text = text + "</tr>";
206 var text = text+"</table></div>\
207 <div id=\"playlist_menu\">\
210 <td><span class=\"button\" onclick=\"RefreshPlaylist()\"><img width=\"20\" src=\"images/playlist.png\"></span><td>\
211 <td><span class=\"button\" onclick=\"return confirm('Add all to the list, are you sure?') ? PlaylistEditCommand('addall','"+EscapeStr(dir)+"') : false;\" ><img width=\"20\" src=\"images/addall.png\"></span><td>\
215 document.getElementById('playlist').innerHTML=text;
218 if (!dir) { dir = ''; };
220 req.open("GET", urlbase+"lists|edit|"+dir, true);
225 function LoadPlayList() {
227 var req = new XMLHttpRequest();
229 req.onreadystatechange = function () {
230 if (this.readyState != 4 || this.status != 200) return;
231 var returnedData = JSON.parse(this.responseText);
232 text="<div id=\"playlist_menu\">\
235 <td><span class=\"button\" onclick=\"RefreshPlaylist()\"><img width=\"20\" src=\"images/playlist.png\"></span><td>\
236 <td><span class=\"button\" onclick=\"confirm('Clear current playlist, are you sure?') ? PlaylistCommandRefStatus('clear') : false;\" ><img width=\"20\" src=\"images/removeall.png\"></span><td>\
242 <tr id=\"items_heading\">\
243 <td></td><td>Name</td><td>Controls</td>\
247 for (var key in returnedData) {
248 var name=returnedData[key];
258 text = text + "<tr id=\""+evText+"\">\
259 <td id=\"track_number\"><a name=\"0\"></a></td>\
260 <td id=\"file\"><span class=\"button\" onclick=\"PlaylistEditCommandRefFull('load','"+EscapeStr(name)+"')\">"+name+"</td>\
261 <td id=\"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>\
265 text=text+"</table></div><div id=\"playlist_menu\">\
268 <td><span class=\"button\" onclick=\"RefreshPlaylist()\"><img width=\"20\" src=\"images/playlist.png\"></span><td>\
269 <td><span class=\"button\" onclick=\"confirm('Clear current playlist, are you sure?') ? PlaylistCommandRefStatus('clear') : false;\" ><img width=\"20\" src=\"images/removeall.png\"></span><td>\
274 document.getElementById('playlist').innerHTML=text;
277 req.open("GET", urlbase+"lists|load", true);
282 function SavePlayList() {
284 var name=window.prompt('List name','');
286 var req = new XMLHttpRequest();
288 req.onreadystatechange = function () {
289 if (this.readyState != 4 || this.status != 200) return;
290 if (this.responseText != 'OK') {
291 window.alert(this.responseText);
295 req.open("GET", urlbase+"lists|save|"+name, true);
300 function DelPlayList(item) {
302 var req = new XMLHttpRequest();
304 req.onreadystatechange = function () {
305 if (this.readyState != 4 || this.status != 200) return;
310 req.open("GET", urlbase+"lists|delete|"+item, true);
315 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 setInterval(RefreshPageStatus, 10000);