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();
    		});
bf776dc8   Benjamin Renard   working
58
                          
0314bd32   Benjamin Renard   Keep a registry o...
59
    	}
bb6e93d9   Benjamin Renard   Implement templat...
60
    	else
7ac3ce50   Benjamin Renard   First implementat...
61
    		me.unmask();
7ac3ce50   Benjamin Renard   First implementat...
62
63
    },
    
bb6e93d9   Benjamin Renard   Implement templat...
64
    rebuildAll: function(paramInfoResult , uiScope) {
0314bd32   Benjamin Renard   Keep a registry o...
65
66
67
    	//Rebuild arguments selection
		this.rebuildArguments(paramInfoResult, uiScope);
		
bb6e93d9   Benjamin Renard   Implement templat...
68
69
70
71
72
73
74
		//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...
75
		if (paramInfoResult.template && paramInfoResult.template.arguments) {
0314bd32   Benjamin Renard   Keep a registry o...
76
77
			//Add default template args definition if needed
			Ext.Object.each(paramInfoResult.template.arguments, function (argKey, argDef) {
bb6e93d9   Benjamin Renard   Implement templat...
78
79
				if (!templateArgsValues[argKey])
					templateArgsValues[argKey] = argDef['default'];
0314bd32   Benjamin Renard   Keep a registry o...
80
			}, this);
bb6e93d9   Benjamin Renard   Implement templat...
81
82
			
			this.paramRequestObject.set('template_args', templateArgsValues);
0314bd32   Benjamin Renard   Keep a registry o...
83
		}
bb6e93d9   Benjamin Renard   Implement templat...
84
85
86
87
88
89
90
91
92
93
94
		
		
		//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...
95
    		}
690e0a87   Benjamin Renard   Add sum in table ...
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    		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...
111
    			item.setValue(this.paramRequestObject.get(item.argId));
7ac3ce50   Benjamin Renard   First implementat...
112
113
114
115
116
117
    	}, this);
    },
    
    getValues: function() {
    	var values = {type : this.paramType};
    	this.items.each(function(item) {
bb6e93d9   Benjamin Renard   Implement templat...
118
    		if (!item.argId)
51b7c77c   Benjamin Renard   Add templated par...
119
    			return;
bb6e93d9   Benjamin Renard   Implement templat...
120
121
    		if (this.regexp_istemplate.test(item.argId)) {
    			var arg_key = this.regexp_istemplate.exec(item.argId)[1];
51b7c77c   Benjamin Renard   Add templated par...
122
123
124
125
126
    			if (!values['template_args'])
    				values['template_args'] = {};
    			values['template_args'][arg_key] = item.getValue();
    		}
    		else
bb6e93d9   Benjamin Renard   Implement templat...
127
    			values[item.argId] = item.getValue();
51b7c77c   Benjamin Renard   Add templated par...
128
    	}, this);
7ac3ce50   Benjamin Renard   First implementat...
129
130
131
132
133
134
    	
    	return values;
    },
    
    resetValues: function() {
    	this.items.each(function (item) {
690e0a87   Benjamin Renard   Add sum in table ...
135
136
    		if (item.reset)
    			item.reset();
7ac3ce50   Benjamin Renard   First implementat...
137
138
139
140
141
142
    	});
    },
    
    resetArguments: function(noArgsMsg) {
    	this.removeAll();
    	if (!noArgsMsg)
bb6e93d9   Benjamin Renard   Implement templat...
143
    		this.add(new Ext.form.Label({text: 'No argument for this parameter', argId: null}));
7ac3ce50   Benjamin Renard   First implementat...
144
145
    },
    
