Вычищение старых конструкций PHP + многочисленная доводка по мелочи.
[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 };
10
11 req.open("GET", "ajax/trackinfo.php", true);
12 req.send();
13
14 }
15
16 function RefreshTitle() {
17
18 var req = new XMLHttpRequest();
19
20 req.onreadystatechange = function () {
21   if (this.readyState != 4 || this.status != 200) return;
22   document.title='MPD Player: '+this.responseText;
23 };
24
25 req.open("GET", "ajax/trackname.php", true);
26 req.send();
27 }
28
29 function RefreshPlayerState() {
30
31 var req = new XMLHttpRequest();
32
33 req.onreadystatechange = function () {
34   if (this.readyState != 4 || this.status != 200) return;
35   if (this.responseText=="play") {
36     document.getElementById('playpausebutton').innerHTML="<span onclick=\"Command('pause')\"><img class=\"button\" src=\"images/pause.png\"></span>";
37   } else {
38     document.getElementById('playpausebutton').innerHTML="<span onclick=\"Command('play')\"><img class=\"button\" src=\"images/play.png\"></span>";
39   }
40 };
41
42 req.open("GET", "ajax/playerstate.php", true);
43 req.send();
44
45 }
46
47 function RefreshRepeatState() {
48
49 var req = new XMLHttpRequest();
50
51 req.onreadystatechange = function () {
52   if (this.readyState != 4 || this.status != 200) return;
53   if (this.responseText=="1") {
54     document.getElementById('repeatstate').innerHTML="<img src=\"images/repeaton.png\"></a>";
55   } else {
56     document.getElementById('repeatstate').innerHTML="<img src=\"images/repeatoff.png\"></a>";
57   }
58 };
59
60 req.open("GET", "ajax/repeatstate.php", true);
61 req.send();
62
63 }
64
65 function RefreshVolume() {
66
67 var req = new XMLHttpRequest();
68
69 req.onreadystatechange = function () {
70   if (this.readyState != 4 || this.status != 200) return;
71   document.getElementById('volume_total').innerHTML="<div id=\"volume_actual\" style=\"width:"+this.responseText+"%\">";
72 };
73
74 req.open("GET", "ajax/volume.php", true);
75 req.send();
76
77 }
78
79 function RefreshPageStatus() {
80
81   RefreshTitle();
82   RefreshNowPlaying();
83   RefreshPlayerState();
84   RefreshRepeatState();
85   RefreshVolume();
86
87 }
88
89 function RefreshPlaylist() {
90
91 var req = new XMLHttpRequest();
92
93 req.onreadystatechange = function () {
94   if (this.readyState != 4 || this.status != 200) return;
95   document.getElementById('playlist').innerHTML=this.responseText;
96 };
97
98 req.open("GET", "ajax/playlist.php", true);
99 req.send();
100
101 }
102
103 function EditPlayList(dir) {
104
105 var req = new XMLHttpRequest();
106
107 req.onreadystatechange = function () {
108   if (this.readyState != 4 || this.status != 200) return;
109   document.getElementById('playlist').innerHTML=this.responseText;
110 };
111
112 if (!dir) { dir = ''; };
113
114 req.open("GET", "ajax/editplaylist.php?dir="+dir, true);
115 req.send();
116
117 }
118
119 function LoadPlayList() {
120
121 var req = new XMLHttpRequest();
122
123 req.onreadystatechange = function () {
124   if (this.readyState != 4 || this.status != 200) return;
125   document.getElementById('playlist').innerHTML=this.responseText;
126 };
127
128 req.open("GET", "ajax/playlists.php", true);
129 req.send();
130
131 }
132
133 function SavePlayList() {
134
135 var name=window.prompt('List name','');
136
137 var req = new XMLHttpRequest();
138
139 req.onreadystatechange = function () {
140   if (this.readyState != 4 || this.status != 200) return;
141   if (this.responseText != 'OK') {
142     window.alert(this.responseText);
143   }
144 };
145
146 req.open("GET", "ajax/save.php?item="+name, true);
147 req.send();
148
149 }
150
151 function DelPlayList(item) {
152
153 var req = new XMLHttpRequest();
154
155 req.onreadystatechange = function () {
156   if (this.readyState != 4 || this.status != 200) return;
157   RefreshPageStatus();
158   LoadPlayList();
159 };
160
161 req.open("GET", "ajax/playlist-command.php?task=dellist&item="+item, true);
162 req.send();
163
164 }
165
166 function RefreshPageContent() {
167
168   RefreshPageStatus();
169   RefreshPlaylist();
170
171 }
172
173 function Command(cmd) {
174
175 var req = new XMLHttpRequest();
176
177 req.onreadystatechange = function () {
178   if (this.readyState != 4 || this.status != 200) return;
179   RefreshPageStatus();
180 };
181
182 req.open("GET", "ajax/command.php?task="+cmd, true);
183 req.send();
184
185 }
186
187 function PlaylistCommand(cmd,item) {
188
189 var req = new XMLHttpRequest();
190
191 req.onreadystatechange = function () {
192   if (this.readyState != 4 || this.status != 200) return;
193   RefreshPageContent();
194 };
195
196 req.open("GET", "ajax/playlist-command.php?item="+item+"&task="+cmd, true);
197 req.send();
198
199 }
200
201 function PlaylistCommandRefStatus(cmd,item) {
202
203 var req = new XMLHttpRequest();
204
205 req.onreadystatechange = function () {
206   if (this.readyState != 4 || this.status != 200) return;
207   RefreshPageStatus();
208 };
209
210 req.open("GET", "ajax/playlist-command.php?item="+item+"&task="+cmd, true);
211 req.send();
212
213 }
214
215 function PlaylistEditCommand(cmd,item) {
216
217 var req = new XMLHttpRequest();
218
219 req.onreadystatechange = function () {
220   if (this.readyState != 4 || this.status != 200) return;
221   RefreshPageStatus();
222 };
223
224 req.open("GET", "ajax/playlist-command.php?item="+item+"&task="+cmd, true);
225 req.send();
226
227 }
228
229 function PlaylistEditCommandRefFull(cmd,item) {
230
231 var req = new XMLHttpRequest();
232
233 req.onreadystatechange = function () {
234   if (this.readyState != 4 || this.status != 200) return;
235   RefreshPageContent();
236 };
237
238 req.open("GET", "ajax/playlist-command.php?item="+item+"&task="+cmd, true);
239 req.send();
240
241 }
242
243
244 function PlaylistItemsCommand(cmd) {
245
246 var req = new XMLHttpRequest();
247
248 var selected = [].filter.call(document.getElementsByName('itemlist[]'), function(c) {
249   return c.checked;
250 }).map(function(c) {
251   return c.value;
252 });
253   
254 req.onreadystatechange = function () {
255   if (this.readyState != 4 || this.status != 200) return;
256   RefreshPageContent();
257 };
258
259 params=selected.map(function(el) {
260         //Map each field into a name=value string, make sure to properly escape!
261         return 'itemlist[]=' + encodeURIComponent(el);
262     }).join('&');
263
264 req.open("POST", "ajax/playlist-command.php?task="+cmd, true);
265 req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
266 req.send(params);
267
268 }
269
270 function PlaylistEditItemsCommand(cmd,dir) {
271
272 var req = new XMLHttpRequest();
273
274 var selected = [].filter.call(document.getElementsByName('itemlist[]'), function(c) {
275   return c.checked;
276 }).map(function(c) {
277   return c.value;
278 });
279   
280 params=selected.map(function(el) {
281         //Map each field into a name=value string, make sure to properly escape!
282         return 'itemlist[]=' + encodeURIComponent(el);
283     }).join('&');
284
285 req.onreadystatechange = function () {
286   if (this.readyState != 4 || this.status != 200) return;
287   RefreshPageStatus();
288 };
289
290 req.open("POST", "ajax/playlist-command.php?dir="+dir+"&task="+cmd, true);
291 req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
292 req.send(params);
293
294 }
295
296 setInterval(RefreshPageStatus, 10000);