2b101867b92b1e79409cecda7c0796f4d0108541
[vlc.git] / share / http / js / vlm.js
1 /*****************************************************************************
2  * vlm.js: VLC media player web interface
3  *****************************************************************************
4  * Copyright (C) 2005-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /* replace quotes and spaces by underscores */
25 function addunderscores( str ){ return str.replace(/\'|\"| /g, '_'); }
26
27 /**********************************************************************
28  * Input dialog functions
29  *********************************************************************/
30
31 function vlm_input_edit( dest )
32 {
33     document.getElementById( 'input_dest' ).value = dest;
34     show( 'input' );
35 }
36
37 function vlm_input_change()
38 {
39     hide( 'input' );
40     document.getElementById( value( 'input_dest' ) ).value = value( 'input_mrl' );
41 }
42
43 function vlm_output_edit( dest )
44 {
45     document.getElementById( 'sout_dest' ).value = dest;
46     show( 'sout' );
47 }
48
49 function vlm_output_change()
50 {
51     hide( 'sout' );
52     document.getElementById( value( 'sout_dest' ) ).value = value( 'sout_mrl' ).substr(6); /* substr <-> remove :sout= */
53 }
54
55 function hide_vlm_add()
56 {
57     document.getElementById( 'vlm_add_broadcast' ).style.display = 'none';
58     document.getElementById( 'vlm_add_vod' ).style.display = 'none';
59     document.getElementById( 'vlm_add_schedule' ).style.display = 'none';
60 }
61
62 function update_vlm_add_broadcast()
63 {
64     cmd = document.getElementById( 'vlm_command' );
65
66     if( value( 'vlm_broadcast_name' ) )
67     {
68         cmd.value = "new " + addunderscores( value( 'vlm_broadcast_name' ) )
69                     + " broadcast";
70
71         if( checked( 'vlm_broadcast_enabled' ) )
72         {
73             cmd.value += " enabled";
74         }
75         
76         if( checked( 'vlm_broadcast_loop' ) )
77         {
78             cmd.value += " loop";
79         }
80
81         if( value( 'vlm_broadcast_input' ) )
82         {
83             cmd.value += " input " + value( 'vlm_broadcast_input' );
84         }
85
86         if( value( 'vlm_broadcast_output' ) )
87         {
88             cmd.value += " output " + value( 'vlm_broadcast_output' );
89         }
90     }
91     else
92     {
93         cmd.value = "";
94     }
95 }
96
97 function update_vlm_add_vod()
98 {
99     cmd = document.getElementById( 'vlm_command' );
100
101     if( value( 'vlm_vod_name' ) )
102     {
103         cmd.value = "new " + addunderscores( value( 'vlm_vod_name' ) )
104                     + " vod";
105
106         if( checked( 'vlm_vod_enabled' ) )
107         {
108             cmd.value += " enabled";
109         }
110         
111         if( value( 'vlm_vod_input' ) )
112         {
113             cmd.value += " input " + value( 'vlm_vod_input' );
114         }
115
116         if( value( 'vlm_vod_output' ) )
117         {
118             cmd.value += " output " + value( 'vlm_vod_output' );
119         }
120     }
121     else
122     {
123         cmd.value = "";
124     }
125 }
126
127 function update_vlm_add_schedule()
128 {
129 }
130
131 function clear_vlm_add()
132 {
133     document.getElementById( 'vlm_command' ).value = "";
134     document.getElementById( 'vlm_broadcast_name' ).value = "";
135     document.getElementById( 'vlm_vod_name' ).value = "";
136 }
137
138 function clear_children( elt )
139 {
140     if( elt )
141         while( elt.hasChildNodes() )
142             elt.removeChild( elt.firstChild );
143 }
144
145 function parse_vlm_cmd()
146 {
147     if( req.readyState == 4 )
148     {
149         if( req.status == 200 )
150         {
151             vlm_answer = req.responseXML.documentElement;
152             error_tag = vlm_answer.getElementsByTagName( 'error' )[0];
153             vlme = document.getElementById( 'vlm_error' );
154             clear_children( vlme );
155             if( error_tag.hasChildNodes() )
156             {
157                 vlme.appendChild( document.createTextNode( 'Error: ' + error_tag.firstChild.data ) );
158                 vlme.style.color = "#f00";
159             }
160             else
161             {
162                 vlme.appendChild( document.createTextNode( 'Command succesful (' + value( 'vlm_command' ) + ') ' ) );
163                 vlme.style.color = "#0f0";
164                 clear_vlm_add();
165             }
166             link = document.createElement( "input" );
167             link.setAttribute( 'type', 'button' );
168             link.setAttribute( 'onclick', 'clear_children( document.getElementById( "vlm_error" ) );' );
169             link.setAttribute( 'value', 'clear' );
170             vlme.appendChild( link );
171
172             vlm_get_elements();
173         }
174     }
175 }
176
177 function parse_vlm_elements()
178 {
179     if( req.readyState == 4 )
180     {
181         if( req.status == 200 )
182         {
183             vlmb = document.getElementById( 'vlm_broadcast_list' );
184             vlmv = document.getElementById( 'vlm_vod_list' );
185             vlms = document.getElementById( 'vlm_schedule_list' );
186
187             clear_children( vlmb );
188             clear_children( vlmv );
189             clear_children( vlms );
190
191             answer = req.responseXML.documentElement;
192
193             elt = answer.firstChild;
194
195             while( elt )
196             {
197                 if( elt.nodeName == "broadcast" || elt.nodeName == "vod" )
198                 {
199                     nb = document.createElement( 'div' );
200                     nb.setAttribute( 'class', 'list_element' );
201                     if( elt.nodeName == "broadcast" )
202                     {
203                         vlmb.appendChild( nb );
204                     }
205                     else
206                     {
207                         vlmv.appendChild( nb );
208                     }
209                     nbname = document.createElement( 'b' );
210                     nbname.appendChild( document.createTextNode( elt.getAttribute( 'name' ) ) );
211                     nb.appendChild( nbname );
212                     
213                     link = document.createElement( 'input' );
214                     link.setAttribute( 'type', 'button' );
215                     if( elt.getAttribute( 'enabled' ) == 'yes' )
216                     {
217                         nb.appendChild( document.createTextNode( " enabled " ) );
218                         link.setAttribute( 'onclick', 'vlm_disable("'+elt.getAttribute( 'name' ) + '");' );
219                         link.setAttribute( 'value', "Disable" );
220                     }
221                     else
222                     {
223                         nb.appendChild( document.createTextNode( " disabled " ) );
224                         link.setAttribute( 'onclick', 'vlm_enable("'+elt.getAttribute( 'name' ) + '");' );
225                         link.setAttribute( 'value', "Enable" );
226                     }
227                     nb.appendChild( link );
228                     
229                     if( elt.nodeName == "broadcast" )
230                     {
231                         link = document.createElement( 'input' );
232                         link.setAttribute( 'type', 'button' );
233                         if( elt.getAttribute( 'loop' ) == 'yes' )
234                         {
235                             nb.appendChild( document.createTextNode( " loop " ) );
236
237                             link.setAttribute( 'onclick', 'vlm_unloop("'+elt.getAttribute( 'name' ) + '");' );
238                             link.setAttribute( 'value', "Un-loop" );
239                         }
240                         else
241                         {
242                             nb.appendChild( document.createTextNode( " play once " ) );
243                             
244                             link.setAttribute( 'onclick', 'vlm_loop("'+elt.getAttribute( 'name' ) + '");' );
245                             link.setAttribute( 'value', "Loop" );
246                         }
247                         nb.appendChild( link );
248
249                         if( elt.getAttribute( 'enabled' ) == 'yes' )
250                         {
251                             nb.appendChild( document.createTextNode( " " ) );
252                             link = document.createElement( 'input' );
253                             link.setAttribute( 'type', 'button' );
254                             link.setAttribute( 'onclick', 'vlm_play("'+elt.getAttribute('name')+'");' );
255                             link.setAttribute( 'value', 'Play' );
256                             nb.appendChild( link );
257                         }
258
259                         nb.appendChild( document.createTextNode( " " ) );
260                         link = document.createElement( 'input' );
261                         link.setAttribute( 'type', 'button' );
262                         link.setAttribute( 'onclick', 'vlm_pause("'+elt.getAttribute('name')+'");' );
263                         link.setAttribute( 'value', 'Pause' );
264                         nb.appendChild( link );
265
266                         nb.appendChild( document.createTextNode( " " ) );
267                         link = document.createElement( 'input' );
268                         link.setAttribute( 'type', 'button' );
269                         link.setAttribute( 'onclick', 'vlm_stop("'+elt.getAttribute('name')+'");' );
270                         link.setAttribute( 'value', 'Stop' );
271                         nb.appendChild( link );
272                     }
273                     
274                     nb.appendChild( document.createTextNode( " " ) );
275                     link = document.createElement( 'input' );
276                     link.setAttribute( 'type', 'button' );
277                     link.setAttribute( 'onclick', 'vlm_delete("'+elt.getAttribute( 'name' ) + '");' );
278                     link.setAttribute( 'value', "Delete" );
279                     nb.appendChild( link );
280
281                     list = document.createElement( "ul" );
282                     /* begin input list */
283                     inputs = elt.getElementsByTagName( 'input' );
284                     for( i = 0; i < inputs.length; i++ )
285                     {
286                         item = document.createElement( "li" );
287                         item.appendChild( document.createTextNode( "Input: " + inputs[i].firstChild.data + " " ) );
288                         link = document.createElement( "input" );
289                         link.setAttribute( 'type', 'button' );
290                         link.setAttribute( 'onclick', 'vlm_delete_input("' + elt.getAttribute( 'name' ) + '", '+(i+1)+' );' );
291                         link.setAttribute( 'value', "Delete" );
292                         item.appendChild( link );
293                         list.appendChild( item );
294                     }
295
296                     /* Add input */
297                     item = document.createElement( "li" );
298                     text = document.createElement( "input" );
299                     text.setAttribute( 'type', 'text' );
300                     text.setAttribute( 'size', '40' );
301                     text.setAttribute( 'id', 'vlm_elt_'+elt.getAttribute('name')+'_input' );
302                     item.appendChild( text );
303                     item.appendChild( document.createTextNode( ' ' ) );
304                     edit = document.createElement( "input" );
305                     edit.setAttribute( 'type', 'button' );
306                     edit.setAttribute( 'value', 'Edit' );
307                     edit.setAttribute( 'onclick', 'vlm_input_edit("vlm_elt_'+elt.getAttribute('name')+'_input");');
308                     item.appendChild( edit );
309                     item.appendChild( document.createTextNode( ' ' ) );
310                     link = document.createElement( "input" );
311                     link.setAttribute( 'type', 'button' );
312                     link.setAttribute( 'onclick', 'vlm_add_input("'+elt.getAttribute('name')+'",document.getElementById("vlm_elt_'+elt.getAttribute('name')+'_input").value );' );
313                     link.setAttribute( 'value', 'Add input' );
314                     item.appendChild( link );
315                     
316                     list.appendChild( item );
317                     /* end of input list */
318                     
319                     /* output */
320                     item = document.createElement( "li" );
321                     outputelt = elt.getElementsByTagName( 'output' )[0];
322                     if( outputelt.hasChildNodes() )
323                     {
324                         output = outputelt.firstChild.data;
325                     }
326                     else
327                     {
328                         output = "";
329                     }
330                     item.appendChild( document.createTextNode( 'Output: ' ) );
331                     text = document.createElement( "input" );
332                     text.setAttribute( 'type', 'text' );
333                     text.setAttribute( 'id', 'vlm_elt_'+elt.getAttribute('name')+'_output' );
334                     text.setAttribute( 'value', output );
335                     item.appendChild( text );
336
337                     item.appendChild( document.createTextNode( ' ' ) );
338
339                     edit = document.createElement( "input" );
340                     edit.setAttribute( 'type', 'button' );
341                     edit.setAttribute( 'value', 'Edit' );
342                     edit.setAttribute( 'onclick', 'vlm_output_edit("vlm_elt_'+elt.getAttribute('name')+'_output");');
343                     item.appendChild( edit );
344                     list.appendChild( item );
345                     item.appendChild( document.createTextNode( ' ' ) );
346                     link = document.createElement( "input" );
347                     link.setAttribute( 'type', 'button' );
348                     link.setAttribute( 'onclick', 'vlm_output("'+elt.getAttribute( 'name' )+ '",document.getElementById("vlm_elt_'+elt.getAttribute( 'name' )+'_output").value);' );
349                     link.setAttribute( 'value', 'Change output' );
350                     item.appendChild( link );
351                     /* end of output */
352
353                     /* begin options list */
354                     options = elt.getElementsByTagName( 'option' );
355                     for( i = 0; i < options.length; i++ )
356                     {
357                         item = document.createElement( "li" );
358                         item.appendChild( document.createTextNode( "Option: " + options[i].firstChild.data ) );
359                         list.appendChild( item );
360                     }
361
362                     /* Add option */
363                     item = document.createElement( "li" );
364                     item.appendChild( document.createTextNode( ' ' ) );
365                     text = document.createElement( "input" );
366                     text.setAttribute( 'type', 'text' );
367                     text.setAttribute( 'size', '40' );
368                     text.setAttribute( 'id', 'vlm_elt_'+elt.getAttribute('name')+'_option' );
369                     item.appendChild( text );
370                     item.appendChild( document.createTextNode( ' ' ) );
371                     link = document.createElement( "input" );
372                     link.setAttribute( 'type', 'button' );
373                     link.setAttribute( 'onclick', 'vlm_option("'+elt.getAttribute('name')+'",document.getElementById("vlm_elt_'+elt.getAttribute('name')+'_option").value );' );
374                     link.setAttribute( 'value', 'Add option' );
375                     item.appendChild( link );
376                     
377                     list.appendChild( item );
378                     /* end of options */
379                     
380                     nb.appendChild( list );
381                     
382                 }
383                 else if( elt.nodeName == "schedule" )
384                 {
385                 }
386                 elt = elt.nextSibling;
387             }
388         }
389     }
390 }
391
392 function vlm_cmd( cmd )
393 {
394     loadXMLDoc( 'requests/vlm_cmd.xml?command='+cmd.replace(/\#/g, '%23'), parse_vlm_cmd );
395 }
396
397 function vlm_get_elements( )
398 {
399     loadXMLDoc( 'requests/vlm.xml', parse_vlm_elements );
400 }
401
402 /* helper functions */
403
404 function vlm_disable( name )
405 {
406     document.getElementById( 'vlm_command' ).value = "setup "+name+" disabled";
407     vlm_cmd( value( 'vlm_command' ) );
408 }
409
410 function vlm_enable( name )
411 {
412     document.getElementById( 'vlm_command' ).value = "setup "+name+" enabled";
413     vlm_cmd( value( 'vlm_command' ) );
414 }
415
416 function vlm_loop( name )
417 {
418     document.getElementById( 'vlm_command' ).value = "setup "+name+" loop";
419     vlm_cmd( value( 'vlm_command' ) );
420 }
421
422 function vlm_unloop( name )
423 {
424     document.getElementById( 'vlm_command' ).value = "setup "+name+" unloop";
425     vlm_cmd( value( 'vlm_command' ) );
426 }
427
428 function vlm_play( name )
429 {
430     document.getElementById( 'vlm_command' ).value = "control "+name+" play";
431     vlm_cmd( value( 'vlm_command' ) );
432 }
433
434 function vlm_pause( name )
435 {
436     document.getElementById( 'vlm_command' ).value = "control "+name+" pause";
437     vlm_cmd( value( 'vlm_command' ) );
438 }
439
440 function vlm_stop( name )
441 {
442     document.getElementById( 'vlm_command' ).value = "control "+name+" stop";
443     vlm_cmd( value( 'vlm_command' ) );
444 }
445
446 function vlm_delete( name )
447 {
448     document.getElementById( 'vlm_command' ).value = "del "+name;
449     vlm_cmd( value( 'vlm_command' ) );
450 }
451
452 function vlm_delete_input( name, num )
453 {
454     document.getElementById( 'vlm_command' ).value = "setup "+name+" inputdeln "+num;
455     vlm_cmd( value( 'vlm_command' ) );
456 }
457
458 function vlm_add_input( name, input )
459 {
460     document.getElementById( 'vlm_command' ).value = "setup "+name+" input "+input;
461     vlm_cmd( value( 'vlm_command' ) );
462 }
463
464 function vlm_output( name, output )
465 {
466     document.getElementById( 'vlm_command' ).value = "setup "+name+" output "+output;
467     vlm_cmd( value( 'vlm_command' ) );
468 }
469
470 function vlm_option( name, option )
471 {
472     document.getElementById( 'vlm_command' ).value = "setup "+name+" option "+option;
473     vlm_cmd( value( 'vlm_command' ) );
474 }
475
476 function vlm_send( )
477 {
478     vlm_cmd( value( 'vlm_command' ) );
479 }