AmdaStateProvider.js 2.98 KB
/** 
 * Project   : AMDA-NG
 * Name      : AmdaStateProvider.js
 * @class    amdaDesktop.AmdaStateProvider
 * @extends  Ext.state.Provider 
 * @brief    Module controlling state storing/restoring at/from Server 
 * @author   Benjamin
 * @version  $Id: AmdaStateProvider.js 2441 2014-06-27 12:35:22Z elena $ 
 */

Ext.define('amdaDesktop.AmdaStateProvider', {
	extend: 'Ext.state.Provider',
	alias: 'widget.stateProvider',
	
	s : {},
	
	constructor : function()
	{
		var me = this;
		this.addEvents('afterrestore,aftersave');
		
		me.callParent(arguments);
	},
	
	set : function(name, value)
	{
		var me = this;
		
		if (typeof value == "undefined" || value === null)
		{
			me.clear(name);
			return;
		}
		
		me.callParent(arguments);
		
		this.state[name] = value;
		this.s[name] = me.encodeValue(value);
	},
	
	get : function(name)
	{
		return this.state[name];
	},
	
	onLoadFinish : function(result, e)
	{
		var me = this;
		
		if (e.status === true)
		{
			if (result.error)
			{
				Ext.Msg.show({title:'State provider - Load error', msg: result.message, icon: Ext.MessageBox.ERROR, buttons: Ext.Msg.OK});
			}
			else
			{
				var keys = [];
		        for (var s in result.state) {
		            if (hasOwnProperty.call(result.state, s))
		            	keys.push(s);
		        }
		        
		        var len = keys.length;
			
				for (var i = 0; i < len; i++)
				{
					if (keys[i] == "_empty_")
						continue;
					this.state[keys[i]] = me.decodeValue(result.state[keys[i]]);
					this.s[keys[i]] = result.state[keys[i]];
				}
			}
		}
		else
		{
			Ext.Msg.show({title:'Error System', msg: e.message, icon: Ext.MessageBox.ERROR, buttons: Ext.Msg.OK});
		}
		
		me.fireEvent('afterrestore', me);
	},
	
	restoreState : function()
	{
		var me = this;
		var o = {};
		AmdaAction.loadState(o,this.onLoadFinish,this);
	},
	
	clear : function(name)
	{
		this.clearKey(name);
		this.callParent(arguments);
	},
	
	clearKey : function(name)
	{
		if (this.state[name])
			this.state[name].remove();
	},
	
	onSaveFinish : function(result, e)
	{
		var me = this;
		if (e.status === true)
		{
			if (result.error)
			{
				Ext.Msg.show({title:'State provider - Save error', msg: result.message, icon: Ext.MessageBox.ERROR, buttons: Ext.Msg.OK});
			}
		}
		else
		{
			Ext.Msg.show({title:'Error System', msg: e.message, icon: Ext.MessageBox.ERROR, buttons: Ext.Msg.OK});
		}
		me.fireEvent('aftersave', me);
	},	
	
	saveState : function()
	{
		var me = this;              
		AmdaAction.saveState(this.s,this.onSaveFinish,this);
	},
        
	saveLastTime : function()
        {                                           
                var me = this;
                var lastTime = {};
                lastTime[myDesktopApp.desktop.id + '_wallpaper'] = this.s[myDesktopApp.desktop.id + '_wallpaper'];
                lastTime['timeinterval'] = this.s['timeinterval'];
                lastTime['timeintervalMining'] = this.s['timeintervalMining'];               
                AmdaAction.saveState(lastTime,this.onSaveFinish,this);
        }
});