scripts/ajax.js
changeset 2 860ba7141641
parent 0 c63de9eb7045
child 3 e7447a6044ec
equal deleted inserted replaced
1:cddc2ba706d6 2:860ba7141641
     1 // Hello world
     1 /**
       
     2  * AJAX functions
       
     3  * 
       
     4  * Web control interface script for Amarok
       
     5  * Written by Dan Fuhry - 2008
       
     6  *
       
     7  * This script is in the public domain. Use it for good, not evil.
       
     8  */
       
     9 
       
    10 var ajax;
       
    11 var is_playing = false, current_track = -1, current_track_length, current_track_pos, ct_advance_timeout = false, ct_counter = false, playlist_md5 = false;
       
    12 
       
    13 function ajaxGet(uri, f)
       
    14 {
       
    15   if (window.XMLHttpRequest)
       
    16   {
       
    17     ajax = new XMLHttpRequest();
       
    18   }
       
    19   else
       
    20   {
       
    21     if (window.ActiveXObject) {           
       
    22       ajax = new ActiveXObject("Microsoft.XMLHTTP");
       
    23     }
       
    24     else
       
    25     {
       
    26       alert('AmaroK client-side runtime error: No AJAX support, unable to continue');
       
    27       return;
       
    28     }
       
    29   }
       
    30   ajax.onreadystatechange = f;
       
    31   ajax.open('GET', uri, true);
       
    32   ajax.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
       
    33   ajax.send(null);
       
    34 }
       
    35 
       
    36 function ajaxPost(uri, parms, f)
       
    37 {
       
    38   if (window.XMLHttpRequest)
       
    39   {
       
    40     ajax = new XMLHttpRequest();
       
    41   }
       
    42   else
       
    43   {
       
    44     if (window.ActiveXObject)
       
    45     {           
       
    46       ajax = new ActiveXObject("Microsoft.XMLHTTP");
       
    47     }
       
    48     else
       
    49     {
       
    50       alert('AmaroK client-side runtime error: No AJAX support, unable to continue');
       
    51       return;
       
    52     }
       
    53   }
       
    54   ajax.onreadystatechange = f;
       
    55   ajax.open('POST', uri, true);
       
    56   ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       
    57   // Setting Content-length in Safari triggers a warning
       
    58   if ( !is_Safari )
       
    59   {
       
    60     ajax.setRequestHeader("Content-length", parms.length);
       
    61   }
       
    62   ajax.setRequestHeader("Connection", "close");
       
    63   ajax.send(parms);
       
    64 }
       
    65 
       
    66 function setAjaxLoading()
       
    67 {
       
    68   $('ajax_status').object.src = img_ajax;
       
    69 }
       
    70 
       
    71 function unsetAjaxLoading()
       
    72 {
       
    73   $('ajax_status').object.src = 'about:blank';
       
    74 }
       
    75 
       
    76 var refresh_playlist = function()
       
    77 {
       
    78   setAjaxLoading();
       
    79   ajaxGet('/action.json/refresh', function()
       
    80     {
       
    81       if ( ajax.readyState == 4 && ajax.status == 200 )
       
    82       {
       
    83         unsetAjaxLoading();
       
    84         var response = (' ' + ajax.responseText).substr(1);
       
    85         // quickie JSON parser :)
       
    86         response = eval('(' + response + ')');
       
    87         // has the playlist been modified?
       
    88         if ( playlist_md5 )
       
    89         {
       
    90           if ( response.playlist_hash != playlist_md5 )
       
    91           {
       
    92             // playlist has changed, reload
       
    93             window.location.reload();
       
    94             return false;
       
    95           }
       
    96         }
       
    97         playlist_md5 = response.playlist_hash;
       
    98         // update track number
       
    99         if ( response.current_track != current_track )
       
   100         {
       
   101           var ot_id = 'track_' + current_track;
       
   102           var nt_id = 'track_' + response.current_track;
       
   103           current_track = response.current_track;
       
   104           if ( $(ot_id).hasClass('current') )
       
   105           {
       
   106             $(ot_id).rmClass('current');
       
   107           }
       
   108           if ( ! $(nt_id).hasClass('current') )
       
   109           {
       
   110             $(nt_id).addClass('current');
       
   111           }
       
   112         }
       
   113         // update playing status
       
   114         var img = $('btn_playpause').object.getElementsByTagName('img')[0];
       
   115         is_playing = response.is_playing;
       
   116         if ( is_playing )
       
   117         {
       
   118           img.src = img_pause;
       
   119         }
       
   120         else
       
   121         {
       
   122           img.src = img_play;
       
   123         }
       
   124         // update volume
       
   125         if ( response.volume != current_volume )
       
   126         {
       
   127           set_volume_fill(response.volume);
       
   128           current_volume = response.volume;
       
   129         }
       
   130         // auto-refresh on track advance
       
   131         if ( ct_advance_timeout )
       
   132         {
       
   133           clearTimeout(ct_advance_timeout);
       
   134         }
       
   135         var time_remaining = response.current_track_length - response.current_track_pos;
       
   136         current_track_length = response.current_track_length;
       
   137         current_track_pos = response.current_track_pos;
       
   138         if ( ct_counter )
       
   139           clearInterval(ct_counter);
       
   140         update_clock();
       
   141         if ( is_playing )
       
   142         {
       
   143           ct_advance_timeout = setTimeout(refresh_playlist, ( 1000 * time_remaining ));
       
   144           ct_counter = setInterval(update_clock, 1000);
       
   145         }
       
   146       }
       
   147     });
       
   148 }
       
   149 
       
   150 function player_action(action)
       
   151 {
       
   152   var act2 = action;
       
   153   setAjaxLoading();
       
   154   ajaxGet('/action.json/' + action, function()
       
   155     {
       
   156       if ( ajax.readyState == 4 && ajax.status == 200 )
       
   157       {
       
   158         unsetAjaxLoading();
       
   159         refresh_playlist();
       
   160       }
       
   161     });
       
   162 }
       
   163 
       
   164 function jump_to_song(tid)
       
   165 {
       
   166   setAjaxLoading();
       
   167   if ( tid == current_track )
       
   168     return false;
       
   169   ajaxGet('/action.json/jump/' + tid, function()
       
   170     {
       
   171       if ( ajax.readyState == 4 && ajax.status == 200 )
       
   172       {
       
   173         unsetAjaxLoading();
       
   174         var response = (' ' + ajax.responseText).substr(1);
       
   175         // quickie JSON parser :)
       
   176         response = eval('(' + response + ')');
       
   177         
       
   178         // update track number
       
   179         var ot_id = 'track_' + current_track;
       
   180         var nt_id = 'track_' + tid;
       
   181         current_track = tid;
       
   182         if ( $(ot_id).hasClass('current') )
       
   183         {
       
   184           $(ot_id).rmClass('current');
       
   185         }
       
   186         if ( ! $(nt_id).hasClass('current') )
       
   187         {
       
   188           $(nt_id).addClass('current');
       
   189         }
       
   190         // update playing status
       
   191         var img = $('btn_playpause').object.getElementsByTagName('img')[0];
       
   192         is_playing = true;
       
   193         img.src = img_play;
       
   194         // auto-refresh on track advance
       
   195         if ( ct_advance_timeout )
       
   196         {
       
   197           clearTimeout(ct_advance_timeout);
       
   198         }
       
   199         if ( ct_counter )
       
   200           clearInterval(ct_counter);
       
   201         var time_remaining = response.current_track_length - response.current_track_pos;
       
   202         current_track_length = response.current_track_length;
       
   203         current_track_pos = response.current_track_pos;
       
   204         if ( is_playing )
       
   205         {
       
   206           ct_advance_timeout = setTimeout(refresh_playlist, ( 1000 * time_remaining ));
       
   207           update_clock();
       
   208           ct_counter = setInterval(update_clock, 1000);
       
   209         }
       
   210       }
       
   211     });
       
   212 }
       
   213 
       
   214 function update_clock()
       
   215 {
       
   216   var str = secs_to_string(current_track_pos) + '/' + secs_to_string(current_track_length);
       
   217   $('playmeter').object.innerHTML = str;
       
   218   current_track_pos++;
       
   219 }
       
   220 
       
   221 function secs_to_string(time)
       
   222 {
       
   223   var count_seconds = time % 60;
       
   224   var count_minutes = ( time - count_seconds ) / 60;
       
   225   return fill_zeroes(count_minutes) + ':' + fill_zeroes(count_seconds);
       
   226 }
       
   227 
       
   228 function fill_zeroes(str, len)
       
   229 {
       
   230   if ( !len )
       
   231     len = 2;
       
   232   if ( typeof(str) == 'number' && str == 0 )
       
   233     str = '0';
       
   234   str = String(str);
       
   235   while ( str.length < len )
       
   236   {
       
   237     str = '0' + str;
       
   238   }
       
   239   return str;
       
   240 }
       
   241 
       
   242 window.onload = refresh_playlist;
       
   243 setInterval(refresh_playlist, 10000);
       
   244