1 /*****************************************************************************
2 * vlm.js: VLC media player web interface
3 *****************************************************************************
4 * Copyright (C) 2005-2006 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /* replace quotes and spaces by underscores */
25 function addunderscores( str ){ return str.replace(/\'|\"| /g, '_'); }
27 /**********************************************************************
28 * Input dialog functions
29 *********************************************************************/
31 function vlm_input_edit( dest )
33 document.getElementById( 'input_dest' ).value = dest;
37 function vlm_input_change()
39 document.getElementById( value( 'input_dest' ) ).value = value( 'input_mrl' );
41 document.getElementById( value( 'input_dest' ) ).focus();
44 function vlm_output_edit( dest )
46 document.getElementById( 'sout_dest' ).value = dest;
50 function vlm_output_change()
52 document.getElementById( value( 'sout_dest' ) ).value = value( 'sout_mrl' ).substr(6); /* substr <-> remove :sout= */
54 document.getElementById( value( 'sout_dest' ) ).focus();
57 function hide_vlm_add()
59 document.getElementById( 'vlm_add_broadcast' ).style.display = 'none';
60 document.getElementById( 'vlm_add_vod' ).style.display = 'none';
61 document.getElementById( 'vlm_add_schedule' ).style.display = 'none';
64 function toggle_schedule_date()
66 if( checked( 'vlm_schedule_now' ) )
68 disable( 'vlm_schedule_year' );
69 disable( 'vlm_schedule_month' );
70 disable( 'vlm_schedule_day' );
71 disable( 'vlm_schedule_hour' );
72 disable( 'vlm_schedule_minute' );
73 disable( 'vlm_schedule_second' );
77 enable( 'vlm_schedule_year' );
78 enable( 'vlm_schedule_month' );
79 enable( 'vlm_schedule_day' );
80 enable( 'vlm_schedule_hour' );
81 enable( 'vlm_schedule_minute' );
82 enable( 'vlm_schedule_second' );
86 function toggle_schedule_repeat()
88 if( checked( 'vlm_schedule_repeat' ) )
90 enable( 'vlm_schedule_period_year' );
91 enable( 'vlm_schedule_period_month' );
92 enable( 'vlm_schedule_period_day' );
93 enable( 'vlm_schedule_period_hour' );
94 enable( 'vlm_schedule_period_minute' );
95 enable( 'vlm_schedule_period_second' );
96 enable( 'vlm_schedule_repeat_times' );
100 disable( 'vlm_schedule_period_year' );
101 disable( 'vlm_schedule_period_month' );
102 disable( 'vlm_schedule_period_day' );
103 disable( 'vlm_schedule_period_hour' );
104 disable( 'vlm_schedule_period_minute' );
105 disable( 'vlm_schedule_period_second' );
106 disable( 'vlm_schedule_repeat_times' );
110 function vlm_schedule_type_change( name )
112 act = document.getElementById( 'vlm_elt_' + name + '_action' ).value;
113 itemname = document.getElementById( 'vlm_elt_' + name + '_name' );
114 opt = document.getElementById( 'vlm_elt_' + name + '_opt' );
115 if( act == "play" || act == "pause" || act == "stop" )
117 itemname.style.display = "";
118 opt.style.display = "none";
120 else if( act == "seek" )
122 itemname.style.display = "";
123 opt.style.display = "";
127 itemname.style.display = "none";
128 opt.style.display = "";
132 function update_vlm_add_broadcast()
134 cmd = document.getElementById( 'vlm_command' );
136 if( value( 'vlm_broadcast_name' ) )
138 cmd.value = "new " + addunderscores( value( 'vlm_broadcast_name' ) )
141 if( checked( 'vlm_broadcast_enabled' ) )
143 cmd.value += " enabled";
146 if( checked( 'vlm_broadcast_loop' ) )
148 cmd.value += " loop";
151 if( value( 'vlm_broadcast_input' ) )
153 cmd.value += " input " + value( 'vlm_broadcast_input' );
156 if( value( 'vlm_broadcast_output' ) )
158 cmd.value += " output " + value( 'vlm_broadcast_output' );
167 function update_vlm_add_vod()
169 cmd = document.getElementById( 'vlm_command' );
171 if( value( 'vlm_vod_name' ) )
173 cmd.value = "new " + addunderscores( value( 'vlm_vod_name' ) )
176 if( checked( 'vlm_vod_enabled' ) )
178 cmd.value += " enabled";
181 if( value( 'vlm_vod_input' ) )
183 cmd.value += " input " + value( 'vlm_vod_input' );
186 if( value( 'vlm_vod_output' ) )
188 cmd.value += " output " + value( 'vlm_vod_output' );
197 function update_vlm_add_schedule()
199 cmd = document.getElementById( 'vlm_command' );
201 check_and_replace_int( 'vlm_schedule_year', '0000' );
202 check_and_replace_int( 'vlm_schedule_month', '00' );
203 check_and_replace_int( 'vlm_schedule_day', '00' );
204 check_and_replace_int( 'vlm_schedule_hour', '00' );
205 check_and_replace_int( 'vlm_schedule_minute', '00' );
206 check_and_replace_int( 'vlm_schedule_second', '00' );
207 check_and_replace_int( 'vlm_schedule_period_year', '0000' );
208 check_and_replace_int( 'vlm_schedule_period_month', '00' );
209 check_and_replace_int( 'vlm_schedule_period_day', '00' );
210 check_and_replace_int( 'vlm_schedule_period_hour', '00' );
211 check_and_replace_int( 'vlm_schedule_period_minute', '00' );
212 check_and_replace_int( 'vlm_schedule_period_second', '00' );
214 if( value( 'vlm_schedule_name' ) )
216 cmd.value = "new " + addunderscores( value( 'vlm_schedule_name' ) ) + " schedule";
218 if( checked( 'vlm_schedule_enabled' ) )
220 cmd.value += " enabled";
223 if( checked( 'vlm_schedule_now' ) )
225 cmd.value += " date now";
229 cmd.value += " date " + value( 'vlm_schedule_year' ) + "/" + value( 'vlm_schedule_month' ) + "/" + value( 'vlm_schedule_day' ) + '-' + value( 'vlm_schedule_hour' ) + ':' + value( 'vlm_schedule_minute' ) + ':' + value( 'vlm_schedule_second' );
232 if( checked( 'vlm_schedule_repeat' ) )
234 cmd.value += " period " + value( 'vlm_schedule_period_year' ) + "/" + value( 'vlm_schedule_period_month' ) + "/" + value( 'vlm_schedule_period_day' ) + '-' + value( 'vlm_schedule_period_hour' ) + ':' + value( 'vlm_schedule_period_minute' ) + ':' + value( 'vlm_schedule_period_second' );
236 if( value( 'vlm_schedule_repeat_times' ) != 0 )
238 cmd.value += " repeat " + (value( 'vlm_schedule_repeat_times' ) - 1 );
249 function clear_vlm_add()
251 document.getElementById( 'vlm_command' ).value = "";
252 document.getElementById( 'vlm_broadcast_name' ).value = "";
253 document.getElementById( 'vlm_vod_name' ).value = "";
256 function clear_children( elt )
259 while( elt.hasChildNodes() )
260 elt.removeChild( elt.firstChild );
263 function create_button( caption, action )
265 link = document.createElement( "input" );
266 link.setAttribute( 'type', 'button' );
267 link.setAttribute( 'onclick', action );
268 link.setAttribute( 'value', caption );
271 function create_option( caption, value )
273 opt = document.createElement( 'option' );
274 opt.setAttribute( 'value', value );
275 opt.appendChild( document.createTextNode( caption ) );
279 function parse_vlm_cmd()
281 if( req.readyState == 4 )
283 if( req.status == 200 )
285 vlm_answer = req.responseXML.documentElement;
286 error_tag = vlm_answer.getElementsByTagName( 'error' )[0];
287 vlme = document.getElementById( 'vlm_error' );
288 clear_children( vlme );
289 if( error_tag.hasChildNodes() )
291 vlme.appendChild( document.createTextNode( 'Error: ' + error_tag.firstChild.data ) );
292 vlme.style.color = "#f00";
296 vlme.appendChild( document.createTextNode( 'Command succesful (' + value( 'vlm_command' ) + ') ' ) );
297 vlme.style.color = "#0f0";
300 vlme.appendChild( create_button( 'clear', 'clear_children( document.getElementById( "vlm_error" ) );' ) );
307 function parse_vlm_elements()
309 if( req.readyState == 4 )
311 if( req.status == 200 )
313 vlmb = document.getElementById( 'vlm_broadcast_list' );
314 vlmv = document.getElementById( 'vlm_vod_list' );
315 vlms = document.getElementById( 'vlm_schedule_list' );
317 clear_children( vlmb );
318 clear_children( vlmv );
319 clear_children( vlms );
321 answer = req.responseXML.documentElement;
323 elt = answer.firstChild;
327 if( elt.nodeName == "broadcast" || elt.nodeName == "vod" )
329 nb = document.createElement( 'div' );
330 nb.setAttribute( 'class', 'list_element' );
331 if( elt.nodeName == "broadcast" )
333 vlmb.appendChild( nb );
337 vlmv.appendChild( nb );
339 nbname = document.createElement( 'b' );
340 nbname.appendChild( document.createTextNode( elt.getAttribute( 'name' ) ) );
341 nb.appendChild( nbname );
343 link = document.createElement( 'input' );
344 link.setAttribute( 'type', 'button' );
345 if( elt.getAttribute( 'enabled' ) == 'yes' )
347 nb.appendChild( document.createTextNode( " enabled " ) );
348 link.setAttribute( 'onclick', 'vlm_disable("'+elt.getAttribute( 'name' ) + '");' );
349 link.setAttribute( 'value', "Disable" );
353 nb.appendChild( document.createTextNode( " disabled " ) );
354 link.setAttribute( 'onclick', 'vlm_enable("'+elt.getAttribute( 'name' ) + '");' );
355 link.setAttribute( 'value', "Enable" );
357 nb.appendChild( link );
359 if( elt.nodeName == "broadcast" )
361 link = document.createElement( 'input' );
362 link.setAttribute( 'type', 'button' );
363 if( elt.getAttribute( 'loop' ) == 'yes' )
365 nb.appendChild( document.createTextNode( " loop " ) );
367 link.setAttribute( 'onclick', 'vlm_unloop("'+elt.getAttribute( 'name' ) + '");' );
368 link.setAttribute( 'value', "Un-loop" );
372 nb.appendChild( document.createTextNode( " play once " ) );
374 link.setAttribute( 'onclick', 'vlm_loop("'+elt.getAttribute( 'name' ) + '");' );
375 link.setAttribute( 'value', "Loop" );
377 nb.appendChild( link );
379 if( elt.getAttribute( 'enabled' ) == 'yes' )
381 nb.appendChild( document.createTextNode( " " ) );
382 nb.appendChild( create_button( 'Play', 'vlm_play("'+elt.getAttribute('name')+'");' ) );
385 nb.appendChild( document.createTextNode( " " ) );
386 nb.appendChild( create_button( 'Pause', 'vlm_pause("'+elt.getAttribute('name')+'");' ) );
388 nb.appendChild( document.createTextNode( " " ) );
389 nb.appendChild( create_button( 'Stop', 'vlm_stop("'+elt.getAttribute('name')+'");' ) );
392 nb.appendChild( document.createTextNode( " " ) );
393 nb.appendChild( create_button( 'Delete', 'vlm_delete("'+elt.getAttribute( 'name' ) + '");' ) );
395 list = document.createElement( "ul" );
396 /* begin input list */
397 inputs = elt.getElementsByTagName( 'input' );
398 for( i = 0; i < inputs.length; i++ )
400 item = document.createElement( "li" );
401 item.appendChild( document.createTextNode( "Input: " + inputs[i].firstChild.data + " " ) );
402 item.appendChild( create_button( "Delete", 'vlm_delete_input("' + elt.getAttribute( 'name' ) + '", '+(i+1)+' );' ) );
403 list.appendChild( item );
407 item = document.createElement( "li" );
408 text = document.createElement( "input" );
409 text.setAttribute( 'type', 'text' );
410 text.setAttribute( 'size', '40' );
411 text.setAttribute( 'id', 'vlm_elt_'+elt.getAttribute('name')+'_input' );
412 item.appendChild( text );
413 item.appendChild( document.createTextNode( ' ' ) );
414 item.appendChild( create_button( 'Edit', 'vlm_input_edit("vlm_elt_'+elt.getAttribute('name')+'_input");') );
415 item.appendChild( document.createTextNode( ' ' ) );
416 item.appendChild( create_button( 'Add input', 'vlm_add_input("'+elt.getAttribute('name')+'",document.getElementById("vlm_elt_'+elt.getAttribute('name')+'_input").value );' ) );
418 list.appendChild( item );
419 /* end of input list */
422 item = document.createElement( "li" );
423 outputelt = elt.getElementsByTagName( 'output' )[0];
424 if( outputelt.hasChildNodes() )
426 output = outputelt.firstChild.data;
432 item.appendChild( document.createTextNode( 'Output: ' ) );
433 text = document.createElement( "input" );
434 text.setAttribute( 'type', 'text' );
435 text.setAttribute( 'id', 'vlm_elt_'+elt.getAttribute('name')+'_output' );
436 text.setAttribute( 'value', output );
437 item.appendChild( text );
439 item.appendChild( document.createTextNode( ' ' ) );
441 item.appendChild( create_button( 'Edit', 'vlm_output_edit("vlm_elt_'+elt.getAttribute('name')+'_output");' ) );
442 item.appendChild( document.createTextNode( ' ' ) );
443 item.appendChild( create_button( 'Change output', 'vlm_output("'+elt.getAttribute( 'name' )+ '",document.getElementById("vlm_elt_'+elt.getAttribute( 'name' )+'_output").value);' ) );
444 list.appendChild( item );
447 /* begin options list */
448 options = elt.getElementsByTagName( 'option' );
449 for( i = 0; i < options.length; i++ )
451 item = document.createElement( "li" );
452 item.appendChild( document.createTextNode( "Option: " + options[i].firstChild.data ) );
453 list.appendChild( item );
457 item = document.createElement( "li" );
458 item.appendChild( document.createTextNode( ' ' ) );
459 text = document.createElement( "input" );
460 text.setAttribute( 'type', 'text' );
461 text.setAttribute( 'size', '40' );
462 text.setAttribute( 'id', 'vlm_elt_'+elt.getAttribute('name')+'_option' );
463 item.appendChild( text );
464 item.appendChild( document.createTextNode( ' ' ) );
465 item.appendChild( create_button( 'Add option', 'vlm_option("'+elt.getAttribute('name')+'",document.getElementById("vlm_elt_'+elt.getAttribute('name')+'_option").value );' ) );
467 list.appendChild( item );
470 nb.appendChild( list );
473 else if( elt.nodeName == "schedule" )
475 nb = document.createElement( 'div' );
476 nb.setAttribute( 'class', 'list_element' );
477 vlms.appendChild( nb );
479 nbname = document.createElement( 'b' );
480 nbname.appendChild( document.createTextNode( elt.getAttribute( 'name' ) ) );
481 nb.appendChild( nbname );
483 link = document.createElement( 'input' );
484 link.setAttribute( 'type', 'button' );
485 if( elt.getAttribute( 'enabled' ) == 'yes' )
487 nb.appendChild( document.createTextNode( " enabled " ) );
488 link.setAttribute( 'onclick', 'vlm_disable("'+elt.getAttribute( 'name' ) + '");' );
489 link.setAttribute( 'value', "Disable" );
493 nb.appendChild( document.createTextNode( " disabled " ) );
494 link.setAttribute( 'onclick', 'vlm_enable("'+elt.getAttribute( 'name' ) + '");' );
495 link.setAttribute( 'value', "Enable" );
497 nb.appendChild( link );
499 nb.appendChild( document.createTextNode( " " ) );
500 nb.appendChild( create_button( "Delete", 'vlm_delete("'+elt.getAttribute( 'name' ) + '");' ) );
502 list = document.createElement( 'ul' );
504 item = document.createElement( 'li' );
505 item.appendChild( document.createTextNode( "Date: " + elt.getAttribute( 'date' ) ) );
506 list.appendChild( item );
508 item = document.createElement( 'li' );
509 item.appendChild( document.createTextNode( "Period (in seconds): " + elt.getAttribute( 'period' ) ) );
510 list.appendChild( item );
512 item = document.createElement( 'li' );
513 if( elt.getAttribute( 'repeat' ) == -1 )
515 item.appendChild( document.createTextNode( "Number of repeats left: for ever" ) );
519 item.appendChild( document.createTextNode( "Number of repeats left: " + elt.getAttribute( 'repeat' ) ) );
521 list.appendChild( item );
523 commands = elt.getElementsByTagName( 'command' );
524 for( i = 0; i < commands.length; i++ )
526 item = document.createElement( "li" );
527 item.appendChild( document.createTextNode( "Command: " + commands[i].firstChild.data + " " ) );
528 list.appendChild( item );
531 item = document.createElement( 'li' );
532 sel = document.createElement( 'select' );
533 sel.setAttribute( 'id', 'vlm_elt_'+elt.getAttribute('name')+'_action' );
534 sel.setAttribute( 'onchange', 'vlm_schedule_type_change("'+elt.getAttribute('name')+'");');
535 sel.appendChild( create_option( 'play', 'play' ) );
536 sel.appendChild( create_option( 'pause', 'pause' ) );
537 sel.appendChild( create_option( 'stop', 'stop' ) );
538 sel.appendChild( create_option( 'seek', 'seek' ) );
539 sel.appendChild( create_option( '(other)', '' ) );
540 item.appendChild( sel );
542 item.appendChild( document.createTextNode( " " ) );
543 text = document.createElement( 'input' );
544 text.setAttribute( 'type', 'text' );
545 text.setAttribute( 'id', 'vlm_elt_'+elt.getAttribute('name')+'_name' );
546 text.setAttribute( 'size', '10' );
547 text.setAttribute( 'value', '(name)' );
548 text.setAttribute( 'onfocus', 'if( this.value == "(name)" ) this.value = "";' );
549 text.setAttribute( 'onblur', 'if( this.value == "" ) this.value = "(name)";' );
550 item.appendChild( text );
552 item.appendChild( document.createTextNode( " " ) );
553 text = document.createElement( 'input' );
554 text.setAttribute( 'type', 'text' );
555 text.setAttribute( 'id', 'vlm_elt_'+elt.getAttribute('name')+'_opt' );
556 text.setAttribute( 'size', '30' );
557 text.setAttribute( 'value', '(options)' );
558 text.setAttribute( 'onfocus', 'if( this.value == "(options)" ) this.value = "";' );
559 text.setAttribute( 'onblur', 'if( this.value == "" ) this.value = "(options)";' );
560 item.appendChild( text );
561 item.appendChild( document.createTextNode( " " ) );
562 item.appendChild( create_button( "Append command", 'vlm_schedule_append("' + elt.getAttribute( 'name' ) + '");') );
564 list.appendChild( item );
566 nb.appendChild( list );
567 vlm_schedule_type_change( elt.getAttribute('name') );
570 elt = elt.nextSibling;
576 function vlm_cmd( cmd )
578 loadXMLDoc( 'requests/vlm_cmd.xml?command='+cmd.replace(/\#/g, '%23'), parse_vlm_cmd );
581 function vlm_get_elements( )
583 loadXMLDoc( 'requests/vlm.xml', parse_vlm_elements );
586 /* helper functions */
588 function vlm_disable( name )
590 document.getElementById( 'vlm_command' ).value = "setup "+name+" disabled";
591 vlm_cmd( value( 'vlm_command' ) );
594 function vlm_enable( name )
596 document.getElementById( 'vlm_command' ).value = "setup "+name+" enabled";
597 vlm_cmd( value( 'vlm_command' ) );
600 function vlm_loop( name )
602 document.getElementById( 'vlm_command' ).value = "setup "+name+" loop";
603 vlm_cmd( value( 'vlm_command' ) );
606 function vlm_unloop( name )
608 document.getElementById( 'vlm_command' ).value = "setup "+name+" unloop";
609 vlm_cmd( value( 'vlm_command' ) );
612 function vlm_play( name )
614 document.getElementById( 'vlm_command' ).value = "control "+name+" play";
615 vlm_cmd( value( 'vlm_command' ) );
618 function vlm_pause( name )
620 document.getElementById( 'vlm_command' ).value = "control "+name+" pause";
621 vlm_cmd( value( 'vlm_command' ) );
624 function vlm_stop( name )
626 document.getElementById( 'vlm_command' ).value = "control "+name+" stop";
627 vlm_cmd( value( 'vlm_command' ) );
630 function vlm_delete( name )
632 document.getElementById( 'vlm_command' ).value = "del "+name;
633 vlm_cmd( value( 'vlm_command' ) );
636 function vlm_delete_input( name, num )
638 document.getElementById( 'vlm_command' ).value = "setup "+name+" inputdeln "+num;
639 vlm_cmd( value( 'vlm_command' ) );
642 function vlm_add_input( name, input )
644 document.getElementById( 'vlm_command' ).value = "setup "+name+" input "+input;
645 vlm_cmd( value( 'vlm_command' ) );
648 function vlm_output( name, output )
650 document.getElementById( 'vlm_command' ).value = "setup "+name+" output "+output;
651 vlm_cmd( value( 'vlm_command' ) );
654 function vlm_option( name, option )
656 document.getElementById( 'vlm_command' ).value = "setup "+name+" option "+option;
657 vlm_cmd( value( 'vlm_command' ) );
660 function vlm_schedule_append( name )
662 act = document.getElementById( 'vlm_elt_' + name + '_action' ).value;
663 document.getElementById( 'vlm_command' ).value = "setup " + name + " append ";
664 itemname = document.getElementById( 'vlm_elt_' + name + '_name' ).value;
665 if( itemname == "(name)" ) itemname = "";
666 opt = document.getElementById( 'vlm_elt_' + name + '_opt' ).value;
667 if( opt == "(options)" ) opt = "";
671 document.getElementById( 'vlm_command' ).value += opt;
675 document.getElementById( 'vlm_command' ).value += 'control ' + itemname + " " + act + " " + opt;
677 vlm_cmd( value( 'vlm_command' ) );
681 vlm_cmd( value( 'vlm_command' ) );