Nicely working now with several small javascript widgets.
[adorno.git] / www / js / adorno.js
blob2cef1587e7b8d44740c9252f6f09146f9e48c10a
1 // Javascript for Adorno Music Player
3 var base_url = /(https?:\/\/[a-z0-9.-]+\/)/i.exec(document.URL)
4 base_url = base_url[0];
7 ////////////////////////////////////////////////////////////////
8 // Toggle the Artist/Album/Genre/Stream selection
9 ////////////////////////////////////////////////////////////////
10 function toggle_type() {
11   var type_span = document.getElementById('current_type');
12   var type = type_span.innerHTML.toLowerCase();
13   var old_type = type;
15   if ( type == 'artist' )       { type = 'album'; }
16   else if ( type == 'album' )   { type = 'stream'; }
17   else if ( type == 'stream' )  { type = 'genre'; }
18   else if ( type == 'genre' )   { type = 'artist'; }
20   type_span.innerHTML = type.substring(0,1).toUpperCase() + type.substring(1);
22   var alphabet = document.getElementById('alphabet').childNodes;
23   
24   var href;
25   var title;
26   var title_match = new RegExp('([0-9]+ )?' + old_type,'g');
28   for (var i = 0; i < alphabet.length; i++) {
29     href = alphabet[i].href;
30     alphabet[i].href = href.replace(old_type,type);
31 //    if ( i == 2 ) {
32 //      alert( href + "\n" + alphabet[i].href );
33 //    }
34     title = alphabet[i].title;
35     alphabet[i].title = title.replace(title_match,type);
36   }
38   var search_form = document.getElementById('search_form');
39   for (var i=0; i < search_form.elements.length; i++ ) {
40     if ( search_form.elements[i].name == 'type' ) {
41       search_form.elements[i].value = type;
42       break;
43     }
44   }
48 var status_req = new XMLHttpRequest();
49 var mytime;
50 var redo_queue = true;
52 function update_now_playing() {
53   var td = document.getElementById('now_playing');
54   status_req.open('GET', base_url + 'current.php', true);
55   status_req.onreadystatechange = function (aEvt) {
56     if ( status_req.readyState == 4 ) {
57       if ( status_req.status == 200 ) {
58         if ( td.innerHTML != status_req.responseText ) {
59           td.innerHTML = status_req.responseText;
60           var in_td = td.childNodes;
61           var title = '';
62           if ( in_td.length == 1 ) {
63             var nodecontent = status_req.responseText;
64             if ( nodecontent.match(/Nothing is currently playing/i) ) {
65               title = 'Adorno: stopped';
66             }
67             else if ( nodecontent.match(/paused/i) ) {
68               title = 'Adorno: paused';
69             }
70             in_td = in_td[0].childNodes;
71           }
72           for ( var i=0; i < in_td.length; i++ ) {
73             if ( in_td[i].innerHTML == undefined ) continue;
74             if ( title == '' )
75               title = in_td[i].innerHTML;
76             else
77               title = title + ' : ' + in_td[i].innerHTML;
78           }
79           document.title = title;
80           update_queue();
81         }
82         else if ( redo_queue )
83           update_queue();
84       }
85     }
86   };
87   status_req.send(null);
88   mytime=setTimeout('update_now_playing()',10000);
91 function update_queue() {
92   var td = document.getElementById('queue');
93   redo_queue = false;
94   status_req.open('GET', base_url + 'queue.php', true);
95   status_req.onreadystatechange = function (aEvt) {
96     if ( status_req.readyState == 4 ) {
97       if ( status_req.status == 200 ) {
98         td.innerHTML = status_req.responseText;
99       }
100       else
101         redo_queue = true;
102     }
103   };
104   status_req.send(null);
108 var last_action_result = '';
109 function act() {
110   var argv = act.arguments;
111   var argc = argv.length;
113   if ( argc < 1 || argc > 2 ) {
114     alert( 'act(action,details) - only 1 or two arguments, not ' + argc + ' please!' );
115     return;
116   }
118   var action = argv[0];
119   var details = '';
120   if ( argc == 2 ) details = argv[1];
122   var act_req = new XMLHttpRequest();
123   status_req.open('GET', base_url + 'action.php?action=' + action + details, true);
124   status_req.onreadystatechange = function (aEvt) {
125     if ( status_req.readyState == 4 ) {
126       if ( status_req.status == 200 ) {
127         last_action_result = 'success';
128         redo_queue = true;
129         update_now_playing();
130       }
131       else {
132         redo_queue = true;
133         last_action_result = 'failure';
134       }
135     }
136   };
137   status_req.send(null);
141 function enqueue(artist,album,track) {
142   var details = '&l=' + album;
143   if ( artist != '' ) details = details + '&a=' + artist;
144   if ( track != '' ) details = details + '&t=' + track;
145   act('enqueue',details);
149 mytime=setTimeout('update_now_playing()',1000);