글 작성자: 써니루루

잠시 참고용으로 분석한 내용;


var POST="POST";
var GET="GET";
if(!g_fModuleLoaded)var g_fModuleLoaded={};
if(!g_fModuleLoaded[".\\dom/ajaxrequest.js".toLowerCase()])(function(){
 Type.createNamespace("Owa.Dom");
 var fChecked=false;
 var fUseNative=true;
 var rgRequests=[];
 Owa.Dom.AjaxRequest = function Owa$Dom$AjaxRequest(sUrl,sCmd,oCtx,cb,fSync,fNeverDispose){
  var oXHR;
  if(fChecked){
   oXHR=fUseNative ? new XMLHttpRequest() : new ActiveXObject("MSXML2.xmlhttp")
  }
  else
  {
   try
   {
    oXHR=new XMLHttpRequest();
    fUseNative=true
   }
   catch(e)
   {
    oXHR=new ActiveXObject("MSXML2.xmlhttp");
    fUseNative=false
   }
   fChecked=true
  }
  this.oXmlHttp=oXHR;
  this.oXmlHttp.open(sCmd,sUrl,!fSync);
  this.sUrl=sUrl;
  this.oCtx=oCtx;
  this.fSync=fSync;
  this.fLock=false;
  this._fNeverDispose=!!fNeverDispose;
  this.iIndex=rgRequests.length;
  rgRequests.push(this);
  if(cb){
   this.cb=cb;
   if(!fSync){
    this.oXmlHttp.onreadystatechange=new Function("Owa.Dom.AjaxRequest._cbOnReadyStateChange("+this.iIndex+");")
   }
  }
  Owa.Dom.AjaxRequest.get_OnRequestCreated().dispatch(this)
 };
 Owa.Dom.AjaxRequest.prototype.send=function Owa$Dom$AjaxRequest$send(sBody){
  if(isDf(typeof(sBody))){
   this.oXmlHttp.send(sBody)
  }
  else{
   this.oXmlHttp.send(null)
  }
  if(this.fSync){
   return this.onComplete()
  }
  else{
   return null
  }
 };
 Owa.Dom.AjaxRequest.prototype.setRequestHeader=function Owa$Dom$AjaxRequest$setRequestHeader(sHeaderName,sHeaderValue){
  this.oXmlHttp.setRequestHeader(sHeaderName,sHeaderValue)
 };
 Owa.Dom.AjaxRequest.prototype.cancelCallback=function Owa$Dom$AjaxRequest$cancelCallback(){
  if(this.fLock)throw"cannot cancel the callback when it is already finished or cancelled";
  this.fLock=true;
  rgRequests[this.iIndex].oXmlHttp.abort();
  delete rgRequests[this.iIndex]
 };
 Owa.Dom.AjaxRequest.prototype.onComplete=function Owa$Dom$AjaxRequest$onComplete(){
  if(!this.fLock)
  {
   this.fLock=true;
   Owa.Dom.AjaxRequest.get_OnResponseReceived().dispatch(this);
   var oCallbackResult=null;
   if(this.cb!=null)oCallbackResult=this.cb(this);
   delete rgRequests[this.iIndex];
   Owa.Dom.AjaxRequest.get_OnCallbackCompleted().dispatch(this);
   return oCallbackResult
  }
 };
 Owa.Dom.AjaxRequest._cbOnReadyStateChange=function Owa$Dom$AjaxRequest$_cbOnReadyStateChange(i){
  var oRequest=rgRequests[i];
  if(oRequest&&4==oRequest.oXmlHttp.readyState){
   if(0==oRequest.oXmlHttp.status&&(Owa.Utility.isFirefox()||Owa.Utility.isSafari())){
    oRequest.cancelCallback()
   }
   else{
    oRequest.onComplete()
   }
  }
 };
 Owa.Dom.AjaxRequest.disposeAjaxRequests=function Owa$Dom$AjaxRequest$disposeAjaxRequests(){
  for(var i=0;i<rgRequests.length;i++){
   if(rgRequests[i]){
    var oR=rgRequests[i];
    if(oR._fNeverDispose)continue;
    Owa.Dom.AjaxRequest.abortAnXMLHttpRequest(oR.oXmlHttp);
    rgRequests[i]=null
   }
  }
 };
 Owa.Dom.AjaxRequest.abortAjaxRequests=function Owa$Dom$AjaxRequest$abortAjaxRequests(sNs,oCmd){
  var oR=null;
  for(var i=0;i<rgRequests.length;i++){
   oR=rgRequests[i];
   if(oR&&oR.oCtx&&isDf(typeof(oR.oCtx.sNs))&&(oR.oCtx.sNs==sNs)&&isDf(typeof(oR.oCtx.sEv))&&isDf(typeof(oCmd[oR.oCtx.sEv]))){
    if(oR.fLock) continue;
    oR.fLock=true;
    Owa.Dom.AjaxRequest.abortAnXMLHttpRequest(oR.oXmlHttp);
    rgRequests[i]=null
   }
  }
 };
 Owa.Dom.AjaxRequest.abortAnXMLHttpRequest=function Owa$Dom$AjaxRequest$abortAnXMLHttpRequest(oX){
  if(isDf(typeof(oX))&&oX&&isDf(typeof(oX.abort))){
   try{
    oX.onreadystatechange=function(){};
    oX.abort()
   }
   catch(e){}
  }
 };
 var ON_REQUEST_CREATED="onRequestCreated";
 var ON_RESPONSE_RECEIVED="onResponseReceived";
 var ON_CALLBACK_COMPLETED="onCallbackCompleted";
 var _oStaticEvents={};
 _oStaticEvents[ON_REQUEST_CREATED]=new Owa.Utility.EventHandlerCollection();
 _oStaticEvents[ON_RESPONSE_RECEIVED]=new Owa.Utility.EventHandlerCollection();
 _oStaticEvents[ON_CALLBACK_COMPLETED]=new Owa.Utility.EventHandlerCollection();
 Owa.Dom.AjaxRequest.get_OnRequestCreated=function Owa$Dom$AjaxRequest$get_OnRequestCreated(){
  return _oStaticEvents[ON_REQUEST_CREATED]
 };
 Owa.Dom.AjaxRequest.get_OnResponseReceived=function Owa$Dom$AjaxRequest$get_OnResponseReceived(){
  return _oStaticEvents[ON_RESPONSE_RECEIVED]
 };
 Owa.Dom.AjaxRequest.get_OnCallbackCompleted=function Owa$Dom$AjaxRequest$get_OnCallbackCompleted(){
  return _oStaticEvents[ON_CALLBACK_COMPLETED]
 }
})();
g_fModuleLoaded[".\\dom/ajaxrequest.js".toLowerCase()]=1;