Mhttpd.js: Difference between revisions

From MidasWiki
Jump to navigation Jump to search
Line 26: Line 26:
= Functions =
= Functions =


function getMouseXY(e)
* function getMouseXY(e)
{
* function XMLHttpRequestGeneric()
  try {
      var x = e.pageX;
      var y = e.pageY;
      var p = 'abs: ' + x + '/' + y;
      i = document.getElementById('refimg');
      if (i == null)
        return false;
      document.body.style.cursor = 'crosshair';
      x -= i.offsetLeft;
      y -= i.offsetTop;
      while (i = i.offsetParent) {
        x -= i.offsetLeft;
        y -= i.offsetTop;
      }
      p += '  rel: ' + x + '/' + y;
      window.status = p;
      return true;
      }
  catch (e) {
      return false;
  }
}


function XMLHttpRequestGeneric()
* function ODBSet(path, value, pwdname)
{
* function ODBGet(path, format, defval, len, type)
  var request;
* function ODBMGet(paths, callback, formats)
  try {
* function ODBGetRecord(path)
      request = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
* function ODBExtractRecord(record, key)
  }
* function ODBKey(path)
  catch (e) {
* function ODBCopy(path, format)
      try {
* function ODBRpc_rev0(name, rpc, args)
        request = new ActiveXObject('Msxml2.XMLHTTP'); // Internet Explorer
* function ODBRpc_rev1(name, rpc, max_reply_length, args)
      }
* function ODBGetMsg(n)
      catch (e) {
* function ODBGenerateMsg(m)
        try {
* function ODBGetAlarms()
            request = new ActiveXObject('Microsoft.XMLHTTP');
* function ODBEdit(path)
        }
        catch (e) {
          alert('Your browser does not support AJAX!');
          return undefined;
        }
      }
  }
 
  return request;
}
 
function ODBSet(path, value, pwdname)
{
  var value, request, url;
 
  if (pwdname != undefined)
      pwd = prompt('Please enter password', '');
  else
      pwd = '';
 
  var request = XMLHttpRequestGeneric();
 
  url = '?cmd=jset&odb=' + path + '&value=' + value;
 
  if (pwdname != undefined)
      url += '&pnam=' + pwdname;
 
  request.open('GET', url, false);
 
  if (pwdname != undefined)
      request.setRequestHeader('Cookie', 'cpwd='+pwd);
 
  request.send(null);
 
  if (request.status != 200 || request.responseText != 'OK')
      alert('ODBSet error:\nPath: '+path+'\nHTTP Status: '+request.status+'\nMessage: '+request.responseText+'\n'+document.location) ;
}
 
function ODBGet(path, format, defval, len, type)
{
  var request = XMLHttpRequestGeneric();
 
  var url = '?cmd=jget&odb=' + path;
  if (format != undefined && format != '')
      url += '&format=' + format;
  request.open('GET', url, false);
  request.send(null);
 
  if (path.match(/[*]/)) {
      if (request.responseText == null)
        return null;
      if (request.responseText == '<DB_NO_KEY>') {
        url = '?cmd=jset&odb=' + path + '&value=' + defval + '&len=' + len + '&type=' + type;
 
        request.open('GET', url, false);
        request.send(null);
        return defval;
      } else {
        var array = request.responseText.split('\n');
        return array;
      }
  } else {
      if ((request.responseText == '<DB_NO_KEY>' ||
          request.responseText == '<DB_OUT_OF_RANGE>') && defval != undefined) {
        url = '?cmd=jset&odb=' + path + '&value=' + defval + '&len=' + len + '&type=' + type;
 
        request.open('GET', url, false);
        request.send(null);
        return defval;
      }
      return request.responseText.split('\n')[0];
  }
}
 
function ODBMGet(paths, callback, formats)
{
  var request = XMLHttpRequestGeneric();
 
  var url = '?cmd=jget';
  for (var i=0 ; i<paths.length ; i++) {
      url += '&odb'+i+'='+paths[i];
      if (formats != undefined && formats != '')
        url += '&format'+i+'=' + formats[i];
  }
 
  if (callback != undefined) {
      request.onreadystatechange = function()
        {
        if (request.readyState == 4) {
            if (request.status == 200) {
              var array = request.responseText.split('$#----#$\n');
              for (var i=0 ; i<array.length ; i++)
                  if (paths[i].match(/[*]/)) {
                    array[i] = array[i].split('\n');
                    array[i].length--;
                  } else
                    array[i] = array[i].split('\n')[0];
              callback(array);
            }
        }
      }
      request.open('GET', url, true);
  } else
      request.open('GET', url, false);
  request.send(null);
 
  if (callback == undefined) {
      var array = request.responseText.split('$#----#$\n');
      for (var i=0 ; i<array.length ; i++) {
        if (paths[i].match(/[*]/)) {
            array[i] = array[i].split('\n');
            array[i].length--;
        } else
            array[i] = array[i].split('\n')[0];
      }
      return array;
  }
}
 
function ODBGetRecord(path)
{
  var request = XMLHttpRequestGeneric();
 
  var url = '?cmd=jget&odb=' + path + '&name=1';
  request.open('GET', url, false);
  request.send(null);
  return request.responseText;
}
 
function ODBExtractRecord(record, key)
{
  var array = record.split('\n');
  for (var i=0 ; i<array.length ; i++) {
      var ind = array[i].indexOf(':');
      if (ind > 0) {
        var k = array[i].substr(0, ind);
        if (k == key)
            return array[i].substr(ind+1, array[i].length);
      }
      var ind = array[i].indexOf('[');
      if (ind > 0) {
        var k = array[i].substr(0, ind);
        if (k == key) {
            var a = new Array();
            for (var j=0 ; ; j++,i++) {
              if (array[i].substr(0, ind) != key)
                  break;
              var k = array[i].indexOf(':');
              a[j] = array[i].substr(k+1, array[i].length);
            }
            return a;
        }
      }
  }
  return null;
}
 
function ODBKey(path)
{
  var request = XMLHttpRequestGeneric();
 
  var url = '?cmd=jkey&odb=' + path;
  request.open('GET', url, false);
  request.send(null);
  if (request.responseText == null)
      return null;
  var res = request.responseText.split('\n');
  this.name = res[0];
  this.type = res[1];
  this.num_values = res[2];
  this.item_size = res[3];
  this.last_written = res[4];
}
 
function ODBCopy(path, format)
{
  var request = XMLHttpRequestGeneric();
 
  var url = '?cmd=jcopy&odb=' + path;
  if (format != undefined && format != '')
      url += '&format=' + format;
  request.open('GET', url, false);
  request.send(null);
  return request.responseText;
}
 
function ODBRpc_rev0(name, rpc, args)
{
  var request = XMLHttpRequestGeneric();
 
  var url = '?cmd=jrpc_rev0&name=' + name + '&rpc=' + rpc;
  for (var i = 2; i < arguments.length; i++) {
    url += '&arg'+(i-2)+'='+arguments[i];
  };
  request.open('GET', url, false);
  request.send(null);
  if (request.responseText == null)
      return null;
  this.reply = request.responseText.split('\n');
}
 
function ODBRpc_rev1(name, rpc, max_reply_length, args)
{
  var request = XMLHttpRequestGeneric();
 
  var url = '?cmd=jrpc_rev1&name=' + name + '&rpc=' + rpc + '&max_reply_length=' + max_reply_length;
  for (var i = 3; i < arguments.length; i++) {
    url += '&arg'+(i-3)+'='+arguments[i];
  };
  request.open('GET', url, false);
  request.send(null);
  if (request.responseText == null)
      return null;
  return request.responseText;
}
 
function ODBGetMsg(n)
{
  var request = XMLHttpRequestGeneric();
 
  var url = '?cmd=jmsg&n=' + n;
  request.open('GET', url, false);
  request.send(null);
 
  if (n > 1) {
      var array = request.responseText.split('\n');
      return array;
  } else
      return request.responseText;
}
 
function ODBGenerateMsg(m)
{
  var request = XMLHttpRequestGeneric();
 
  var url = '?cmd=jgenmsg&msg=' + m;
  request.open('GET', url, false);
  request.send(null);
  return request.responseText;
}
 
function ODBGetAlarms()
{
  var request = XMLHttpRequestGeneric();
  request.open('GET', '?cmd=jalm', false);
  request.send(null);
  var a = request.responseText.split('\n');
  a.length = a.length-1;
  return a;
}
 
function ODBEdit(path)
{
  var value = ODBGet(path);
  var new_value = prompt('Please enter new value', value);
  if (new_value != undefined) {
      ODBSet(path, new_value);
      window.location.reload();
  }
}

Revision as of 14:50, 12 August 2013

About

mhttpd.js contains javascript function wrappers for most MIDAS AJAX functions typically used for implementing interactive custom pages for MIDAS experiments.

mhttpd.js is typically served by mhttpd from $MIDASSYS/resources/mhttpd.js

<html>
<head>
...
<script src='mhttpd.js'></script>
...
</head>
...
</html>

A mostly up to date copy of mhttpd.js is linked here:

An example for using all mhttpd.js functions is included in the MIDAS distribution under examples/javascript1:

Functions

  • function getMouseXY(e)
  • function XMLHttpRequestGeneric()
  • function ODBSet(path, value, pwdname)
  • function ODBGet(path, format, defval, len, type)
  • function ODBMGet(paths, callback, formats)
  • function ODBGetRecord(path)
  • function ODBExtractRecord(record, key)
  • function ODBKey(path)
  • function ODBCopy(path, format)
  • function ODBRpc_rev0(name, rpc, args)
  • function ODBRpc_rev1(name, rpc, max_reply_length, args)
  • function ODBGetMsg(n)
  • function ODBGenerateMsg(m)
  • function ODBGetAlarms()
  • function ODBEdit(path)