xajax.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* xajax Javascript library :: version 0.2.4 */
  2. Array.prototype.containsValue=function(valueToCheck){for(var i=0;i<this.length;i++){if(this[i]==valueToCheck)return true;}
  3. return false;}
  4. function Xajax(){this.DebugMessage=function(text){if(text.length > 1000)text=text.substr(0,1000)+"...\n[long response]\n...";try{if(this.debugWindow==undefined||this.debugWindow.closed==true){this.debugWindow=window.open('about:blank','xajax-debug','width=800,height=600,scrollbars=1,resizable,status');this.debugWindow.document.write('<html><head><title>Xajax debug output</title></head><body><h2>Xajax debug output</h2><div id="debugTag"></div></body></html>');}
  5. text=text.replace(/&/g,"&amp;")
  6. text=text.replace(/</g,"&lt;")
  7. text=text.replace(/>/g,"&gt;")
  8. debugTag=this.debugWindow.document.getElementById('debugTag');debugTag.innerHTML=('<b>'+(new Date()).toString()+'</b>: '+text+'<hr/>')+debugTag.innerHTML;}catch(e){alert("Xajax Debug:\n "+text);}
  9. };this.workId='xajaxWork'+new Date().getTime();this.depth=0;this.responseErrorsForAlert=["400","401","402","403","404","500","501","502","503"];this.getRequestObject=function(){if(xajaxDebug)this.DebugMessage("Initializing Request Object..");var req=null;if(typeof XMLHttpRequest!="undefined")
  10. req=new XMLHttpRequest();if(!req&&typeof ActiveXObject!="undefined"){try{req=new ActiveXObject("Msxml2.XMLHTTP");}
  11. catch(e){try{req=new ActiveXObject("Microsoft.XMLHTTP");}
  12. catch(e2){try{req=new ActiveXObject("Msxml2.XMLHTTP.4.0");}
  13. catch(e3){req=null;}
  14. }
  15. }
  16. }
  17. if(!req&&window.createRequest)
  18. req=window.createRequest();if(!req)this.DebugMessage("Request Object Instantiation failed.");return req;}
  19. this.$=function(sId){if(!sId){return null;}
  20. var returnObj=document.getElementById(sId);if(!returnObj&&document.all){returnObj=document.all[sId];}
  21. if(xajaxDebug&&!returnObj&&sId!=this.workId){this.DebugMessage("Element with the id \""+sId+"\" not found.");}
  22. return returnObj;}
  23. this.include=function(sFileName){var objHead=document.getElementsByTagName('head');var objScript=document.createElement('script');objScript.type='text/javascript';objScript.src=sFileName;objHead[0].appendChild(objScript);}
  24. this.stripOnPrefix=function(sEventName){sEventName=sEventName.toLowerCase();if(sEventName.indexOf('on')==0){sEventName=sEventName.replace(/on/,'');}
  25. return sEventName;}
  26. this.addOnPrefix=function(sEventName){sEventName=sEventName.toLowerCase();if(sEventName.indexOf('on')!=0){sEventName='on'+sEventName;}
  27. return sEventName;}
  28. this.addHandler=function(sElementId,sEvent,sFunctionName){if(window.addEventListener){sEvent=this.stripOnPrefix(sEvent);eval("this.$('"+sElementId+"').addEventListener('"+sEvent+"',"+sFunctionName+",false);");}
  29. else{sAltEvent=this.addOnPrefix(sEvent);eval("this.$('"+sElementId+"').attachEvent('"+sAltEvent+"',"+sFunctionName+",false);");}
  30. }
  31. this.removeHandler=function(sElementId,sEvent,sFunctionName){if(window.addEventListener){sEvent=this.stripOnPrefix(sEvent);eval("this.$('"+sElementId+"').removeEventListener('"+sEvent+"',"+sFunctionName+",false);");}
  32. else{sAltEvent=this.addOnPrefix(sEvent);eval("this.$('"+sElementId+"').detachEvent('"+sAltEvent+"',"+sFunctionName+",false);");}
  33. }
  34. this.create=function(sParentId,sTag,sId){var objParent=this.$(sParentId);objElement=document.createElement(sTag);objElement.setAttribute('id',sId);if(objParent)
  35. objParent.appendChild(objElement);}
  36. this.insert=function(sBeforeId,sTag,sId){var objSibling=this.$(sBeforeId);objElement=document.createElement(sTag);objElement.setAttribute('id',sId);objSibling.parentNode.insertBefore(objElement,objSibling);}
  37. this.insertAfter=function(sAfterId,sTag,sId){var objSibling=this.$(sAfterId);objElement=document.createElement(sTag);objElement.setAttribute('id',sId);objSibling.parentNode.insertBefore(objElement,objSibling.nextSibling);}
  38. this.getInput=function(sType,sName,sId){var Obj;if(!window.addEventListener){Obj=document.createElement('<input type="'+sType+'" id="'+sId+'" name="'+sName+'">');}
  39. else{Obj=document.createElement('input');Obj.setAttribute('type',sType);Obj.setAttribute('name',sName);Obj.setAttribute('id',sId);}
  40. return Obj;}
  41. this.createInput=function(sParentId,sType,sName,sId){var objParent=this.$(sParentId);var objElement=this.getInput(sType,sName,sId);if(objParent&&objElement)
  42. objParent.appendChild(objElement);}
  43. this.insertInput=function(sBeforeId,sType,sName,sId){var objSibling=this.$(sBeforeId);var objElement=this.getInput(sType,sName,sId);if(objElement&&objSibling&&objSibling.parentNode)
  44. objSibling.parentNode.insertBefore(objElement,objSibling);}
  45. this.insertInputAfter=function(sAfterId,sType,sName,sId){var objSibling=this.$(sAfterId);var objElement=this.getInput(sType,sName,sId);if(objElement&&objSibling&&objSibling.parentNode){objSibling.parentNode.insertBefore(objElement,objSibling.nextSibling);}
  46. }
  47. this.remove=function(sId){objElement=this.$(sId);if(objElement&&objElement.parentNode&&objElement.parentNode.removeChild){objElement.parentNode.removeChild(objElement);}
  48. }
  49. this.replace=function(sId,sAttribute,sSearch,sReplace){var bFunction=false;if(sAttribute=="innerHTML")
  50. sSearch=this.getBrowserHTML(sSearch);eval("var txt=this.$('"+sId+"')."+sAttribute);if(typeof txt=="function"){txt=txt.toString();bFunction=true;}
  51. if(txt.indexOf(sSearch)>-1){var newTxt='';while(txt.indexOf(sSearch)>-1){x=txt.indexOf(sSearch)+sSearch.length+1;newTxt+=txt.substr(0,x).replace(sSearch,sReplace);txt=txt.substr(x,txt.length-x);}
  52. newTxt+=txt;if(bFunction){eval('this.$("'+sId+'").'+sAttribute+'=newTxt;');}
  53. else if(this.willChange(sId,sAttribute,newTxt)){eval('this.$("'+sId+'").'+sAttribute+'=newTxt;');}
  54. }
  55. }
  56. this.getFormValues=function(frm){var objForm;var submitDisabledElements=false;if(arguments.length > 1&&arguments[1]==true)
  57. submitDisabledElements=true;var prefix="";if(arguments.length > 2)
  58. prefix=arguments[2];if(typeof(frm)=="string")
  59. objForm=this.$(frm);else
  60. objForm=frm;var sXml="<xjxquery><q>";if(objForm&&objForm.tagName=='FORM'){var formElements=objForm.elements;for(var i=0;i < formElements.length;i++){if(!formElements[i].name)
  61. continue;if(formElements[i].name.substring(0,prefix.length)!=prefix)
  62. continue;if(formElements[i].type&&(formElements[i].type=='radio'||formElements[i].type=='checkbox')&&formElements[i].checked==false)
  63. continue;if(formElements[i].disabled&&formElements[i].disabled==true&&submitDisabledElements==false)
  64. continue;var name=formElements[i].name;if(name){if(sXml!='<xjxquery><q>')
  65. sXml+='&';if(formElements[i].type=='select-multiple'){for(var j=0;j < formElements[i].length;j++){if(formElements[i].options[j].selected==true)
  66. sXml+=name+"="+encodeURIComponent(formElements[i].options[j].value)+"&";}
  67. }
  68. else{sXml+=name+"="+encodeURIComponent(formElements[i].value);}
  69. }
  70. }
  71. }
  72. sXml+="</q></xjxquery>";return sXml;}
  73. this.objectToXML=function(obj){var sXml="<xjxobj>";for(i in obj){try{if(i=='constructor')
  74. continue;if(obj[i]&&typeof(obj[i])=='function')
  75. continue;var key=i;var value=obj[i];if(value&&typeof(value)=="object"&&this.depth <=50){this.depth++;value=this.objectToXML(value);this.depth--;}
  76. sXml+="<e><k>"+key+"</k><v>"+value+"</v></e>";}
  77. catch(e){if(xajaxDebug)this.DebugMessage(e.name+": "+e.message);}
  78. }
  79. sXml+="</xjxobj>";return sXml;}
  80. this._nodeToObject=function(node){if(node.nodeName=='#cdata-section'){var data="";for(var j=0;j<node.parentNode.childNodes.length;j++){data+=node.parentNode.childNodes[j].data;}
  81. return data;}
  82. else if(node.nodeName=='xjxobj'){var data=new Array();for(var j=0;j<node.childNodes.length;j++){var child=node.childNodes[j];var key;var value;if(child.nodeName=='e'){for(var k=0;k<child.childNodes.length;k++){if(child.childNodes[k].nodeName=='k'){key=child.childNodes[k].firstChild.data;}
  83. else if(child.childNodes[k].nodeName=='v'){value=this._nodeToObject(child.childNodes[k].firstChild);}
  84. }
  85. if(key!=null&&value!=null){data[key]=value;key=value=null;}
  86. }
  87. }
  88. return data;}
  89. }
  90. this.loadingFunction=function(){};this.doneLoadingFunction=function(){};var loadingTimeout;this.call=function(sFunction,aArgs,sRequestType){var i,r,postData;if(document.body&&xajaxWaitCursor)
  91. document.body.style.cursor='wait';if(xajaxStatusMessages==true)window.status='Sending Request...';clearTimeout(loadingTimeout);loadingTimeout=setTimeout("xajax.loadingFunction();",400);if(xajaxDebug)this.DebugMessage("Starting xajax...");if(sRequestType==null){var xajaxRequestType=xajaxDefinedPost;}
  92. else{var xajaxRequestType=sRequestType;}
  93. var uri=xajaxRequestUri;var value;switch(xajaxRequestType){case xajaxDefinedGet:{var uriGet=uri.indexOf("?")==-1?"?xajax="+encodeURIComponent(sFunction):"&xajax="+encodeURIComponent(sFunction);if(aArgs){for(i=0;i<aArgs.length;i++){value=aArgs[i];if(typeof(value)=="object")
  94. value=this.objectToXML(value);uriGet+="&xajaxargs[]="+encodeURIComponent(value);}
  95. }
  96. uriGet+="&xajaxr="+new Date().getTime();uri+=uriGet;postData=null;}break;case xajaxDefinedPost:{postData="xajax="+encodeURIComponent(sFunction);postData+="&xajaxr="+new Date().getTime();if(aArgs){for(i=0;i <aArgs.length;i++){value=aArgs[i];if(typeof(value)=="object")
  97. value=this.objectToXML(value);postData=postData+"&xajaxargs[]="+encodeURIComponent(value);}
  98. }
  99. }break;default:
  100. alert("Illegal request type: "+xajaxRequestType);return false;break;}
  101. r=this.getRequestObject();if(!r)return false;r.open(xajaxRequestType==xajaxDefinedGet?"GET":"POST",uri,true);if(xajaxRequestType==xajaxDefinedPost){try{r.setRequestHeader("Method","POST "+uri+" HTTP/1.1");r.setRequestHeader("Content-Type","application/x-www-form-urlencoded");}
  102. catch(e){alert("Your browser does not appear to support asynchronous requests using POST.");return false;}
  103. }
  104. r.onreadystatechange=function(){if(r.readyState!=4)
  105. return;if(r.status==200){if(xajaxDebug)xajax.DebugMessage("Received:\n"+r.responseText);if(r.responseXML&&r.responseXML.documentElement)
  106. xajax.processResponse(r.responseXML);else{var errorString="Error: the XML response that was returned from the server is invalid.";errorString+="\nReceived:\n"+r.responseText;trimmedResponseText=r.responseText.replace(/^\s+/g,"");trimmedResponseText=trimmedResponseText.replace(/\s+$/g,"");if(trimmedResponseText!=r.responseText)
  107. errorString+="\nYou have whitespace in your response.";if(xajaxDebug) alert(errorString);document.body.style.cursor='default';if(xajaxStatusMessages==true)window.status='Invalid XML response error';}
  108. }
  109. else{if(xajax.responseErrorsForAlert.containsValue(r.status)){var errorString="Error: the server returned the following HTTP status: "+r.status;errorString+="\nReceived:\n"+r.responseText;alert(errorString);}
  110. document.body.style.cursor='default';if(xajaxStatusMessages==true)window.status='Invalid XML response error';}
  111. delete r;r=null;}
  112. if(xajaxDebug)this.DebugMessage("Calling "+sFunction+" uri="+uri+" (post:"+postData+")");r.send(postData);if(xajaxStatusMessages==true)window.status='Waiting for data...';delete r;return true;}
  113. this.getBrowserHTML=function(html){tmpXajax=this.$(this.workId);if(!tmpXajax){tmpXajax=document.createElement("div");tmpXajax.setAttribute('id',this.workId);tmpXajax.style.display="none";tmpXajax.style.visibility="hidden";document.body.appendChild(tmpXajax);}
  114. tmpXajax.innerHTML=html;var browserHTML=tmpXajax.innerHTML;tmpXajax.innerHTML='';return browserHTML;}
  115. this.willChange=function(element,attribute,newData){if(!document.body){return true;}
  116. if(attribute=="innerHTML"){newData=this.getBrowserHTML(newData);}
  117. elementObject=this.$(element);if(elementObject){var oldData;eval("oldData=this.$('"+element+"')."+attribute);if(newData!==oldData)
  118. return true;}
  119. return false;}
  120. this.viewSource=function(){return "<html>"+document.getElementsByTagName("HTML")[0].innerHTML+"</html>";}
  121. this.processResponse=function(xml){clearTimeout(loadingTimeout);this.doneLoadingFunction();if(xajaxStatusMessages==true)window.status='Processing...';var tmpXajax=null;xml=xml.documentElement;if(xml==null)
  122. return;var skipCommands=0;for(var i=0;i<xml.childNodes.length;i++){if(skipCommands > 0){skipCommands--;continue;}
  123. if(xml.childNodes[i].nodeName=="cmd"){var cmd;var id;var property;var data;var search;var type;var before;var objElement=null;for(var j=0;j<xml.childNodes[i].attributes.length;j++){if(xml.childNodes[i].attributes[j].name=="n"){cmd=xml.childNodes[i].attributes[j].value;}
  124. else if(xml.childNodes[i].attributes[j].name=="t"){id=xml.childNodes[i].attributes[j].value;}
  125. else if(xml.childNodes[i].attributes[j].name=="p"){property=xml.childNodes[i].attributes[j].value;}
  126. else if(xml.childNodes[i].attributes[j].name=="c"){type=xml.childNodes[i].attributes[j].value;}
  127. }
  128. if(xml.childNodes[i].childNodes.length > 1&&xml.childNodes[i].firstChild.nodeName=="#cdata-section"){data="";for(var j=0;j<xml.childNodes[i].childNodes.length;j++){data+=xml.childNodes[i].childNodes[j].data;}
  129. }
  130. else if(xml.childNodes[i].firstChild&&xml.childNodes[i].firstChild.nodeName=='xjxobj'){data=this._nodeToObject(xml.childNodes[i].firstChild);objElement="XJX_SKIP";}
  131. else if(xml.childNodes[i].childNodes.length > 1){for(var j=0;j<xml.childNodes[i].childNodes.length;j++){if(xml.childNodes[i].childNodes[j].childNodes.length > 1&&xml.childNodes[i].childNodes[j].firstChild.nodeName=="#cdata-section"){var internalData="";for(var k=0;k<xml.childNodes[i].childNodes[j].childNodes.length;k++){internalData+=xml.childNodes[i].childNodes[j].childNodes[k].nodeValue;}
  132. }else{var internalData=xml.childNodes[i].childNodes[j].firstChild.nodeValue;}
  133. if(xml.childNodes[i].childNodes[j].nodeName=="s"){search=internalData;}
  134. if(xml.childNodes[i].childNodes[j].nodeName=="r"){data=internalData;}
  135. }
  136. }
  137. else if(xml.childNodes[i].firstChild)
  138. data=xml.childNodes[i].firstChild.nodeValue;else
  139. data="";if(objElement!="XJX_SKIP")objElement=this.$(id);var cmdFullname;try{if(cmd=="cc"){cmdFullname="addConfirmCommands";var confirmResult=confirm(data);if(!confirmResult){skipCommands=id;}
  140. }
  141. if(cmd=="al"){cmdFullname="addAlert";alert(data);}
  142. else if(cmd=="js"){cmdFullname="addScript/addRedirect";eval(data);}
  143. else if(cmd=="jc"){cmdFullname="addScriptCall";var scr=id+'(';if(data[0]!=null){scr+='data[0]';for(var l=1;l<data.length;l++){scr+=',data['+l+']';}
  144. }
  145. scr+=');';eval(scr);}
  146. else if(cmd=="in"){cmdFullname="addIncludeScript";this.include(data);}
  147. else if(cmd=="as"){cmdFullname="addAssign/addClear";if(this.willChange(id,property,data)){eval("objElement."+property+"=data;");}
  148. }
  149. else if(cmd=="ap"){cmdFullname="addAppend";eval("objElement."+property+"+=data;");}
  150. else if(cmd=="pp"){cmdFullname="addPrepend";eval("objElement."+property+"=data+objElement."+property);}
  151. else if(cmd=="rp"){cmdFullname="addReplace";this.replace(id,property,search,data)
  152. }
  153. else if(cmd=="rm"){cmdFullname="addRemove";this.remove(id);}
  154. else if(cmd=="ce"){cmdFullname="addCreate";this.create(id,data,property);}
  155. else if(cmd=="ie"){cmdFullname="addInsert";this.insert(id,data,property);}
  156. else if(cmd=="ia"){cmdFullname="addInsertAfter";this.insertAfter(id,data,property);}
  157. else if(cmd=="ci"){cmdFullname="addCreateInput";this.createInput(id,type,data,property);}
  158. else if(cmd=="ii"){cmdFullname="addInsertInput";this.insertInput(id,type,data,property);}
  159. else if(cmd=="iia"){cmdFullname="addInsertInputAfter";this.insertInputAfter(id,type,data,property);}
  160. else if(cmd=="ev"){cmdFullname="addEvent";property=this.addOnPrefix(property);eval("this.$('"+id+"')."+property+"= function(){"+data+";}");}
  161. else if(cmd=="ah"){cmdFullname="addHandler";this.addHandler(id,property,data);}
  162. else if(cmd=="rh"){cmdFullname="addRemoveHandler";this.removeHandler(id,property,data);}
  163. }
  164. catch(e){if(xajaxDebug)
  165. alert("While trying to '"+cmdFullname+"' (command number "+i+"), the following error occured:\n"
  166. +e.name+": "+e.message+"\n"
  167. +(id&&!objElement?"Object with id='"+id+"' wasn't found.\n":""));}
  168. delete objElement;delete cmd;delete cmdFullname;delete id;delete property;delete search;delete data;delete type;delete before;delete internalData;delete j;delete k;}
  169. }
  170. delete xml;delete i;document.body.style.cursor='default';if(xajaxStatusMessages==true)window.status='Done';}
  171. }
  172. var xajax=new Xajax();xajaxLoaded=true;