AliasUI.js 5.61 KB
/**
 * Project   : AMDA-NG
 * Name      : AliasUI.js
 * @class 	 amdaUI.AliasUI
 * @extends  Ext.form.Panel
 * @brief	 Alias UI definition (View)
 * @author 	 Myriam
 * @version  $Id: AliasUI.js 966 2012-07-03 14:50:05Z elena $
 */ 

Ext.define('amdaUI.AliasUI',{
	extend: 'Ext.form.Panel',
	alias: 'widget.alias', 
	
    /**
     * @cfg {String} data models
     * @required
     */
	nodeDataModel : 'amdaModel.AliasNode',
	
 
	setAlias : function(obj) {	  
		var paramValue = obj ? obj.get('id') : null;
		var aliasValue = obj && obj.get('alias') ? obj.get('alias') : null;
		this.parameterField.setValue(paramValue);		
		this.aliasField.setValue(aliasValue);
	},
	
	setAliasJson : function(obj) {	  
		var paramValue = obj ? obj.id : null;
		var aliasValue = obj && obj.alias ? obj.alias : null;
		this.parameterField.setValue(paramValue);
		this.aliasField.setValue(aliasValue);		
	},
	
	setLinkedNode : function (myLinkedNode){
		this.linkedNode = myLinkedNode;		
	},
	
	setParamNode : function (paramNode){
		this.paramNode = paramNode;
		if (this.paramNode.get('alias') && amdaModel.AliasNode.deletedAliases) {
			this.checkDeleted(this.paramNode.get('id'));
		}
	},
  
// check if alias was deleted in this session => set param alias == null	
	checkDeleted : function(paramId) {
		 var itemIndex = -1;
		 Ext.Array.each(amdaModel.AliasNode.deletedAliases,
				function(name, index, item) {
				    if (name === paramId) {
				      itemIndex = index;
				      return false;
				    }
				}
		    );
		 if (itemIndex >= 0) {
			 Ext.Array.erase(amdaModel.AliasNode.deletedAliases, itemIndex, 1);
		 }
	},
	
	createLinkedNode : function ()
	{
		var newNode = Ext.create(this.nodeDataModel, {
			leaf : true,
			contextNode : this.contextNode
		});		  
	 
		this.setLinkedNode(newNode);
	},
	
	validate : function(alias,param) 
	{     
		var aliasNodeByName = this.linkedNode.get('rootNode').findChild('text', alias);		       
		if (aliasNodeByName) {
			//TODO process
			var paramId =  aliasNodeByName.get('id').substr(6);
				Ext.Msg.alert('Alias','Alias '+ alias +' already exists for parameter\n' + paramId);
				return false;
		}    
		
		return true;
	}, 
	
	updateConstruct : function(oldval,newval) {
		Ext.Array.each(myDesktopApp.paramModulesID, function(id){
			var targetModule = myDesktopApp.getLoadedModule(id);
			if (targetModule && typeof targetModule.getUiContent() !== "undefined")
				targetModule.getUiContent().updateConstruct(oldval,newval);
		});			 
	},
		
	saveAlias: function() 
	{  
		if (this.parameterField.value.match(/[a-z,0-9]/gi) == null) {
			Ext.Msg.show({title:'No Parameter', msg: 'Please select parameter to create alias for', 
								icon: Ext.Msg.WARNING, buttons: Ext.Msg.OK});
			return;
		}
		
		var tree = Ext.getCmp(amdaUI.ExplorerUI.RESRC_TAB.TREE_ID);
		var rootNode = tree.getRootNode().findChild('id','alias-treeRootNode',true);
		
		if (!rootNode.isExpanded()) rootNode.expand(false);	   					    						    
		
		if (!this.paramNode.get('alias')) {
			this.createLinkedNode();
			var isNew = true;
		} 
		else {
			var aliasNode = rootNode.findChild('text', this.paramNode.get('alias'));
			if (!aliasNode) {
				//TODO error 
				alert('NOT FOUND');
			}
			this.setLinkedNode(aliasNode);
			var isNew = false;
		}  

		this.linkedNode.set('rootNode', rootNode);
		var alias = this.aliasField.value;
		var param = this.parameterField.value;
		var component_info = this.paramNode.get('component_info');
		var iconClass = this.paramNode.get('iconCls');
		// now validation							    
		if (this.validate(alias,param)) {	
			if (isNew) {
				if (alias.match(/[a-z,0-9]/gi) == null) {
					Ext.Msg.show({title:'No Alias', msg: 'No alias was created for parameter '+ param, 
							icon: Ext.Msg.WARNING, buttons: Ext.Msg.OK}); 
				}
				else {
					// create alias 
					this.linkedNode.create(alias, param, component_info, iconClass);
					this.updateConstruct(param,"#"+alias);
				}
			}   
			else if (alias.match(/[a-z,0-9]/gi) == null) {
				var alias = this.paramNode.get('alias');
				Ext.Msg.confirm('Delete', 'Are you sure you want to delete this alias ?', function(btn, text){
					if (btn == 'yes'){
						// remove alias		                                	
						this.updateConstruct("#"+alias,param);
						this.linkedNode.deleteNode();
					}
				},this);
			}   
			else {
				// rename alias
				this.linkedNode.renameNode(this.paramNode.get('alias'),alias, param);
				this.updateConstruct("#"+this.paramNode.get('alias'),"#"+alias);
			}
			// Delete alias from array of deleted aliases
			this.checkDeleted(this.paramNode.get('id'));
			this.paramNode.set('alias',alias);
		}						    						    	  
		var win = myDesktopApp.getDesktop().getWindow('alias-win');
		if (win) win.close();	  
	},
	
	initComponent : function (){
		
		if (this.initialConfig.paramNode)
			this.setParamNode(this.initialConfig.paramNode);	    	    
		
		this.parameterField = new Ext.form.field.Text({
			fieldLabel: 'AMDA Parameter', name:'param',  labelAlign: 'top', 
			disabled: true, width : '90%', padding : '10px 0 0 10px', allowBlank: false		 
		});
 
		this.aliasField = new Ext.form.field.Text({
			fieldLabel: 'Your Alias', padding : '10px 0 0 10px', width : '90%',
			labelWidth: 70, allowBlank: false, stripCharsRe: /(^\s+|\s+$)/g,
			emptyText: 'Please no spaces!' 
		});
	    
		this.setAlias(this.paramNode);
	    	 
		var config = {		
			defaultType: 'textfield', 
			bodyStyle: 'background:#ddecfe;',  
			items: [
					this.parameterField,
					this.aliasField
			],
			fbar: [
				{ 
					text: 'Save Alias', 
					scope: this,
					handler: this.saveAlias
				}
			]
		};
		Ext.apply (this , Ext.apply(this.initialConfig, config));
		this.callParent(arguments);
	}  
});