Blame view

js/app/views/ParamsMgrUI.js 12 KB
16035364   Benjamin Renard   First commit
1
2
/**
 * Project   : AMDA-NG
74b77f58   Elena.Budnik   init remote branch
3
4
 * Name      : ParamsMgrUI.js
 * @class    amdaUI.ParamsMgrUI
16035364   Benjamin Renard   First commit
5
6
 * @extends  Ext.Panel.Panel 
 * @brief     
74b77f58   Elena.Budnik   init remote branch
7
 * @author Elena         
16035364   Benjamin Renard   First commit
8
9
 */

74b77f58   Elena.Budnik   init remote branch
10
11
12
13
14
15
16
Ext.define('amdaUI.ParamsMgrUI',{
	extend: 'Ext.panel.Panel',	
	alias: 'widget.paramsMgrPanel',
	
	baseId : null,
	srcTree : null,
	destTree : null,
16035364   Benjamin Renard   First commit
17
    
74b77f58   Elena.Budnik   init remote branch
18
19
20
	//TODO where keep this (==baseId) description ? Global variable?
	basesNames : ['CDAWEB', 'THEMIS', 'MAPSKP'],
	bases : [],
16035364   Benjamin Renard   First commit
21
   
74b77f58   Elena.Budnik   init remote branch
22
23
24
25
26
27
28
	configSrc  : {title: 'source', enableDrag : true, enableDrop: false},
	configDest : {title: 'destination', enableDrag : false, enableDrop:true},
	       
	constructor: function(config){    
		this.init(config);
		this.callParent(arguments);
	}, 
16035364   Benjamin Renard   First commit
29
	
74b77f58   Elena.Budnik   init remote branch
30
31
32
33
34
35
36
37
38
39
	/*
	* Load new data bases trees	: source & destination
	*/   
	loadTrees : function(button, pressed){
		if (pressed) {    
			this.baseId =  button.text;	
			this.loadTree('source');
			this.loadTree('destination');
		}
	},
16035364   Benjamin Renard   First commit
40
	
74b77f58   Elena.Budnik   init remote branch
41
42
43
44
45
46
47
	/*
	* Real Load  data base tree	
	*/   
	loadTree : function(type){
		var title = this.baseId;
		if (type == 'source'){
			var store = this.srcTree.getStore();
04a579a5   Elena.Budnik   sort trees
48
			
74b77f58   Elena.Budnik   init remote branch
49
50
51
52
		}
		else {
			var store = this.destTree.getStore();
			title = 'My '+ this.baseId;
04a579a5   Elena.Budnik   sort trees
53
54
55
56
57
58
59
60
		}
		// sort alphabetically
		store.sort([
			{ sorterFn: function(o1, o2){						
					return o1.get('text').toUpperCase()  < o2.get('text').toUpperCase() ? -1 : 1;
				} 
			}
		]);
74b77f58   Elena.Budnik   init remote branch
61
62
63
64
65
66
67
68
69
70
71
		// or RELOAD?
		var root = store.getRootNode();		   
		store.load({
			node: root,
			params: {
				nodeType: type,
				baseId: this.baseId
			}
		});	
		root.set('text', title);		    		  
	},
16035364   Benjamin Renard   First commit
72
	
74b77f58   Elena.Budnik   init remote branch
73
74
75
76
77
78
79
80
81
82
83
	/*
	*   Store and tree creation
	*/
	initTree:  function(config){		  
		if (this.baseId){
			var title = config.title == 'source' ? this.baseId : 'My '+ this.baseId;
		}
		else 
			var title = config.title;
	   
		var selMode =  config.title == 'source' ? {mode: 'SIMPLE'} : {mode: 'SINGLE'};
16035364   Benjamin Renard   First commit
84

74b77f58   Elena.Budnik   init remote branch
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
		var store = Ext.create('Ext.data.TreeStore', {
			model: 'amdaModel.AmdaNode',		     
			root: {			  			  
					text: title,
					nodeType : 'remoteParam',
					expanded: true
			},
			listeners: {
				scope : this,
				beforeload: function(store, operation){
					store.proxy.extraParams = {
						nodeType: config.title,
						baseId: this.baseId
					}
				}}
		});
	
		var menu = new Ext.menu.Menu({
			items: [{
				text: 'Delete Data'
			}],
			listeners: {
				scope : this,
				click : this.deleteMulti
16035364   Benjamin Renard   First commit
109
			}
74b77f58   Elena.Budnik   init remote branch
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
		});
	
		var tree = Ext.create('Ext.tree.Panel', {
			store: store,
			id : config.title,
			flex: 1,
			height: 500,
			selModel: selMode, //{mode: 'SIMPLE'},
			viewConfig: {
				plugins:{
					ptype: 'treeviewdragdrop',
					allowContainerDrops : true,
					enableDrag: config.enableDrag,
					enableDrop: config.enableDrop,
					//TODO dataset drag/drop by parameters SELECTION
					dragText : "{0} selected parameter{1}",
					expandDelay : 100,   
					ddGroup : "RemoteTreeDD",
					
					isValidDropPoint : function(){	            
						return true;
					},
					
					onContainerOver : function() { 
						return this.dropAllowed;      
					},
					
					onNodeOver : function() { 
						return this.dropAllowed;      
					},
					
					onNodeDrop : function(nodeData, dd, e, data){ 
						if (!data.records)
							return false;
						Ext.Array.each(data.records, this.onDrop, this);
						// deselect source tree selection		      
						data.view.getSelectionModel().deselectAll();
						
						return true;      
					},
					
					onContainerDrop : function(dd, e, data){      
						if (!data.records)
							return false;		
						Ext.Array.each(data.records, this.onDrop, this); 
						// deselect source tree selection		      
						data.view.getSelectionModel().deselectAll();
						
						return true;
					},
					
					onDrop : function(record) {    
						var root = tree.getRootNode();		   
						var srcNode = record;		   
						var id = srcNode.get('id');						
						// Check if node exists already at destination
d3f8a6d0   Elena.Budnik   internal paramID
166
167
168
						var idToCheck = id.toLowerCase().replace(":", "_", "gi");
						// TODO check at sever side
						if (root.findChild('id',idToCheck, true)) {
74b77f58   Elena.Budnik   init remote branch
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
							myDesktopApp.infoMsg('This parameter exists already in your treee');
							return;
						}
						var ddNode = srcNode;
						// Array of parent nodes description  
						var Arr = [];
						while (!ddNode.isRoot()) { 
							ddNode = ddNode.parentNode;
							Arr.push(ddNode);
						}
						// start with the highest node    
						Arr.reverse();   
						// create parent nodes if they do not exist
						var parentNode = root;
						Ext.Array.each(Arr,function(srcNode, index) {
							if (index > 0) {
								var nodeId = srcNode.get('id');
								var node = root.findChild('id',nodeId, true);
								if (!node) {
									node = srcNode.copy();
									parentNode.appendChild(node);
									parentNode.expand();
									node.setDirty();
								} 
								parentNode = node;
							}       
						});
16035364   Benjamin Renard   First commit
196

d3f8a6d0   Elena.Budnik   internal paramID
197
198
						// Add new node to correct location    
						parentNode.expand(!this.isDataSet, function(res){ 
74b77f58   Elena.Budnik   init remote branch
199
200
201
202
203
204
205
							var newNode = srcNode.copy();
							parentNode.appendChild(newNode); 	
							//to sync treeStore			  
							newNode.setDirty();
							//expand the whole subtree of added node
							tree.selectPath(newNode.getPath( 'id', '|'), 'id', '|');
						});
d3f8a6d0   Elena.Budnik   internal paramID
206
						return true; 
74b77f58   Elena.Budnik   init remote branch
207
208
209
210
					},
					
					onViewRender : function(view){
						var me = this;
16035364   Benjamin Renard   First commit
211

74b77f58   Elena.Budnik   init remote branch
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
						if (me.enableDrag){
							me.dragZone = Ext.create('Ext.tree.ViewDragZone', {
								view: view,
								ddGroup: me.dragGroup || me.ddGroup,
								dragText: me.dragText
							});
						}
						
						if (me.enableDrop){
							me.dropZone = Ext.create('Ext.tree.ViewDropZone', {
								view: view,
								ddGroup: me.dropGroup || me.ddGroup,
								allowContainerDrops: me.allowContainerDrops,
								expandDelay: me.expandDelay,
								isValidDropPoint : me.isValidDropPoint,
								onContainerDrop : me.onContainerDrop,
								onNodeDrop      : me.onNodeDrop,
								onContainerOver : me.onContainerOver,
								onNodeOver : me.onNodeOver,
								onDrop : me.onDrop,
								onSimpleDrop : me.onSimpleDrop
							});
						}
					}
				}
			},
			
16035364   Benjamin Renard   First commit
239
			listeners: {
74b77f58   Elena.Budnik   init remote branch
240
241
242
243
244
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
				afterrender: function(comp) {		   
					var view = comp.getView();                  
					view.tip = Ext.create('Ext.tip.ToolTip', {
						// The overall target element.
						target: view.el,
						// Each grid row causes its own seperate show and hide.
						delegate: view.itemSelector,			  				   				 
						// Moving within the row should not hide the tip.
						trackMouse: true,			   
						autoRender: true,				   
						listeners: {
							// Change content dynamically depending on which element triggered the show.
							beforeshow: function updateTipBody(tip) {	
								var trigger =  view.getRecord(tip.triggerElement);
								if (trigger) {
									var info = trigger.get('info');
									if (!info || info == '') {    
										tip.addCls('hide');  
									}
									else { 
										tip.removeCls('hide'); 
										tip.update(info); 
									}  
								}
							}
						}
					});
				},
				
				itemmouseenter: function(view, record, item){ 
					if(record.get('allowDrag') && view.ownerCt.id == 'source'){		 
						var el = Ext.get(item),
						td = el.down('td > div');
						td.setStyle('cursor', 'crosshair');				
					}
				},
				
				beforeselect: function(selmodel, record, index) {		    
						if (!(record.get('isRemoteDataSet') || record.get('isParameter')) && tree.id == 'source') return false;			 	  			      
				},
				
				select: function(selmodel, record, index){			
					if (record.get('isRemoteDataSet') && tree.id == 'source' && record.isExpanded()) {
						var toSelect = selmodel.getCount() !== record.childNodes.length + 1;
						var alreadySelected = selmodel.isSelected(record.firstChild);
							
						selmodel.deselectAll();
							
						if (toSelect && !alreadySelected) 
							selmodel.select(record.childNodes);			 
					}
				},
				
				itemcontextmenu: function(view, rec, item, index, e)
				{		  
					e.preventDefault();//stopEvent();
					if (tree.id == 'destination' && rec.getDepth() > 0)  		        
f286b43c   Elena.Budnik   process response ...
297
								menu.showAt(e.getXY());        
16035364   Benjamin Renard   First commit
298
				}
74b77f58   Elena.Budnik   init remote branch
299
			}
f286b43c   Elena.Budnik   process response ...
300
301
		});
		
74b77f58   Elena.Budnik   init remote branch
302
303
304
305
306
307
308
309
310
		return tree;
	},
	
	// for the moment SINGLE!    
	deleteMulti: function(){
		var selected = this.destTree.getSelectionModel().getSelection();
		Ext.Array.each(selected, function(rec){			
			rec.deleteData();
		}); 
16035364   Benjamin Renard   First commit
311
		
74b77f58   Elena.Budnik   init remote branch
312
313
314
315
316
317
318
319
320
321
		// reload destination tree and expand created nodes	     		     
		var store = this.destTree.getStore();
		var root = store.getRootNode();		
		store.load({ node : root, 
			params : { nodeType: 'destination',
							baseId : this.baseId }
		});
	},
       
	successCallBack : function(batch, options) {      
f286b43c   Elena.Budnik   process response ...
322
323
324
325
326
327
328
329
330
331
332
333
334
335
		loadMask.hide(); 
		// Check Errors
		var res = batch.operations[0].response.result;
		if (res.err) {
			myDesktopApp.errorMsg(res.err);
			this.loadTree('destination');	
		}
		else {
			var createdNodes = options.operations.create;
			// define Pathes to the created nodes	     
			Arr = [];	
			Ext.Array.each(createdNodes,function(item){ 			
				if (item.get('isParameter')){ // parameter level 
					Arr.push(item.getPath('id', '|')); 
74b77f58   Elena.Budnik   init remote branch
336
				}
f286b43c   Elena.Budnik   process response ...
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
			});		
			// reload destination tree and expand created nodes	     
			var panel = this.destTree;	     
			var store = this.destTree.getStore();
			var root = store.getRootNode();		
			store.load({ node : root, 
				params : { nodeType: 'destination',
								baseId : this.baseId },	
				scope : this,
				callback : function(){
					for (var i = 0; i < Arr.length; i++){
						panel.selectPath(Arr[i], 'id', '|');
					}
				}
			});
			
			this.updateUserTree();
		}
74b77f58   Elena.Budnik   init remote branch
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
	},
	
	failureCallBack : function(batch, options) {     
		myDesktopApp.errorMsg('Error while saving modifs');  
		loadMask.hide();	      
		this.loadTree('destination');	       	
	},   
	
	/*
	*  Update User Tree in Explorer after the the modifs
	*/
	updateUserTree : function() {
		// reload RemoteParam Tree in explorer
		var explorerTree = Ext.getCmp(amdaUI.ExplorerUI.RESRC_TAB.TREE_ID);
		if (explorerTree) 
		{					  
			var explorerTreeStore = explorerTree.getStore();
			var explorerRoot = explorerTreeStore.getRootNode();
			var explorerPath = '/root/parameters-treeBase/myRemoteData-treeRootNode/';
			
			explorerTreeStore.load( { node : explorerRoot, 
					params : { nodeType: 'resources'},
					callback : function(){
						explorerTree.selectPath(explorerPath);						 
					}
			});	      	 
		}
f286b43c   Elena.Budnik   process response ...
382
		myDesktopApp.infoMsg('Parameter Tree has been updated');
74b77f58   Elena.Budnik   init remote branch
383
384
385
386
387
388
389
390
391
392
393
	},
	
	/*
	*  Panel Trees generation
	*/
	init: function(config) {
		if (config.baseId && config.baseId != 'root') 
			this.baseId = config.baseId;
	
		this.srcTree = this.initTree(this.configSrc);
		this.destTree = this.initTree(this.configDest);
16035364   Benjamin Renard   First commit
394
		  
74b77f58   Elena.Budnik   init remote branch
395
396
397
398
399
400
401
402
403
404
405
406
407
		// synchron load of destination tree if the corresponding  node exists		  
		this.srcTree.on('itemexpand', function(node) { 		      
			var root = this.destTree.getRootNode();
			var destNode = root.findChild('id',node.get('id'),true);
			if (destNode) destNode.expand();
		}, this);
		
		// toolbar		  
		Ext.Array.each(this.basesNames, function(baseName,index){
			var isTheBase = false;
			if (this.baseId && baseName == this.baseId) var isTheBase = true;
			this.bases[index] = {text: baseName,  toggleGroup: 'remoteBases', pressed: isTheBase, scope : this, toggleHandler: this.loadTrees}
		}, this);
16035364   Benjamin Renard   First commit
408
						    
74b77f58   Elena.Budnik   init remote branch
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
		var myConf = {   
			id : 'rb_tab_panel', 
			title : 'Remote Data Base',
			items: [                 
				this.srcTree, 
				this.destTree                      
			],
			tbar: this.bases,	                       
			fbar: [
				{ text: 'Save Modifs', 
					scope : this,					  
					handler: function(){				  
						var store = this.destTree.getStore();	
						var new_nodes = store.getNewRecords();	
						var removed_nodes =  store.getRemovedRecords();
						// if only removed: no sync - already removed at Server by node.delete	
						if ( new_nodes.length === 0 && removed_nodes.length > 0) this.updateUserTree();
						if ( new_nodes.length > 0 ){  
							loadMask.show();
							store.sync({ success : this.successCallBack, failure: this.failureCallBack, scope : this});	
						}
16035364   Benjamin Renard   First commit
430
					}
74b77f58   Elena.Budnik   init remote branch
431
432
				} 
			]
16035364   Benjamin Renard   First commit
433
		};
74b77f58   Elena.Budnik   init remote branch
434
435
436
		Ext.apply(this, Ext.apply(arguments, myConf)); 
	}	   		 	      
});