ParamArgumentsUI.js 4.89 KB
/**
 * Project   : AMDA-NG
 * Name      : ParamArgumentsUI.js
 * @class   amdaUI.ParamArgumentsUI
 * @extends Ext.container.Container
 * @brief   Parameter arguments definition (View)
 * @author  Benjamin Renard
 * @version $Id: ParamArgumentsUI.js benjamin $
 */


Ext.define('amdaUI.ParamArgumentsUI', {
	extend: 'Ext.container.Container',
	alias: 'widget.paramArguments',
	
	onChange: null,
	
	// -1 -> unknown, 0 -> scalar, 1 -> Tab1D, 2 -> Tab2D
	paramType: 0,
	
	constructor: function(config) {
		this.init(config);	    
		this.callParent(arguments);
	},
	
	init : function(config) {
		var myConf = {
			layout: {
				type: 'vbox',
				align: 'stretch'
			},	
		};
		
		Ext.apply (this , Ext.apply (arguments, myConf));
	},
	
	editParameter: function(paramId, crtArgsValues, uiScope, onReady) {
		var me = this;
    	me.mask();
    	me.resetArguments();
    	AmdaAction.getParamInfo({"paramId" : paramId}, function (result, e) {
    		if (e.status === true)
    		{
    			if (!result.success)
    			{
    				if (result.message && result.message != '')
    					Ext.Msg.show({title:'Get parameter arguments error ', msg: result.message, icon: Ext.MessageBox.ERROR, buttons: Ext.Msg.OK});
    				else
    					Ext.Msg.show({title:'Get parameter arguments error', msg: 'Unknown error', icon: Ext.MessageBox.ERROR, buttons: Ext.Msg.OK});
    			}
    			else
    			{
    				//Rebuild arguments selection
    				me.rebuildArguments(result.data, uiScope);
    				//Set current values
    				if (crtArgsValues)
    					me.setValues(crtArgsValues);
    				if (onReady)
    					onReady(uiScope);
    			}
    		}
    		else
    		{
    			Ext.Msg.show({title:'Error System', msg: e.message, icon: Ext.MessageBox.ERROR, buttons: Ext.Msg.OK});
    		}
    		me.unmask();
		});
    },
    
    setValues: function(values) {
    	this.items.each(function (item) {
    		if (values[item.getId()])
    			item.setValue(values[item.getId()]);
    	}, this);
    },
    
    getValues: function() {
    	var values = {type : this.paramType};
    	this.items.each(function(item) {
    		values[item.getId()] = item.getValue();
    	});
    	
    	return values;
    },
    
    resetValues: function() {
    	this.items.each(function (item) {
    		item.reset();
    	});
    },
    
    resetArguments: function(noArgsMsg) {
    	this.removeAll();
    	if (!noArgsMsg)
    		this.add(new Ext.form.Label({text: 'No arguments for this parameter'}));
    },
    
    rebuildArguments: function(data, uiScope) {
    	this.resetArguments(true);
    	
    	this.paramType = -1;
    	if (data && data.dimensions) {
    		this.paramType = 0;
    		if (data.dimensions.dim_1 && (parseInt(data.dimensions.dim_1) > 1)) {
    			this.buildDimIndexSelection("dim_1", data, uiScope);
    			++this.paramType;
    		}
    		if (data.dimensions.dim_2 && (parseInt(data.dimensions.dim_2) > 1)) {
    			this.buildDimIndexSelection("dim_2", data, uiScope);
    			++this.paramType;
    		}
    	}
    },
    
    buildDimIndexSelection: function(relatedDim, data, uiScope) {
    	//Check if this dimension is attached to a table
		var relatedTable = null;
		if (data.tables) {
			Ext.each(data.tables, function(table, index) {
				if (table.relatedDim == relatedDim)
					relatedTable = table;
			}, this);
		}
			
		var title = '';
		
		var indexes = [];
		indexes.push({'key' : '*', 'value' : 'All'});
		
		if (relatedTable) {
			//If a table is defined for this dimension
			title = relatedTable.name;
			if (relatedTable.units != '')
				title += ' (' + relatedTable.units + ')';
			
			Ext.Object.each(relatedTable.channels, function (index, channel) {
				indexes.push({'key' : index.toString(), 'value' : index + ' : [' + channel.min + ', ' + channel.max + ']'});
			});
		}
		else {
			//Else, use components
			if (relatedDim == "dim_1")
				title = 'Dim. 1';
			else if (relatedDim == "dim_2")
				title = 'Dim. 2';
			
			Ext.Object.each(data.components, function (index, component) {
				if (relatedDim == "dim_1" && component.index_1 != "")
					indexes.push({'key' : component.index_1, 'value' : index + ' : ' + component.name});
				else if (relatedDim == "dim_2" && component.index_2 != "")
					indexes.push({'key' : component.index_2, 'value' : index + ' : ' + component.name});
			});
		}
		
		//Add combo box
		var indexesStore = Ext.create('Ext.data.Store', {
		    fields: ['key', 'value'],
		    data : indexes
		});

		var indexesCombo = Ext.create('Ext.form.ComboBox', {
		    fieldLabel: title,
		    store: indexesStore,
		    queryMode: 'local',
		    displayField: 'value',
		    valueField: 'key',
		    value: '*',
		    editable: false,
		    id: relatedDim,
		    listeners: {
            	change: function(field, newValue, oldValue, eOpts) {
            		if (this.onChange != null)
            			this.onChange(uiScope, relatedDim, newValue, oldValue);
            	},
            	scope: this
            }
		    
		});
		
		this.add(indexesCombo);
		
		return indexesCombo;
    }
});