Blame view

js/app/views/ParamArgumentsUI.js 13.9 KB
7ac3ce50   Benjamin Renard   First implementat...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * 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',
	
51b7c77c   Benjamin Renard   Add templated par...
16
17
	regexp_istemplate: /^template_(.*)/,
	
bb6e93d9   Benjamin Renard   Implement templat...
18
	paramRequestObject: null,
7ac3ce50   Benjamin Renard   First implementat...
19
	onChange: null,
690e0a87   Benjamin Renard   Add sum in table ...
20
21
	onModifyHeight: null,
	pluginOwner: null,
7ac3ce50   Benjamin Renard   First implementat...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
	
	// -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));
	},
	
bb6e93d9   Benjamin Renard   Implement templat...
42
43
	editParameter: function(paramRequestObject, uiScope, onReady) {
		this.paramRequestObject = paramRequestObject;
7ac3ce50   Benjamin Renard   First implementat...
44
45
46
		var me = this;
    	me.mask();
    	me.resetArguments();
0314bd32   Benjamin Renard   Keep a registry o...
47
48
49
50
    	
    	var explorerModule = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.explorer.id);
    	
    	if (explorerModule) {
bb6e93d9   Benjamin Renard   Implement templat...
51
52
53
54
    		explorerModule.getParamInfo(paramRequestObject.get('paramid'), function (paramInfo) {
    			if (paramInfo)
    				me.rebuildAll(paramInfo, uiScope);
				if (onReady)
0314bd32   Benjamin Renard   Keep a registry o...
55
					onReady(uiScope);
bb6e93d9   Benjamin Renard   Implement templat...
56
57
				me.unmask();
    		});
0314bd32   Benjamin Renard   Keep a registry o...
58
    	}
bb6e93d9   Benjamin Renard   Implement templat...
59
    	else
7ac3ce50   Benjamin Renard   First implementat...
60
    		me.unmask();
7ac3ce50   Benjamin Renard   First implementat...
61
62
    },
    
bb6e93d9   Benjamin Renard   Implement templat...
63
    rebuildAll: function(paramInfoResult , uiScope) {
0314bd32   Benjamin Renard   Keep a registry o...
64
65
66
    	//Rebuild arguments selection
		this.rebuildArguments(paramInfoResult, uiScope);
		
bb6e93d9   Benjamin Renard   Implement templat...
67
68
69
70
71
72
73
		//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 = {};
0314bd32   Benjamin Renard   Keep a registry o...
74
		if (paramInfoResult.template && paramInfoResult.template.arguments) {
0314bd32   Benjamin Renard   Keep a registry o...
75
76
			//Add default template args definition if needed
			Ext.Object.each(paramInfoResult.template.arguments, function (argKey, argDef) {
bb6e93d9   Benjamin Renard   Implement templat...
77
78
				if (!templateArgsValues[argKey])
					templateArgsValues[argKey] = argDef['default'];
0314bd32   Benjamin Renard   Keep a registry o...
79
			}, this);
bb6e93d9   Benjamin Renard   Implement templat...
80
81
			
			this.paramRequestObject.set('template_args', templateArgsValues);
0314bd32   Benjamin Renard   Keep a registry o...
82
		}
bb6e93d9   Benjamin Renard   Implement templat...
83
84
85
86
87
88
89
90
91
92
93
		
		
		//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]);
51b7c77c   Benjamin Renard   Add templated par...
94
    		}
690e0a87   Benjamin Renard   Add sum in table ...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    		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
bb6e93d9   Benjamin Renard   Implement templat...
110
    			item.setValue(this.paramRequestObject.get(item.argId));
