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