AjaxCallObject.js
来源:互联网 发布:佛山市发发鱼网站源码 编辑:程序博客网 时间:2024/05/16 11:05
__AJAXCboList = new Array();
__bPageIsStored = false;
__bUnloadStoredPage = false;
__bTracing = false;
__PreviousOnFormSubmit = null;
__PreviousPostBack = null;
__PreviousOnPageLoad = null;
__PreviousOnPageBeforeUnload = null;
__PreviousOnPageUnload = null;
__TraceWindows = new Array();
__ClockID = 0;
function AjaxCallObject()
{
this.Init();
}
AjaxCallObject.prototype.Init = function()
{
this.XmlHttp = this.GetHttpObject();
}
AjaxCallObject.prototype.GetHttpObject = function()
{
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
AjaxCallObject.prototype.GetAjaxCallType = function(element)
{
if (element == null)
return "none";
var attrib = element.getAttribute("AjaxCall");
if (attrib != null)
return attrib.toLowerCase();
if (element.parentNode == null || element.parentNode == document.body)
return "none";
else
return this.GetAjaxCallType(element.parentNode);
}
AjaxCallObject.prototype.HookAjaxCall = function(bPageIsStored, bUnloadStoredPage, bTracing)
{
__PreviousOnFormSubmit = document.forms[0].onsubmit;
document.forms[0].onsubmit = this.OnFormSubmit;
if (typeof __doPostBack != 'undefined')
{
__PreviousPostBack = __doPostBack;
__doPostBack = this.DoPostBack;
}
__bPageIsStored = bPageIsStored;
__bUnloadStoredPage = bUnloadStoredPage;
__bTracing = bTracing;
if (typeof(RBS_Controls) != "undefined")
{
for (var i=0; i < RBS_Controls.length; i++)
RBS_Controls_Store[i].setAttribute("ExcludeFromPost", "true");
}
if ( !bPageIsStored || !bUnloadStoredPage )
{
__PreviousOnPageLoad = window.onload;
window.onload = this.OnPageLoad;
__PreviousOnPageBeforeUnload = window.onbeforeunload;
window.onbeforeunload = this.OnPageBeforeUnload;
}
__PreviousOnPageUnload = window.onunload;
window.onunload = this.OnPageUnload;
}
AjaxCallObject.prototype.OnFormSubmit = function()
{
// Empty the cached html of RenderedByScript controls
if (typeof(RBS_Controls) != "undefined")
{
for (var i=0; i < RBS_Controls.length; i++)
RBS_Controls_Store[i].value = "";
}
var target;
if ("activeElement" in document)
{
// Internet Explorer
target = document.activeElement;
}
else
{
// Firefox
target = arguments[0] ? arguments[0].explicitOriginalTarget : null;
}
if (__PreviousOnFormSubmit != null)
if ( __PreviousOnFormSubmit() == false )
return false;
var cbType = AJAXCbo.GetAjaxCallType(target);
if (cbType != "none")
{
AJAXCbo.DoAjaxCall(target.name, "", cbType);
return false;
}
else
{
AJAXCbo.ClearTracingWindows();
return true;
}
}
// Replaces normal __doPostBack
AjaxCallObject.prototype.DoPostBack = function(eventTarget, eventArgument)
{
// Empty the cached html of RenderedByScript controls
if (typeof(RBS_Controls) != "undefined")
{
for (var i=0; i < RBS_Controls.length; i++)
RBS_Controls_Store[i].value = "";
}
var target;
var elemUniqueID = eventTarget.split("$").join(":");
var ids = elemUniqueID.split(":");
// Checks the unique id and its parents until it finds a target element
// i.e. for ajaxPanel_grid:row:field it checks
// ajaxPanel_grid_row_field
// ajaxPanel_grid_row
// ajaxPanel_grid
for (var num=ids.length; num > 0; num--)
{
var elemID = "";
for (var i=0; i < num; i++)
elemID += (i==0 ? "" : "_") + ids[i];
target = document.getElementById(elemID);
if (target != null)
break;
}
var cbType = AJAXCbo.GetAjaxCallType(target);
if (cbType != "none")
{
AJAXCbo.DoAjaxCall(eventTarget, eventArgument, cbType);
}
else
{
if (__PreviousPostBack != null)
{
__PreviousPostBack(eventTarget, eventArgument);
}
}
}
AjaxCallObject.prototype.OnPageLoad = function()
{
// Restore the html of RenderedByScript controls
if (typeof(RBS_Controls) != "undefined")
{
for (var i=0; i < RBS_Controls.length; i++)
{
var html = RBS_Controls_Store[i].value;
if (html != "")
{
RBS_Controls[i].innerHTML = html.substring(5, html.length);
RBS_Controls_Store[i].value = "";
}
}
}
if (__PreviousOnPageLoad != null)
return __PreviousOnPageLoad();
}
AjaxCallObject.prototype.OnPageBeforeUnload = function()
{
// Save the html of RenderedByScript controls, so that it can be restored for the
// browser's "Back Button"
if (typeof(RBS_Controls) != "undefined")
{
for (var i=0; i < RBS_Controls.length; i++)
RBS_Controls_Store[i].value = "HTML:" + RBS_Controls[i].innerHTML;
}
if (__PreviousOnPageBeforeUnload != null)
return __PreviousOnPageBeforeUnload();
}
AjaxCallObject.prototype.OnPageUnload = function()
{
AJAXCbo.ClearTracingWindows();
if ( !__bPageIsStored || !__bUnloadStoredPage )
return;
if (document.forms[0]["__AJAX_PAGEKEY"] == null)
return;
var thePage = document.forms[0].action;
var index = thePage.indexOf("?");
if (index != -1)
thePage = thePage.substring(0, index);
thePage = thePage + "?__AJAX_PAGEUNLOAD=" + encodeURIComponent(document.forms[0]["__AJAX_PAGEKEY"].value);
var oThis = AJAXCbo;
__AJAXCboList.push(oThis);
AJAXCbo = new AjaxCallObject();
if( oThis.XmlHttp )
{
oThis.XmlHttp.open('GET', thePage, true);
oThis.XmlHttp.onreadystatechange = function(){ };
oThis.XmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
oThis.XmlHttp.send(null);
}
if (__PreviousOnPageUnload != null)
return __PreviousOnPageUnload();
}
AjaxCallObject.prototype.DoAjaxCall = function(eventTarget, eventArgument, ajaxCallType)
{
var theData = '';
var theform = document.forms[0];
var thePage = theform.action;
var eName = '';
theData = '__EVENTTARGET=' + this.EncodePostData(eventTarget.split("$").join(":")) + '&';
theData += '__EVENTARGUMENT=' + this.EncodePostData(eventArgument) + '&';
theData += '__AJAXCALL=true&';
if ( ! __bPageIsStored )
theData += '__VIEWSTATE=' + this.EncodePostData(theform.__VIEWSTATE.value) + '&';
for( var i=0; i<theform.elements.length; i++ )
{
eName = theform.elements[i].name;
if( eName && eName != '')
{
if( eName == '__EVENTTARGET' || eName == '__EVENTARGUMENT' || eName == '__VIEWSTATE' )
{
// Do Nothing
}
else if (theform.elements[i].getAttribute("ExcludeFromPost") == null || theform.elements[i].getAttribute("ExcludeFromPost").toLowerCase() != "true")
{
var type = theform.elements[i].type;
var val = theform.elements[i].value;
if ( type == "submit" || type == "button" )
continue;
if ( type == "textarea" )
{
// Firefox has the bad habit of altering the "/r/n" of a textarea to "/n",
// so convert all single "/n" to "/r/n"
val = val.split("/r/n").join("/n").split("/n").join("/r/n");
}
val = this.EncodePostData(val);
if ( type == "select-multiple" || type == "select-one" )
{
for (var j=0; j < theform.elements[i].options.length; j++)
if (theform.elements[i].options[j].selected)
theData = theData + this.EncodePostData(eName) + '=' + this.EncodePostData(theform.elements[i].options[j].value) + '&';
}
else if ( (type != "checkbox" && type != "radio") || theform.elements[i].checked )
{
theData = theData + this.EncodePostData(eName) + '=' + val;
if( i != theform.elements.length - 1 )
theData = theData + '&';
}
}
}
}
if (theData.substr(theData.length-1) == "&")
theData = theData.substr(0, theData.length-1);
if( this.XmlHttp )
{
if (waitElement)
{
waitElement.style.visibility = 'visible';
MoveWaitElement();
}
var oThis = this;
__AJAXCboList.push(oThis);
AJAXCbo = new AjaxCallObject();
if (__bTracing)
{
this.CreateTracingWindow();
this.TraceSentData(theData);
}
if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 )
{
if ( ! ajaxCallType || ajaxCallType.toLowerCase() != "sync")
{
// Asynchronous
this.XmlHttp.open("POST", thePage, true);
this.XmlHttp.onreadystatechange = function(){ oThis.ReadyStateChange(); };
this.XmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
this.XmlHttp.send(theData);
}
else
{
// Synchronous
// Use a timeout so that the screen refreshes before getting stack waiting the AjaxCall.
window.setTimeout(
function()
{
oThis.XmlHttp.open("POST", thePage, false);
oThis.XmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
oThis.XmlHttp.send(theData);
if( oThis.XmlHttp.status == 200 && oThis.XmlHttp.statusText == "OK" )
oThis.OnComplete(oThis.XmlHttp.responseText, oThis.XmlHttp.responseXML);
else
oThis.OnError(oThis.XmlHttp.status, oThis.XmlHttp.statusText, oThis.XmlHttp.responseText);
}, 1);
}
}
}
}
AjaxCallObject.prototype.OnLoading = function()
{
// Loading
}
AjaxCallObject.prototype.OnLoaded = function()
{
// Loaded
}
AjaxCallObject.prototype.OnInteractive = function()
{
// Interactive
}
AjaxCallObject.prototype.OnComplete = function(responseText, responseXml)
{
if (__bTracing)
{
this.TraceReceivedData(responseText);
}
// Checking if the data were fully loaded, without being aborted
var flag = "'AJAX_LOADING_OK';";
if (responseText.substr(responseText.length - flag.length) != flag)
return false;
eval(responseText);
if (waitElement)
waitElement.style.visibility = 'hidden';
return true;
}
AjaxCallObject.prototype.OnAbort = function()
{
if (__bTracing)
{
this.WriteTrace("<b>!!! AjaxCall was aborted !!!</b>");
this.CloseTrace();
}
if (waitElement)
waitElement.style.visibility = 'hidden';
}
AjaxCallObject.prototype.OnError = function(status, statusText, responseText)
{
if (__bTracing)
{
this.WriteTrace("<b>!!! Server reported an Error !!!</b>");
this.CloseTrace();
}
if (waitElement)
waitElement.style.visibility = 'hidden';
document.close(); // for IE
document.write(responseText);
document.close(); // for Firefox
}
AjaxCallObject.prototype.ReadyStateChange = function()
{
if( this.XmlHttp.readyState == 1 )
{
this.OnLoading();
}
else if( this.XmlHttp.readyState == 2 )
{
this.OnLoaded();
}
else if( this.XmlHttp.readyState == 3 )
{
this.OnInteractive();
}
else if( this.XmlHttp.readyState == 4 )
{
if( this.XmlHttp.status == 0 )
this.OnAbort();
else if( this.XmlHttp.status == 200 && this.XmlHttp.statusText == "OK" )
this.OnComplete(this.XmlHttp.responseText, this.XmlHttp.responseXML);
else
this.OnError(this.XmlHttp.status, this.XmlHttp.statusText, this.XmlHttp.responseText);
// Remove this AJAXCbo from the list
for (var i=0; i < __AJAXCboList.length; i++)
if (__AJAXCboList[i] == this)
{
__AJAXCboList[i].XmlHttp = null;
__AJAXCboList.splice(i, 1);
break;
}
}
}
AjaxCallObject.prototype.TraceWindow = null;
AjaxCallObject.prototype.SetIntervalForAjaxCall = function(milliSec)
{
if (__ClockID != 0)
this.ClearIntervalForAjaxCall();
__ClockID = window.setInterval("AJAXCbo.DoAjaxCall('__AJAX_AJAXCALLTIMER','','async')", milliSec);
}
AjaxCallObject.prototype.ClearIntervalForAjaxCall = function()
{
window.clearInterval(__ClockID);
__ClockID = 0;
}
AjaxCallObject.prototype.CreateTracingWindow = function()
{
for (var i=0; i < __TraceWindows.length; i++)
{
if (__TraceWindows[i].closed)
{
__TraceWindows.splice(i, 1);
i--;
}
}
this.TraceWindow = null;
for (var i=0; i < __TraceWindows.length; i++)
{
if (__TraceWindows[i].TraceFinished)
{
this.TraceWindow = __TraceWindows[i];
}
}
if (this.TraceWindow == null)
{
this.TraceWindow = window.open("","_blank","location=no,resizable=yes,scrollbars=yes");
__TraceWindows.push(this.TraceWindow);
}
this.TraceWindow.TraceFinished = false;
}
AjaxCallObject.prototype.ClearTracingWindows = function()
{
for (var i=0; i < __TraceWindows.length; i++)
{
__TraceWindows[i].close();
__TraceWindows.splice(i, 1);
i--;
}
}
AjaxCallObject.prototype.TraceSentData = function(data)
{
this.WriteTrace("<b>Ajax Call invoked at " + new Date().toLocaleTimeString() + "<br>");
if (__bPageIsStored)
this.WriteTrace("Page Store Mode: Stored (Session or Cache)<br>");
else
this.WriteTrace("Page Store Mode: NoStore<br>");
this.WriteTrace("Form Data sent to server (" + data.length + " characters):<br>");
this.WriteTrace("------------------------------</b><br>");
var fields = data.split("&");
for (var i=0; i < fields.length; i++)
{
var parts = fields[i].split("=");
this.WriteTrace("<b>" + parts[0] + "=</b>");
this.WriteTrace(this.EncodeTraceData(parts[1]) + "<br>");
}
this.WriteTrace("<b>------------------------------</b><br>");
this.WriteTrace("Waiting response from server.....<br>");
}
AjaxCallObject.prototype.TraceReceivedData = function(data)
{
this.WriteTrace("<b>Server responsed at " + new Date().toLocaleTimeString() + "<br>");
this.WriteTrace("Javascript code received from server (" + data.length + " characters):<br>");
this.WriteTrace("------------------------------</b><br>");
this.WriteTrace(this.EncodeTraceData(data) + "<br>");
this.WriteTrace("<b>------------------------------</b><br>");
this.CloseTrace();
}
AjaxCallObject.prototype.WriteTrace = function(text)
{
this.TraceWindow.document.write(text);
}
AjaxCallObject.prototype.CloseTrace = function()
{
this.TraceWindow.document.close();
this.TraceWindow.TraceFinished = true;
}
AjaxCallObject.prototype.EncodeTraceData = function(data)
{
return data.split("<").join("<").split(" ").join(" ").split("/r/n").join("<br>");
}
AjaxCallObject.prototype.EncodePostData = function(data)
{
return data.split("%").join("%25").split("=").join("%3d").split("&").join("%26").split("+").join("%2b");
}
AjaxCallObject.prototype.SetAttributesOfControl = function(clientID, attributes)
{
var place = document.getElementById(clientID);
if (place != null && attributes != "")
{
var attribs = attributes.split("|");
for (var i=0; i < attribs.length; i++)
{
var parts = attribs[i].split("=");
place.setAttribute(parts[0], parts[1])
}
}
}
AjaxCallObject.prototype.AddElementScript = function(parentID, tagName, elementID, html, beforeElemID)
{
var place = document.getElementById(parentID);
var child = (elementID != "") ? document.getElementById(elementID) : null;
if (place != null && child == null)
{
child = document.createElement(tagName);
child.id = elementID;
var before = (beforeElemID != null) ? document.getElementById(beforeElemID) : null;
place.insertBefore(child, before);
child.innerHTML = html;
}
else
this.SetHtmlOfElementScript (html, elementID);
}
AjaxCallObject.prototype.RemoveElementScript = function(parentID, elementID)
{
var place = document.getElementById(parentID);
var child = document.getElementById(elementID);
if (place != null && child != null)
place.removeChild(child);
}
AjaxCallObject.prototype.SetFieldScript = function(fieldName, fieldValue)
{
var field = document.forms[0][fieldName];
if (field != null)
field.value = fieldValue;
}
AjaxCallObject.prototype.SetFieldIfEmptyScript = function(fieldName, fieldValue)
{
var field = document.forms[0][fieldName];
if (field != null && field.value == '')
field.value = fieldValue;
}
AjaxCallObject.prototype.SetHtmlOfElementScript = function(html, elementID)
{
var place = document.getElementById(elementID);
if (place != null)
place.innerHTML=html;
}
AjaxCallObject.prototype.SetHtmlOfPage = function(html)
{
document.close(); // for IE
document.write(html);
document.close(); // for Firefox
}
AjaxCallObject.prototype.SetVisibilityOfElement = function(elementID, visible)
{
var place = document.getElementById(elementID);
if (place != null)
place.style.display = (visible) ? "" : "none";
}
AjaxCallObject.prototype.Alert = function(message)
{
window.alert(message);
}
// It's used by AjaxPanel
AjaxCallObject.prototype.ExtendedSetHtmlOfElementScript = function(html, elementID)
{
var store = document.createElement("span");
store.innerHTML = html;
var spans = store.getElementsByTagName("span");
for (var i=0; i < spans.length; i++)
{
var elem = spans[i];
if (elem.getAttribute("name") == "__ajaxmark")
{
var elemOnPage = document.getElementById(elem.id);
if (elemOnPage != null)
elem.innerHTML = elemOnPage.innerHTML;
}
}
var place=document.getElementById(elementID);
if (place != null)
place.innerHTML = store.innerHTML;
}
var AJAXCbo = new AjaxCallObject();
// wait element
CreateWaitElement();
if (window.addEventListener) {
window.addEventListener('scroll', MoveWaitElement, false);
window.addEventListener('resize', MoveWaitElement, false);
}
else if (window.attachEvent) {
window.attachEvent('onscroll', MoveWaitElement);
window.attachEvent('onresize', MoveWaitElement);
}
var waitElement;
var scrollX, scrollY = -1;
function MoveWaitElement() {
var scrollYT, scrollXT;
if (!waitElement)
CreateWaitElement();
if (typeof(window.pageYOffset) == "number") {
scrollYT = window.pageYOffset;
scrollXT = window.pageXOffset;
}
else if (document.body && document.documentElement && document.documentElement.scrollTop) {
scrollYT = document.documentElement.scrollTop;
scrollXT = document.body.scrollLeft;
}
else if (document.body && typeof(document.body.scrollTop) == "number") {
scrollYT = document.body.scrollTop;
scrollXT = document.body.scrollLeft;
}
if (scrollX != scrollXT || scrollY != scrollYT) {
scrollX = scrollXT;
scrollY = scrollYT;
var width = document.body.clientWidth;
waitElement.style.top = scrollYT + "px";
waitElement.style.right = -scrollXT + "px";
}
}
//function CreateWaitElement() {
// var elem = document.getElementById('__AjaxCall_Wait');
// if (!elem) {
// elem = document.createElement("div");
// elem.id = '__AjaxCall_Wait';
// elem.style.position = 'absolute';
// elem.style.height = 17;
// elem.border = "1px";
// elem.style.paddingLeft = "3px";
// elem.style.paddingRight = "3px";
// elem.style.fontSize = "12px";
// elem.style.borderColor = "#ffffff";
// elem.style.borderSize = "1px";
// elem.style.backgroundColor = "DimGray";
// elem.style.color = "#ffffff";
// elem.innerHTML = 'Loading ...';
// elem.style.visibility = 'hidden';
// document.body.insertBefore(elem, document.body.firstChild);
// }
// waitElement = elem;
//}
// end wait element
function CreateWaitElement() {
var elem = document.getElementById('__AjaxCall_Wait');
if (!elem) {
elem = document.createElement("div");
elem.id = '__AjaxCall_Wait';
elem.style.position = 'absolute';
elem.style.height = 17;
elem.border = "1px";
elem.style.paddingLeft = "3px";
elem.style.paddingRight = "3px";
elem.style.fontSize = "12px";
elem.style.borderColor = "#990000";
elem.style.borderWidth = "0";
elem.style.borderStyle="solid";
elem.style.backgroundColor = "#990000";
elem.style.color = "#FFFFFF";
elem.innerHTML = "正在加载...";
elem.style.visibility = 'hidden';
document.body.insertBefore(elem, document.body.firstChild);
}
waitElement = elem;
}
// end wait element