7ac3ce50   Benjamin Renard   First implementat...
111
112
113
114
115
116
    	}, this);
    },
    
    getValues: function() {
    	var values = {type : this.paramType};
    	this.items.each(function(item) {
bb6e93d9   Benjamin Renard   Implement templat...
117
    		if (!item.argId)
51b7c77c   Benjamin Renard   Add templated par...
118
    			return;
bb6e93d9   Benjamin Renard   Implement templat...
119
120
    		if (this.regexp_istemplate.test(item.argId)) {
    			var arg_key = this.regexp_istemplate.exec(item.argId)[1];
51b7c77c   Benjamin Renard   Add templated par...
121
122
123
124
125
    			if (!values['template_args'])
    				values['template_args'] = {};
    			values['template_args'][arg_key] = item.getValue();
    		}
    		else
bb6e93d9   Benjamin Renard   Implement templat...
126
    			values[item.argId] = item.getValue();
51b7c77c   Benjamin Renard   Add templated par...
127
    	}, this);
7ac3ce50   Benjamin Renard   First implementat...
128
129
130
131
132
133
    	
    	return values;
    },
    
    resetValues: function() {
    	this.items.each(function (item) {
690e0a87   Benjamin Renard   Add sum in table ...
134
135
    		if (item.reset)
    			item.reset();
7ac3ce50   Benjamin Renard   First implementat...
136
137
138
139
140
141
    	});
    },
    
    resetArguments: function(noArgsMsg) {
    	this.removeAll();
    	if (!noArgsMsg)
bb6e93d9   Benjamin Renard   Implement templat...
142
    		this.add(new Ext.form.Label({text: 'No argument for this parameter', argId: null}));
7ac3ce50   Benjamin Renard   First implementat...
143
144
    },
    
51b7c77c   Benjamin Renard   Add templated par...
145
    rebuildArguments: function(result, uiScope) {
7ac3ce50   Benjamin Renard   First implementat...
146
147
148
    	this.resetArguments(true);
    	
    	this.paramType = -1;
51b7c77c   Benjamin Renard   Add templated par...
149
    	if (result.data && result.data.dimensions) {
7ac3ce50   Benjamin Renard   First implementat...
150
    		this.paramType = 0;
690e0a87   Benjamin Renard   Add sum in table ...
151
    		if (result.data.dimensions.dim1 && (parseInt(result.data.dimensions.dim1) > 1) || this.getRelatedTableFromDim('dim1',result.data.tables)) {
bb6e93d9   Benjamin Renard   Implement templat...
152
    			this.buildDimIndexSelection("dim1", result.data, uiScope);
7ac3ce50   Benjamin Renard   First implementat...
153
154
    			++this.paramType;
    		}
690e0a87   Benjamin Renard   Add sum in table ...
155
    		if (result.data.dimensions.dim2 && (parseInt(result.data.dimensions.dim2) > 1) || this.getRelatedTableFromDim('dim2',result.data.tables)) {
bb6e93d9   Benjamin Renard   Implement templat...
156
    			this.buildDimIndexSelection("dim2", result.data, uiScope);
7ac3ce50   Benjamin Renard   First implementat...
157
158
159
    			++this.paramType;
    		}
    	}
33ce72a7   Benjamin Renard   Do not show argum...
160
    	
51b7c77c   Benjamin Renard   Add templated par...
161
162
163
164
165
    	var isTemplate = (result.template && result.template.arguments);
    	if (isTemplate)
    		this.buildTemplateArguments(result.template.arguments, uiScope);
    	
    	if ((this.paramType <= 0) && !isTemplate)
33ce72a7   Benjamin Renard   Do not show argum...
166
167
    		//Add no args message
    		this.resetArguments(false);
7ac3ce50   Benjamin Renard   First implementat...
168
169
    },
    
