Creating External Custom Interfaces

 
 
 

Showcase includes a sample JavaScript file that you can use to manage the XML/HTTP interactions between the remote Web interface and Showcase. This script ensures that onClick commands are interpreted correctly and sent to Showcase. The script is located in the extras\Ajax\js folder.

To use the script, add the following line to the interface html file within the header tag. The src attribute should point to the location of the actual xmlhttp.js file:

  <script language="Javascript" src="xmlhttp.js" />

xmlhttp.js

The following code is contained in xtras\Ajax\js\xmlhttp.js. Observe that xmlhttp.js is only a helper JavaScript file, and that the actual data passed to the Showcase webserver is an XML formatted string sent from an XMLHttpRequest object.

var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}


// This function accepts one or more messages for Showcase in the following form:
//	msg1||msg2||msg3||...
//
// and each message is formatted as:
//	msg_id::msg_param1::msg_param2::msg_param3::...
//
// some message parameters may be lists or tuples, in which case the portions of
// the parameter are separated by ,, .  But that formatting must be picked up in Showcase
// and is not dealt with here (i.e. in the client).
//
// Use || and :: and ,, as separators because we need to minimize the chances that a message
// parameter will contain this sequence.
//
// Warning: avoid the use of whitespace when calling this function!!
//
// The messages are sent to Showcase in XML, suitable for parsing by Showcase's XMLSocketInterpreter.
//
function send_request(request) {
	if (!xmlhttp) {
		alert("Error: browser does not support XmlHttp - unable to send messages");
		return;
	}

	var msgArray, xmlRequest, thisRequest, tempStr, dataValue;

	msgArray = String(request).split('||');

	xmlRequest = "<messages>";
	for (var i=0; i < msgArray.length; i++) {

		// Split the message into the message id and all its parameters.
		thisRequest = msgArray[i].split('::');

		xmlRequest += "<message id=\"" + thisRequest[0] + "\">";

		// Process all the message parameters.
		for(var j = 1; j < thisRequest.length; j ++) {
			tempStr = thisRequest[j];
			dataValue = "";
			// Check for invalid XML characters; if they exist, replace with the 
			// escape-charset equivalents.
			for(var k = 0; k < thisRequest[j].length; k ++) {
				if(tempStr.charAt(k) == "<") dataValue += "&lt;";
				else if(tempStr.charAt(k) == ">") dataValue += "&gt;";	
				else if(tempStr.charAt(k) == "'") dataValue += "&apos;";
				else if(tempStr.charAt(k) == "\"") dataValue += "&quot;";			
				else if(tempStr.charAt(k) == "\n") dataValue += "&#x0d;";
				else dataValue += tempStr.charAt(k);
			}
			xmlRequest += "<data value=\"" + dataValue + "\"/>";				
		}
		xmlRequest += "</message>";
	}
	xmlRequest += "</messages>";

	// Send the xml formatted message via XMLHttpRequest.
	//
	xmlhttp.open("MSG", xmlRequest, true);
	xmlhttp.setRequestHeader("Content-Type", "text/xml")
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status != 200) {
			alert("Error " + xmlhttp.status + ": Showcase unable to accept this command")
		}
	}
	xmlhttp.send(null);
}