51b7c77c   Benjamin Renard   Add templated par...
146
    rebuildArguments: function(result, uiScope) {
7ac3ce50   Benjamin Renard   First implementat...
147
148
149
    	this.resetArguments(true);
    	
    	this.paramType = -1;
51b7c77c   Benjamin Renard   Add templated par...
150
    	if (result.data && result.data.dimensions) {
7ac3ce50   Benjamin Renard   First implementat...
151
    		this.paramType = 0;
690e0a87   Benjamin Renard   Add sum in table ...
152
    		if (result.data.dimensions.dim1 && (parseInt(result.data.dimensions.dim1) > 1) || this.getRelatedTableFromDim('dim1',result.data.tables)) {
bb6e93d9   Benjamin Renard   Implement templat...
153
    			this.buildDimIndexSelection("dim1", result.data, uiScope);
7ac3ce50   Benjamin Renard   First implementat...
154
155
    			++this.paramType;
    		}
690e0a87   Benjamin Renard   Add sum in table ...
156
    		if (result.data.dimensions.dim2 && (parseInt(result.data.dimensions.dim2) > 1) || this.getRelatedTableFromDim('dim2',result.data.tables)) {
bb6e93d9   Benjamin Renard   Implement templat...
157
    			this.buildDimIndexSelection("dim2", result.data, uiScope);
7ac3ce50   Benjamin Renard   First implementat...
158
159
160
    			++this.paramType;
    		}
    	}
33ce72a7   Benjamin Renard   Do not show argum...
161
    	
51b7c77c   Benjamin Renard   Add templated par...
162
163
164
165
166
    	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...
167
168
    		//Add no args message
    		this.resetArguments(false);
7ac3ce50   Benjamin Renard   First implementat...
169
170
    },
    