690e0a87   Benjamin Renard   Add sum in table ...
170
171
172
173
    getRelatedTableFromDim : function(relatedDim, tables) {
    	var relatedTable = null;
		if (tables) {
			Ext.each(tables, function(table, index) {
7ac3ce50   Benjamin Renard   First implementat...
174
175
176
177
				if (table.relatedDim == relatedDim)
					relatedTable = table;
			}, this);
		}
690e0a87   Benjamin Renard   Add sum in table ...
178
179
180
181
182
183
184
		return relatedTable;
    },
    
    buildDimIndexSelection: function(relatedDim, data, uiScope) {
    	//Check if this dimension is attached to a table
		var relatedTable = this.getRelatedTableFromDim(relatedDim, data.tables);
		
7ac3ce50   Benjamin Renard   First implementat...
185
186
187
188
189
190
		var title = '';
		
		var indexes = [];
		indexes.push({'key' : '*', 'value' : 'All'});
		
		if (relatedTable) {
690e0a87   Benjamin Renard   Add sum in table ...
191
			//If it's not a variable table => enable channel selection
7ac3ce50   Benjamin Renard   First implementat...
192
193
194
195
			title = relatedTable.name;
			if (relatedTable.units != '')
				title += ' (' + relatedTable.units + ')';
			
690e0a87   Benjamin Renard   Add sum in table ...
196
197
198
199
			if (!relatedTable.variable)
				Ext.Object.each(relatedTable.channels, function (index, channel) {
					indexes.push({'key' : index.toString(), 'value' : index + ' : [' + channel.min + ', ' + channel.max + ']'});
				});
7ac3ce50   Benjamin Renard   First implementat...
200
201
202
		}
		else {
			//Else, use components
bb6e93d9   Benjamin Renard   Implement templat...
203
			if (relatedDim == "dim1")
7ac3ce50   Benjamin Renard   First implementat...
204
				title = 'Dim. 1';
bb6e93d9   Benjamin Renard   Implement templat...
205
			else if (relatedDim == "dim2")
7ac3ce50   Benjamin Renard   First implementat...
206
207
208
				title = 'Dim. 2';
			
			Ext.Object.each(data.components, function (index, component) {
bb6e93d9   Benjamin Renard   Implement templat...
209
				if (relatedDim == "dim1" && component.index_1 != "")
7ac3ce50   Benjamin Renard   First implementat...
210
					indexes.push({'key' : component.index_1, 'value' : index + ' : ' + component.name});
bb6e93d9   Benjamin Renard   Implement templat...
211
				else if (relatedDim == "dim2" && component.index_2 != "")
7ac3ce50   Benjamin Renard   First implementat...
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
					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,
bb6e93d9   Benjamin Renard   Implement templat...
230
		    argId: relatedDim,
690e0a87   Benjamin Renard   Add sum in table ...
231
		    hidden: (relatedTable ? relatedTable.variable : false),
7ac3ce50   Benjamin Renard   First implementat...
232
233
		    listeners: {
            	change: function(field, newValue, oldValue, eOpts) {
690e0a87   Benjamin Renard   Add sum in table ...
234
            		this.paramRequestObject.set(relatedDim+'-index', newValue);
7ac3ce50   Benjamin Renard   First implementat...
235
            		if (this.onChange != null)
51b7c77c   Benjamin Renard   Add templated par...
236
            			this.onChange(uiScope, relatedDim, newValue, oldValue, false);
7ac3ce50   Benjamin Renard   First implementat...
237
238
239
240
241
242
243
244
            	},
            	scope: this
            }
		    
		});
		
		this.add(indexesCombo);
		
690e0a87   Benjamin Renard   Add sum in table ...
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
		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
                	}
                }
			];
			
