scripts/ajax.js
author Dan
Sun, 23 Mar 2008 23:55:50 -0400
changeset 8 a8d108f37363
parent 6 5f35ebc4f9bb
child 10 d3059e20b0fa
permissions -rw-r--r--
Modified to trap signals properly and not write to the root directory (only working directory)

/**
 * AJAX functions
 * 
 * Greyhound - real web management for Amarok
 * Copyright (C) 2008 Dan Fuhry
 *
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
 */

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, first_load = true;

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;
  $('ajax_status').object.style.display = 'block';
}

function unsetAjaxLoading()
{
  $('ajax_status').object.src = 'about:blank';
  $('ajax_status').object.style.display = 'none';
}

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
        is_playing = response.is_playing;
        if ( allow_control )
        {
          var img = $('btn_playpause').object.getElementsByTagName('img')[0];
          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);
        }
        if ( first_load )
        {
          first_load = false;
          var top = $('track_' + current_track).Top() - 138;
          window.scroll(0, top);
        }
      }
    });
}

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;
  if ( !allow_control )
    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);