]> git.sesse.net Git - vlc/blob - share/lua/http/js/controlers.js
luaHTTP: behave like oldHTTP for seeking
[vlc] / share / lua / http / js / controlers.js
1 var current_id  =       1;
2 var currentArt  =       null;
3 var current_que =       'main';
4 function updateStatus(){
5         $.ajax({
6                 url: 'requests/status.xml',
7                 success: function(data,status,jqXHR){
8                         if(current_que=='main'){
9                                 $('.dynamic').empty();
10                                 $('#mediaTitle').append($('[name="filename"]',data).text());
11                                 $('#totalTime').append(format_time($('length',data).text()));
12                                 $('#currentTime').append(format_time($('time',data).text()));
13                                 $('#seekSlider').slider({value: ($('position',data).text()*100) });
14                                 $('#currentVolume').append(Math.round($('volume',data).text()/5.12)+'%');
15                                 $('#volumeSlider').slider({value: ($('volume',data).text()/5.12) });
16                                 $('#rateSlider').slider({value: ($('rate',data).text()) });
17                                 $('#currentRate').append(Math.round($('rate',data).text()*100)/100+'x');
18                                 $('#audioSlider').slider({value: ($('audiodelay',data).text()) });
19                                 $('#currentAudioDelay').append(Math.round($('audiodelay',data).text()*100)/100+'s');
20                                 $('#subtitleSlider').slider({value: ($('subtitledelay',data).text()) });
21                                 $('#currentSubtitleDelay').append(Math.round($('subtitledelay',data).text()*100)/100+'s');
22                                 $('#seekSlider').attr('totalLength',$('length',data).text());
23                                 $('#buttonPlay').attr('state',$('state',data).text());
24                                 $('#buttonPlay').attr('mrl',$('[name="filename"]',data).text());
25                                 if($('state',data).text()=='playing'){
26                                         $('#buttonPlay').removeClass('paused');
27                                         $('#buttonPlay').addClass('playing');
28                                 }else{
29                                         $('#buttonPlay').removeClass('playing');
30                                         $('#buttonPlay').addClass('paused');
31                                 }
32                                 if($('random',data).text()=='true'){
33                                         $('#buttonShuffle').removeClass('ui-state-default');
34                                         $('#buttonShuffle').addClass('ui-state-active');
35                                 }else{
36                                         $('#buttonShuffle').addClass('ui-state-default');
37                                         $('#buttonShuffle').removeClass('ui-state-active');
38                                 }
39                                 if($('loop',data).text()=='true'){
40                                         $('#buttonLoop').removeClass('ui-state-default');
41                                         $('#buttonLoop').addClass('ui-state-active');
42                                 }else{
43                                         $('#buttonLoop').addClass('ui-state-default');
44                                         $('#buttonLoop').removeClass('ui-state-active');
45                                 }
46                                 if($('repeat',data).text()=='true'){
47                                         $('#buttonRepeat').removeClass('ui-state-default');
48                                         $('#buttonRepeat').addClass('ui-state-active');
49                                 }else{
50                                         $('#buttonRepeat').addClass('ui-state-default');
51                                         $('#buttonRepeat').removeClass('ui-state-active');
52                                 }
53                                 if($('[name="artwork_url"]',data).text()!=currentArt){
54                                         var tmp =       new Date();
55                                         $('#albumArt').attr('src','/art?'+tmp.getTime());
56                                         currentArt      =       $('[name="artwork_url"]',data).text();
57                                         $('#albumArt').css({
58                                                 'visibility':'visible',
59                                                 'display':'block'
60                                         });
61                                 }else if($('[name="artwork_url"]',data).text()==""){
62                                         $('#albumArt').attr('src','images/vlc-48.png');
63                                 }
64                                 if(pollStatus){
65                                         setTimeout( updateStatus, 1000 );
66                                 }
67                                 
68                         }
69                         $('band',data).each(function(){
70                                 var id          =       $(this).attr('id');
71                                 var freq        =       convertHz($(this).attr('freqency'));
72                                 var value       =       $(this).text() ? $(this).text() : 0;
73                                 if(!$('#eq_container'+id).length){
74                                         $('#window_equalizer').append('<div style="float:left;width:44px;" align="center" id="eq_container'+id+'"><div id="eq'+id+'_txt">'+value+'dB</div><div class="eqBand" id="eq'+id+'" style="font-size: 18px;"></div><div>'+freq+'</div></div>');
75                                         $('#eq'+id).slider({
76                                                 min: -20,
77                                                 max: 20,
78                                                 step: 0.1,
79                                                 range: "min",
80                                                 value: value,
81                                                 animate: true,
82                                                 orientation: "vertical",
83                                                 stop: function(event,ui){
84                                                         $('#'+$(this).attr('id')+'_txt').empty().append(ui.value+'dB');
85                                                         sendCommad({
86                                                                 command:'equalizer',
87                                                                 val: ui.value,
88                                                                 band: $(this).attr('id').substr(2)
89                                                         })
90                                                 },
91                                                 slide: function(event,ui){
92                                                         $('#'+$(this).attr('id')+'_txt').empty().append(ui.value+'dB');
93                                                 }
94                                         });
95                                 }else{
96                                         $('#eq'+id).slider({value:value});
97                                         $('#eq'+id+'_txt').empty().append(Math.round(value*100)/100+'dB');
98                                 }
99                         });
100                         $('#preamp').slider('value',$('preamp',data).text());
101                         $('#preamp_txt').empty().append(Math.round($('preamp',data).text()*100)/100+'dB');
102                 },
103                 error: function(jqXHR,status,error){
104                         setTimeout( updateStatus, 500 );
105                 }
106         });
107 }
108 function updatePlayList(){
109         $('#libraryTree').jstree('refresh',-1);
110 }
111 function sendCommand(params,append){
112         if(current_que=='stream'){
113                 $.ajax({
114                         url: 'requests/status.xml',
115                         data: params,
116                         success:function(data,status,jqXHR){
117                                 if(append!=undefined){
118                                         eval(append);
119                                 }
120                                 updateStatus();
121                                 updatePlayList();
122                         }
123                 });
124         }else{
125                 if(params.plreload===false){
126                         $.ajax({
127                                 url: 'requests/status.xml',
128                                 data: params,
129                                 success:function(data,status,jqXHR){
130                                         if(append!=undefined){
131                                                 eval(append);
132                                         }
133                                 }
134                         });
135                 }else{
136                         $.ajax({
137                                 url: 'requests/status.xml',
138                                 data: params,
139                                 success:function(data,status,jqXHR){
140                                         if(append!=undefined){
141                                                 eval(append);
142                                         }
143                                         updatePlayList();
144                                 }
145                         });
146                 }       
147         }
148 }
149 function browse(dir){
150         dir     =       dir==undefined ? '~' : dir;
151         $.ajax({
152                 url: 'requests/browse.xml',
153                 data:'dir='+encodeURIComponent(dir),
154                 success: function(data,status,jqXHR){
155                         var tgt =       browse_target.indexOf('__')==-1 ? browse_target : browse_target.substr(0,browse_target.indexOf('__'));
156                         $('#browse_elements').empty();
157             $('element',data).each(function(){
158                                 if($(this).attr('type')=='dir' || $.inArray($(this).attr('name').substr(-3),video_types)!=-1 || $.inArray($(this).attr('name').substr(-3),audio_types)!=-1){
159                                         $('#browse_elements').append(createElementLi($(this).attr('name'),$(this).attr('type'),$(this).attr('path'),$(this).attr('name').substr(-3)));
160                                 }
161                         });
162                         $('[opendir]').dblclick(function(){
163                                 browse($(this).attr('opendir'));
164                         });
165                         $('[openfile]').dblclick(function(){
166                                 switch(tgt){
167                                         case '#stream_input':
168                                                 $(browse_target).val($(this).attr('openfile'));
169                                                 break;
170                                         case '#mosaic_open':
171                                                 $('li',browse_target).remove();
172                                                 $(browse_target).append(this);
173                                                 $(this).css({
174                                                         'margin-left' : -40,
175                                                         'margin-top' : -46,
176                                                         'float' : 'left'
177                                                 });
178                                                 break;
179                                         case '#mobile':
180                                                 break;
181                                         default:
182                                                 sendCommand('command=in_play&input=file://'+encodeURIComponent($(this).attr('openfile')));
183                                                 break;
184                                 }
185                                 $('#window_browse').dialog('close');
186                         });
187                         $('[opendir]').click(function(){
188                                 switch(tgt){
189                                         case '#mobile':
190                                                 browse($(this).attr('opendir'));
191                                                 break;
192                                         default:
193                                                 break;
194                                 }
195                         });
196                         $('[openfile]').click(function(){
197                                 switch(tgt){
198                                         case '#mobile':
199                                                 sendCommand('command=in_play&input=file://'+encodeURIComponent($(this).attr('openfile')),"window.location='mobile.html'");
200                                                 break;
201                                         default:
202                                                 break;
203                                 }
204                         });
205                         switch(tgt){
206                                 case '#mobile':
207                                         break;
208                                 default:
209                                         $('[selectable]').selectable();
210                                         break;
211                         }
212                 },
213                 error: function(jqXHR,status,error){
214                         setTimeout('browse("'+dir+'")',1041);
215                 }
216         });
217 }
218 function updateStreams(){
219         $.ajax({
220                 url: 'requests/vlm.xml',
221                 success:function(data,status,jqXHR){
222                         $('#stream_info').accordion("destroy");
223                         $('#stream_info').empty();
224                         $('broadcast',data).each(function(){
225                                 var stream_div  =       $('#stream_status_').clone();
226                                 var name                =       $(this).attr('name');
227                                 var loop                =       $(this).attr('loop')=='yes';
228                                 var playing             =       $('instance',$(this)).attr('state')=='playing';
229                                 var file                =       $('input',$(this)).text();
230                                 var output              =       $('output',$(this)).text();
231                                 var time                =       isNaN(Math.round($('instance',$(this)).attr('time')/1000000)) ? 0 : Math.round($('instance',$(this)).attr('time')/1000000);
232                                 var length              =       isNaN(Math.round($('instance',$(this)).attr('length')/1000000)) ? 0 : Math.round($('instance',$(this)).attr('length')/1000000);
233                                 $('[id]',stream_div).each(function(){
234                                         $(this).attr('id',$(this).attr('id')+name);
235                                 });
236                                 $(stream_div).attr('id',$(stream_div).attr('id')+name);
237                                 $('#stream_title_'+name,stream_div).append(name);
238                                 $('#stream_file_'+name,stream_div).append(file);
239                                 $('#stream_pos_'+name,stream_div).slider({
240                                         value: 0,
241                                         range: "min",
242                                         min: 0,
243                                         slide: function( event, ui ) {
244                                                 $( "#stream_current_time_"+name,stream_div ).empty();
245                                                 $( "#stream_current_time_"+name,stream_div ).append( format_time(ui.value) );
246                                                 $( "#stream_total_time_"+name,stream_div ).empty();
247                                                 $( "#stream_total_time_"+name,stream_div ).append( format_time($('#stream_pos_'+name,stream_div).slider('option','max')) );
248                                                 sendVLMCmd('control '+name+' seek '+Math.round(ui.value/$('#stream_pos_'+name,stream_div).slider('option','max')*100));
249                                         },
250                                         change: function(event, ui){
251                                                 $( "#stream_current_time_"+name,stream_div ).empty();
252                                                 $( "#stream_current_time_"+name,stream_div ).append( format_time(ui.value) );
253                                                 $( "#stream_total_time_"+name,stream_div ).empty();
254                                                 $( "#stream_total_time_"+name,stream_div ).append( format_time($('#stream_pos_'+name,stream_div).slider('option','max')) );
255                                         }
256                                 });
257                                 $('#button_stream_stop_'+name,stream_div).click(function(){
258                                         sendVLMCmd('control '+name+' stop');
259                                         return false;
260                                 });
261                                 $('#button_stream_play_'+name,stream_div).click(function(){
262                                         if($('span',this).hasClass('ui-icon-pause')){
263                                                 sendVLMCmd('control '+name+' pause');
264                                         }else{
265                                                 sendVLMCmd('control '+name+' play');
266                                         }
267                                 });
268                                 $('#button_stream_loop_'+name,stream_div).click(function(){
269                                         if(loop){
270                                                 sendVLMCmd('setup '+name+' unloop');
271                                         }else{
272                                                 sendVLMCmd('setup '+name+' loop');
273                                         }
274                                 });
275                                 $('#button_stream_delete_'+name,stream_div).click(function(){
276                                         sendVLMCmd('del '+name);
277                                 });
278                                 $('#stream_pos_'+name,stream_div).slider({
279                                         max: length,
280                                         value: time
281                                 });
282                                 if(playing){
283                                         $('span',$('#button_stream_play_'+name,stream_div)).removeClass('ui-icon-play');
284                                         $('span',$('#button_stream_play_'+name,stream_div)).addClass('ui-icon-pause');
285                                 }
286                                 if(loop){
287                                         $('#button_stream_loop_'+name,stream_div).addClass('ui-state-active');
288                                 }
289                                 $(stream_div).css({
290                                         'visibility':'',
291                                         'display':''
292                                 });
293                                 $('#stream_info').append(stream_div);
294
295                         });
296                         $('.button').hover(
297                                 function() { $(this).addClass('ui-state-hover'); },
298                                 function() { $(this).removeClass('ui-state-hover'); }
299                         );
300                         $('#stream_info').accordion({
301                                 header: "h3",
302                                 collapsible: true,
303                                 autoHeight: true
304                         });
305                         if(current_que=='stream'){
306                                 $('.dynamic').empty();
307                                 $('#mediaTitle').append($('[name="Current"] input',data).text());
308                                 $('#totalTime').append(format_time(isNaN($('[name="Current"] instance',data).attr('length')) ? 0 : $('[name="Current"] instance',data).attr('length')/1000000));
309                                 $('#currentTime').append(format_time(isNaN($('[name="Current"] instance',data).attr('time')) ? 0 : $('[name="Current"] instance',data).attr('time')/1000000));
310                                 $('#seekSlider').slider({value: (($('[name="Current"] instance',data).attr('time')/1000000)/($('[name="Current"] instance',data).attr('length')/1000000)*100) });
311                                 $('#seekSlider').attr('totalLength',$('[name="Current"] instance',data).attr('length')/1000000);
312                                 $('#buttonPlay').attr('state',$('[name="Current"] instance',data).length>0 ? $('[name="Current"] instance',data).attr('state') : 'stopped');
313                                 if($('[name="Current"] instance',data).attr('state')=='playing'){
314                                         $('#buttonPlay').removeClass('paused');
315                                         $('#buttonPlay').addClass('playing');
316                                 }else{
317                                         $('#buttonPlay').removeClass('playing');
318                                         $('#buttonPlay').addClass('paused');
319                                 }
320                                 setTimeout( updateStreams, 1000 );
321                         }
322                 }
323         });
324 }
325 function updateEQ(){
326         $.ajax({
327                 url: 'requests/equalizer.xml',
328                 success: function(data,status,jqXHR){
329                         $('band',data).each(function(){
330                                 var id          =       $(this).attr('id');
331                                 var freq        =       convertHz($(this).attr('freqency'));
332                                 var value       =       $(this).text() ? $(this).text() : 0;
333                                 if(!$('#eq_container'+id).length){
334                                         $('#window_equalizer').append('<div style="float:left;width:44px;" align="center" id="eq_container'+id+'"><div id="eq'+id+'_txt">'+value+'dB</div><div class="eqBand" id="eq'+id+'" style="font-size: 18px;"></div><div>'+freq+'</div></div>');
335                                         $('#eq'+id).slider({
336                                                 min: -20,
337                                                 max: 20,
338                                                 step: 0.1,
339                                                 range: "min",
340                                                 value: value,
341                                                 animate: true,
342                                                 orientation: "vertical",
343                                                 stop: function(event,ui){
344                                                         $('#'+$(this).attr('id')+'_txt').empty().append(ui.value+'dB');
345                                                         sendEQCmd({
346                                                                 command:'equalizer',
347                                                                 val: ui.value,
348                                                                 band: $(this).attr('id').substr(2)
349                                                         })
350                                                 },
351                                                 slide: function(event,ui){
352                                                         $('#'+$(this).attr('id')+'_txt').empty().append(ui.value+'dB');
353                                                 }
354                                         });
355                                 }else{
356                                         $('#eq'+id).slider({value:value});
357                                         $('#eq'+id+'_txt').empty().append(Math.round(value*100)/100+'dB');
358                                 }
359                         });
360                         $('#preamp').slider('value',$('preamp',data).text());
361                         $('#preamp_txt').empty().append(Math.round($('preamp',data).text()*100)/100+'dB');
362                 }
363         })
364 }
365 function sendVLMCmd(command,append){
366         var commands    =       command.split(';');
367         if(commands.length>1){
368                 sendBatchVLMCmd(command,append);
369         }else{
370                 if(current_que=='main'){
371                         $.ajax({
372                                 url: 'requests/vlm_cmd.xml',
373                                 data: 'command='+encodeURIComponent(command),
374                                 success: function(data,status,jqXHR){
375                                         if($('error',data).text()){
376                                                 $('#error_container').append('<div>'+$('error',data).text()+'</div>');
377                                                 $('#window_error').dialog('open');
378                                         }
379                                         if(append!=undefined){
380                                                 eval(append);
381                                         }
382                                         updateStreams();
383                                 }
384                         });
385                 }else{
386                         $.ajax({
387                                 url: 'requests/vlm_cmd.xml',
388                                 data: 'command='+encodeURIComponent(command),
389                                 success: function(data,status,jqXHR){
390                                         if($('error',data).text()){
391                                                 $('#error_container').append('<div>'+$('error',data).text()+'</div>');
392                                                 $('#window_error').dialog('open');
393                                         }
394                                         updateStreams();
395                                 }
396                         });
397                 }
398         }
399 }
400 function sendBatchVLMCmd(command,append){
401         var commands    =       command.split(';');
402         $.ajax({
403                 url: 'requests/vlm_cmd.xml',
404                 data: 'command='+encodeURIComponent(commands.shift()),
405                 success:function(data,status,jqXHR){
406                         if($('error',data).text()){
407                                 $('#error_container').append('<div>'+$('error',data).text()+'</div>');
408                                 $('#window_error').dialog('open');
409                         }
410                         sendVLMCmd(commands.join(';'),append);
411                 }
412         });
413 }
414 function sendEQCmd(params){
415         $.ajax({
416                 url: 'requests/equalizer.xml',
417                 data: params,
418                 success: function(data,status,jqXHR){
419                         updateEQ();
420                 }
421         });
422 }
423
424 $(function(){
425         $('#libraryTree').jstree({
426                 "xml_data":{
427                         "ajax":{
428                                 "url" : "requests/playlist_jstree.xml"
429                         },
430                         "xsl" : "nest"
431                 },
432                 "themeroller":{
433                         "item_leaf":"ui-icon-video"
434                 },
435                 "plugins" : ["xml_data","ui","themeroller"]
436         }).bind("loaded.jstree", function (event, data) {
437                 $('[current]','[id^="plid_"]').each(function(){
438                         $(this).addClass('ui-state-highlight');
439                         current_id      =       $(this).attr('id').substr(5);
440                 });
441         }).bind("refresh.jstree",function(event,data){
442                 $('[current]','[id^="plid_"]').each(function(){
443                         $(this).addClass('ui-state-highlight');
444                         current_id      =       $(this).attr('id').substr(5);
445                 });
446         });
447         updateStatus();
448         updateStreams();
449         updateEQ();
450 });