/*************************************************************************** * module.rpc.js * ------------------------ * Copyleft : (c) 2007 maborak.com * Version : 0.2 * ***************************************************************************/ /*************************************************************************** * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * ***************************************************************************/ /** * @class rpc */ leimnud.Package.Public({ info :{ Class :"maborak", File :"module.rpc.js", Name :"rpc", Type :"module", Version :"0.1" }, content :{ /* * @class xmlhttp * @param {Object} options Options * @param {String} options.url Url to Open * @param {String} options.method Method * @param {String} options.arguments Arguments [Optional,Default=""] * @param {String} options.async Asynchronous? [Optional,Default=true] * * @param {Function | Object | Virtual function} Instance.callback Callback for this process * @example: * var process = new leimnud.module.rpc.xmlhttp({ * url :"g.xml", * method :"POST", * arguments :"u=iuiu" * }); * process.callback=functionCallback; * * || * * process.callback=leimnud.closure({method:Myinstance.callback,instance:MyInstance,arguments:[process,"demo",99]}); * * || * * process.callback={Function:myFunction,arguments:[process,"demo"]}; * process.make(); * */ xmlhttp:function(options) { this.options=options || {}; this.headers=[]; this.core = function() { try{ xmlhttp = false; if ( window.ActiveXObject ) xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { xmlhttp = false; } } return (!xmlhttp && typeof XMLHttpRequest!='undefined')? new XMLHttpRequest():xmlhttp || new function(){}; }; /* * Make this process */ this.make=function() { this.xmlhttp = this.core(); this.url = this.options.url || false; if(!this.options.url || !this.xmlhttp){return false;} this.method = (this.options.method || "POST").toUpperCase(); this.args = this.options.args || ""; this.async = (this.options.async===false)?false:true; if(this.method=="POST"){this.header("Content-Type","application/x-www-form-urlencoded");} this.open(); return true; }; /* * * Open this.options.url request * */ this.open=function() { this.url = ((this.url.substr(this.url.length-1,1)!=="?" && this.method==="GET")?this.url+"?":this.url); this.url = ((this.method=="GET")?(this.url+this.args):this.url); this.xmlhttp.open(this.method,this.url+((this.options.nocache)?"&rand_rpc="+Math.random():""),this.async); this.applyHeaders(); this.xmlhttp.send((this.method=="GET")?null:this.args); //this.xmlhttp.onreadystatechange=this.parent.closure({method:this.changes,instance:this,args:98989898}); this.xmlhttp.onreadystatechange=this.changes; }; /* * * Method for this.xmlhttp.onreadystatechange * */ this.changes=function(g) { if (this.xmlhttp.readyState==4) { if(this.callback) { //this.callback=(this.callback.isObject)?this.parent.closure(this.callback):this.callback; this.callback.args(this)(); } } }; /* * * Apply headers * */ this.applyHeaders=function() { for(var i=0;i65000) { this.interval = clearInterval(this.interval); var rt; try{ rt = window[this.tmp].data.parseJSON(); } catch(e) { rt = ""; } if(this.options.debug===true && console.info) { console.info(rt) } /* Create XML BEGIN */ var myDocument; if(document.implementation.createDocument) { var parser = new DOMParser(); try{ window.lk = myDocument = parser.parseFromString(rt || "empty", "text/xml"); }catch(e) { myDocument = parser.parseFromString("empty", "text/xml"); } } else if (window.ActiveXObject){ myDocument = new ActiveXObject("Microsoft.XMLDOM"); myDocument.async="false"; try{ myDocument.loadXML(rt || "empty"); } catch(e) { myDocument.loadXML("empty"); } } /* Create XML END */ this.json ={ responseText:rt, responseXML:myDocument }; if(this.parent.browser.isIE) { window[this.tmp]=null; } else { delete window[this.tmp]; } this.script.parentNode.removeChild(this.script); if(this.callback) { this.callback.args(this)(); } } }; this.expand(this); return this; } } });