TabResultUI.js 7.94 KB
/**
 * Project   : AMDA-NG
 * Name      : TabResultUI.js
 * @class    amdaUI.TabResultUI
 * @extends  Ext.panel.Panel 
 * @brief   Tab Result UI definition (View)
 * @author    
 * @version  $Id: TabResultUI.js 2618 2014-10-24 12:27:42Z elena $  
 */

Ext.define('amdaUI.TabResultUI', {
	extend: 'Ext.panel.Panel',

	alias: 'widget.tabResult',
	
	requires: [ 'amdaUI.SendToSampButtonUI'],
	
	constructor: function(config) {	 
		this.initConfig(config);
		this.callParent();	
	},  
        
	initConfig: function(config){
		this.type = config.nodeType;
		this.id = 'tab-' + config.nodeType;	 
		//var title = config.nodeType +' Result';	
		switch (config.nodeType) {
			case 'condition': var title = 'Data Mining Results';
						break;
			case 'request' : var title = 'Plot Results';
						break;	   
			case 'download': var title = 'Download Results';
						break;
			case 'statistics': var title = 'Statistics Results';
						break;
			default:
		}
		
		var newConfig = {
			id: this.id,          	    
			title: title,
			autoScroll: true,
			height: 250,
			closable: true,
			layout: {
				type: 'vbox',
				align : 'stretch',
				autoSize : true
			},
			listeners: {
				scope : this,
				beforeclose : function() {
				//delete linked nodes connected to ResultModule and corresponding to this Tab
					var module = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.result.id);
					if (module.linkedNodes) {
						var indices = new Array();
						var i = 0;
						module.linkedNodes.each(function(item,index){
							if(this.type == item.get('jobType')) {
								indices[i] = index;
								i++;
							}
						}, this);
						if (indices.length > 0) {
							Ext.Array.each(indices, function(item) {
								module.linkedNodes.removeAt(item);
							});			    
						}
					}
				}
			}
		}
		Ext.apply(this, newConfig);	 
	},
     
	addResult: function(resultName, resultId, folderId, compression, format, processId) { 
		this.insert(0,[ {xtype : 'resultItem', type : this.type, title: resultName, folderId: folderId, resultId: resultId, compress : compression, format: format, processId: processId}]); 
	}
});

