TimeTableOperationUI.js 11.8 KB
/**
 * Project   : AMDA-NG
 * Name      : TimeTableOperationUI.js
 * @class 	 amdaUI.TimeTableOperationUI
 * @extends  Ext.container.Container
 * @brief	 Time Table Operations Module UI definition (View)
 * @author 	 Myriam
 * @version  $Id: TimeTableOperationUI.js 1937 2013-11-27 14:42:41Z myriam $
 ********************************************************************************
 *    FT Id     :   Date   : Name - Description
 *******************************************************************************
 *             01/09/2011: Myriam - Migration extjs4
 */

Ext.define('amdaUI.TimeTableOperationUI', {
	extend: 'Ext.container.Container',
	alias: 'widget.panelTtsOpe',

	constructor: function(config) {			
		this.init(config);
		this.callParent(arguments);
	},

	addTT : function(newTTName,newTTid) {
		// search for an existing record in store with this unique name
        var existingIndex = this.TTGrid.store.findExact( 'name', newTTName);
        // if no corresponding TT found
        if (existingIndex == -1){
            // adding the time table to the TTGrid of TT download 
            var r = Ext.create('amdaModel.TTobject', { id: newTTid, name: newTTName });
            this.TTGrid.store.insert(this.TTGrid.store.getCount(),r);   
        }  
	}, 

	findValidName : function(originalName, crtIndex, module, onValidName){
		if (module == null)
			return;
		
		if (originalName == "")
			return;
		
		originalName = originalName.replace(" ", "_");
		
		var crtName = originalName;
		if (crtIndex > 0)
			crtName = originalName + "_" + crtIndex;
		
		var me = this;
		
        module.linkedNode.isValidName(crtName, function (res) {
    		if (!res)
    			return;
    			
    		if (res.valid)
    		{
    			//
    			onValidName(crtName);
    			return;
    		}
    			
    		//If name is not valid, increment the index and test the new name
    		++crtIndex;
    		me.findValidName(originalName, crtIndex, module, onValidName);
    			
    	});
	},

	/**
	 * 
	 * this method will be used on timetableOperation edition when this win is already opened
	 */
	setObject : function () {
	},

	/**
	 * Check if changes were made before closing window 
	 * @return false
	 */	
	fclose : function() {
		return false;
	},
	
	init : function(config) {    

	    var store = Ext.create('Ext.data.Store', {
	        model: 'amdaModel.TTobject'	
	    });

        this.TTGrid = Ext.create('Ext.grid.Panel', {
        	anchor: '80% 85%',
            itemId: 1,
            store : store,  
            columns: [ 
                Ext.create('Ext.grid.RowNumberer', { width: 20 } ),
                { header: "Time Table Name", dataIndex: 'name', flex:1, sortable : false },
                { 
                    width: 30, renderer: function(){
                        return'<div class="icon-remover" style="width: 15px; height: 15px;"></div>';
                    }
                }
            ]
		,
	    listeners :
		{
		  render : function(o,op)
		  {
		    var me = this;
		    var el = me.body.dom;
		    var dropTarget = Ext.create('Ext.dd.DropTarget', el, {
		        ddGroup: 'explorerTree',
		        notifyEnter : function(ddSource, e, data) 
		        {
		    	  
		        },
		        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;
		          var timeTableOperationModule = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.tt_op.id);
		          if (!timeTableOperationModule)
		        	  return false;
		          timeTableOperationModule.getUiContent().addTT(data.records[0].get('text'),data.records[0].get('id'));
		          return true;
		        }
		    });
		  }
		}
        });		
        this.TTGrid.on('cellclick', function(view, cell, cellIndex, record, row, recordIndex, e){
            if (cellIndex == 2)
            	    this.TTGrid.getStore().remove(record);
        },this);
        

		var myConf = {
		    layout: 'border',
		    defaults: { layout: 'fit', border: false },
			items: [
			  {
				 xtype: 'form',
                 region: 'center',
                 buttonAlign: 'left',
                 bodyStyle: {background : '#dfe8f6'},
                 padding: '5 5 5 5',
                 layout: {type: 'vbox', pack: 'start', align: 'stretch'},	
                 items: [ 
			        {xtype: 'splitter', height: 15},
                    {						        	        	        	
                        xtype: 'container',
                        border: false,
                        padding: '0 0 5 5',
                        flex: 4,					
                        layout: 'fit',
                        items: [ this.TTGrid ] 					        	        
                    }			        
			        ],            	
			        fbar: [
			               {
			            	   text: 'Merge',
			            	   scope: this,
			            	   handler: function(){
				            	   	if (this.TTGrid.getStore().count()==0) {
				            	   		Ext.Msg.show({title: "Time Table Operations", msg: "Please select at least one time table", icon: Ext.MessageBox.WARNING, buttons: Ext.Msg.OK});
				            	   		return;
				            	   	}
			            	   		var tts = this.TTGrid.getStore().data.items;
			            	   		var ttids = [];
			            	   		var name= "";
			            	   		Ext.each(tts, function(tt, index) {
			            	   			ttids[index] = tt.data.id;
			            	   			name = name + tt.data.name + " ";
			            	   		});
			            	   		// Time table's name and history field
			            	        if ( ttids.length == 1 ) {
			                       		var newTabName = tts[0].data.name + "_M";
			                       		var history = "Merged intervals from "+name;
			                    	} else if ( ttids.length == 2 ) {
			                       		var newTabName = tts[0].data.name + "_M_" + tts[1].data.name;
			                       		var history = "Union between "+ name;
			                    	}	
			                    	else {
			                       		var newTabName = ttids.length + "_tables_M";			                       		
			                       		var history = "Union between " + name;
			                    	}

			            	        var me = this;
			            	        myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.tt_op.id, true, function (module) {
			            	        	
				            	        
				            	        me.findValidName(newTabName, 0, module, function (validName) {
				            	        	AmdaAction.merge({ids:ttids, name:validName, history:history},function(result, e){		
					            	   			var t = e.getTransaction();
					            	   			if (e.status) {	
					            	   				// SUCCESS  
					            	   				if (result && !result.error) {
					            	   					module.linkedNode.set('id',result.id);
					            	   					module.linkedNode.set('text', validName);
					            	   					module.linkedNode.set('object',null);
					            	   					module.linkedNode.set('info',result.info);
					            	   					module.linkedNode.getRootNode().appendChild(module.linkedNode);
					            	   					module.linkedNode.editLeaf();
					            	   				} else {
					            	   					// EXCEPTION : 
					            	   					Ext.Msg.show({title: "Time Table Operations", msg: t.action + "." + t.method + " : " + e.message, icon: Ext.MessageBox.ERROR, buttons: Ext.Msg.OK});
					            	   				}
					            	   			}
					            	   			else
					            	   			{
					            	   				// FAILURE
					            	   				Ext.Msg.show({title:'Error System', msg: e.message, icon: Ext.MessageBox.ERROR, buttons: Ext.Msg.OK});
					            	   			}   
					            	   		},this); 
				            	        	
				            	        	// New node for another operation
					            	        module.createLinkedNode();
					            	       
				            	        });
			            	        });  
			               		}  
			               },
			               {
			            	   text: 'Intersect',
			            	   scope: this,
			            	   handler: function(){
				            	   	if (this.TTGrid.getStore().count()!==2) {
				            	   		Ext.Msg.show({title: "Time Tables Operations", msg: "Intersection is only available for 2 tables", icon: Ext.MessageBox.WARNING, buttons: Ext.Msg.OK});
				            	   		return;
				            	   	}
				            	   	var tts = this.TTGrid.getStore().data.items;
			            	   		var ttids = [];
			            	   		Ext.each(tts, function(tt, index) {
			            	   			ttids[index] = tt.data.id;
			            	   		});
			            	   		// Time table's name and history field
			            	   		var newTabName = tts[0].data.name + "_I_" + tts[1].data.name;
			            	   		var history = "Intersection between "+ tts[0].data.name + " and " + tts[1].data.name;
			            	        
			            	   		var me = this;
			            	        myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.tt_op.id, true, function (module) {
			            	        	
				            	        
			            	        	me.findValidName(newTabName, 0, module, function (validName) {
				            	        	AmdaAction.intersect({ids:ttids, name:newTabName, history:history},function(result, e){		
					            	   			var t = e.getTransaction();
					            	   			if (e.status) {	
					            	   				// SUCCESS  
					            	   				if (result && !result.error) {
					            	   					if ( result  == "empty" ) Ext.Msg.show({title: "Time Tables Operations", msg: "The intersection of these tables is empty ", icon: Ext.MessageBox.WARNING, buttons: Ext.Msg.OK});
					            	   					else {
					            	   						module.linkedNode.set('id',result.id);
					            	   						module.linkedNode.set('text', validName);
					            	   						module.linkedNode.set('object',null);
					            	   						module.linkedNode.set('info',result.info);
					            	   						module.linkedNode.getRootNode().appendChild(module.linkedNode);
					            	   						module.linkedNode.editLeaf();
					            	   					}
					            	   				} else {
					            	   					// EXCEPTION : 
					            	   					Ext.Msg.show({title: "Time Tables Operations", msg: t.action + "." + t.method + " : " + e.message, icon : Ext.MessageBox.ERROR, buttons: Ext.Msg.OK});
					            	   				}
					            	   			}
					            	   			else
					            	   			{
					            	   				// FAILURE
					            	   				Ext.Msg.show({title:'Error System', msg: e.message, icon: Ext.MessageBox.ERROR, buttons: Ext.Msg.OK});
					            	   			}   
					            	   		},this); 
				            	        	
				            	        	// New node for another operation
					            	        module.createLinkedNode();
					            	        
				            	        });
			            	        });   
			               	  }
			               }
			              ]
               },                            
               {
                   xtype: 'panel',
                   title: 'Information',
                   region: 'south',
                   height: 100,
                   collapsible: true,
                   layout: 'fit',
                   autoHide: false,
                   iconCls: 'icon-information',
		   bodyStyle: 'padding:5px',
                   loader: {
 				      autoLoad: true,
 				      url: helpDir+'ttOperationsHOWTO'
 				    }
               }
            ]   
		};

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

	}
});