Blame view

js/app/views/ParamArgumentsUI.js 4.89 KB
7ac3ce50   Benjamin Renard   First implementat...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
 * 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;
    }
});