ParamArgumentsUI.js 8.19 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',
	
	regexp_istemplate: /^template_(.*)/,
	
	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, uiScope);
    				
    				//Set values
    				var argsValues = crtArgsValues;
    				if (!argsValues)
    					argsValues = {};
    				if (result.template && result.template.arguments) {
    					if (!argsValues['template_args'])
    						argsValues['template_args'] = {};
    					//Add default template args definition if needed
    					Ext.Object.each(result.template.arguments, function (argKey, argDef) {
    						if (!argsValues['template_args'][argKey])
    							argsValues['template_args'][argKey] = argDef['default'];
    					}, me);
    				}
    				me.setValues(argsValues);
    				
    				//Set default values
    				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 (this.regexp_istemplate.test(item.getId())) {
    			var arg_key = this.regexp_istemplate.exec(item.getId())[1];
    			if (values['template_args'] && values['template_args'][arg_key])
    				item.setValue(values['template_args'][arg_key]);
    		}
    		else if (values[item.getId()])
    			item.setValue(values[item.getId()]);
    	}, this);
    },
    
    getValues: function() {
    	var values = {type : this.paramType};
    	this.items.each(function(item) {
    		if (item.getId() == 'no-arg')
    			return;
    		if (this.regexp_istemplate.test(item.getId())) {
    			var arg_key = this.regexp_istemplate.exec(item.getId())[1];
    			if (!values['template_args'])
    				values['template_args'] = {};
    			values['template_args'][arg_key] = item.getValue();
    		}
    		else
    			values[item.getId()] = item.getValue();
    	}, this);
    	
    	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 argument for this parameter', id: 'no-arg'}));
    },
    
    rebuildArguments: function(result, uiScope) {
    	this.resetArguments(true);
    	
    	this.paramType = -1;
    	if (result.data && result.data.dimensions) {
    		this.paramType = 0;
    		if (result.data.dimensions.dim_1 && (parseInt(result.data.dimensions.dim_1) > 1)) {
    			this.buildDimIndexSelection("dim_1", result.data, uiScope);
    			++this.paramType;
    		}
    		if (result.data.dimensions.dim_2 && (parseInt(result.data.dimensions.dim_2) > 1)) {
    			this.buildDimIndexSelection("dim_2", result.data, uiScope);
    			++this.paramType;
    		}
    	}
    	
    	var isTemplate = (result.template && result.template.arguments);
    	if (isTemplate)
    		this.buildTemplateArguments(result.template.arguments, uiScope);
    	
    	if ((this.paramType <= 0) && !isTemplate)
    		//Add no args message
    		this.resetArguments(false);
    },
    
    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, false);
            	},
            	scope: this
            }
		    
		});
		
		this.add(indexesCombo);
		
		return indexesCombo;
    },
    
    buildTemplateArguments: function(arguments, uiScope) {
    	var me = this;
    	Ext.Object.each(arguments, function (key, argument) {
    		switch (argument.type) {
    		case 'float' :
    			var argumentField = Ext.create('Ext.form.NumberField', {
    				id: 'template_' + key,
    				fieldLabel: argument.name,
    				decimalPrecision : 3,
    				allowBlank       : false,
    				value: parseFloat(argument.default),
    				listeners: {
    					change: function(field, newValue, oldValue, eOpts) {
    						if (me.onChange != null)
    							me.onChange(uiScope, key, newValue, oldValue, true);
    					},
    					scope: me
    				}
                });
    			
    			me.add(argumentField);  
    			break;
    		case 'list' :
    			var items = [];
    			Ext.Object.each(argument.items, function (itemKey, itemName) {
    				items.push({'key' : itemKey, 'value' : itemName});
    			});
    			
    			var itemsStore = Ext.create('Ext.data.Store', {
    			    fields: ['key', 'value'],
    			    data : items
    			});
    			
    			var itemsCombo = Ext.create('Ext.form.ComboBox', {
    			    fieldLabel: argument.name,
    			    store: itemsStore,
    			    queryMode: 'local',
    			    displayField: 'value',
    			    valueField: 'key',
    			    value: argument.default,
    			    editable: false,
    			    id: 'template_' + key,
    			    listeners: {
    	            	change: function(field, newValue, oldValue, eOpts) {
    	            		if (me.onChange != null)
    	            			me.onChange(uiScope, key, newValue, oldValue, true);
    	            	},
    	            	scope: me
    	            }
    			    
    			});
    			
    			me.add(itemsCombo);    			
    			break;
    		default:
    			console.log('Template argument type not yet implemented: '+argument.type);	
    		}
    	});
    }
});