scripts/ajax.js
changeset 2 860ba7141641
parent 0 c63de9eb7045
child 3 e7447a6044ec
--- a/scripts/ajax.js	Sun Mar 23 15:24:06 2008 -0400
+++ b/scripts/ajax.js	Sun Mar 23 20:24:33 2008 -0400
@@ -1,1 +1,244 @@
-// Hello world
+/**
+ * AJAX functions
+ * 
+ * Web control interface script for Amarok
+ * Written by Dan Fuhry - 2008
+ *
+ * This script is in the public domain. Use it for good, not evil.
+ */
+
+var ajax;
+var is_playing = false, current_track = -1, current_track_length, current_track_pos, ct_advance_timeout = false, ct_counter = false, playlist_md5 = false;
+
+function ajaxGet(uri, f)
+{
+  if (window.XMLHttpRequest)
+  {
+    ajax = new XMLHttpRequest();
+  }
+  else
+  {
+    if (window.ActiveXObject) {           
+      ajax = new ActiveXObject("Microsoft.XMLHTTP");
+    }
+    else
+    {
+      alert('AmaroK client-side runtime error: No AJAX support, unable to continue');
+      return;
+    }
+  }
+  ajax.onreadystatechange = f;
+  ajax.open('GET', uri, true);
+  ajax.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
+  ajax.send(null);
+}
+
+function ajaxPost(uri, parms, f)
+{
+  if (window.XMLHttpRequest)
+  {
+    ajax = new XMLHttpRequest();
+  }
+  else
+  {
+    if (window.ActiveXObject)
+    {           
+      ajax = new ActiveXObject("Microsoft.XMLHTTP");
+    }
+    else
+    {
+      alert('AmaroK client-side runtime error: No AJAX support, unable to continue');
+      return;
+    }
+  }
+  ajax.onreadystatechange = f;
+  ajax.open('POST', uri, true);
+  ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
+  // Setting Content-length in Safari triggers a warning
+  if ( !is_Safari )
+  {
+    ajax.setRequestHeader("Content-length", parms.length);
+  }
+  ajax.setRequestHeader("Connection", "close");
+  ajax.send(parms);
+}
+
+function setAjaxLoading()
+{
+  $('ajax_status').object.src = img_ajax;
+}
+
+function unsetAjaxLoading()
+{
+  $('ajax_status').object.src = 'about:blank';
+}
+
+var refresh_playlist = function()
+{
+  setAjaxLoading();
+  ajaxGet('/action.json/refresh', function()
+    {
+      if ( ajax.readyState == 4 && ajax.status == 200 )
+      {
+        unsetAjaxLoading();
+        var response = (' ' + ajax.responseText).substr(1);
+        // quickie JSON parser :)
+        response = eval('(' + response + ')');
+        // has the playlist been modified?
+        if ( playlist_md5 )
+        {
+          if ( response.playlist_hash != playlist_md5 )
+          {
+            // playlist has changed, reload
+            window.location.reload();
+            return false;
+          }
+        }
+        playlist_md5 = response.playlist_hash;
+        // update track number
+        if ( response.current_track != current_track )
+        {
+          var ot_id = 'track_' + current_track;
+          var nt_id = 'track_' + response.current_track;
+          current_track = response.current_track;
+          if ( $(ot_id).hasClass('current') )
+          {
+            $(ot_id).rmClass('current');
+          }
+          if ( ! $(nt_id).hasClass('current') )
+          {
+            $(nt_id).addClass('current');
+          }
+        }
+        // update playing status
+        var img = $('btn_playpause').object.getElementsByTagName('img')[0];
+        is_playing = response.is_playing;
+        if ( is_playing )
+        {
+          img.src = img_pause;
+        }
+        else
+        {
+          img.src = img_play;
+        }
+        // update volume
+        if ( response.volume != current_volume )
+        {
+          set_volume_fill(response.volume);
+          current_volume = response.volume;
+        }
+        // auto-refresh on track advance
+        if ( ct_advance_timeout )
+        {
+          clearTimeout(ct_advance_timeout);
+        }
+        var time_remaining = response.current_track_length - response.current_track_pos;
+        current_track_length = response.current_track_length;
+        current_track_pos = response.current_track_pos;
+        if ( ct_counter )
+          clearInterval(ct_counter);
+        update_clock();
+        if ( is_playing )
+        {
+          ct_advance_timeout = setTimeout(refresh_playlist, ( 1000 * time_remaining ));
+          ct_counter = setInterval(update_clock, 1000);
+        }
+      }
+    });
+}
+
+function player_action(action)
+{
+  var act2 = action;
+  setAjaxLoading();
+  ajaxGet('/action.json/' + action, function()
+    {
+      if ( ajax.readyState == 4 && ajax.status == 200 )
+      {
+        unsetAjaxLoading();
+        refresh_playlist();
+      }
+    });
+}
+
+function jump_to_song(tid)
+{
+  setAjaxLoading();
+  if ( tid == current_track )
+    return false;
+  ajaxGet('/action.json/jump/' + tid, function()
+    {
+      if ( ajax.readyState == 4 && ajax.status == 200 )
+      {
+        unsetAjaxLoading();
+        var response = (' ' + ajax.responseText).substr(1);
+        // quickie JSON parser :)
+        response = eval('(' + response + ')');
+        
+        // update track number
+        var ot_id = 'track_' + current_track;
+        var nt_id = 'track_' + tid;
+        current_track = tid;
+        if ( $(ot_id).hasClass('current') )
+        {
+          $(ot_id).rmClass('current');
+        }
+        if ( ! $(nt_id).hasClass('current') )
+        {
+          $(nt_id).addClass('current');
+        }
+        // update playing status
+        var img = $('btn_playpause').object.getElementsByTagName('img')[0];
+        is_playing = true;
+        img.src = img_play;
+        // auto-refresh on track advance
+        if ( ct_advance_timeout )
+        {
+          clearTimeout(ct_advance_timeout);
+        }
+        if ( ct_counter )
+          clearInterval(ct_counter);
+        var time_remaining = response.current_track_length - response.current_track_pos;
+        current_track_length = response.current_track_length;
+        current_track_pos = response.current_track_pos;
+        if ( is_playing )
+        {
+          ct_advance_timeout = setTimeout(refresh_playlist, ( 1000 * time_remaining ));
+          update_clock();
+          ct_counter = setInterval(update_clock, 1000);
+        }
+      }
+    });
+}
+
+function update_clock()
+{
+  var str = secs_to_string(current_track_pos) + '/' + secs_to_string(current_track_length);
+  $('playmeter').object.innerHTML = str;
+  current_track_pos++;
+}
+
+function secs_to_string(time)
+{
+  var count_seconds = time % 60;
+  var count_minutes = ( time - count_seconds ) / 60;
+  return fill_zeroes(count_minutes) + ':' + fill_zeroes(count_seconds);
+}
+
+function fill_zeroes(str, len)
+{
+  if ( !len )
+    len = 2;
+  if ( typeof(str) == 'number' && str == 0 )
+    str = '0';
+  str = String(str);
+  while ( str.length < len )
+  {
+    str = '0' + str;
+  }
+  return str;
+}
+
+window.onload = refresh_playlist;
+setInterval(refresh_playlist, 10000);
+