PlotCurveObject.js 5.28 KB
/**
 * Project      : AMDA-NG
 * Name         : PlotCurveObject.js
 * @class   amdaPlotObj.PlotCurveObject
 * @extends Ext.data.Model
 * @brief   Plot Curve Business Object Definition 
 * @author  Benjamin Renard
 * @version $Id: PlotCurveObject.js benjamin $
 ******************************************************************************
 *    FT Id     :   Date   : Name - Description
 ******************************************************************************
 *	:           :03/09/2015: BRE  - file creation
 */

Ext.define('amdaPlotObj.PlotCurveObject', {
	extend: 'Ext.data.Model',
	idProperty: 'id',
	
	requires: [
	           'amdaPlotObj.PlotCurveDef',
	           'amdaPlotObj.PlotCurveParamObject'
	],
	
	fields : [
	          {name: 'id', type: 'int'},
	          {name: 'curve-name', type: 'string'},
	          {name: 'curve-serie-id', type: 'int', useNull:true},
	          {name: 'curve-line-style', type: 'string'},
	          {name: 'curve-line-width', type: 'float'},
	          {name: 'curve-line-color', type: 'string'}
	],
	
	associations : [
	                {
	              	  type : 'hasMany', 
	              	  model : 'amdaPlotObj.PlotCurveParamObject',
	              	  name  : 'params'
	                }
    ],
	
	constructor: function(){
		var me = this;
        me.callParent(arguments);
        if ((arguments.length > 0) && arguments[0])
        {
        	if (arguments[0].params)
        		me.loadParams(arguments[0].params);
        }
        else
        {
        	//new object, set default fields values
        	me.setDefaultValues();
        }
        this.dirty = false;
    },
    
    getCurvesDefStore: function(onCurvesDefGet)
    {
    	var curvesDefStore = Ext.data.StoreManager.lookup('curvesDefStore');

		if (!curvesDefStore)
		{
			curvesDefStore = Ext.create('Ext.data.Store', {
				model: 'amdaPlotObj.PlotCurveDef',
				storeId: 'curvesDefStore'
			});
			
			curvesDefStore.load({
				scope: this,
				callback: function(records, operation, success) {
					if (onCurvesDefGet != null)
						onCurvesDefGet(curvesDefStore);
				}
			});
		}
		else
		{
			if (curvesDefStore.isLoading()) {
				var me = this;
				var task = new Ext.util.DelayedTask(function(){
					me.getCurvesDefStore(onCurvesDefGet);
				});
				task.delay(100);
				return;
			}
			
			if (onCurvesDefGet != null)
				onCurvesDefGet(curvesDefStore);
		}
    },
    
    setCurveName: function(curveName, onReady)
    {
    	this.set('curve-name', curveName);
    	
    	var me = this;
    	this.getCurvesDefStore(function (curvesDefStore) {
    		me.params().removeAll();
    		curvesDefStore.each(function(curveDef) {
    			if (curveDef.get('id') == curveName)
    			{
    				curveDef.params().each(function (paramDef) {
    					me.params().add({
    						'curve-param-name' : paramDef.get('name'),
    						'curve-param-internal' : paramDef.get('internal'),
    						'curve-param-value' : paramDef.get('defaultValue')});
    				});
    			}
    		});
    		if (onReady != null)
    			onReady(me);
    	});    	
    },
    
    loadParams: function(params)
    {
       this.params().loadData(params);
    },
    
    loadFromData: function(curveData,serieId)
    {
    	var me = this;
    	if (curveData['curve-name']) {
    		this.setCurveName(curveData['curve-name'], function(curveObject) {
    			if (curveData['curve-line-style'])
    				me.set('curve-line-style', curveData['curve-line-style']);
    			if (curveData['curve-line-width'])
    				me.set('curve-line-width', curveData['curve-line-width']);
    			if (curveData['curve-line-color'])
    				me.set('curve-line-color', curveData['curve-line-color']);
    			
    			me.set('curve-serie-id', serieId);
    			
    			if (curveData["curve-attributes"])
    	    		Ext.Object.each(curveData["curve-attributes"], function(key, value) {
    	    			me.params().each(function (param) {
    	    				if (param.get('curve-param-name') == value['curve-param-name'])
    	    					param.set('curve-param-value', value['curve-param-value'])
    	    			});
    	    		}, me);
    		});
    	}
    },
	
	setDefaultValues: function()
    {
    	this.set('curve-name', '');
    	this.set('curve-serie-id', null);
    	this.set('curve-line-style', amdaPlotObj.PlotObjectConfig.defaultValues.curves.line.style);
    	this.set('curve-line-width', amdaPlotObj.PlotObjectConfig.defaultValues.curves.line.width);
    	this.set('curve-line-color', amdaPlotObj.PlotObjectConfig.defaultValues.curves.line.color);
    },
    
    isDirty : function()
    {
    	if (this.dirty)
    		return true;
    	
    	var d = false;
    	this.params().each(function (param, index) {
    		if (param.dirty)
    			d = true;
    	});
    	
    	return d;
    },
    
    getJsonValues : function() 
    {
    	var curveValues  = new Object();
    	
    	curveValues['id'] = this.get('id');
    	
    	curveValues['curve-name'] = this.get('curve-name');
    	curveValues['curve-serie-id'] = this.get('curve-serie-id');
    	curveValues['curve-line-style'] = this.get('curve-line-style');
    	curveValues['curve-line-width'] = this.get('curve-line-width');
    	curveValues['curve-line-color'] = this.get('curve-line-color');
    		
    	curveValues['params'] = [];
    	
    	this.params().each(function (param, index) {
    		curveValues['params'][index] = param.getJsonValues();
    	});
    	
    	return curveValues;
    }
});