123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- define(['bi/authoring/utils/U_Object', 'bi/authoring/utils/rsPromptHandler'], function(U_Object, rsPromptHandler){
- function C_rsRestRequest( v_oGlassContext, v_oListener, v_oRequest )
- {
- this.m_oGlassContext = v_oGlassContext;
- this.m_oListener = v_oListener;
- this.m_oRequest = v_oRequest;
- this.m_oPromptCallbackResponse = null;
- this.M_iResponseDelay = 0;
- };
- function f_fireEvent( v_sEvent )
- {
- if (this.m_oListener && this.m_oListener[v_sEvent])
- {
-
- setTimeout( this.m_oListener[v_sEvent].bind( this.m_oListener, this ), 0 );
- }
- };
- function f_isSuccessStatus()
- {
- var v_iStatus = this.F_GetStatus();
- return v_iStatus >= 200 && v_iStatus < 300;
- };
- function f_onRestResponseDontCare()
- {
-
- };
- var f_processAsyncResponse;
-
- function f_doResponseProcessing()
- {
- if (this.F_GetStatus() == 202)
- {
- f_processAsyncResponse.bind(this)();
- }
- else
- {
- if (this.m_bAborted)
- {
- return;
- }
-
-
- this.F_ProcessResponse();
- f_fireEvent.bind(this)("F_Request_OnComplete");
- }
- };
- function f_initiateResponseProcessing()
- {
- if (this.M_iResponseDelay)
- {
- setTimeout(f_doResponseProcessing.bind(this), this.M_iResponseDelay);
- return;
- }
-
- f_doResponseProcessing.bind(this)();
- };
- function f_onRestResponseOK(v_oResponseData, v_sStatus, v_oXHR )
- {
- this.m_oResponseData = v_oResponseData;
- this.m_oXHR = v_oXHR;
- f_initiateResponseProcessing.bind(this)();
- };
- function f_onRestResponseFailed(v_sStatus, v_oXHR, v_sErrorThrown)
- {
- this.m_oXHR = v_oXHR;
- this.m_oFailed = { status: v_sStatus, error: v_sErrorThrown, response: this.F_GetResponseText() };
-
- this.F_OnFailed();
-
- f_initiateResponseProcessing.bind(this)();
- };
- function f_processAsyncResponse()
- {
-
- var v_bPrompting = this.m_oResponseData && this.m_oResponseData.status == 'prompting';
- var v_sLocation = this.m_oXHR.getResponseHeader('Location');
-
- var v_sAffinity = this.m_oXHR.getResponseHeader('X-CA-Affinity');
- var v_oRequestHeaders = {};
- if (v_sAffinity)
- {
-
- v_oRequestHeaders['X-CA-Affinity'] = v_sAffinity;
- }
- var v_oRequest = {
- url : v_sLocation,
- headers : v_oRequestHeaders
- };
-
- if (this.m_bAborted)
- {
- v_oRequest.type = 'DELETE';
- Application.GlassContext.services.ajax.ajax(v_oRequest)
- .done(f_onRestResponseDontCare.bind(this))
- .fail(f_onRestResponseDontCare.bind(this));
- }
- else
- {
- v_oRequest.type = 'GET';
- if (v_bPrompting)
- {
-
-
-
-
- v_oRequest.dataType = "text";
- if (!rsPromptHandler.F_DoRestPrompting(this, v_oRequest, this.m_oGlassContext))
- {
- this.m_oFailed = { status: 'popup blocked' };
- this.F_ProcessResponse();
- f_fireEvent.bind(this)("F_Request_OnComplete");
- }
- }
- else
- {
- this.m_oGlassContext.services.ajax.ajax(v_oRequest)
- .done(f_onRestResponseOK.bind(this))
- .fail(f_onRestResponseFailed.bind(this));
- }
- }
- };
- function f_getPropertyInTryCatch( o, p )
- {
- try
- {
- return o[p];
- }
- catch ( e )
- {
- return null;
- }
- };
-
-
- C_rsRestRequest.prototype.F_GetListener = function()
- {
-
- return this.m_oListener;
- };
- C_rsRestRequest.prototype.F_SetListener = function(v_oListener)
- {
-
- this.m_oListener = v_oListener;
- };
- C_rsRestRequest.prototype.F_SetHeaders = function( v_oRequestHeaders )
- {
-
- return v_oRequestHeaders;
- };
- C_rsRestRequest.prototype.F_Send = function()
- {
- this.m_oRequest.headers = this.F_SetHeaders( this.m_oRequest.headers );
-
- this.m_oGlassContext.services.ajax.ajax(this.m_oRequest)
- .done(f_onRestResponseOK.bind(this))
- .fail(f_onRestResponseFailed.bind(this));
- };
- C_rsRestRequest.prototype.F_Abort = function()
- {
-
- this.m_bAborted = true;
- f_fireEvent.bind(this)("F_Request_OnAborted");
- this.F_SetListener(null);
- };
- C_rsRestRequest.prototype.F_GetStatus = function()
- {
-
- return this.m_oXHR ? f_getPropertyInTryCatch( this.m_oXHR, "status" ) : null;
- };
- C_rsRestRequest.prototype.F_ProcessResponse = function()
- {
-
-
- };
- C_rsRestRequest.prototype.F_GetResponseData = function()
- {
- return this.m_oResponseData;
- };
- C_rsRestRequest.prototype.F_GetResponseText = function()
- {
- return this.m_oXHR ? this.m_oXHR.responseText : null;
- };
- C_rsRestRequest.prototype.F_OnFailed = function()
- {
-
- };
- C_rsRestRequest.prototype.F_GetFailed = function()
- {
- return this.m_oFailed;
- };
- C_rsRestRequest.prototype.F_SetPromptCallbackResponse = function( v_oResponse, v_oAttachments )
- {
-
-
-
- this.m_oPromptCallbackResponse = v_oResponse ? U_Object.F_Clone(v_oResponse) : null;
- };
- C_rsRestRequest.prototype.F_OnServerPromptingComplete = function()
- {
- if (this.m_oPromptCallbackResponse)
- {
- this.m_oResponseData = this.m_oPromptCallbackResponse;
- this.m_oPromptCallbackResponse = null;
- this.m_oFailed = null;
- this.F_ProcessResponse();
- }
- else
- {
- this.m_oFailed = { status: 'cancelled' };
- }
- f_fireEvent.bind(this)("F_Request_OnComplete");
- };
- return C_rsRestRequest;
- });
|