/*
*
*
*AjaxFactory£¬¸ºÔðÉú²ú²¢Î¬»¤AjaxÀàʵÀý
*
*
*/
function DefaultAjaxFactory(){
this.map=new Map();
}
DefaultAjaxFactory.prototype.createAjax=function(){
var name=Math.random();
var ajax=new Ajax(name);
this.map.put(name,ajax);
return ajax;
}
DefaultAjaxFactory.prototype.getAjax=function(name){
var ajax=this.map.get(name);
return ajax;
}
/*
*
*
*
*XMLObject£¬Î¬»¤Ò»¸öÓÉxmlÎĵµ×ª»¯³ÉµÄ¶ÔÏó
*
*
*
*
*/
function XMLObject(){
this.stack=new Array();
this.object="";
this.exp="";
}
XMLObject.prototype.addProperty=function(name){
this.exp="this";
for(var i=0;i<this.stack.length;i++){
var s=this.stack[i];
var obj=eval(this.exp);
if(this.exp!="this"){
var len=obj.length-1;
this.exp=this.exp+"["+len+"]"+"."+s;
}else{
this.exp=this.exp+"."+s;
}
}
var tmpobj=eval(this.exp);
if(!tmpobj){
command=this.exp+"=new Array();";
debug(command);
eval(command);
command=this.exp+"[0]=new Array();";
debug(command);
eval(command);
}else{
var len=tmpobj.length;
command=this.exp+"["+len+"]=new Array();";
debug(command);
eval(command);
}
}
XMLObject.prototype.setProperty=function(value){
var obj=eval(this.exp);
var len=obj.length-1;
var command=this.exp+"["+len+"]=new String(/""+value+"/")";
debug(command);
eval(command);
}
/*
*
*
*Ajax £¬Òì²½»¯Ìá½»Ò»¸öÇëÇó²¢»ñµÃ½á¹û(¶ÔÏó»òÊý×é)
*
*
*/
function Ajax(name){
try {
¡¡¡¡this.req = new ActiveXObject("Msxml2.XMLHTTP");
¡¡} catch (e) {
¡¡¡¡try {
¡¡¡¡¡¡this.req = new ActiveXObject("Microsoft.XMLHTTP");
¡¡¡¡} catch (E) {
¡¡¡¡¡¡this.req = false;
¡¡¡¡}
¡¡}
¡¡if (!this.req && typeof XMLHttpRequest!='undefined') {
¡¡¡¡this.req = new XMLHttpRequest();
¡¡}
// this.req=new ActiveXObject("Msxml2.XMLHTTP");
this.name=name;
this.xmlDoc=null;
this.status="";
this.url="<%=request.getContextPath()%>/servlet/AjaxProcessor";
this.body=new Map();
this.doSuccess=function(){return null;};
this.doFail=function(){return null;};
this.object=new XMLObject();
this.async=true;//default is asynchronized
this.method="GET";
this.processor="";
}
Ajax.prototype.addBody=function(key,value){
this.body.add(key,value);
}
Ajax.prototype.enableAsync=function(){
this.async=true;
}
Ajax.prototype.disableAsync=function(){
this.async=false;
}
Ajax.prototype.submit=function(){
this.addBody("SysGenRandomValue",Math.random());
var keys=this.body.keys;
var body="";
for(var i=0;i<keys.length;i++){
var key=keys[i];
var value=this.body.list[i];
body+=key+"="+value+"&";
}
body=body.substring(0,body.length-1);
$("AjaxNotice").style.display="";
//×¢ÒâÕâÒ»¾ä£¬Õâ¾ÍÊÇ×¢²ástatechange´¦Àíº¯ÊýµÄÓï¾ä
this.req.onreadystatechange = new Function("var temp_ajax=AjaxFactory.getAjax(/""+this.name+"/");temp_ajax.processResponse()");
if(this.method.toLowerCase()=="get"){
body="?"+body;
this.req.open(this.method,this.url+body,this.async);
this.req.send("");
}else if(this.method.toLowerCase()=="post"){
// this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// alert(body);
// this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
this.req.open(this.method,this.url,this.async);
this.req.send(body);
}
}
Ajax.prototype.processResponse=function(){
if(this.req.readyState==4){
if(this.req.status==200){
this.preHandleSuccess();
this.doSuccess();
this.status="success";
}else{
this.preHandleFail();
this.doFail();
this.status="fail";
}
}
}
Ajax.prototype.setSuccessHandler=function(successFunc){
this.doSuccess=successFunc;
}
Ajax.prototype.setFailHandler=function(failFunc){
}
Ajax.prototype.preHandleSuccess=function(){
//fire after process succeeded
var xmlDoc=this.req.responseXML.getElementsByTagName("Object")[0];
this.visitTree(xmlDoc);
$("AjaxNotice").style.display="none";
}
Ajax.prototype.preHandleFail=function(){
//fire after process failed
$("AjaxNotice").style.display="none";
}
Ajax.prototype.visitTree=function(root){
this.object.stack.push(root.tagName);
if(root.hasChildNodes){
objname=root.tagName;
this.object.addProperty(objname);
for(var i=0;i<root.childNodes.length;i++){
var node=root.childNodes[i];
this.visitTree(node);
}
}else{
var objvalue=root.nodeValue;
var mom=root.parentNode;
var lastchild=mom.lastChild;
this.object.setProperty(objvalue);
}
this.object.stack.pop();
}
/*for it is not synchronized,please call the following functions in your success handlers*/
Ajax.prototype.getObject=function(){
return this.object.Object[0];
}
Ajax.prototype.getData=function(){
var obj=this.getObject();
return obj.data[0];
}
Ajax.prototype.getDatas=function(){
var obj=this.getObject();
return obj.data;
}
Ajax.prototype.getStatus=function(){
var obj=this.getObject();
return obj.statCode[0];
}
Ajax.prototype.getMsg=function(){
var obj=this.getObject();
return obj.msg[0];
}
Ajax.prototype.getQuery=function(){
var obj=this.getObject();
var res=new Array();
return res;
}
Ajax.prototype.submitForm=function(form){
var elems=form.elements;
for(var i=0;i<elems.length;i++){
var name=elems[i].name;
if(name&&name!=""){
var map=this.visitForm(elems[i]);
var keys=map.keys;
for(var j=0;j<keys.length;j++){
this.addBody(map.keys[j],map.list[j]);
}
}
}
if(form.processor){
this.addBody("Ajax_Handler_ClassName",form.processor);
}
this.url=form.action;
this.method=form.method;
this.submit();
}
Ajax.prototype.setAction=function(actionId){
this.addBody("DspId",actionId);
}
Ajax.prototype.visitForm=function(elem){
var map=new Map();
var name=elem.name;
if(elem.type=="checkbox"){
if(elem.checked){
map.add(name,elem.value);
}else{
map.add(name,"");
}
}else if(elem.type=="select_multiple"){
var options=elem.options;
for(var i=0;i<options.length;i++){
if(options[i].selected){
map.add(name,options[i]);
}
}
}else if(elem.type=="radio"){
if(elem.checked){
map.add(name,elem.value);
}
}else{
map.add(name,elem.value);
}
return map;
}
function DoAfterOK(){
var status=this.getStatus();
var msg=this.getMsg();
if(status=="success"){
alert("±£´æ³É¹¦£¡");
window.close();
}else if(status=="fail"){
alert("±£´æʧ°Ü£¡"+msg);
}else{
alert("±£´æʧ°Ü£¡Î´Öª´íÎó");
}
}
function getSeqId(seqid,handler){
var ajax=AjaxFactory.createAjax();
ajax.url="<%=request.getContextPath()%>/servlet/AjaxDispatcher";
ajax.setAction("<%=com.support.ajax.AjaxPara.GET_SEQ_ID%>");
ajax.addBody("seqid",seqid);
ajax.setSuccessHandler(handler);
ajax.submit();
}
/* end */
var AjaxFactory=new DefaultAjaxFactory();
/*usage:
function ss(){
var obj=this.getObject();
}
var ajax=AjaxFactory.createAjax("abc1");
ajax.url="http://localhost:8080/UserAdmin/servlet/AjaxDispatcher?DspId=999999";
ajax.setSuccessHandler(ss);
ajax.submit();
*/