Web interface can now update with AJAX default tip
authorDan
Tue, 20 Jan 2009 22:08:17 -0500
changeset 52 a8f0e99883d1
parent 51 508400fc5282
Web interface can now update with AJAX
htdocs/ajax-active.php
htdocs/ajax-update.js
htdocs/index.php
modules/doctor.php
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/htdocs/ajax-active.php	Tue Jan 20 22:08:17 2009 -0500
@@ -0,0 +1,52 @@
+<?php
+
+require_once('../stats-fe.php');
+require_once('../timezone.php');
+
+if ( !isset($channel) )
+{
+  $channel_list = stats_channel_list();
+  $first_channel = $channel_list[0];
+  $channel = ( isset($_REQUEST['channel']) && in_array($_REQUEST['channel'], $channel_list) ) ? $_REQUEST['channel'] : $first_channel;
+}
+
+?>
+    <h1>Active members</h1>
+    <p>For the last 1, 5, and 15 minutes:
+        <?php echo count(stats_activity_percent($channel, 1)) . ', ' .
+                   count(stats_activity_percent($channel, 5)) . ', ' .
+                   count(stats_activity_percent($channel, 15)) . ' (respectively)';
+        ?>
+        </p>
+    <h1>Currently active members:</h1>
+    <p>These people have posted in the last 3 minutes:</p>
+    <ul>
+      <?php
+      $datum = stats_activity_percent($channel, 3);
+      $count = stats_message_count($channel, 3);
+      if ( empty($datum) )
+        echo '<li>No recent posts.</li>';
+      foreach ( $datum as $usernick => $pct )
+      {
+        $total = round($pct * $count);
+        $pct = round(100 * $pct, 1);
+        echo "<li>$usernick - $pct% ($total)</li>\n";
+      }
+      ?>
+    </ul>
+    <p>Last 20 minutes:</p>
+    <ul>
+      <?php
+      $datum = stats_activity_percent($channel, 20);
+      $count = stats_message_count($channel, 20);
+      if ( empty($datum) )
+        echo '<li>No recent posts.</li>';
+      foreach ( $datum as $usernick => $pct )
+      {
+        $total = round($pct * $count);
+        $pct = round(100 * $pct, 1);
+        echo "<li>$usernick - $pct% ($total)</li>\n";
+      }
+      ?>
+    </ul>
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/htdocs/ajax-update.js	Tue Jan 20 22:08:17 2009 -0500
@@ -0,0 +1,121 @@
+function update_stats()
+{
+  var body = document.getElementsByTagName('body')[0];
+  body.style.cursor = 'wait';
+  ajaxGet('ajax-active.php', function()
+    {
+      if ( window.ajax.readyState == 4 && window.ajax.status == 200 )
+      {
+        document.getElementById('active-members').innerHTML = ajax.responseText;
+      }
+    });
+  var images = document.getElementsByTagName('img');
+  for ( var i = 0; i < images.length; i++ )
+  {
+    var image = images[i];
+    if ( image.className.indexOf('graph') != -1 )
+    {
+      image.src = (String(image.src)).replace(/&seed=[0-9]+$/, '') + '&seed=' + Math.floor(Math.random() * 100000);
+    }
+  }
+  window.setTimeout(function()
+    {
+      body.style.cursor = null;
+    }, 250);
+}
+
+window.onload = function()
+{
+  var ivl = parseFloat(readCookie('interval'));
+  if ( ivl == 0 || isNaN(ivl) )
+    ivl = 30.0;
+  
+  var textbox = document.getElementById('update_ivl');
+  textbox.value = String(ivl);
+  textbox.onkeyup = process_update_ivl;
+  
+  ivl = parseInt(ivl * 1000);
+  
+  window.ajax_update_ivl = window.setInterval('update_stats();', ivl);
+}
+
+function set_update_ivl(ivl)
+{
+  window.clearInterval(ajax_update_ivl);
+  createCookie('interval', ivl, 3650);
+  ivl = parseInt(ivl * 1000);
+  
+  window.ajax_update_ivl = window.setInterval('update_stats();', ivl);
+}
+
+function process_update_ivl()
+{
+  var val = parseFloat(this.value);
+  
+  if ( isNaN(val) || val < 5 )
+    val = 10;
+  
+  set_update_ivl(val);
+}
+
+/**
+ * Core AJAX library
+ */
+
+function ajaxMakeXHR()
+{
+  var ajax;
+  if (window.XMLHttpRequest)
+  {
+    ajax = new XMLHttpRequest();
+  }
+  else
+  {
+    if (window.ActiveXObject)
+    {           
+      ajax = new ActiveXObject("Microsoft.XMLHTTP");
+    }
+    else
+    {
+      return false;
+    }
+  }
+  return ajax;
+}
+
+function ajaxGet(uri, f, call_editor_safe) {
+  window.ajax = ajaxMakeXHR();
+  if ( !ajax )
+  {
+    return false;
+  }
+  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, call_editor_safe) {
+  // Is the editor open?
+  window.ajax = ajaxMakeXHR();
+  if ( !ajax )
+  {
+    return false;
+  }
+  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);
+}
+
+// Cookie manipulation
+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;}
+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=/";}
+function eraseCookie(name) {createCookie(name,"",-1);}
+
--- a/htdocs/index.php	Tue Jan 20 22:08:07 2009 -0500
+++ b/htdocs/index.php	Tue Jan 20 22:08:17 2009 -0500
@@ -1,6 +1,6 @@
 <?php
-require('../stats-fe.php');
-require('../timezone.php');
+require_once('../stats-fe.php');
+require_once('../timezone.php');
 
 $channel_list = stats_channel_list();
 $first_channel = $channel_list[0];
@@ -9,6 +9,7 @@
 $title = "$nick - Statistics";
 require("./themes/$webtheme/header.php");
 ?>
+    <script type="text/javascript" src="ajax-update.js"></script>
     <div style="float: right;">
       <p>
         <?php
@@ -32,51 +33,21 @@
           }
         ?>
       </p>
+      <p>Update every <input id="update_ivl" size="3" value="..." style="text-align: center;" /> seconds<br />
+         <small>(Javascript required, min. 5 secs, default 30)</small><br />
+         </p>
     </div>
-    <h1>Active members</h1>
-    <p>For the last 1, 5, and 15 minutes:
-        <?php echo count(stats_activity_percent($channel, 1)) . ', ' .
-                   count(stats_activity_percent($channel, 5)) . ', ' .
-                   count(stats_activity_percent($channel, 15)) . ' (respectively)';
-        ?>
-        </p>
-    <h1>Currently active members:</h1>
-    <p>These people have posted in the last 3 minutes:</p>
-    <ul>
+    <div id="active-members">
       <?php
-      $datum = stats_activity_percent($channel, 3);
-      $count = stats_message_count($channel, 3);
-      if ( empty($datum) )
-        echo '<li>No recent posts.</li>';
-      foreach ( $datum as $usernick => $pct )
-      {
-        $total = round($pct * $count);
-        $pct = round(100 * $pct, 1);
-        echo "<li>$usernick - $pct% ($total)</li>\n";
-      }
+      require('ajax-active.php');
       ?>
-    </ul>
-    <p>Last 20 minutes:</p>
-    <ul>
-      <?php
-      $datum = stats_activity_percent($channel, 20);
-      $count = stats_message_count($channel, 20);
-      if ( empty($datum) )
-        echo '<li>No recent posts.</li>';
-      foreach ( $datum as $usernick => $pct )
-      {
-        $total = round($pct * $count);
-        $pct = round(100 * $pct, 1);
-        echo "<li>$usernick - $pct% ($total)</li>\n";
-      }
-      ?>
-    </ul>
+    </div>
     <h1>Last 60 minutes</h1>
-    <img alt="Graph image" src="graph.php?mode=lasthour&amp;channel=<?php echo urlencode($channel); ?>" />
+    <img class="graph" alt="Graph image" src="graph.php?mode=lasthour&amp;channel=<?php echo urlencode($channel); ?>" />
     <h1>Last 24 hours</h1>
-    <img alt="Graph image" src="graph.php?mode=lastday&amp;channel=<?php echo urlencode($channel); ?>" />
+    <img class="graph" alt="Graph image" src="graph.php?mode=lastday&amp;channel=<?php echo urlencode($channel); ?>" />
     <h1>Last 2 weeks</h1>
-    <img alt="Graph image" src="graph.php?mode=lastweek&amp;channel=<?php echo urlencode($channel); ?>" />
+    <img class="graph" alt="Graph image" src="graph.php?mode=lastweek&amp;channel=<?php echo urlencode($channel); ?>" />
 <?php
 require("./themes/$webtheme/footer.php");
 
--- a/modules/doctor.php	Tue Jan 20 22:08:07 2009 -0500
+++ b/modules/doctor.php	Tue Jan 20 22:08:17 2009 -0500
@@ -33,7 +33,7 @@
   
   if ( isset($doctor[$message['nick']]) && $message['message'] != '!doctor' )
   {
-    $chan->msg(eb_censor_words($doctor[$message['nick']]->listen($message['message'])));
+    $chan->msg(eb_censor_words("{$message['nick']}: " . $doctor[$message['nick']]->listen($message['message'])));
   }
 }