Ext.define('amdaUI.ResultItem', {
	extend: 'Ext.form.FieldSet',

	alias: 'widget.resultItem',
	
	linkedNode : null,
	
	delete: function(processId) {
		var module = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.result.id);
		var jobLinkedNode = module.linkedNodes.getByKey(processId);
		jobLinkedNode.deleteNode();
		module.linkedNodes.remove(jobLinkedNode);    
	},
		
	saveTable: function(folderId,ttName) {
		this.linkedNode = Ext.create('amdaModel.TimeTableNode',{leaf : true});
		AmdaAction.getTmpObject(
				folderId,
				ttName,
				this.linkedNode.get('nodeType'),
				this.getResultCallback,
				this);       
	},
    
	saveCatalog: function(folderId,ttName) {    
		this.linkedNode = Ext.create('amdaModel.CatalogNode',{leaf : true});
		AmdaAction.getTmpObject(
				folderId,
				ttName,
				this.linkedNode.get('nodeType'),
				this.getResultCallback,
				this);       
	},
    
	getResultCallback : function(result,remoteEvent){ //result, e) {
		var t = remoteEvent.getTransaction();
		//AKKA - catch error
		if (!remoteEvent.status){
			myDesktopApp.errorMsg('Internal error');
			return;
		}
		
		if (result.error){
			myDesktopApp.errorMsg(result.error);
			return;
		}
		
		var paramObj = Ext.create(this.linkedNode.get('objectDataModel'), result);
		paramObj.set('fromPlugin',true);
		paramObj.set('intervals',result.intervals);
		// set parameter into node
		this.linkedNode.set('object',paramObj);
			
		var me = this;
		myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.result.id, true, function (module) {
			module.setLinkedNode(me.linkedNode);
			// Edition of parameter into parameter Module	    
			me.linkedNode.editInModule();
		});
	},
    
	onSendVOTableToSamp : function (clientId, sendOpt){
		loadMask.show();
		AmdaAction.getRequestByProcessId(sendOpt, function(res,e) {
			var request = res;
			request.sendToSamp = true;
			AmdaAction.execute({nodeType : 'download'}, request, function(res,e) {
				loadMask.hide();
				var files = res.result.split(",");
				Ext.each(files, function(file) {
					var href = 'data/'+sessionID+'/RES/'+ res.folder + '/' + file;
					myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.interop.id, true, function (module) {
						module.sendVOTable(href, clientId ? clientId : 'hub');
					});
				});
			});
		});
	},
    
	constructor: function(config) {
		this.initConfig(config);
		this.callParent();	
	},  
    
	initConfig: function(config){	
		var jobtitle = config.title;

		switch (config.type) {			
			case 'request' : 
				var resultId = config.resultId;
				var folderId = config.folderId;
				var href = 'data/'+sessionID+'/RES/'+ folderId  + '/' + resultId;
			break; 
			case 'download' :
				//TODO suffix parsing		     
				var suffix = '.tar.gz';			     
				if (config.compress == 'zip') var suffix = '.zip';
				var downId = config.resultId;
				var downFolder = config.folderId;			      
				var href = 'data/'+sessionID+'/RES/'+ downFolder + '/' + downId + suffix;			      
				break;
			default:
		}	  
       
		var height = 60;  
	
		var configTT = {
			intId : config.title, 
			height : height,	
			title: jobtitle,
			collapsible: true,
			defaultType: 'button',
			items :[
			{
				xtype: 'label',
				text: 'Time Table '
			},{
				text: 'Edit/Save',
				scope : this,
				handler: function() {
					this.saveTable(config.folderId,config.resultId);
				}
			},  {
				xtype: 'label',
				text: '  || Gaps Time Table '
			}, {
				text: 'Edit/Save',
				scope : this,
				handler: function() {
					this.saveTable(config.folderId,'Gaps_'+config.resultId); 
				}
			}, {
				xtype: 'label',
				text: ' || ',
				width: 50
			}, {
				text: 'Delete Both',
				scope : this,
				handler: function() {
					this.delete(config.processId);
					this.ownerCt.remove(this);
				}
			}]
		};
	  
		var configCat = {
			intId : config.title, 
			height : height,	
			title: jobtitle,
			collapsible: true,
			defaultType: 'button',
			items :[
			{
				xtype: 'label',
				text: 'Catalog '
			}, {
				text: 'Edit/Save',
				scope : this,
				handler: function() {
					this.saveCatalog(config.folderId,config.resultId);
				}
			}, {
				text: 'Delete',
				scope : this,
				handler: function() {
					this.delete(config.processId);
					this.ownerCt.remove(this);
				}
			}]
		};
		
		//TODO make this properly	  	  
		var configPlot = {
			intId : config.processId,
			height :  height,	
			title : jobtitle,
			collapsible: true,
			defaultType: 'button',
			items :[
			{
				xtype: 'label',
				text: 'Plot '
			}, {
				text: 'Download',
				href: href,
				target: '_blank', // removed in 4.1    , hrefTarget: '_blank' // used in 4.1	
				hrefTarget: '_blank'
			},  {
				xtype: 'label',
				text: '  ||  '
			}, {
				text: 'Delete',
				scope : this,
				handler: function() {
					this.delete(config.processId);
					this.ownerCt.remove(this);
				}
			}]
		};
		
		var configDown = {
			intId : config.processId,
			height :  height,	
			title: jobtitle,
			collapsible: true,
			defaultType: 'button',
			items :[
			{
				xtype: 'label',
				text: 'File '
			}, {
				text: 'Download',
				href: href,
				target: '_self', // removed in 4.1    , hrefTarget: '_blank' // used in 4.1	
				hrefTarget: '_self'
			}, {
				xtype: 'label',
				text: '  ||  '
			}, 
			{
				text: 'Delete',
				scope : this,
				handler: function() {
					this.delete(config.processId);
					this.ownerCt.remove(this);
				}
			},{
				xtype: 'label',
				text: '  ||  '
			},{
				xtype        : 'sendToSampButton',
				type         : 'votable',
				onSendToSamp : this.onSendVOTableToSamp,
				sendOpt      : {'processId' : config.processId}
			}]
		};
	
		switch (config.type) {
			case 'condition' : 
				Ext.apply(this, configTT);	
				break;
			case 'statistics' : 
				Ext.apply(this, configCat);	
				break;
			case 'request' : 		      
				Ext.apply(this, configPlot);
				break; 
			case 'download' :			     
				Ext.apply(this, configDown);
				break;
			default:
		}
	}    
});