9a9c6ebf   Elena.Budnik   sum in variable r...
279
280
			var sumRangeFieldSet = Ext.create('Ext.form.FieldSet', {
				title: relatedTable.variable ? title + " - Sum. in range" : "Sum. in range",
690e0a87   Benjamin Renard   Add sum in table ...
281
				collapsed: true,
9a9c6ebf   Elena.Budnik   sum in variable r...
282
283
284
285
286
287
288
				checkboxName: 'range_checkbox_'+relatedDim,
				checkboxToggle: true,
				name: 'range_fieldset_'+relatedDim,
				layout: {
						type: 'vbox',
						pack: 'start',
						align: 'stretch'
690e0a87   Benjamin Renard   Add sum in table ...
289
290
291
292
				},
				items: rangeItems,
				listeners: {
					expand: function(fieldset, eOpts) {
925f2ce0   Elena.Budnik   error msg if sum-...
293
							if (relatedTable.variable) {
12547118   Elena.Budnik   auto fill of min/...
294
295
296
297
298
299
300
301
302
								if (Ext.Object.isEmpty(relatedTable.minmax)) {
									myDesktopApp.warningMsg('Min/Max '+title+' values are undefined<br/>Arbitrary values are taken');
									var minValue = 10;
									var maxValue = 10000;
								}
								else {
									var minValue = relatedTable.minmax.min;
									var maxValue = relatedTable.minmax.max;
								}
925f2ce0   Elena.Budnik   error msg if sum-...
303
304
							}
							else {
605e4bfd   Elena.Budnik   parseInt()
305
306
								var minValue = parseInt(relatedTable.channels[0].min);
								var maxValue = parseInt(relatedTable.channels[relatedTable.channels.length - 1].max);
9a9c6ebf   Elena.Budnik   sum in variable r...
307
308
309
							}
							fieldset.items.items[ + (minValue > maxValue)].setValue(minValue); 
							fieldset.items.items[ + (minValue < maxValue)].setValue(maxValue);
925f2ce0   Elena.Budnik   error msg if sum-...
310
311
312
313
314
315
316
317
318
319
	// 						if (indexesCombo.getValue() == "*") {
	// 							Ext.Object.each(relatedTable.channels, function (index, channel) {
	// 								//ToDo
	// 							});
	// 						}
	// 						else
	// 						{
	// 							 
	// 							//ToDo
	// 						}
9a9c6ebf   Elena.Budnik   sum in variable r...
320
							indexesCombo.setDisabled(true);
925f2ce0   Elena.Budnik   error msg if sum-...
321
								
9a9c6ebf   Elena.Budnik   sum in variable r...
322
323
324
325
326
							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);
690e0a87   Benjamin Renard   Add sum in table ...
327
328
					},
					collapse: function(fieldset, eOpts) {
9a9c6ebf   Elena.Budnik   sum in variable r...
329
330
331
332
333
334
335
336
337
							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
690e0a87   Benjamin Renard   Add sum in table ...
338
				}
9a9c6ebf   Elena.Budnik   sum in variable r...
339
			});
690e0a87   Benjamin Renard   Add sum in table ...
340
341
			this.add(sumRangeFieldSet);
		}
7ac3ce50   Benjamin Renard   First implementat...
342
		return indexesCombo;
9a9c6ebf   Elena.Budnik   sum in variable r...
343
	},
51b7c77c   Benjamin Renard   Add templated par...
344
345
346
347
348
349
350
    
    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', {
bb6e93d9   Benjamin Renard   Implement templat...
351
    				argId: 'template_' + key,
51b7c77c   Benjamin Renard   Add templated par...
352
353
354
355
356
357
    				fieldLabel: argument.name,
    				decimalPrecision : 3,
    				allowBlank       : false,
    				value: parseFloat(argument.default),
    				listeners: {
    					change: function(field, newValue, oldValue, eOpts) {
bb6e93d9   Benjamin Renard   Implement templat...
358
359
360
361
362
    						var template_args = me.paramRequestObject.get('template_args');
    						if (!template_args)
    	            			template_args = {};
    						template_args[key] = newValue;
    						me.paramRequestObject.set('template_args', template_args);
51b7c77c   Benjamin Renard   Add templated par...
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
    						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,
bb6e93d9   Benjamin Renard   Implement templat...
391
    				argId: 'template_' + key,
51b7c77c   Benjamin Renard   Add templated par...
392
393
    			    listeners: {
    	            	change: function(field, newValue, oldValue, eOpts) {
bb6e93d9   Benjamin Renard   Implement templat...
394
395
396
397
398
399
    	            		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)
51b7c77c   Benjamin Renard   Add templated par...
400
401
402
403
404
405
406
407
408
    	            			me.onChange(uiScope, key, newValue, oldValue, true);
    	            	},
    	            	scope: me
    	            }
    			    
    			});
    			
    			me.add(itemsCombo);    			
    			break;
54be8b2e   Benjamin Renard   Add boolean argum...
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
    		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;
51b7c77c   Benjamin Renard   Add templated par...
430
431
432
433
    		default:
    			console.log('Template argument type not yet implemented: '+argument.type);	
    		}
    	});
7ac3ce50   Benjamin Renard   First implementat...
434
435
    }
});