Веб-интерфейс к MPD.
[mpd-web.git] / ajax / mpd.js
1
2 function RefreshNowPlaying() {
3
4 var req = new XMLHttpRequest();
5
6 req.onreadystatechange = function () {
7   if (this.readyState != 4 || this.status != 200) return;
8   document.getElementById('nowplaying_content').innerHTML=this.responseText;
9   document.title='MPD Player: '+this.responseText;
10 };
11
12 req.open("GET", "ajax/trackinfo.php", true);
13 req.send();
14
15 }
16
17 function RefreshPlayerState() {
18
19 var req = new XMLHttpRequest();
20
21 req.onreadystatechange = function () {
22   if (this.readyState != 4 || this.status != 200) return;
23   if (this.responseText=="play") {
24     document.getElementById('playpausebutton').innerHTML="<span onclick=\"Command('pause')\"><img class=\"button\" src=\"images/pause.png\"></span>";
25   } else {
26     document.getElementById('playpausebutton').innerHTML="<span onclick=\"Command('play')\"><img class=\"button\" src=\"images/play.png\"></span>";
27   }
28 };
29
30 req.open("GET", "ajax/playerstate.php", true);
31 req.send();
32
33 }
34
35 function RefreshRepeatState() {
36
37 var req = new XMLHttpRequest();
38
39 req.onreadystatechange = function () {
40   if (this.readyState != 4 || this.status != 200) return;
41   if (this.responseText=="1") {
42     document.getElementById('repeatstate').innerHTML="<img src=\"images/repeaton.png\"></a>";
43   } else {
44     document.getElementById('repeatstate').innerHTML="<img src=\"images/repeatoff.png\"></a>";
45   }
46 };
47
48 req.open("GET", "ajax/repeatstate.php", true);
49 req.send();
50
51 }
52
53 function RefreshVolume() {
54
55 var req = new XMLHttpRequest();
56
57 req.onreadystatechange = function () {
58   if (this.readyState != 4 || this.status != 200) return;
59   document.getElementById('volume_total').innerHTML="<div id=\"volume_actual\" style=\"width:"+this.responseText+"%\">";
60 };
61
62 req.open("GET", "ajax/volume.php", true);
63 req.send();
64
65 }
66
67 function RefreshPageStatus() {
68
69   RefreshNowPlaying();
70   RefreshPlayerState();
71   RefreshRepeatState();
72   RefreshVolume();
73
74 }
75
76 function RefreshPlaylist() {
77
78 var req = new XMLHttpRequest();
79
80 req.onreadystatechange = function () {
81   if (this.readyState != 4 || this.status != 200) return;
82   document.getElementById('playlist').innerHTML=this.responseText;
83 };
84
85 req.open("GET", "ajax/playlist.php", true);
86 req.send();
87
88 }
89
90 function EditPlayList(dir) {
91
92 var req = new XMLHttpRequest();
93
94 req.onreadystatechange = function () {
95   if (this.readyState != 4 || this.status != 200) return;
96   document.getElementById('playlist').innerHTML=this.responseText;
97 };
98
99 if (!dir) { dir = ''; };
100
101 req.open("GET", "ajax/editplaylist.php?dir="+dir, true);
102 req.send();
103
104 }
105
106 function RefreshPageContent() {
107
108   RefreshPageStatus();
109   RefreshPlaylist();
110
111 }
112
113 function Command(cmd) {
114
115 var req = new XMLHttpRequest();
116
117 req.onreadystatechange = function () {
118   if (this.readyState != 4 || this.status != 200) return;
119   RefreshPageStatus();
120 };
121
122 req.open("GET", "ajax/command.php?task="+cmd, true);
123 req.send();
124
125 }
126
127 function PlaylistCommand(cmd,item) {
128
129 var req = new XMLHttpRequest();
130
131 req.onreadystatechange = function () {
132   if (this.readyState != 4 || this.status != 200) return;
133   RefreshPageContent();
134 };
135
136 req.open("GET", "ajax/playlist-command.php?item="+item+"&task="+cmd, true);
137 req.send();
138
139 }
140
141 function PlaylistEditCommand(cmd,item) {
142
143 var req = new XMLHttpRequest();
144
145 req.open("GET", "ajax/playlist-command.php?item="+item+"&task="+cmd, true);
146 req.send();
147
148 }
149
150
151 function PlaylistItemsCommand(cmd) {
152
153 var req = new XMLHttpRequest();
154
155 var selected = [].filter.call(document.getElementsByName('itemlist[]'), function(c) {
156   return c.checked;
157 }).map(function(c) {
158   return c.value;
159 });
160   
161 req.onreadystatechange = function () {
162   if (this.readyState != 4 || this.status != 200) return;
163   RefreshPageContent();
164 };
165
166 params=selected.map(function(el) {
167         //Map each field into a name=value string, make sure to properly escape!
168         return 'itemlist[]=' + encodeURIComponent(el);
169     }).join('&');
170
171 req.open("POST", "ajax/playlist-command.php?task="+cmd, true);
172 req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
173 req.send(params);
174
175 }
176
177 function PlaylistEditItemsCommand(cmd,dir) {
178
179 var req = new XMLHttpRequest();
180
181 var selected = [].filter.call(document.getElementsByName('itemlist[]'), function(c) {
182   return c.checked;
183 }).map(function(c) {
184   return c.value;
185 });
186   
187 params=selected.map(function(el) {
188         //Map each field into a name=value string, make sure to properly escape!
189         return 'itemlist[]=' + encodeURIComponent(el);
190     }).join('&');
191
192 req.open("POST", "ajax/playlist-command.php?dir="+dir+"&task="+cmd, true);
193 req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
194 req.send(params);
195
196 }
197
198 // setInterval(RefreshPageStatus, 5000);