ParamArgumentsUI.js 9.43 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_(.*)/,
	
	paramRequestObject: null,
	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(paramRequestObject, uiScope, onReady) {
		this.paramRequestObject = paramRequestObject;
		var me = this;
    	me.mask();
    	me.resetArguments();
    	
    	var explorerModule = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.explorer.id);
    	
    	if (explorerModule) {
    		explorerModule.getParamInfo(paramRequestObject.get('paramid'), function (paramInfo) {
    			if (paramInfo)
    				me.rebuildAll(paramInfo, uiScope);
				if (onReady)
					onReady(uiScope);
				me.unmask();
    		});
    	}
    	else
    		me.unmask();
    },
    
    rebuildAll: function(paramInfoResult , uiScope) {
    	//Rebuild arguments selection
		this.rebuildArguments(paramInfoResult, uiScope);
		
		//Set parameter type (scalar, vector or Tab2D)
		this.paramRequestObject.set('type', this.paramType);
		
		//Add default template args values
		var templateArgsValues = this.paramRequestObject.get('template_args');
		if (!templateArgsValues)
			templateArgsValues = {};
		if (paramInfoResult.template && paramInfoResult.template.arguments) {
			//Add default template args definition if needed
			Ext.Object.each(paramInfoResult.template.arguments, function (argKey, argDef) {
				if (!templateArgsValues[argKey])
					templateArgsValues[argKey] = argDef['default'];
			}, this);
			
			this.paramRequestObject.set('template_args', templateArgsValues);
		}
		
		
		//Init values in interface
		this.items.each(function (item) {
    		if (!item.argId)
    			return;
    		if (this.regexp_istemplate.test(item.argId)) {
    			var arg_key = this.regexp_istemplate.exec(item.argId)[1];
    			
    			if (this.paramRequestObject.get('template_args') && this.paramRequestObject.get('template_args')[arg_key])
    				item.setValue(this.paramRequestObject.get('template_args')[arg_key]);
    		}
    		else if (this.paramRequestObject.get(item.argId))
    			item.setValue(this.paramRequestObject.get(item.argId));
    	}, this);
    },
    
    getValues: function() {
    	var values = {type : this.paramType};
    	this.items.each(function(item) {
    		if (!item.argId)
    			return;
    		if (this.regexp_istemplate.test(item.argId)) {
    			var arg_key = this.regexp_istemplate.exec(item.argId)[1];
    			if (!values['template_args'])
    				values['template_args'] = {};
    			values['template_args'][arg_key] = item.getValue();
    		}
    		else
    			values[item.argId] = 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', argId: null}));
    },
    
    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("dim1", result.data, uiScope);
    			++this.paramType;
    		}
    		if (result.data.dimensions.dim_2 && (parseInt(result.data.dimensions.dim_2) > 1)) {
    			this.buildDimIndexSelection("dim2", 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 == "dim1")
				title = 'Dim. 1';
			else if (relatedDim == "dim2")
				title = 'Dim. 2';
			
			Ext.Object.each(data.components, function (index, component) {
				if (relatedDim == "dim1" && component.index_1 != "")
					indexes.push({'key' : component.index_1, 'value' : index + ' : ' + component.name});
				else if (relatedDim == "dim2" && 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,
		    argId: relatedDim,
		    listeners: {
            	change: function(field, newValue, oldValue, eOpts) {
            		this.paramRequestObject.set(relatedDim, newValue);
            		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', {
    				argId: 'template_' + key,
    				fieldLabel: argument.name,
    				decimalPrecision : 3,
    				allowBlank       : false,
    				value: parseFloat(argument.default),
    				listeners: {
    					change: function(field, newValue, oldValue, eOpts) {
    						var template_args = me.paramRequestObject.get('template_args');
    						if (!template_args)
    	            			template_args = {};
    						template_args[key] = newValue;
    						me.paramRequestObject.set('template_args', template_args);
    						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,
    				argId: 'template_' + key,
    			    listeners: {
    	            	change: function(field, newValue, oldValue, eOpts) {
    	            		var template_args = me.paramRequestObject.get('template_args');
    	            		if (!template_args)
    	            			template_args = {};
    						template_args[key] = newValue;
    						me.paramRequestObject.set('template_args', template_args);
    						if (me.onChange != null)
    	            			me.onChange(uiScope, key, newValue, oldValue, true);
    	            	},
    	            	scope: me
    	            }
    			    
    			});
    			
    			me.add(itemsCombo);    			
    			break;
    		case 'bool' :
    			var argumentField = Ext.create('Ext.form.Checkbox', {
    				argId: 'template_' + key,
    				fieldLabel: argument.name,
    				value: (parseInt(argument.default) == 1),
    				listeners: {
    					change: function(field, newValue, oldValue, eOpts) {
    						var template_args = me.paramRequestObject.get('template_args');
    						if (!template_args)
    	            			template_args = {};
    						template_args[key] = newValue;
    						me.paramRequestObject.set('template_args', template_args);
    						if (me.onChange != null)
    							me.onChange(uiScope, key, newValue, oldValue, true);
    					},
    					scope: me
    				}
                });
    			
    			me.add(argumentField);  
    			break;
    		default:
    			console.log('Template argument type not yet implemented: '+argument.type);	
    		}
    	});
    }
});