SearchParamEditorPlugin.js 8.21 KB
/**
  * Project      :  AMDA-NG
  * Name         : searchParamEditorPlugin.js
  * @plugin 	  amdaUI.ParamEditorPlugin
  * @extends 	  Ext.util.Observable 
  * @ptype 	 searchParamEditorPlugin	  
  * @version $Id: SearchParamEditorPlugin.js 2850 2015-03-31 09:51:58Z elena $      
  */


Ext.define('amdaUI.SearchParamEditorPlugin', {
	extend: 'Ext.util.Observable',
	alias: 'plugin.searchParamEditorPlugin',
	
	requires : [
	   'amdaModel.ArgGroups'
	],
	
	win: null,
	
	confCombo : { width: 100, 
		      labelAlign: 'top',
		      margin: '5 10 0 0',								      
		      queryMode: 'local',
		      editable: false, 						 
		      displayField: 'value' 
	},
	
	confCombo1 : { width: 100, 
		      labelAlign: 'top',
		      margin: '5 10 0 0',								      
		      queryMode: 'local',
		      editable: false, 						 
		      displayField: 'value',
		      valueField: 'arg'
	},
	
	confField : { width: 100, 
		      labelAlign: 'top',
		      allowDecimals : false,
		      hideTrigger: true,
		      margin : '5 10 0 0'
	},
	
	configFS : {layout:'hbox', height: 70},
	
	confCB : {boxLabel: 'Average', margin: '5 10 0 0'},	
	
	init: function(cmp) {	 
	    this.hostCmp = cmp;	   
	    this.hostCmp.on('openParamEditor', this.onOpen, this);	   
	},
	 
	
	apply: function() { 
            var args = this.form.getForm().getValues();	
            // if component - insert before brackets                
            var bracketPos = this.paramId.indexOf("(");              
            if (bracketPos > 0) {
                if (args.argMax) 
                        this.hostCmp.addParam(Ext.String.insert(this.paramId, '_' +args.argMin +'_'+args.argMax, bracketPos), true);                       
                else 
                        this.hostCmp.addParam(Ext.String.insert(this.paramId, '_' +args.argMin, bracketPos), true);                        
                    
            }
            else {    // TODO check number of args	; here 2 maximum 
                if (args.argMax) {
                        this.hostCmp.addParam(this.paramId + '_' +args.argMin +'_'+args.argMax, true);                    
                }
                else {
                    if (args.argMin == 'all') {
                    alert('Select channel, pls : ALL not implemeted yet');
                    return false;
                    }
                    // string value
                    if (isNaN(args.argMin)) {
                        this.hostCmp.addParam(this.paramId + '_' +args.argMin, true);
                    }
                    // Energy channel
                    else {
                        arg = args.argMin - 1;
                        //TODO this is temporary!!!
                        if (this.paramId.substr(this.paramId.length-2, 2) == '_E') this.paramId = this.paramId.substr(0,this.paramId.length-2);
                        this.hostCmp.addParam(this.paramId + '(' +arg + ')', true);
                    }
                }
            }
	 
	  this.win.close();
	}, 
	
	reset: function() {
	    this.form.getForm().reset();
	}, 
	
	onOpen: function(id) {
	        if (this.win) this.win.close();			 
		this.paramId = id;
		this.win = new Ext.Window({			
			id: 'searchparameditor-win', 
			width: 280, 
			height: 150,
			x: 0, y: 0,
			baseCls:'x-panel',	 
			title: 'Select Arguments for ' + this.paramId,
			layout: 'fit',
			constrain: true,
			ghost: false,
			renderTo: this.hostCmp.ownerCt.getId(),
			items: this.getFormConfig(),
			listeners: {
			    scope: this,			    
			    beforeclose: function(){			      			     
				Ext.PluginManager.unregister(this);
			    } 
			},
			getConstrainVector: function(constrainTo){
				var me = this;
		        if (me.constrain || me.constrainHeader) {
		            constrainTo = constrainTo || (me.floatParent && me.floatParent.getTargetEl()) || me.container || me.el.getScopeParent();
		            return (me.constrainHeader ? me.header.el : me.el).getConstrainVector(constrainTo);
		        }
		    }
		});
	//	this.hostCmp.ownerCt.add(this.win); 
		this.win.show()
                Ext.PluginManager.register(this);		
	 	this.win.setPosition(0,0);		 
	},
	
	getFormConfig: function(){
	  
	   this.form = new Ext.form.Panel( {
	    	frame: true, buttonAlign: 'left', autoScroll: true,  
	    	layout: { type: 'vbox', align : 'stretch', autoSize: true},
		 fbar: [
		  {
		      text: 'Apply',
		      scope : this,
		      handler: this.apply
		  },
		   {
		      text: 'Reset',
		      scope : this,		       
		      handler: this.reset 
		   }
		  ]
		});
	      
	   // Get parameter arguments from Server
	     
		var argStore = Ext.create('Ext.data.Store',{model: 'amdaModel.ArgGroups'});
		    //is sent to server to find parameter XML description
		    argStore.getProxy().extraParams.paramID = this.paramId;
		    argStore.getProxy().extraParams.application = 'search';
		    
		    argStore.load({
		      scope   : this,
		      callback: function(records, operation, success) {			  
			  if (success && records[0].get('group') != 'nok') {
			      this.simpleGroup = false;
			      
			      argStore.each(function(grp){				 				 
				  var isMulti = false;
				  var args = grp.args();
				  var units = grp.get('units');	
				 
				  if (grp.get('groupE') || grp.get('groupEmulti')) {
				    var argsN = args.count();
				    var argMin  = args.first();
				    var argMax = args.getAt(argsN-1);
				  
				    var title = grp.get('groupE') || grp.get('groupEmulti');
				    if (grp.get('groupEmulti'))  isMulti = true;
					    
				    var fieldset = new Ext.form.FieldSet(this.configFS);
				    var suffix = 'Min';
				    var argValue = argMin;
				    
				    for (var i = 0; i < 2; i++) {
				          var label = title + suffix;				  
					  var fieldlabel = label + ' ' +units;
					  var name = 'arg' +  suffix;
					  
					  if (isMulti){
						var config = {name: name, fieldLabel: fieldlabel, minValue: argMin.get('value'), maxValue: argMax.get('value'), value: argValue.get('value')};					  
						Ext.applyIf(config, this.confField);				    
						var combo = new Ext.form.field.Number(config); 
					  }
					  else {
						var config = {name: name, fieldLabel: fieldlabel, store: args, value: argValue};					  
						Ext.applyIf(config, this.confCombo);				    
						var combo = new Ext.form.field.ComboBox(config);				  
					  }
					  fieldset.add(combo);
					  
					  var suffix = 'Max';   
					  argValue = argMax;
				    } 
                        // average checkbox				    
			//	    var config = {name: title + 'CB'};
			//	    Ext.applyIf(config, this.confCB);
			//	    var checkBox = new Ext.form.field.Checkbox(config);
			//	    fieldset.add(checkBox);				   	
				    this.form.add(fieldset);				  
				  }   
				  else { 
                                     
				      var title =  grp.get('group');
				      var suffix =  this.simpleGroup ? 'Max' : 'Min';
				      if (title) {
					var name = 'arg' +  suffix;
					title +=  ' ' + units; 
                                        
                                        var config = {name: name,  fieldLabel: title, store: args, value: args.first()};
                                      
                                        if (grp.get('group') == 'ClockAngle') {                                         
                                            Ext.applyIf(config, this.confField);
                                            var newField = new Ext.form.field.Number(config);         
                                        }
					else {
                                            Ext.applyIf(config, this.confCombo1);	
                                            var newField = new Ext.form.field.ComboBox(config);
                                        } 
                                        
                                        if (!this.simpleGroup) this.fieldsetSimple = new Ext.form.FieldSet(this.configFS);	
                                            this.simpleGroup = true;
                                            this.fieldsetSimple.add(newField);				    
                                        }					         
				  }
			      }, this);
			      if (this.simpleGroup) this.form.add(this.fieldsetSimple);
			  }
			  
			  else {
      //TODO process errors		      
			  }
		      }
		    });
	
	    return this.form;
	}    
		
});