Blame view

js/app/views/ParameterUI.js 15.1 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
6
7
/**
 * Project   : AMDA-NG
 * Name      : ParameterUI.js
 * @class 	 amdaUI.ParameterUI
 * @extends  Ext.container.Container
 * @brief	 Parameter  Module UI definition (View)
 * @author 	  
3ab0bee9   Elena.Budnik   aprams with args ...
8
 * @version  $Id: ParameterUI.js 2257 2014-04-02 12:21:27Z elena $             
16035364   Benjamin Renard   First commit
9
10
 */

3ab0bee9   Elena.Budnik   aprams with args ...
11
12
Ext.define('amdaUI.ParameterUI', 
{
16035364   Benjamin Renard   First commit
13
	extend: 'Ext.container.Container',
3ab0bee9   Elena.Budnik   aprams with args ...
14

16035364   Benjamin Renard   First commit
15
16
17
	alias: 'widget.panelParam',
	
	requires: [
3ab0bee9   Elena.Budnik   aprams with args ...
18
19
20
21
22
		'amdaUI.AliasUI',     
		'amdaUI.CalculatorUI',
		'extensions.SelectableTextArea',
		'amdaUI.ParamArgumentsPlug'
	],
16035364   Benjamin Renard   First commit
23
	
3ab0bee9   Elena.Budnik   aprams with args ...
24
25
26
27
28
29
30
31
32
33
34
	constructor: function(config) 
	{    
		this.init(config);
		this.callParent(arguments);
		// load object into form
		this.formPanel.getForm().loadRecord(this.object);
		var paramArgsPlug = this.getPlugin('derived-param-arguments-plugin');
		
		if (paramArgsPlug)
			paramArgsPlug.onApply = this.onApplyParameterArgs;
	},
16035364   Benjamin Renard   First commit
35

3ab0bee9   Elena.Budnik   aprams with args ...
36
37
38
39
40
41
42
	addParamInEditor : function(param) 
	{
		var selection = this.constructionField.getSelection();
		this.constructionField.setValue(selection.beforeText + param + selection.afterText);
		this.constructionField.focus();
		this.constructionField.setCaretPosition(this.constructionField.getValue().length);
	},
33ce72a7   Benjamin Renard   Do not show argum...
43
    
3ab0bee9   Elena.Budnik   aprams with args ...
44
45
46
47
48
	onApplyParameterArgs : function(uiScope, paramRequestObject) 
	{
		var fullParam = paramRequestObject.getParamFullName();
		uiScope.addParamInEditor(fullParam);
	},
7ac3ce50   Benjamin Renard   First implementat...
49
    
3ab0bee9   Elena.Budnik   aprams with args ...
50
51
52
53
54
55
56
	addParam : function(newParamName, isLeaf, needArgs, components) 
	{
		if (!isLeaf || needArgs || components)
			this.editParameterArgs(newParamName, components);
		else
			this.addParamInEditor(newParamName);
	},
16035364   Benjamin Renard   First commit
57
    
3ab0bee9   Elena.Budnik   aprams with args ...
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
	editParameterArgs: function(name, components) 
	{
		var paramObj = {
			paramid: name,
			type: 0,
			'dim1-is-range': false,
			'dim1-min-range': 0.,
			'dim1-max-range': 0.,
			'dim1-index': '*',
			'dim2-is-range': false,
			'dim2-min-range': 0.,
			'dim2-max-range': 0.,
			'dim2-index': '*',
			template_args: {}
		};
	
		if (components) 
		{
			if (components['index1']) {
				paramObj['dim1-index'] = components['index1'];
				++paramObj['type'];
			}
			
			if (components['index2']) {
				paramObj['dim2-index'] = components['index2'];
				++paramObj['type'];
			}
		}
	
7ac3ce50   Benjamin Renard   First implementat...
87
		var paramArgsPlug = this.getPlugin('derived-param-arguments-plugin');
3ab0bee9   Elena.Budnik   aprams with args ...
88
		
bb6e93d9   Benjamin Renard   Implement templat...
89
		if (paramArgsPlug) {
690e0a87   Benjamin Renard   Add sum in table ...
90
			var workinRequestParamObject = Ext.create('amdaModel.RequestParamObject', paramObj);
bb6e93d9   Benjamin Renard   Implement templat...
91
92
			paramArgsPlug.show('derived-param-arguments-plugin', workinRequestParamObject);
		}
7ac3ce50   Benjamin Renard   First implementat...
93
94
	},
    
3ab0bee9   Elena.Budnik   aprams with args ...
95
96
97
98
99
100
	setObject : function (object) 
	{
		this.object = object; 
		// load object into form	 
		this.formPanel.getForm().loadRecord(this.object);
	},
16035364   Benjamin Renard   First commit
101
       
3ab0bee9   Elena.Budnik   aprams with args ...
102
103
104
105
106
107
108
109
	/**
	* update this.object from form
	*/
	updateObject : function()
	{    
		// get the basic form
		var basicForm = this.formPanel.getForm();        
		var updateStatus = true;
16035364   Benjamin Renard   First commit
110

3ab0bee9   Elena.Budnik   aprams with args ...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
		var fieldsWithoutName = basicForm.getFields().items;
		Ext.Array.each(fieldsWithoutName, function(item, index,allItems)
		{
			if(item !== this.fieldName) 
			{ 		     
				if (!item.isValid()) {
					// set update isn't allowed
					updateStatus = false;    
				}
			}
		}, this);
		// if the update is allowed
		if (updateStatus) {
			/// real object update
			// update TimeTable object with the content of form
			basicForm.updateRecord(this.object);	
16035364   Benjamin Renard   First commit
127
		}
3ab0bee9   Elena.Budnik   aprams with args ...
128
129
130
		// return the update status
		return updateStatus;	    
	},
16035364   Benjamin Renard   First commit
131
 
3ab0bee9   Elena.Budnik   aprams with args ...
132
133
	updateConstruct : function(oldval,newval) 
	{
16035364   Benjamin Renard   First commit
134
135
136
137
138
		// update constructionField (Construct parameter) in window parameter
		var expression = this.constructionField.value;
		oldval = oldval.replace(/[(]/g,"\\(");
		oldval = oldval.replace(/[)]/g,"\\)");	
		var reg=new RegExp(oldval, "g");
3ab0bee9   Elena.Budnik   aprams with args ...
139
140
		expression = expression.replace(reg, newval);
		this.constructionField.setValue(expression);
16035364   Benjamin Renard   First commit
141
142
	},
    
a0b3d64e   Benjamin Renard   Derived parameter...
143
144
145
	/**
	 * Parameter compilation
	 */
3ab0bee9   Elena.Budnik   aprams with args ...
146
147
	compilParam : function($action) 
	{
a0b3d64e   Benjamin Renard   Derived parameter...
148
		var obj = {
3ab0bee9   Elena.Budnik   aprams with args ...
149
150
151
152
153
154
155
156
157
158
159
160
			paramId : 'ws_'+this.object.get('name')
		};
		AmdaAction.compilParam(obj, function (result, e) {
			if (!result || !result.success)
			{
				if (result.message)
					myDesktopApp.warningMsg(result.message);
				else
					myDesktopApp.warningMsg('Unknown error during parameter compilation');
				return;
			}
		});
a0b3d64e   Benjamin Renard   Derived parameter...
161
162
	},
	
3ab0bee9   Elena.Budnik   aprams with args ...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
	/**
	* save method called by Save button
	*/
	saveProcess : function(toRename)
	{        
		if(this.object.dirty) 
		{
			// Parameter Module
			var paramModule = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.param.id);            
			// if the name has been modified this is a creation
			if (this.object.isModified('name'))
			{                
				// if object already has an id : it's a 'rename' of an existing TimeTable
				if (this.object.get('id'))
				{
					// the context Node is the parent node of current edited one
					var contextNode = paramModule.linkedNode.parentNode;
					// link a new node to the TimeTableModule
					paramModule.createLinkedNode();
					// set the contextNode
					paramModule.linkedNode.set('contextNode',contextNode);
					// create a new object linked		   		  
					paramModule.createObject(this.object.getJsonValues());
					var paramobj = paramModule.linkedNode.get('object');
					//synchronize objects
					this.object = paramobj;
					if (toRename) paramModule.linkedNode.toRename = true;
				}                
				paramModule.linkedNode.create({scope : this, callback : this.compilParam});
				paramModule.linkedNode.set('iconCls', 'icon-scalar');
				paramModule.linkedNode.set('isParameter', true);            
			} else 
			{            
				paramModule.linkedNode.set('contextNode',paramModule.contextNode); 
				paramModule.linkedNode.update({scope : this, callback : this.compilParam});				
			}
		}        
	},
16035364   Benjamin Renard   First commit
201

3ab0bee9   Elena.Budnik   aprams with args ...
202
203
204
205
206
207
208
209
210
211
	/**
	* overwrite metod called by Save button
	*/
	overwriteProcess : function(btn)
	{
		if (btn == 'cancel') return; 
			
		this.fieldName.clearInvalid();  
		this.saveProcess(true);        
	},
16035364   Benjamin Renard   First commit
212
    
3ab0bee9   Elena.Budnik   aprams with args ...
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
	/**
	*   Check if brackets are balanced
	*/   
	isBalanced : function(str)
	{  
		str =  (""+str).replace(/[^()\[\]{}]/g, ""); 
		var bracket = {
						"]": "[",
						"}": "{",
						")": "("
					},
			openBrackets = [], 
			isClean = true,
			i = 0,
			len = str.length;
16035364   Benjamin Renard   First commit
228

3ab0bee9   Elena.Budnik   aprams with args ...
229
230
231
232
233
234
235
236
237
238
239
240
241
		for(; isClean && i<len; i++ )
		{                
			if( bracket[ str[ i ] ] ){
				isClean = ( openBrackets.pop() === bracket[ str[ i ] ] );
			} else {
				openBrackets.push(str[i]);
			}
		}
		if (!(isClean && !openBrackets.length)) return 'Brackets are not balanced';
		
		return true; 
	},
	
16035364   Benjamin Renard   First commit
242
243
244
245
	/**
	 * Check if changes were made before closing window 
	 * @return true if changes
	 */	   
3ab0bee9   Elena.Budnik   aprams with args ...
246
247
	fclose : function() 
	{
16035364   Benjamin Renard   First commit
248
249
250
251
		var isDirty = this.formPanel.getForm().isDirty();
		return isDirty;
	},
	
3ab0bee9   Elena.Budnik   aprams with args ...
252
253
	init: function(config) 
	{		
16035364   Benjamin Renard   First commit
254
		this.fieldName = new Ext.form.field.Text({
3ab0bee9   Elena.Budnik   aprams with args ...
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
				labelAlign: 'top', itemId: 'formParamName',
				fieldLabel: 'Parameter Name*',
				name : 'name',
				allowBlank : false, //blankText : 'Name is required',
				emptyText: 'Please only low case here!',
				width: 150,
				validateOnChange: false,
				validateOnBlur: false,
				enableKeyEvents: true,
				validFlag: false,
				validator : function() 
				{
					return this.validFlag;
				},
				stripCharsRe: /(^\s+|\s+$)/g,
				listeners : 
				{
					keyUp : function(field, e, opt) 
					{
						this.setValue(this.getValue().toLowerCase());
					}
				}
16035364   Benjamin Renard   First commit
277
		});
3ab0bee9   Elena.Budnik   aprams with args ...
278
	
16035364   Benjamin Renard   First commit
279
280
281
282
283
284
285
286
287
		this.constructionField = new extensions.SelectableTextArea({
			labelAlign: 'top',
			itemId: 'formParamConstructParameter',
			fieldLabel:'Construct Parameter*',
		 	allowBlank : false, blankText : 'Construct Parameter is required',
			name : 'buildchain',
			flex: 0.9,
			validateOnChange: false,
			validateOnBlur: false,
3ab0bee9   Elena.Budnik   aprams with args ...
288
			validator : this.isBalanced,
16035364   Benjamin Renard   First commit
289
290
			listeners :
			{
3ab0bee9   Elena.Budnik   aprams with args ...
291
292
293
294
295
296
297
298
299
300
				render : function(o,op)
				{
					var me = this;
					var meEl = me.bodyEl;
					var dropTarget = Ext.create('Ext.dd.DropTarget', meEl, 
					{
						ddGroup: 'explorerTree',
						notifyEnter : function(ddSource, e, data) {},
						notifyOver  : function(ddSource, e, data)
						{
8497e83c   Elena.Budnik   redmine 5515
301
							if (data.records[0].get('nodeType') == 'localParam' && data.records[0].get('notyet')) 
3ab0bee9   Elena.Budnik   aprams with args ...
302
303
304
305
306
							{
								this.valid = false;
								return this.dropNotAllowed;   
							}
							
8497e83c   Elena.Budnik   redmine 5515
307
308
309
310
311
312
313
							if (((data.records[0].get('nodeType') == 'localParam')   ||
								(data.records[0].get('nodeType') == 'remoteParam') ||
								(data.records[0].get('nodeType') == 'remoteSimuParam') ||
								(data.records[0].get('nodeType') == 'derivedParam') ||
								(data.records[0].get('nodeType')== 'myDataParam') ||
								(data.records[0].get('nodeType') == 'alias')) &&
								(data.records[0].isLeaf() || data.records[0].get('isParameter')) && 
3ab0bee9   Elena.Budnik   aprams with args ...
314
315
316
317
318
319
320
321
322
323
324
325
326
327
								!(data.records[0].data.disable))
							{
								this.valid = true;
								return this.dropAllowed;
							}
							this.valid = false;
							return this.dropNotAllowed;
						},
						notifyDrop  : function(ddSource, e, data)
						{			          
							if (!this.valid)
								return false;
							var nameToSent;
							var components = null;
8497e83c   Elena.Budnik   redmine 5515
328
							switch (data.records[0].get('nodeType'))
3ab0bee9   Elena.Budnik   aprams with args ...
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
							{
								case 'localParam' :
								case 'remoteParam' :
								case 'remoteSimuParam' :    
									nameToSent = data.records[0].get('id');
									if (data.records[0].get('alias')!= "" )
										var nameToSent = "#"+data.records[0].get('alias');
									var component_info = data.records[0].get('component_info');
									if (component_info && component_info.parentId) 
									{													
										if ( component_info.index1 && component_info.index2 )
										{															 
											nameToSent = component_info.parentId;
											components = [];												
											if (component_info.index1)
												components['index1'] = component_info.index1;
											if (component_info.index2)
												components['index2'] = component_info.index2;
										}	
										if ( data.records[0].get('needsArgs') )
										{															
											nameToSent = component_info.parentId;	
											if (component_info.index1)
											{
												components = [];	
												components['index1'] = component_info.index1;
											}
										}													
									}
									break;
								case 'alias' :
									nameToSent = "#"+data.records[0].get('text');
									break;
								case 'derivedParam' :
									nameToSent = "ws_"+data.records[0].get('text');
									break;
								case 'myDataParam' :
									var name = data.records[0].get('text');
									nameToSent = "wsd_"+name;
									var size = data.records[0].get('size');
									if (size &&  size > 1) 
									{
										nameToSent += "(0)";
										myDesktopApp.warningMsg("parameter "+name+" is array of size: "
												+size+"<br/>Please put index");
									}
									break;
								default :
									return false;
							}
							var paramModule = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.param.id);
							if (paramModule) {
								paramModule.addParam(nameToSent, data.records[0].get('leaf'), data.records[0].get('needsArgs'), components);
							}
							return true;
						}
					});
				}
16035364   Benjamin Renard   First commit
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
			}
            //TODO: ?validator on formula structure?
//			listener : {
//				valid : function(field) {
//
//				}
//			}
		});
		
		this.formPanel = new Ext.form.Panel({
			id: 'vbox-paramForm',			
			bodyStyle: {background : '#dfe8f6'},
			border: false,
			buttonAlign: 'left',
			region: 'center',
			trackResetOnLoad: true, //reset to the last loaded record
			layout: {
3ab0bee9   Elena.Budnik   aprams with args ...
404
405
406
				type: 'vbox',
				defaultMargins: {top: 10, left:10, bottom:5, right:5},			   
				align: 'stretch'
16035364   Benjamin Renard   First commit
407
408
			},
			defaults: {
3ab0bee9   Elena.Budnik   aprams with args ...
409
				border: false
16035364   Benjamin Renard   First commit
410
411
412
			},
			items: [
					{
3ab0bee9   Elena.Budnik   aprams with args ...
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
						flex: 1,
						xtype: 'container',
						border: false,
						layout: {
							type: 'table',
							columns: 3
						},
						defaultType: 'textfield',
						defaults: {
							labelAlign: 'top'
						},
						items: [
							this.fieldName,
							{
								xtype:'component', width: 20
							},
							{
0f7460c1   Elena.Budnik   redmine 5518
430
								xtype : 'numberfield',
3ab0bee9   Elena.Budnik   aprams with args ...
431
432
433
								itemId: 'formParamTimeStep', fieldLabel: 'Time Step (sec)*', labelAlign: 'top',
								allowBlank : false, blankText : 'Time Step is required',
								name : 'timestep',
0f7460c1   Elena.Budnik   redmine 5518
434
435
436
437
								minValue : 0.001,
								hideTrigger: true,
								width: 150,
								//validateOnChange: false,
3ab0bee9   Elena.Budnik   aprams with args ...
438
439
440
								validateOnBlur: false
							},
							{
3ab0bee9   Elena.Budnik   aprams with args ...
441
442
443
444
445
446
447
448
								itemId: 'formParamUnit', 
								fieldLabel: 'Units',
								name : 'units',
								width: 150
							},
							{
								xtype:'component', width: 20
							},
0f7460c1   Elena.Budnik   redmine 5518
449
							{								
3ab0bee9   Elena.Budnik   aprams with args ...
450
451
452
453
454
455
								itemId: 'formParamYTitle', 
								fieldLabel: 'Y Title for Plot',
								name : 'ytitle',
								width: 150
							},
							{
3ab0bee9   Elena.Budnik   aprams with args ...
456
457
458
459
460
461
462
463
								itemId: 'formParamDescription',
								name: 'description', 
								xtype: 'textarea', fieldLabel:'Description',
								width: 320,  height: 75,
								colspan: 3
							}]                        
					},
					this.constructionField
16035364   Benjamin Renard   First commit
464
465
466
				],		        
				fbar: [
					{
3ab0bee9   Elena.Budnik   aprams with args ...
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
						text: 'Save',
						scope : this,
						handler: function(){
							if (this.updateObject())
							{
								var paramModule = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.param.id);	
								if (!paramModule)
									return;
								var me = this;
								paramModule.linkedNode.isValidName(this.fieldName.getValue(), function (res) 
								{
									if (!res)
									{
										me.fieldName.validFlag = 'Error during object validation';
										myDesktopApp.errorMsg(me.fieldName.validFlag);
										me.fieldName.validate();
										return;
									}
16035364   Benjamin Renard   First commit
485
								  
3ab0bee9   Elena.Budnik   aprams with args ...
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
									if (!res.valid)
									{
										if (res.error)
										{
											if (res.error.search('subtree') != -1) {  							
												Ext.MessageBox.show({title:'Warning', 
															msg: res.error+'<br/>Do you want to overwrite it?',
															width: 300,
															buttons: Ext.MessageBox.OKCANCEL, 
															fn : me.overwriteProcess,
															icon: Ext.MessageBox.WARNING,
															scope : me
												});
												me.fieldName.validFlag = true;
											}
											else
												me.fieldName.validFlag = res.error;
										}
										else
										{
											me.fieldName.validFlag = 'Invalid object name';
											myDesktopApp.errorMsg(me.fieldName.validFlag);
										}
										me.fieldName.validate();
										return;
									}
									
									me.fieldName.validFlag = true;
									me.fieldName.validate();
									me.saveProcess(false);
								});							  
							}
						}
16035364   Benjamin Renard   First commit
519
520
					},
					{
3ab0bee9   Elena.Budnik   aprams with args ...
521
522
523
524
525
526
527
528
529
530
531
532
						text: 'Reset',
						scope: this,
						handler: function()
						{
							var paramModule = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.param.id);			
							paramModule.createLinkedNode();
							paramModule.createObject();
							this.setObject(paramModule.getLinkedNode().get('object'));
							/* reset with keeping old object
							this.formPanel.getForm().reset();
							*/
						}
16035364   Benjamin Renard   First commit
533
					}
3ab0bee9   Elena.Budnik   aprams with args ...
534
				] 	
16035364   Benjamin Renard   First commit
535
536
537
538
539
540
541
		});
				
		var myConf = {
				layout: 'border',
				border: false,
				defaults: { layout: 'fit', border: false },
				items: [
3ab0bee9   Elena.Budnik   aprams with args ...
542
543
544
545
546
					this.formPanel,
					{
						xtype: 'panel', region: 'south',
						title: 'Information',
						collapsible: true,
42863f42   Elena.Budnik   collapseMode: "he...
547
						collapseMode: 'header',
3ab0bee9   Elena.Budnik   aprams with args ...
548
549
550
551
552
553
554
555
						height: 100,
						autoHide: false,
						iconCls: 'icon-information',
						bodyStyle: 'padding:5px',
						loader: {
						autoLoad: true,
						url: helpDir+'myParameterHOWTO'
						}
16035364   Benjamin Renard   First commit
556
				} ],
3ab0bee9   Elena.Budnik   aprams with args ...
557
558
559
560
				plugins: [ 
						{ptype: 'calculator', myBtns:[], context: 'Parameter' }, 
						{ptype: 'paramArgumentsPlugin', pluginId: 'derived-param-arguments-plugin'}
				]
16035364   Benjamin Renard   First commit
561
562
563
564
565
			};
	              
			Ext.apply(this, Ext.apply(arguments, myConf));
	}
});