?? engine.js
字號:
if (callData.errorHandler == null) callData.errorHandler = DWREngine._errorHandler;
if (callData.warningHandler == null) callData.warningHandler = DWREngine._warningHandler;
// Save the callMetaData
DWREngine._handlersMap[id] = callData;
// Copy extra callData into the map
var data, prop;
for (prop in callData) {
data = callData[prop];
if (typeof data != "function") {
DWREngine._batch.map[prop] = "" + data;
}
}
// Add in the page and session ids
DWREngine._batch.map.httpSessionId = DWREngine._httpSessionId;
DWREngine._batch.map.scriptSessionId = DWREngine._scriptSessionId;
DWREngine._batch.map.page = window.location.pathname;
DWREngine._batch.map[prefix + "scriptName"] = scriptName;
DWREngine._batch.map[prefix + "methodName"] = methodName;
DWREngine._batch.map[prefix + "id"] = id;
// Serialize the parameters into batch.map
for (i = 0; i < params.length; i++) {
DWREngine._serializeAll(DWREngine._batch, [], params[i], prefix + "param" + i);
}
// Now we have finished remembering the call, we incr the call count
DWREngine._batch.map.callCount++;
if (singleShot) {
DWREngine.endBatch();
}
};
/**
* @private Poll the server to see if there is any data waiting
*/
DWREngine._poll = function(overridePath) {
if (!DWREngine._reverseAjax) {
return;
}
// Get a unique ID for this call
var random = Math.floor(Math.random() * 10001);
var id = (random + "_" + new Date().getTime()).toString();
// Create a batch object that describes how we are to call the server
var batch = {
completed:false,
map:{
callCount:1,
'c0-scriptName':'DWRSystem',
'c0-methodName':'poll',
'c0-id':id,
httpSessionId:DWREngine._httpSessionId,
scriptSessionId:DWREngine._scriptSessionId,
page:window.location.pathname
},
method:DWREngine._pollMethod,
isPoll:true,
verb:"POST",
async:true,
paramCount:0,
ids:[ id ],
path:(overridePath) ? overridePath : DWREngine._defaultPath,
preHooks:(DWREngine._preHook) ? [ DWREngine._preHook ] : [],
postHooks:(DWREngine._postHook) ? [ DWREngine._postHook ] : [],
timeout:0
};
// IE can't handle XHR polling, so we must back off to iframe
if (!window.XMLHttpRequest && DWREngine._pollMethod == DWREngine.XMLHttpRequest) {
batch.method = DWREngine.IFrame;
}
// Create an entry in the handlers map for what happens when the reply arrives
DWREngine._handlersMap[id] = {
errorHandler:DWREngine._errorHandler,
warningHandler:DWREngine._warningHandler,
callback:DWREngine._triggerNextPoll
};
// Send the data
DWREngine._sendData(batch);
DWREngine._batches[DWREngine._batches.length] = batch;
DWREngine._checkCometPoll();
};
/**
* Setup an interval to kick off the next poll
*/
DWREngine._triggerNextPoll = function(pause) {
setTimeout("DWREngine._poll()", pause);
}
/**
* @private Check for reverse Ajax activity
*/
DWREngine._checkCometPoll = function() {
if (DWREngine._pollComet) {
if (DWREngine._pollFrame) {
setTimeout("DWREngine._checkCometPoll()", DWREngine._pollCometInterval);
var text = DWREngine._getTextFromCometIFrame();
DWREngine._processCometResponse(text);
}
else if (DWREngine._pollReq) {
setTimeout("DWREngine._checkCometPoll()", DWREngine._pollCometInterval);
try {
var xhrtext = DWREngine._pollReq.responseText;
DWREngine._processCometResponse(xhrtext);
}
catch (ex) {
// IE complains for no good reason. Ignore
}
}
}
}
/**
* @private Extract the whole (executed an all) text from the current iframe
*/
DWREngine._getTextFromCometIFrame = function() {
var frameDocument;
if (DWREngine._pollFrame.contentDocument) {
frameDocument = DWREngine._pollFrame.contentDocument.defaultView.document;
}
else if (DWREngine._pollFrame.contentWindow) {
frameDocument = DWREngine._pollFrame.contentWindow.document
}
else {
return "";
}
var bodyNodes = frameDocument.getElementsByTagName("body");
if (bodyNodes == null || bodyNodes.length == 0) return "";
if (bodyNodes[0] == null) return "";
var text = bodyNodes[0].innerHTML.toString();
// IE plays silly-pants and adds <PRE>...</PRE> for some unknown reason
if (text.indexOf("<PRE>") == 0) text = text.substring(5, text.length - 7);
return text;
}
/**
* @private Some more text might have come in, test and execute the new stuff
*/
DWREngine._processCometResponse = function(response) {
if (DWREngine._pollCometSize != response.length) {
var firstStartTag = response.indexOf("//@DWR-START@", DWREngine._pollCometSize);
if (firstStartTag == -1) {
// There is no start tag so we ignore the rest
// DWRUtil.debug("Missing //@DWR-START@ Skipping: " + response.substring(DWREngine._pollCometSize));
DWREngine._pollCometSize = response.length;
}
else {
// if (firstStartTag != DWREngine._pollCometSize) {
// DWRUtil.debug("//@DWR-START@ not at start skipping: " + response.substring(DWREngine._pollCometSize, firstStartTag));
// }
var lastEndTag = response.lastIndexOf("//@DWR-END@");
if (lastEndTag != -1) {
var executeString = response.substring(firstStartTag + 13, lastEndTag);
// DWRUtil.debug(executeString);
eval(executeString);
// Skip the end tag too for next time
DWREngine._pollCometSize = lastEndTag + 11;
}
// else {
// DWRUtil.debug("Missing //@DWR-END@ Postponing: " + executeString);
// }
}
}
}
/**
* @private Actually send the block of data in the batch object.
*/
DWREngine._sendData = function(batch) {
// If the batch is empty, don't send anything
if (batch.map.callCount == 0) return;
// Call any pre-hooks
for (var i = 0; i < batch.preHooks.length; i++) {
batch.preHooks[i]();
}
batch.preHooks = null;
// Set a timeout
if (batch.timeout && batch.timeout != 0) {
batch.interval = setInterval(function() {
clearInterval(batch.interval);
DWREngine._abortRequest(batch);
}, batch.timeout);
}
// A quick string to help people that use web log analysers
var statsInfo;
if (batch.map.callCount == 1) {
statsInfo = batch.map["c0-scriptName"] + "." + batch.map["c0-methodName"] + ".dwr";
}
else {
statsInfo = "Multiple." + batch.map.callCount + ".dwr";
}
// Get setup for XMLHttpRequest if possible
if (batch.method == DWREngine.XMLHttpRequest) {
if (window.XMLHttpRequest) {
batch.req = new XMLHttpRequest();
}
// IE5 for the mac claims to support window.ActiveXObject, but throws an error when it's used
else if (window.ActiveXObject && !(navigator.userAgent.indexOf('Mac') >= 0 && navigator.userAgent.indexOf("MSIE") >= 0)) {
batch.req = DWREngine._newActiveXObject(DWREngine._XMLHTTP);
}
}
var query = "";
var prop;
// This equates to (batch.method = XHR && browser supports XHR)
if (batch.req) {
// Proceed using XMLHttpRequest
if (batch.async) {
batch.req.onreadystatechange = function() {
DWREngine._stateChange(batch);
};
}
// If we're polling, record this for monitoring
if (batch.isPoll) DWREngine._pollReq = batch.req;
// Workaround for Safari 1.x POST bug
var indexSafari = navigator.userAgent.indexOf('Safari/');
if (indexSafari >= 0) {
// So this is Safari, are we on 1.x? POST is broken
var version = navigator.userAgent.substring(indexSafari + 7);
var verNum = parseInt(version, 10);
if (verNum < 400) {
batch.verb == "GET";
}
}
if (batch.verb == "GET") {
// Some browsers (Opera/Safari2) seem to fail to convert the value
// of batch.map.callCount to a string in the loop below so we do it
// manually here.
batch.map.callCount = "" + batch.map.callCount;
for (prop in batch.map) {
var qkey = encodeURIComponent(prop);
var qval = encodeURIComponent(batch.map[prop]);
if (qval == "") {
if (DWREngine._warningHandler) {
DWREngine._warningHandler("Found empty qval for qkey=" + qkey);
}
}
query += qkey + "=" + qval + "&";
}
query += "jsessionid=" + DWREngine._httpSessionId;
try {
batch.req.open("GET", batch.path + "/plainjs/" + statsInfo + "?" + query, batch.async);
batch.req.send(null);
if (!batch.async) {
DWREngine._stateChange(batch);
}
}
catch (ex) {
DWREngine._handleMetaDataError(null, ex);
}
}
else {
for (prop in batch.map) {
if (typeof batch.map[prop] != "function") {
query += prop + "=" + batch.map[prop] + "\n";
}
}
try {
// This might include Safari too, but it shouldn't do any harm
// if (navigator.userAgent.indexOf('Gecko') >= 0) {
// batch.req.setRequestHeader('Connection', 'close');
// }
batch.req.open("POST", batch.path + "/plainjs/" + statsInfo, batch.async);
batch.req.setRequestHeader('Content-Type', 'text/plain');
batch.req.send(query);
if (!batch.async) {
DWREngine._stateChange(batch);
}
}
catch (ex) {
DWREngine._handleMetaDataError(null, ex);
}
}
}
else if (batch.method != DWREngine.ScriptTag) {
var idname = "dwr-if-" + batch.map["c0-id"];
// Proceed using iframe
batch.div = document.createElement('div');
batch.div.innerHTML = "<iframe src='javascript:void(0)' frameborder='0' width='0' height='0' id='" + idname + "' name='" + idname + "'></iframe>";
document.body.appendChild(batch.div);
batch.iframe = document.getElementById(idname);
batch.iframe.setAttribute('style', 'width:0px; height:0px; border:0px;');
// Settings that vary if we are polling
var baseUrl;
if (batch.isPoll) {
DWREngine._pollFrame = batch.iframe;
DWREngine._pollCometSize = 0;
baseUrl = batch.path + "/plainjs/" + statsInfo;
}
else {
baseUrl = batch.path + "/htmljs/" + statsInfo;
}
if (batch.verb == "GET") {
for (prop in batch.map) {
if (typeof batch.map[prop] != "function") {
query += encodeURIComponent(prop) + "=" + encodeURIComponent(batch.map[prop]) + "&";
}
}
query = query.substring(0, query.length - 1);
batch.iframe.setAttribute('src', baseUrl + "?" + query);
document.body.appendChild(batch.iframe);
}
else {
batch.form = document.createElement('form');
batch.form.setAttribute('id', 'dwr-form');
batch.form.setAttribute('action', baseUrl);
batch.form.setAttribute('target', idname);
batch.form.target = idname;
batch.form.setAttribute('method', 'post');
for (prop in batch.map) {
var formInput = document.createElement('input');
formInput.setAttribute('type', 'hidden');
formInput.setAttribute('name', prop);
formInput.setAttribute('value', batch.map[prop]);
batch.form.appendChild(formInput);
}
document.body.appendChild(batch.form);
batch.form.submit();
}
}
else {
if (!batch.scriptTagBase) {
DWREngine._handleError("Please specify the scriptTagBase property within the call data.");
return;
}
for (prop in batch.map) {
if (typeof batch.map[prop] != "function") {
query += encodeURIComponent(prop) + "=" + encodeURIComponent(batch.map[prop]) + "&";
}
}
query = query.substring(0, query.length - 1);
batch.script = document.createElement('script');
batch.script.id = "dwr-st-" + batch.map["c0-id"];
batch.script.src = batch.scriptTagBase + batch.path + "/plainjs/" + statsInfo + "?" + query;
document.body.appendChild(batch.script);
}
};
/**
* @private Called by XMLHttpRequest to indicate that something has happened
*/
DWREngine._stateChange = function(batch) {
if (!batch.completed && batch.req.readyState == 4) {
try {
var reply = batch.req.responseText;
var status = batch.req.status;
if (reply == null || reply == "") {
DWREngine._handleMetaDataError(null, "No data received from server");
return;
}
// DWR doesn't use response codes for failure conditions, so we should be able
// to delete this. If no one complains, we will.
// if (status != 200) {
// if (reply == null) reply = "Unknown error occured";
// DWREngine._handleMetaDataError(null, reply);
// return;
// }
var contentType = batch.req.getResponseHeader('Content-Type');
if (DWREngine._textHtmlHandler && contentType.match(/^text\/html/)) {
DWREngine._textHtmlHandler();
return;
}
// Comet replies might have already partially executed
if (batch.req == DWREngine._pollReq) {
DWREngine._processCometResponse(reply);
}
else {
// This should get us out of 404s etc.
if (reply.search("DWREngine._handle") == -1) {
DWREngine._handleMetaDataError(null, "Invalid reply from server");
return;
}
eval(reply);
}
// We're done. Clear up
DWREngine._clearUp(batch);
}
catch (ex) {
if (ex == null) ex = "Unknown error occured";
DWREngine._handleMetaDataError(null, ex);
}
finally {
// If there is anything on the queue waiting to go out, then send it.
// We don't need to check for ordered mode, here because when ordered mode
// gets turned off, we still process *waiting* batches in an ordered way.
if (DWREngine._batchQueue.length != 0) {
var sendbatch = DWREngine._batchQueue.shift();
DWREngine._sendData(sendbatch);
DWREngine._batches[DWREngine._batches.length] = sendbatch;
}
}
}
};
/**
* @private Called by reply scripts generated as a result of remote requests
* @param id The identifier of the call that we are handling a response for
* @param reply The data to pass to the callback function
*/
DWREngine._handleResponse = function(id, reply) {
// Clear this callback out of the list - we don't need it any more
var handlers = DWREngine._handlersMap[id];
DWREngine._handlersMap[id] = null;
// TODO: How can we do this - delete DWREngine._handlersMap.id
if (handlers) {
// Error handlers inside here indicate an error that is nothing to do
// with DWR so we handle them differently.
try {
if (handlers.callback) handlers.callback(reply);
}
catch (ex) {
DWREngine._handleMetaDataError(handlers, ex);
}
}
// Finalize the call for IFrame transport
var responseBatch = DWREngine._batches[DWREngine._batches.length-1];
if (responseBatch.method == DWREngine.IFrame) {
// Only finalize after the last call has been handled
if (responseBatch.map["c"+(responseBatch.map.callCount-1)+"-id"] == id) {
DWREngine._clearUp(responseBatch);
}
}
};
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -