/*
termlib_socket.js - termlib.js Socket Extension v.1.02
applies to the termlib.js license (cf: )
(c) Norbert Landsteiner 2003-2007
mass:werk - media environments
load termlib.js first!
Syntax is JS 1.5 or higher (no NS4 compatibility)
# Synopsis:
Integrates async XMLHttpRequests (AJAX/JSON) tightly into termlib.js
# Example:
myTerm = new Terminal( { handler: myTermHandler } );
myTerm.open();
function myTermHandler() {
this.newLine();
if (this.lineBuffer == 'get file') {
myTerm.send(
{
url: 'myservice',
data: {
book: 'theBook',
chapter: 7,
page: 45
},
callback: myCallback
}
);
return;
}
else {
// ...
}
this.prompt();
}
function myCallback() {
if (this.socket.success) {
this.write(this.socket.responseText);
}
else {
this.write('OOPS: ' + this.socket.status + ' ' + this.socket.statusText);
if (this.socket.errno) {
this.newLine();
this.write('Error: ' + this.socket.errstring);
}
}
this.prompt();
}
# Documentation:
for usage and description see readme.txt chapter 13:
or refer to the sample page:
# Version History:
2007-03: v. 1.01
2007-04: v. 1.02 just a little clean up
*/
if (window.Terminal && typeof window.Terminal == 'function') {
Terminal.prototype._HttpSocket = function() {
var req=null;
if (window.XMLHttpRequest) {
try {
req=new XMLHttpRequest();
}
catch(e) {}
}
else if (window.ActiveXObject) {
var prtcls=this._msXMLHttpObjects;
for (var i=0; i1) this.prototype._msXMLHttpObjects= [ prtcls[i] ];
break;
}
}
catch(e) {}
}
}
this.request=req;
this.url;
this.data=null;
this.query='';
this.timeoutTimer=null;
this.localMode=Boolean(window.location.href.search(/^file:/i)==0);
this.error=0;
}
Terminal.prototype._HttpSocket.prototype = {
version: '1.02',
// config
useXMLEncoding: false, // use ";" as separator if true, "&" else
defaulTimeout: 10000, // request timeout in ticks (milliseconds)
defaultMethod: 'GET',
forceNewline: true, // translate line-breaks in responseText to newlines
// static const
errno: {
OK: 0,
NOTIMPLEMENTED: 1,
FATALERROR: 2,
TIMEOUT: 3,
NETWORKERROR: 4,
LOCALFILEERROR: 5
},
errstring: [
'',
'XMLHttpRequest not implemented.',
'Could not open XMLHttpRequest.',
'The connection timed out.',
'Network error.',
'The requested local document was not found.'
],
// private static data
_msXMLHttpObjects: [
'Msxml2.XMLHTTP',
'Microsoft.XMLHTTP',
'Msxml2.XMLHTTP.5.0',
'Msxml2.XMLHTTP.4.0',
'Msxml2.XMLHTTP.3.0'
],
// internal methods
serializeData: function() {
this.query=this.serialize(this.data);
},
serialize: function(data) {
var v='';
if( data != null ) {
switch (typeof data) {
case 'object':
var d=[];
if (data instanceof Array) {
// array
for (var i=0; i= 200) && (r.status < 300)) {
success=true;
}
else if (r.status >= 12000) {
// MSIE network error
failed=true;
this.error=this.errno.NETWORKERROR;
}
}
}
}
catch(e) {}
if (!failed) {
response.status=r.status;
response.statusText= (r.status==404)? 'Not Found':r.statusText; // force correct header
response.responseText=r.responseText;
response.responseXML=r.responseXML;
if (this.getHeaders) {
if (this.getHeaders instanceof Array) {
for (var i=0; i