// ------------------------------------------------------------------------------------------------------------ /* Documentation: http://barney.wallst.com/wsodwiki/index.php/Content_Buffer ContentBuffer() generic data buffering object usage: var c = new ContentBuffer(); c.load({ url: "data.asp", method: "get", onload: a, debug: true }); function a (connection) { alert("x:" + connection.getResult()); }; */ // ------------------------------------------------------------------------------------------------------------ function Remoting_class() { this.connections = []; this.connectionsMax = 100; this.connectionsActive = 0; this.connectionsPending = []; this.debug = false; this.context; // window default contexting moved to context implementations this.connectionId = 0; if (arguments.length && typeof arguments[0] == "object") { this.context = arguments[0]; }; }; //Remoting_class.using("WSDOM.Serializer.3"); // ------------------------------------------------------------------------------------------------------------ // load(contentPackge) /* contentPackge: .url: page to load in the hidden buffer .onload: function reference that gets called on a successful buffer load [optional] .onerror: function reference that gets called on an unsuccessful buffer load [optional] .method: get|post [optional] .data: {name: value} list [optional] .[arbitrary]: optional arbitrary parameters that get passed to .callback; allows for state management with an asynch request */ // ------------------------------------------------------------------------------------------------------------ Remoting_class.prototype.load = function (contentPackage) { // data validation try { contentPackage.method = contentPackage.method.toLowerCase(); if (contentPackage.method != "get" && contentPackage.method != "post") { contentPackage.method = "post"; }; } catch (e) { contentPackage.method = "post"; }; if (!contentPackage.data) { contentPackage.data = {}; }; // enable cache flushing if (document.location.search.match(/\.\.nocache\.\.=on/i)) { contentPackage.data["..nocache.."] = "on"; }; // pass debugchartsrv var dbgChartSrv = document.location.search.match(/\.\.debugchartsrv\.\.=([a-zA-Z]+)/i); if (dbgChartSrv) { contentPackage.data["..debugchartsrv.."] = dbgChartSrv[1]; } if (contentPackage.protocolType && contentPackage.protocolType.toString().match(/JsonRPC/gi)) { return new JsonRPC_Connection_class(this, this.connectionId++, contentPackage); } else { return new Connection_class(this, this.connectionId++, contentPackage); } }; // ------------------------------------------------------------------------------------------------------------ // loadXMLHTTP() // ------------------------------------------------------------------------------------------------------------ Remoting_class.prototype._loadXMLHTTP = function (connection) { var thisConnection = connection; var contentPackage = thisConnection.contentPackage; function stateMonitor () { thisBuffer._monitorConnectionState(thisConnection); }; this.connectionsActive++; /* if (!this.connections.length) { if (!this.initConnection()) { if (this.debug || contentPackage.debug) { dbg("ContentBuffer initialization error", "unsupported", "red"); }; return; }; }; */ var dataPackage = null; var thisBuffer = this; thisConnection.active = true; //contentPackage.connectionId = this.connectionId++; // necessary? contentPackage.params = {}; if (typeof contentPackage.contentType == "string") { contentPackage.params["..contenttype.."] = contentPackage.contentType; }; if (this.debug || contentPackage.debug) { WSDOM.Console.startGroup("Remoting"); }; // uniquely identify contentbuffer requests contentPackage.params["..requester.."] = "ContentBuffer"; if (contentPackage.method == "post") { dataPackage = ""; WSDOM.Console.dir(contentPackage); dataPackage = "inputs=" + this.encode(WSDOM.Serializer.serialize(contentPackage.data)); for (var i in contentPackage.params) { dataPackage += (dataPackage.length ? "&" : "") + this.encode(i) + "=" + this.encode(contentPackage.params[i]); }; if (this.debug || contentPackage.debug) { WSDOM.Console.log("ContentBuffer post data", dataPackage); }; } else { /*for (var i in contentPackage.data) { contentPackage.url += (contentPackage.url.indexOf("?") == -1 ? "?" : "&") + this.encode(i) + "=" + this.encode(contentPackage.data[i]); };*/ contentPackage.url += (contentPackage.url.indexOf("?") == -1 ? "?" : "&") + "data=" + this.encode(WSDOM.Serializer.serialize(contentPackage.data)); }; if (this.debug || contentPackage.debug) { WSDOM.Console.log("ContentBuffer loading [" + thisConnection.connectionId + "]", contentPackage.url + " [" + contentPackage.method + "]"); }; if (this.debug || contentPackage.debug) { var debugUrl = contentPackage.url; if (dataPackage) { debugUrl += (debugUrl.indexOf("?") == -1 ? "?" : "&") + dataPackage; }; debugUrl = debugUrl.replace(/\&?\.\.[^\=\&]*\.\.\=[^\&]*/g, ""); if (debugUrl.indexOf("/") != 0 && debugUrl.indexOf("http") != 0) { var path = String(window.location).replace(/https*:\/\//, ""); debugUrl = path.substr(path.indexOf("/"), path.lastIndexOf("/") + 1 - path.indexOf("/")) + debugUrl; } WSDOM.Console.link("ContentBuffer URL", debugUrl); }; try { // netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); thisConnection.c.open(contentPackage.method.toUpperCase(), contentPackage.url, true); thisConnection.c.onreadystatechange = stateMonitor; } catch (e) { // alert(e); }; if (contentPackage.method == "post") { thisConnection.c.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); }; //thisConnection.contentPackage = contentPackage; //dbg(thisConnection.c.send(), "trying connectionId"); thisConnection.c.send(dataPackage); if (this.debug || contentPackage.debug) { WSDOM.Console.endGroup("Remoting"); }; //return thisConnection; }; // ------------------------------------------------------------------------------------------------------------ // monitorConnectionState() // ------------------------------------------------------------------------------------------------------------ Remoting_class.prototype._monitorConnectionState = function (connection) { try { if (connection.c.readyState == 4) { if (connection.c.status != 200) { /*if (this.debug || connection.contentPackage.debug) { WSDOM.Console.log("ContentBuffer load error", connection.c.status, "red"); WSDOM.Console.log("ContentBuffer result [" + connection.connectionId + "]", connection.c.responseText); };*/ try { var result = connection.c.responseText; } catch (e) { var result = null; }; connection.contentPackage.result = result; if (typeof connection.contentPackage.onerror == "function") { connection.contentPackage.onerror.apply(connection.context || window, [connection]); }; return; }; var responseType = connection.contentPackage.contentType || connection.c.getResponseHeader("Content-Type"); var result = null; if (responseType.match(/text\/html/g) || responseType.match(/text\/plain/g)) { result = connection.c.responseText; } else if (responseType.match(/text\/xml/g)) { result = connection.c.responseXML; } else if (responseType.match(/text\/javascript/g)) { try { result = connection.c.responseText; if (!connection.contentPackage.preventEval) { connection.context.__evalBuffer = function() { eval(result); } connection.context.__evalBuffer(); }; } catch (e) { if (this.debug || connection.contentPackage.debug) { WSDOM.Console.error("Remoting javascript eval error", e.message); WSDOM.Console.dir(e); //if (typeof dbgObject != "undefined"){ dbgObject(e); } }; }; } else if (responseType.match(/application\/json/gi)) { try { result = connection.c.responseText; result = WSDOM.Serializer.deserialize(result); } catch (e) { if (this.debug || connection.contentPackage.debug) { WSDOM.Console.error("Remoting Serializer Error", e.message); //if (typeof dbgObject != "undefined"){ dbgObject(e); } WSDOM.Console.dir(e); }; }; }; connection.contentPackage.result = result; if (this.debug || connection.contentPackage.debug) { // dbg("ContentBuffer result [" + connection.contentPackage.connectionId + "]", result.replace(/\/g, ">")); }; if (typeof connection.contentPackage.onload == "function") { connection.contentPackage.onload.apply(connection.context || window, [connection]); }; this.finishConnection(connection); }; } catch (e) { if (this.debug || connection.contentPackage.debug) { WSDOM.Console.error("state monitoring error", e.message); WSDOM.Console.dir(e); //dbg("state monitoring error", e.message, "red"); //if (typeof dbgObject != "undefined"){ dbgObject(e); } }; this.finishConnection(connection); }; }; // ------------------------------------------------------------------------------------------------------------ // isActive // ------------------------------------------------------------------------------------------------------------ Remoting_class.prototype.isActive = function() { for (var i=0; i