htdocs/ajax-update.js
changeset 52 a8f0e99883d1
equal deleted inserted replaced
51:508400fc5282 52:a8f0e99883d1
       
     1 function update_stats()
       
     2 {
       
     3   var body = document.getElementsByTagName('body')[0];
       
     4   body.style.cursor = 'wait';
       
     5   ajaxGet('ajax-active.php', function()
       
     6     {
       
     7       if ( window.ajax.readyState == 4 && window.ajax.status == 200 )
       
     8       {
       
     9         document.getElementById('active-members').innerHTML = ajax.responseText;
       
    10       }
       
    11     });
       
    12   var images = document.getElementsByTagName('img');
       
    13   for ( var i = 0; i < images.length; i++ )
       
    14   {
       
    15     var image = images[i];
       
    16     if ( image.className.indexOf('graph') != -1 )
       
    17     {
       
    18       image.src = (String(image.src)).replace(/&seed=[0-9]+$/, '') + '&seed=' + Math.floor(Math.random() * 100000);
       
    19     }
       
    20   }
       
    21   window.setTimeout(function()
       
    22     {
       
    23       body.style.cursor = null;
       
    24     }, 250);
       
    25 }
       
    26 
       
    27 window.onload = function()
       
    28 {
       
    29   var ivl = parseFloat(readCookie('interval'));
       
    30   if ( ivl == 0 || isNaN(ivl) )
       
    31     ivl = 30.0;
       
    32   
       
    33   var textbox = document.getElementById('update_ivl');
       
    34   textbox.value = String(ivl);
       
    35   textbox.onkeyup = process_update_ivl;
       
    36   
       
    37   ivl = parseInt(ivl * 1000);
       
    38   
       
    39   window.ajax_update_ivl = window.setInterval('update_stats();', ivl);
       
    40 }
       
    41 
       
    42 function set_update_ivl(ivl)
       
    43 {
       
    44   window.clearInterval(ajax_update_ivl);
       
    45   createCookie('interval', ivl, 3650);
       
    46   ivl = parseInt(ivl * 1000);
       
    47   
       
    48   window.ajax_update_ivl = window.setInterval('update_stats();', ivl);
       
    49 }
       
    50 
       
    51 function process_update_ivl()
       
    52 {
       
    53   var val = parseFloat(this.value);
       
    54   
       
    55   if ( isNaN(val) || val < 5 )
       
    56     val = 10;
       
    57   
       
    58   set_update_ivl(val);
       
    59 }
       
    60 
       
    61 /**
       
    62  * Core AJAX library
       
    63  */
       
    64 
       
    65 function ajaxMakeXHR()
       
    66 {
       
    67   var ajax;
       
    68   if (window.XMLHttpRequest)
       
    69   {
       
    70     ajax = new XMLHttpRequest();
       
    71   }
       
    72   else
       
    73   {
       
    74     if (window.ActiveXObject)
       
    75     {           
       
    76       ajax = new ActiveXObject("Microsoft.XMLHTTP");
       
    77     }
       
    78     else
       
    79     {
       
    80       return false;
       
    81     }
       
    82   }
       
    83   return ajax;
       
    84 }
       
    85 
       
    86 function ajaxGet(uri, f, call_editor_safe) {
       
    87   window.ajax = ajaxMakeXHR();
       
    88   if ( !ajax )
       
    89   {
       
    90     return false;
       
    91   }
       
    92   ajax.onreadystatechange = f;
       
    93   ajax.open('GET', uri, true);
       
    94   ajax.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
       
    95   ajax.send(null);
       
    96 }
       
    97 
       
    98 function ajaxPost(uri, parms, f, call_editor_safe) {
       
    99   // Is the editor open?
       
   100   window.ajax = ajaxMakeXHR();
       
   101   if ( !ajax )
       
   102   {
       
   103     return false;
       
   104   }
       
   105   ajax.onreadystatechange = f;
       
   106   ajax.open('POST', uri, true);
       
   107   ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       
   108   // Setting Content-length in Safari triggers a warning
       
   109   if ( !is_Safari )
       
   110   {
       
   111     ajax.setRequestHeader("Content-length", parms.length);
       
   112   }
       
   113   ajax.setRequestHeader("Connection", "close");
       
   114   ajax.send(parms);
       
   115 }
       
   116 
       
   117 // Cookie manipulation
       
   118 function readCookie(name) {var nameEQ = name + "=";var ca = document.cookie.split(';');for(var i=0;i < ca.length;i++){var c = ca[i];while (c.charAt(0)==' ') c = c.substring(1,c.length);if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);}return null;}
       
   119 function createCookie(name,value,days){if (days){var date = new Date();date.setTime(date.getTime()+(days*24*60*60*1000));var expires = "; expires="+date.toGMTString();}else var expires = "";document.cookie = name+"="+value+expires+"; path=/";}
       
   120 function eraseCookie(name) {createCookie(name,"",-1);}
       
   121