scripts/ajax.js
changeset 10 d3059e20b0fa
parent 6 5f35ebc4f9bb
child 11 0faea3a6c881
equal deleted inserted replaced
9:63a257541313 10:d3059e20b0fa
    12  */
    12  */
    13 
    13 
    14 var ajax;
    14 var ajax;
    15 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;
    15 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;
    16 
    16 
       
    17 var onload_hooks = new Array();
       
    18 
       
    19 function addOnloadHook(func)
       
    20 {
       
    21   if ( typeof ( func ) == 'function' )
       
    22   {
       
    23     if ( typeof(onload_hooks.push) == 'function' )
       
    24     {
       
    25       onload_hooks.push(func);
       
    26     }
       
    27     else
       
    28     {
       
    29       onload_hooks[onload_hooks.length] = func;
       
    30     }
       
    31   }
       
    32 }
       
    33 
       
    34 function runOnloadHooks(e)
       
    35 {
       
    36   var _errorTrapper = 0;
       
    37   for ( var _oLc = 0; _oLc < onload_hooks.length; _oLc++ )
       
    38   {
       
    39     _errorTrapper++;
       
    40     if ( _errorTrapper >= 1000 )
       
    41       break;
       
    42     var _f = onload_hooks[_oLc];
       
    43     if ( typeof(_f) == 'function' )
       
    44     {
       
    45       _f(e);
       
    46     }
       
    47   }
       
    48 }
       
    49 
    17 function ajaxGet(uri, f)
    50 function ajaxGet(uri, f)
    18 {
    51 {
    19   if (window.XMLHttpRequest)
    52   if (window.XMLHttpRequest)
    20   {
    53   {
    21     ajax = new XMLHttpRequest();
    54     ajax = new XMLHttpRequest();
   145         current_track_length = response.current_track_length;
   178         current_track_length = response.current_track_length;
   146         current_track_pos = response.current_track_pos;
   179         current_track_pos = response.current_track_pos;
   147         if ( ct_counter )
   180         if ( ct_counter )
   148           clearInterval(ct_counter);
   181           clearInterval(ct_counter);
   149         update_clock();
   182         update_clock();
   150         if ( is_playing )
   183         if ( is_playing && time_remaining > 0 )
   151         {
   184         {
   152           ct_advance_timeout = setTimeout(refresh_playlist, ( 1000 * time_remaining ));
   185           ct_advance_timeout = setTimeout(refresh_playlist, ( 1000 * time_remaining ));
   153           ct_counter = setInterval(update_clock, 1000);
   186           ct_counter = setInterval(update_clock, 1000);
   154         }
   187         }
   155         if ( first_load )
   188         if ( first_load )
   226         }
   259         }
   227       }
   260       }
   228     });
   261     });
   229 }
   262 }
   230 
   263 
       
   264 function set_playback_position(pos)
       
   265 {
       
   266   pos = Math.round(( pos / 100 ) * current_track_length);
       
   267   setAjaxLoading();
       
   268   if ( !allow_control )
       
   269     return false;
       
   270   ajaxGet('/action.json/seek/' + pos, function()
       
   271     {
       
   272       if ( ajax.readyState == 4 && ajax.status == 200 )
       
   273       {
       
   274         unsetAjaxLoading();
       
   275         current_track_pos = pos;
       
   276         update_clock();
       
   277       }
       
   278     });
       
   279 }
       
   280 
   231 function update_clock()
   281 function update_clock()
   232 {
   282 {
       
   283   posslide_set_position((100 * (current_track_pos / current_track_length)));
   233   var str = secs_to_string(current_track_pos) + '/' + secs_to_string(current_track_length);
   284   var str = secs_to_string(current_track_pos) + '/' + secs_to_string(current_track_length);
   234   $('playmeter').object.innerHTML = str;
   285   $('playmeter').object.innerHTML = str;
   235   current_track_pos++;
   286   current_track_pos++;
   236 }
   287 }
   237 
   288 
   254     str = '0' + str;
   305     str = '0' + str;
   255   }
   306   }
   256   return str;
   307   return str;
   257 }
   308 }
   258 
   309 
   259 window.onload = refresh_playlist;
       
   260 setInterval(refresh_playlist, 10000);
   310 setInterval(refresh_playlist, 10000);
   261 
   311 
       
   312 window.onload = function(e)
       
   313 {
       
   314   runOnloadHooks(e);
       
   315 }
       
   316 
       
   317 addOnloadHook(refresh_playlist);
       
   318