InteractiveIntervalPlugin.js 17.9 KB
/**
  * Project      :  AMDA-NG
  * Name         : InteractiveIntervalPlug.js
  * @plugin 	  amdaUI.InteractiveIntervalPlug
  * @extends 	  Ext.util.Observable 
  * @ptype 	 	  interactiveIntervalPlugin
  * @brief		 Plot interactive session UI (View)
  * @author Myriam
  * @version $Id: InteractiveIntervalPlugin.js 2617 2014-10-24 12:06:57Z elena $
  ********************************************************************************
  *    FT Id     :   Date   : Name - Description
  *******************************************************************************
  *  :          
  */


Ext.define('amdaUI.InteractiveIntervalPlugin', {
        extend:  'Ext.util.Observable',//'Ext.AbstractPlugin',
        alias: 'plugin.interactiveIntervalPlugin',
        
        linkedNode : null,

        moduleId : 'timetab-win',

        rootNode : null,
        win : null,

        addInterval : false,

        constructor: function(config) {            
            Ext.apply(this, config);            
            this.callParent(arguments);
        },
 
 
        init: function(cmp) {          
            this.hostCmp = cmp;             
            this.hostCmp.on({            
                open: this.onOpen,            
                startTime: this.setStart,            
                stopTime: this.setStop,    
                scope: this
          });
          
//Global event	    
          myDesktopApp.EventManager.on({            
              refresh : this.insertInterval,            
              scope: this}); 
        },

        onDestroy : function() {          
            this.win = null;
        },
	
        setStart: function(a){         
            if (this.form)             
                this.form.getForm().findField('start').setValue(a);
        },	
		
        setStop: function(b){          
            var a = new Date(this.form.getForm().findField('start').getValue());                 
            if (a <= b) {                       
                this.form.getForm().findField('stop').setValue(b);                                           
            } else {            
                this.form.getForm().findField('start').setValue(b);            
                this.form.getForm().findField('stop').setValue(a);                                           
            }                  
        },
        
/**
 *  add Interval to Time table
 **/        
        insertInterval: function() { 
       
            if (this.addInterval) {
            	var targetModule = myDesktopApp.getLoadedModule(this.moduleId);
            	if (targetModule)
            	{
            		var targetModuleUI = targetModule.getUiContent();
            		if (targetModuleUI)
            			targetModuleUI.addInterval(this.start,this.stop);
            	}
            }          
          this.addInterval = false;
        },

/**
 *  creation of the window
 */        
        onOpen: function() {          
            if (!this.win) {             
                this.win = new Ext.Window({                
                    id: 'interactiveInter-win-' + this.hostCmp.ownerCt.ownerCt.getId(), // Plot window ID                                                   
                    width: 380,               
                    height: 160,              
                    x: 0, y: 0,                
                    baseCls:'x-panel',                
                    title: 'Interactive Interval',			                
                    layout: 'fit',			                
                    constrain: true,			                
                    collapsible: true,			               
                    ghost: false,			                
                    renderTo: this.hostCmp.ownerCt.getId(),                                 
                    items: this.getFormConfig(),                
                    listeners: {			                      
                        scope: this,			    			                      
                        beforeclose: function() {      
                            this.hostCmp.ownerCt.destroyMire();	                    
                            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.win.on('destroy', this.onDestroy, this);
          
                this.id = 'interactiveInter-' + this.hostCmp.ownerCt.ownerCt.getId();
                        
                Ext.PluginManager.register(this);	           		              
                this.win.show();		 			              
                this.win.setPosition(0,0);		  
                var tree = Ext.getCmp(amdaUI.ExplorerUI.RESRC_TAB.TREE_ID);		              
                this.rootNode = tree.getRootNode().findChild('id','timeTable-treeRootNode',true);
                
                amdaModel.InteractiveNode.preloadNodes(this.rootNode,
                		function()
                        	{
                        	});
            }                             
        },

/**
 *   check if multi or individual operation
 */        
        isMultiZoom: function() { 
          
            var win = myDesktopApp.desktop.getWindow(myDesktopApp.dynamicModules.interactive_plot.id);		          				
            if (!win) return false;
            
            var multiPlotUI = win.items.getAt(0);		  
            var checked = multiPlotUI.checkBoxGroup.getChecked();
            
            if (checked.length == 0) return false;		  
            // check if this Plot is not checked then this is individual zoom		  
            var hostId = this.hostCmp.ownerCt.ownerCt.getId();
            
            var isUnique = Ext.Array.every(checked, function(item){
                if (item.name === hostId)             
                return false;            
            });
            
            return !isUnique;		            	  	         
        },
        
        resetStartStop: function() {
            this.form.getForm().findField('start').setValue('');                                
            this.form.getForm().findField('stop').setValue('');                                 
            this.hostCmp.ownerCt.resetMire();                  
        },
           

/**
 *        Main form
 */
        getFormConfig: function(){	    
          this.form = new Ext.form.FormPanel( {	    	
            frame: true,		
            width: 380,		
            height: 160,	    	
            layout: 'hbox',	    	
            fieldDefaults: {		  
              labelWidth: 100		
            },		
            items: [
               {                          
                 xtype: 'container',			
                 width : 250,			
                 layout : 'vbox',			
                 defaults: { height : 25, width: 250 },	
                   items: [ 
                      {                             
                        xtype:'datefield', name:'start', fieldLabel: 'Start Time',                           
                        format : 'Y-m-d\\TH:i:s'                                                                           
                      },                         
                      {                                                      
                        xtype:'datefield', name:'stop', fieldLabel: 'Stop Time',                                                      
                        format : 'Y-m-d\\TH:i:s'                                                                                                                                                         
                      },
                      {                            
                        xtype: 'textfield',                               
                        fieldLabel: 'Add in TimeTable',                            
                        name: 'timeTableName',    
                        listeners :
                          {                             
                            render : function(o,op)  
                            { 
                              var me = this;   
                              var el = me.el;
                              var dropTarget = Ext.create('Ext.dd.DropTarget', el, {
                                ddGroup: 'explorerTree',
                                notifyOver  : function(ddSource, e, data)
                                {
                                  if ((data.records[0].data.nodeType == 'timeTable') && (data.records[0].data.leaf))						
                                  {  
                                    this.valid = true; 
                                    return this.dropAllowed;                                    
                                  }
                                  this.valid = false;
                                  return this.dropNotAllowed;                                  
                                },
                                notifyDrop  : function(ddSource, e, data)	
                                {
                                  if (!this.valid)
                                    return false;  
                                  me.setValue(data.records[0].get('text'));
                                  return true;                           
                                }                                    
                              });                                                                                          
                            }                                                                                  
                          }                                                    
                      }                     
                  ]                     
              },		    
              {		        
                xtype: 'container',			
                width : 130,			
                margin: '0 0 0 15',			
                layout : 'vbox',			
                defaults: {height : 25, width: 100},   			
                items: [			 
                  {                            
                    xtype: 'button',			                                
                    text: 'Reset Start/Stop',                                                       
                    scope: this,                            
                    handler: function() {
                        this.resetStartStop();
                    }
                  },	 		
                  { xtype: 'component', height: 34 },                                                     			
                  {                            
                    xtype: 'button',                            
                    text: 'Insert Interval',                         			    
                    scope: this,                            
                    handler: function(){
                      //   linkedNode.isValidName(value);	                                
                      this.start = this.form.getForm().findField('start').getValue();                                
                      this.stop = this.form.getForm().findField('stop').getValue();
                     // new TimeTable or typed manually				 				
                      var targetModule = myDesktopApp.getLoadedModule(this.moduleId);
                      var targetModuleUI = null;
                      if (targetModule)
                    	  targetModuleUI = targetModule.getUiContent();				
                      var TTname =  this.form.getForm().findField('timeTableName').getValue();
// TT window was closed or TTname was changed				
                      if (!targetModuleUI || !this.linkedNode || TTname !== this.linkedNode.get('text')) {						
                        var nodeWithSameName = this.rootNode.findChild('text', TTname, true);					
                        if (nodeWithSameName !== null) {					    					    
                          this.linkedNode = nodeWithSameName;					    
                          this.linkedNode.editLeaf();					    
                          this.addInterval = true;					                          
                        } 
                        else {					   
                          obj = Ext.create('amdaModel.TimeTable', {name : TTname, fromPlugin : true});					    
                          this.linkedNode = Ext.create('amdaModel.TimeTableNode', {						  
                            leaf : true,  
                            text : TTname,
                            nodeType : 'timeTable',  
                            object : obj                               
                          });					 					
                          //   this.linkedNode.set('id','');					    
                          this.linkedNode.editLeaf();					 				 					  					   
                          //TODO generic PRELOAD problem to fix					
                           Ext.Function.defer(function(){
                        	   var targetModule = myDesktopApp.getLoadedModule(this.moduleId);
                        	   if (targetModule)
                        	   {
                        		   targetModuleUI = targetModule.getUiContent();                            
                        		   targetModuleUI.addInterval(this.start,this.stop); 
                        	   }
                            this.resetStartStop();
                          }, 1000, this);   					                          
                        }
                      }   
                      else  {				  				      
                        targetModuleUI.addInterval(this.start, this.stop);
                        this.resetStartStop();
                      }                                                                                                             
                    }                                             
                  }                                           
                ]		                                                 
              }                            
            ],	    
            fbar : [		    	    
              {                        
                text: 'Zoom In Start/Stop',			                      
                width: 100,			                      
                scope: this,			                      
                handler: function(){			   
                  var startZoom = this.form.getForm().findField('start').getValue();			    
                  var stopZoom = this.form.getForm().findField('stop').getValue(); 
                  if(!startZoom || !stopZoom || !this.form.getForm().isValid()) {
                      myDesktopApp.warningMsg('StartTime or StopTime is incorrect');
                      return;
                  }
                 
                  this.hostCmp.ownerCt.resetMire();     
                  var multiZoom  = this.isMultiZoom();
                			    
                  if (multiZoom) { 
                    var win = myDesktopApp.desktop.getWindow(myDesktopApp.dynamicModules.interactive_plot.id);                                                                             
                    var multiPlotUI = win.items.getAt(0);           
                    var checked = multiPlotUI.checkBoxGroup.getChecked();
        
                    Ext.Array.each(checked,function(item, index, all){  
                      var id = item.name;  
                      var winRes = myDesktopApp.getDesktop().getWindow(id); 
                      
                      if (winRes) {      
                        var panelResult = winRes.items.items[0];     
                        panelResult.setObjectIntoNode();  
                        panelResult.object.fireEvent('execute', true, 'zoom',startZoom,stopZoom);                           
                      }                          
                    });                         
                  }
                  else {
                    this.hostCmp.ownerCt.setObjectIntoNode();	
                    this.hostCmp.ownerCt.object.fireEvent('execute', false, 'zoom',startZoom,stopZoom);				                        
                  }                  
                }                    
              },
              {                              
                text: 'Reset Zoom', 
                width: 100,                           
                scope : this,
                handler: function(){ 	
                  this.hostCmp.ownerCt.resetMire();
                  var multiZoom =  this.isMultiZoom();
                  
                  if (multiZoom) {                    
                    var win = myDesktopApp.desktop.getWindow(myDesktopApp.dynamicModules.interactive_plot.id);                                                                             
                    var multiPlotUI = win.items.getAt(0);           
                    var checked = multiPlotUI.checkBoxGroup.getChecked(); 
                    
                    Ext.Array.each(checked,function(item, index, all){		      
                      var id = item.name;		        
                      var winRes = myDesktopApp.getDesktop().getWindow(id);	  
                      if (winRes) {    
                        var panelResult = winRes.items.items[0]; 
                        panelResult.setObjectIntoNode();     
                        panelResult.object.fireEvent('execute', true, 'resetzoom');                           
                      }
                    }); 
                  }
                  else {
                    this.hostCmp.ownerCt.object.fireEvent('execute', false, 'resetzoom');                      
                  }
              }            
            }       
            ]            
            
          });   
          return this.form;   
        }    
});