Blame view

js/app/views/ParamsMgrUI.js 12.4 KB
16035364   Benjamin Renard   First commit
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
239
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/**
 * Project   : AMDA-NG
 * Name      : amdaUI.ParamsMgrUI
 * @class    :  
 * @extends  Ext.Panel.Panel 
 * @brief     
 * @author Elena 
 * @version  $Id: ParamsMgrUI.js 1871 2013-11-22 13:54:17Z elena $
 ******************************************************************************
 *    FT Id     :   Date   : Name - Description
 ******************************************************************************
 *	          
 */

Ext.define('amdaUI.ParamsMgrUI', {
    extend: 'Ext.panel.Panel',
      
    alias: 'widget.paramsMgrPanel',
    
    baseId : null,
    srcTree : null,
    destTree : null,
    
   
    hasModifs: false,
    
    //TODO where keep this (==baseId) description ? Global variable?
    basesNames : ['VEXGRAZ', 'CDAWEB', 'THEMIS', 'MAPSKP'],
    bases : [],
    
    configSrc : {title: 'source', enableDrag : true, enableDrop: false},
    configDest : {title: 'destination', enableDrag : false, enableDrop:true},
	
       
    constructor: function(config) {	    
	    this.init(config);
	    this.callParent(arguments);
    },
      	   
   /*
    * Load new data bases trees	: source & destination
    */   
    loadTrees : function(button, pressed) {
      if (pressed) {		    
	this.baseId =  button.text;	
	this.loadTree('source');
	this.loadTree('destination');
      }
    },
    
   /*
    * Real Load  data base tree	
    */   
    loadTree : function(type) {
      
      var title = this.baseId;
      if (type == 'source') var store = this.srcTree.getStore();
      else {
	    var store = this.destTree.getStore();
	    title = 'My '+ this.baseId;
      }	
      // or RELOAD?
      var root = store.getRootNode();		   
      store.load({
	node: root,
	params: {
	  nodeType: type,
	  baseId: this.baseId
	}
      });	
      root.set('text', title);		    		  
    },
    
/*
 *   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'};

	var store = Ext.create('Ext.data.TreeStore', {
	  model: 'amdaModel.AmdaNode',		     
	  root: {			  			  
	      text: title,
	      //TODO if use the same logic for localParam?
	      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
	  }
	});
	
	var tree = Ext.create('Ext.tree.Panel', {
	    store: store,
	    id : config.title,
	    flex: 1,
	    height: 400,
	    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		      
		      if (root.findChild('id',id, true)) 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;				
			    } 			      
		      });
	 			      
		    // Add new node to correct location			    
		      parentNode.expand(!this.isDataSet, function(res){  		  	     	    		   		  
			  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', '|');
		    });
		    return true;		     
		    },
		    
		    onViewRender : function(view) {
		      var me = this;

		      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
			    });
		      }
		      }
		  }
	      }, 		
	      listeners: {
		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)  		        
					      menu.showAt(e.getXY());			 		 		        
		    }
	  }
	});	
	return tree;
    },
// for the moment SINGLE!    
    deleteMulti: function(){
	    var selected = this.destTree.getSelectionModel().getSelection();
	    Ext.Array.each(selected, function(rec){
				      rec.deleteData();
	    });      
    },
    
    
    successCallBack : function(batch, options) {   
	    this.hasModifs = true;   
	    loadMask.hide();               
	    var createdNodes = options.operations.update;
	     
// define Pathes to the created nodes	     
	     var Arr = [];	     
	     Ext.Array.each(createdNodes,function(item){	                            	           
		      if (item.getDepth() == 4) { // parameter level			    
			   Arr.push(item.getPath('id', '|'));		 	 		 
		      }
	     });
	     
// 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 },
			  callback : function(){			    			 
			    for (var i = 0; i < Arr.length; i++){			     
			        panel.selectPath(Arr[i], 'id', '|');
			     }			      			  
			  }
		    });	 
	       
	   		      
    },
    
    failureCallBack : function(batch, options) {
      //TODO message error	     
	      alert('FAILURE!'); 
	      loadMask.hide();	      
	      this.loadTree('destination');	       	
    },   
    
 /*
  *  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);
		  
// 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);
						    
		  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 removed = store.getRemovedRecords();
				       var updated = store.getUpdatedRecords();				        				  
			// if only removed: no sync - already removed at Server by node.delete	
			//TODO 	 is this condition OK??				        				       
					if (Ext.Array.union(removed,updated).length != removed.length) {  
						    loadMask.show();
						    store.sync({ success : this.successCallBack, failure: this.failureCallBack, scope : this});	
					}
				    }
				}				   
			  ],
		    listeners : {
				scope : this,
				destroy : function() { 	
				   if (this.hasModifs) {
				      // 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);						 
									}
					      });	      	 
				      }
				   }
				}
		    }
		};

		Ext.apply(this, Ext.apply(arguments, myConf));
 
    }	   		 	  
    
});