PlotTabPanel.js 3.14 KB
/**
 * Project   : AMDA-NG
 * Name      : PlotTabPanel.js
 * @class   amdaPlotComp.PlotTabPanel
 * @extends Ext.tab.Panel
 * @brief   Component use to show Plot tabs definition
 * @author  Benjamin Renard
 * @version $Id: PlotTabPanel.js benjamin $
 */

Ext.define('amdaPlotComp.PlotTabPanel', {
	extend: 'Ext.tab.Panel',
	
	requires: [
	           'amdaPlotComp.PlotTabContent'
	],
	
	//Link to the Plot Element Panel
    plotElementPanel: null,
	
	//Request object
	object: null,
	
	constructor: function(config) {
		this.init(config);	    
		this.callParent(arguments);
	},
	
	setRequestObject: function(object)
	{
		this.object = object;
		this.removeAll();
		this.object.tabs().each(function (rec) {
			this.addPlotTab(rec);
		}, this);
	},
	
	addPlotTab: function(tabObject)
	{
		var tabNumber = this.getTabBar().items.getCount();
		var tabContent = new amdaPlotComp.PlotTabContent({tabObjectId: tabObject.getId(), plotElementPanel: this.plotElementPanel});
		tabContent.setTabObject(tabObject);
    	this.add({
    		title: 'Plot '+tabNumber,
    		tabObjectId: tabObject.getId(),
    		closable: true,
    		layout: 'fit',
    		bodyStyle: 'background: none',
    		defaults: {
				border: false
			},
    		items: [
    		        tabContent
    		],
    		listeners : {
    			scope : this,
    			beforeclose: function( tab, eOpts ) {
    				if (this.items.getCount() == 1)
    				{
    					myDesktopApp.warningMsg('You need to keep at least one plot definition');
    					return false;
    				}
    				return true;
    			},
    			close: function( tab, eOpts ) {
    				var tabObject = this.object.tabs().getById(tab.tabObjectId);
    				this.plotElementPanel.resetElement();
    				if (tabObject != null)
    					this.object.tabs().remove(tabObject);
    			},
    			destroy: function(tab, eOpts) {
    				this.updatePlotTabs();
    			}
    		}
    	}).show();
    	
    	return tabContent;
	},
	
	updatePlotTabs: function()
	{
		
		var i = 0;
		for (i = 0; i < this.items.getCount(); ++i)
		{
			var tabNumber =  i+1;
			this.items.getAt(i).setTitle('Plot '+tabNumber);
			this.items.getAt(i).items.getAt(0).setTabObject(this.object.tabs().getById(this.items.getAt(i).tabObjectId));
		}
	},
	
	getTreeFromPlotTab: function(plotTab)
	{
		return plotTab.child().treePlot;
	},
	
	init : function(config) {
		var me = this;
		
		this.plotElementPanel = config.plotElementPanel;
		
		var myConf = {
				plain: true,
				bodyStyle: { background : '#dfe8f6' },  
				defaults: {
					border: false
				},
				
				tabBar:{
					items:[
					       {
					    text:'+',
					    closable: false,
					    handler:function(btn,e){
					    	var tabContent = me.addPlotTab(me.object.createNewTab());
					    }
					}]
				},
				
				listeners: {
					tabchange: function(tabPanel, newCard, oldCard, eOpts ) {
						var newCardTree = this.getTreeFromPlotTab(newCard);
						var selectedNode = newCardTree.getSelectedNode();
						if (selectedNode == null)
							me.plotElementPanel.resetElement();
						else
							me.plotElementPanel.setElement(selectedNode.type,selectedNode.object, newCardTree);
					},
					scope: me
				}
		};
		
		Ext.apply (this , Ext.apply (arguments, myConf));
	}
});