/** * 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, onModifyHeight: null, pluginOwner: 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 (item.argId == 'dim1' || item.argId == 'dim2') { item.setValue(this.paramRequestObject.get(item.argId+'-index')); var minRangeField = this.down('textfield[name=range_min_' + item.argId + ']'); var maxRangeField = this.down('textfield[name=range_max_' + item.argId + ']'); if (minRangeField && maxRangeField) { minRangeField.setValue(this.paramRequestObject.get(item.argId+'-min-range')); maxRangeField.setValue(this.paramRequestObject.get(item.argId+'-max-range')); var isRangeField = minRangeField.up(); if (this.paramRequestObject.get(item.argId+'-is-range')) isRangeField.expand(); else isRangeField.collapse(); } } else 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) { if (item.reset) 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.dim1 && (parseInt(result.data.dimensions.dim1) > 1) || this.getRelatedTableFromDim('dim1',result.data.tables)) { this.buildDimIndexSelection("dim1", result.data, uiScope); ++this.paramType; } if (result.data.dimensions.dim2 && (parseInt(result.data.dimensions.dim2) > 1) || this.getRelatedTableFromDim('dim2',result.data.tables)) { 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); }, getRelatedTableFromDim : function(relatedDim, tables) { var relatedTable = null; if (tables) { Ext.each(tables, function(table, index) { if (table.relatedDim == relatedDim) relatedTable = table; }, this); } return relatedTable; }, buildDimIndexSelection: function(relatedDim, data, uiScope) { //Check if this dimension is attached to a table var relatedTable = this.getRelatedTableFromDim(relatedDim, data.tables); var title = ''; var indexes = []; indexes.push({'key' : '*', 'value' : 'All'}); if (relatedTable) { //If it's not a variable table => enable channel selection title = relatedTable.name; if (relatedTable.units != '') title += ' (' + relatedTable.units + ')'; if (!relatedTable.variable) 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, hidden: (relatedTable ? relatedTable.variable : false), listeners: { change: function(field, newValue, oldValue, eOpts) { this.paramRequestObject.set(relatedDim+'-index', newValue); if (this.onChange != null) this.onChange(uiScope, relatedDim, newValue, oldValue, false); }, scope: this } }); this.add(indexesCombo); if (relatedTable) { var rangeItems = [ { xtype: 'numberfield', name: 'range_min_'+relatedDim, fieldLabel: 'Min.', decimalPrecision : 3, value: 0., listeners: { change: function(field, newValue, oldValue, eOpts) { this.paramRequestObject.set(relatedDim+'-min-range', newValue); if (this.onChange != null) this.onChange(uiScope, relatedDim, newValue, oldValue, false); }, scope: this } }, { xtype: 'numberfield', name: 'range_max_'+relatedDim, fieldLabel: 'Max.', decimalPrecision : 3, value: 0., listeners: { change: function(field, newValue, oldValue, eOpts) { this.paramRequestObject.set(relatedDim+'-max-range', newValue); if (this.onChange != null) this.onChange(uiScope, relatedDim, newValue, oldValue, false); }, scope: this } } ]; var sumRangeFieldSet = Ext.create('Ext.form.FieldSet', { title: relatedTable.variable ? title + " - Sum. in range" : "Sum. in range", collapsed: true, checkboxName: 'range_checkbox_'+relatedDim, checkboxToggle: true, name: 'range_fieldset_'+relatedDim, layout: { type: 'vbox', pack: 'start', align: 'stretch' }, items: rangeItems, listeners: { expand: function(fieldset, eOpts) { if (relatedTable.variable) { if (Ext.Object.isEmpty(relatedTable.minmax)) { myDesktopApp.warningMsg('Min/Max '+title+' values are undefined
Arbitrary values are taken'); var minValue = 10; var maxValue = 10000; } else { var minValue = relatedTable.minmax.min; var maxValue = relatedTable.minmax.max; } } else { var minValue = parseInt(relatedTable.channels[0].min); var maxValue = parseInt(relatedTable.channels[relatedTable.channels.length - 1].max); } fieldset.items.items[ + (minValue > maxValue)].setValue(minValue); fieldset.items.items[ + (minValue < maxValue)].setValue(maxValue); // if (indexesCombo.getValue() == "*") { // Ext.Object.each(relatedTable.channels, function (index, channel) { // //ToDo // }); // } // else // { // // //ToDo // } indexesCombo.setDisabled(true); this.paramRequestObject.set(relatedDim+'-is-range', true); if (this.onChange != null) this.onChange(uiScope, relatedDim, true, false, false); if (this.onModifyHeight) this.onModifyHeight(this.pluginOwner); }, collapse: function(fieldset, eOpts) { indexesCombo.setDisabled(false); this.paramRequestObject.set(relatedDim+'-is-range', false); if (this.onChange != null) this.onChange(uiScope, relatedDim, false, true, false); if (this.onModifyHeight) this.onModifyHeight(this.pluginOwner); }, scope: this } }); this.add(sumRangeFieldSet); } 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); } }); } });