 /*
 *	http://www.orgaction.com
 * 
 *	Name:		globalListeners.js
 *	Purpose:	
 *	
 *	05/17/2010	DFM		New, ext examples referenced for dataproxy events
 * 
 */

// Application instance for showing user-feedback messages.
var App = new Ext.App({});

Ext.namespace('_oa', '_oa.session', '_oa.session.oSession', '_oa.globalListeners');

_oa.globalListeners.processResponse=function(response,loginCallBack){
	var oSession = _oa.session.oSession;
	if (response.isAbort && response.isAbort === true){
		var rp = { success:false, msg:"Server response was aborted" };
		if (response.isTimeout && response.isTimeout === true){
			var rp = { success:false, msg:"Timeout while waiting for server response" };
		}
	}else if (response.action){
		// Data store updates, response is already parsed
		rp=response;
	}else{
		//parse the response
		try {
			var rp = Ext.util.JSON.decode(response.responseText);
		}catch(err){
			// not every response is JSON... example: autoLoad config on panels
			if (response.status == 200){
				var rp = { success:true};				
			}else{
				var rp = { success:false, msg:"Invalid server response" };
			}
			
			//console.log("** error : Invalid server response" );
			//console.log({response:response});
		}
		// Check response to make sure we are still signed in
		if (rp.sessionStatus){
			if (oSession.checkStatus) {
				oSession.checkStatus(rp.sessionStatus, loginCallBack);
				//_oa.Login.fSessionStatus(rp.sessionStatus,loginCallBack);
			}
		}

	}
	
	//if provided, show message
	if (rp.msg){
		App.setAlert(rp.success, rp.msg);
	}
};

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// * Listen to all DataProxy events
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// 	Listen to DataProxy write events (inlcudes create)
	Ext.data.DataProxy.addListener('write', function(proxy, action, result, res, rs) {
		if (!(res && res.message)) {
			// show a message set by the ExtApp if the server did not supply a msg. Server msg is shown by AJAX listener
			if (proxy.api && proxy.api._oaWriteSuccessMsg) {
				App.setAlert(true, proxy.api._oaWriteSuccessMsg);
			}
		}
	});
	
/*	
	// 	Listen to all DataProxy exception events
	 //redundant with json listeners// 
	Ext.data.DataProxy.addListener('exception', function(proxy, type, action, options, res) {
	    //After user signs in (if required), we can call the following function to complete the original AJAX request. If user is already signed in, or fails to sign in, this will not be called. 
		var loginCallBackDataProxy = function(){
			//Ideally we would just retry the connection as we do with AJAX call, but until we figure out how to do that, show an error message
			App.setAlert(true, "Could not retry rquest after sign in. You may need to reload the page or tab.");
		} 
		_oa.globalListeners.processResponse(res,loginCallBackDataProxy);
		//if (type === 'remote') {
		//	_oa.error("Alert", res.msg); 
		//}else if (type === 'response') {//Unhandled server error, res.responseText contains the html error message/dump
		//	_oa.error("Server Error", "The server did not return a valid response. Please reload to confirm what changes were made."
		//		,"DataProxy error, response object:", res); 
		//}
	});
*/

// 	Listen to all DataProxy beforewrite events
//	Ext.data.DataProxy.addListener('beforewrite', function(proxy, action) {
//		App.setAlert(App.STATUS_NOTICE, "Before " + action);
//	});

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  
// * Listen to Ajax events
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
// 	Listen to all Ajax beforerequest events 
Ext.Ajax.on('beforerequest', function(conn,options){
	// do nothing...
	// App.setAlert(true, "Requesting data...");
}, this);

// 	Listen to all Ajax requestcomplete events 
Ext.Ajax.on('requestcomplete',  function(conn,response,options){
	if(options._oaSuccessMsg){
		 App.setAlert(true, options._oaSuccessMsg);
	}
	//After user signs in (if required), we can call the following function to complete the original AJAX request. If user is already signed in, or fails to sign in, this will not be called. 
	var loginCallBackAjax = function(){
		Ext.Ajax.request(options);
	} 
	_oa.globalListeners.processResponse(response,loginCallBackAjax);
}, this);

// 	Listen to all Ajax requestexception events 
Ext.Ajax.on('requestexception', function(conn,response,options){
	_oa.error("Alert", "Ajax error"); 
	//console.log("** error : ajax requestexception" );
	//console.log({conn:conn, response:response, options:options});
}, this);
