690e0a87   Benjamin Renard   Add sum in table ...
171
172
173
174
    getRelatedTableFromDim : function(relatedDim, tables) {
    	var relatedTable = null;
		if (tables) {
			Ext.each(tables, function(table, index) {
7ac3ce50   Benjamin Renard   First implementat...
175
176
177
178
				if (table.relatedDim == relatedDim)
					relatedTable = table;
			}, this);
		}
690e0a87   Benjamin Renard   Add sum in table ...
179
180
181
182
183
184
185
		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...
186
187
188
189
190
191
		var title = '';
		
		var indexes = [];
		indexes.push({'key' : '*', 'value' : 'All'});
		
		if (relatedTable) {
690e0a87   Benjamin Renard   Add sum in table ...
192
			//If it's not a variable table => enable channel selection
7ac3ce50   Benjamin Renard   First implementat...
193
194
195
196
			title = relatedTable.name;
			if (relatedTable.units != '')
				title += ' (' + relatedTable.units + ')';
			
690e0a87   Benjamin Renard   Add sum in table ...
197
198
199
200
			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...
201
202
203
		}
		else {
			//Else, use components
bb6e93d9   Benjamin Renard   Implement templat...
204
			if (relatedDim == "dim1")
7ac3ce50   Benjamin Renard   First implementat...
205
				title = 'Dim. 1';
bb6e93d9   Benjamin Renard   Implement templat...
206
			else if (relatedDim == "dim2")
7ac3ce50   Benjamin Renard   First implementat...
207
208
209
				title = 'Dim. 2';
			
			Ext.Object.each(data.components, function (index, component) {
bb6e93d9   Benjamin Renard   Implement templat...
210
				if (relatedDim == "dim1" && component.index_1 != "")
7ac3ce50   Benjamin Renard   First implementat...
211
					indexes.push({'key' : component.index_1, 'value' : index + ' : ' + component.name});
bb6e93d9   Benjamin Renard   Implement templat...
212
				else if (relatedDim == "dim2" && component.index_2 != "")
7ac3ce50   Benjamin Renard   First implementat...
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
					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...
231
		    argId: relatedDim,
690e0a87   Benjamin Renard   Add sum in table ...
232
		    hidden: (relatedTable ? relatedTable.variable : false),
7ac3ce50   Benjamin Renard   First implementat...
233
234
		    listeners: {
            	change: function(field, newValue, oldValue, eOpts) {
690e0a87   Benjamin Renard   Add sum in table ...
235
            		this.paramRequestObject.set(relatedDim+'-index', newValue);
7ac3ce50   Benjamin Renard   First implementat...
236
            		if (this.onChange != null)
51b7c77c   Benjamin Renard   Add templated par...
237
            			this.onChange(uiScope, relatedDim, newValue, oldValue, false);
7ac3ce50   Benjamin Renard   First implementat...
238
239
240
241
242
243
244
245
            	},
            	scope: this
            }
		    
		});
		
		this.add(indexesCombo);
		
690e0a87   Benjamin Renard   Add sum in table ...
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
279
		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...
280
281
			var sumRangeFieldSet = Ext.create('Ext.form.FieldSet', {
				title: relatedTable.variable ? title + " - Sum. in range" : "Sum. in range",
690e0a87   Benjamin Renard   Add sum in table ...
282
				collapsed: true,
9a9c6ebf   Elena.Budnik   sum in variable r...
283
284
285
286
287
288
289
				checkboxName: 'range_checkbox_'+relatedDim,
				checkboxToggle: true,
				name: 'range_fieldset_'+relatedDim,
				layout: {
						type: 'vbox',
						pack: 'start',
						align: 'stretch'
690e0a87   Benjamin Renard   Add sum in table ...
290
291
292
293
				},
				items: rangeItems,
				listeners: {
					expand: function(fieldset, eOpts) {
925f2ce0   Elena.Budnik   error msg if sum-...
294
							if (relatedTable.variable) {
12547118   Elena.Budnik   auto fill of min/...
295
296
297
298
299
300
301
302
303
								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-...
304
305
							}
							else {
605e4bfd   Elena.Budnik   parseInt()
306
307
								var minValue = parseInt(relatedTable.channels[0].min);
								var maxValue = parseInt(relatedTable.channels[relatedTable.channels.length - 1].max);
9a9c6ebf   Elena.Budnik   sum in variable r...
308
309
310
							}
							fieldset.items.items[ + (minValue > maxValue)].setValue(minValue); 
							fieldset.items.items[ + (minValue < maxValue)].setValue(maxValue);
925f2ce0   Elena.Budnik   error msg if sum-...
311
312
313
314
315
316
317
318
319
320
	// 						if (indexesCombo.getValue() == "*") {
	// 							Ext.Object.each(relatedTable.channels, function (index, channel) {
	// 								//ToDo
	// 							});
	// 						}
	// 						else
	// 						{
	// 							 
	// 							//ToDo
	// 						}
9a9c6ebf   Elena.Budnik   sum in variable r...
321
							indexesCombo.setDisabled(true);
925f2ce0   Elena.Budnik   error msg if sum-...
322
								
9a9c6ebf   Elena.Budnik   sum in variable r...
323
324
325
326
327
							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 ...
328
329
					},
					collapse: function(fieldset, eOpts) {
9a9c6ebf   Elena.Budnik   sum in variable r...
330
331
332
333
334
335
336
337
338
							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 ...
339
				}
9a9c6ebf   Elena.Budnik   sum in variable r...
340
			});
690e0a87   Benjamin Renard   Add sum in table ...
341
342
			this.add(sumRangeFieldSet);
		}
7ac3ce50   Benjamin Renard   First implementat...
343
		return indexesCombo;
9a9c6ebf   Elena.Budnik   sum in variable r...
344
	},
51b7c77c   Benjamin Renard   Add templated par...
345
346
347
348
349
350
351
    
    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...
352
    				argId: 'template_' + key,
51b7c77c   Benjamin Renard   Add templated par...
353
354
355
356
357
358
    				fieldLabel: argument.name,
    				decimalPrecision : 3,
    				allowBlank       : false,
    				value: parseFloat(argument.default),
    				listeners: {
    					change: function(field, newValue, oldValue, eOpts) {
bb6e93d9   Benjamin Renard   Implement templat...
359
360
361
362
363
    						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...
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
391
    						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...
392
    				argId: 'template_' + key,
51b7c77c   Benjamin Renard   Add templated par...
393
394
    			    listeners: {
    	            	change: function(field, newValue, oldValue, eOpts) {
bb6e93d9   Benjamin Renard   Implement templat...
395
396
397
398
399
400
    	            		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...
401
402
403
404
405
406
407
408
409
    	            			me.onChange(uiScope, key, newValue, oldValue, true);
    	            	},
    	            	scope: me
    	            }
    			    
    			});
    			
    			me.add(itemsCombo);    			
    			break;
54be8b2e   Benjamin Renard   Add boolean argum...
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
    		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...
431
432
433
434
    		default:
    			console.log('Template argument type not yet implemented: '+argument.type);	
    		}
    	});
7ac3ce50   Benjamin Renard   First implementat...
435
    }
bf776dc8   Benjamin Renard   working
436
});