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