PlotTabPanel.js 8.47 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 UI
    plotUI : null,
	
    //Multiplot object
    multiplot_object: null,

    tabbar_destroy : false,
	
    constructor: function(config) {
        this.init(config);	    
        this.callParent(arguments);
    },
	
    setMultiplotObject: function(multiplot_object) {
        var me = this;
        this.removeAll();
        this.multiplot_object = multiplot_object;

        this.multiplot_object.plots().each(function (rec, index) {
            this.addPlotNode(rec, index == 0, false);
        }, this);
        this.refreshMultiPlot();
    },

    reloadPlot : function(plotNode) {
        for (i = 0; i < this.items.getCount(); ++i)
        {
            var tabItem = this.items.getAt(i);
            var tabContent = tabItem.items.getAt(0);
            if (plotNode == tabContent.plotNode) {
                tabContent.updateUI();
            }
        }
    },

    isDirty : function() {
        var dirty = false;
        this.multiplot_object.plots().each(function (rec, index) {
            if (rec.get('object').get('id') != '')
                dirty |= rec.get('object').isDirty();
        });
        return dirty;
    },
	
    addPlotNode: function(plotNode, selectTab, refreshMultiPlot)
    {
        for (i = 0; i < this.items.getCount(); ++i)
        {
            var tabItem = this.items.getAt(i);
            var tabContent = tabItem.items.getAt(0);
            if (plotNode == tabContent.plotNode) {
                //Already opened => select tab
                this.setActiveTab(tabItem);
                return;
            }
        }

        var tabNumber = this.getTabBar().items.getCount();
        var tabContent = new amdaPlotComp.PlotTabContent({plotNode: plotNode, tabIndex: tabNumber-1, plotTabPanel: this});
        var me = this;
        var tabComp = this.add({
            title: tabContent.getPlotTitle(),
            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 ) {
                    if (tab.items.getAt(0).plotNode) {
                        tab.items.getAt(0).resetModif();
                        this.multiplot_object.removePlotByInternalId(tab.items.getAt(0).plotNode.internalId);
                    }
                },
                destroy: function(tab, eOpts) {
                    if (tab.items.getAt(0) && tab.items.getAt(0).plotNode) {
                        tab.items.getAt(0).resetModif();
                    }
                    if (!this.tabbar_destroy)
                        this.updatePlotTabs();
                }
            }
        });
    	
        if (selectTab)
            this.setActiveTab(tabComp);

        if (refreshMultiPlot)
            this.refreshMultiPlot();
    	
        return tabContent;
    },

    duplicatePlotNode: function(plotNode)
    {
        var newPlotNode = this.multiplot_object.duplicatePlot(plotNode);
        if (!newPlotNode)
            return null;
        return newPlotNode;
    },
	
    updatePlotTabs: function()
    {
        for (i = 0; i < this.items.getCount(); ++i)
        {
            var tabItem = this.items.getAt(i);
            var tabContent = tabItem.items.getAt(0);
            tabContent.tabIndex = i;
            var plotNode = tabContent.plotNode;
            var title = tabContent.getPlotTitle();
            tabItem.setTitle(title);
            if (!this.getActiveTab())
                this.setActiveTab(tabItem);
        }
        this.refreshMultiPlot();
    },

    closePlotTabIfOpened: function(plotNodeToClose) {
        for (i = 0; i < this.items.getCount(); ++i)
        {
            var tabItem = this.items.getAt(i);
            var tabContent = tabItem.items.getAt(0);
            var plotNode = tabContent.plotNode;
            if (plotNode == renamedNode) {
                this.remove(tabItem.getId());
                this.refreshMultiPlot();
            }
        }
    },

    updateRequestName: function(renamedNode)
    {
        for (i = 0; i < this.items.getCount(); ++i)
        {
            var tabItem = this.items.getAt(i);
            var tabContent = tabItem.items.getAt(0);
            var plotNode = tabContent.plotNode;
            if (plotNode == renamedNode) {
		plotNode.get('object').set('name', renamedNode.get('text'));
                tabContent.updateUI();
                this.refreshMultiPlot();
            }
        }
    },

    getCurrentPlotTabContent : function() {
        if (this.getActiveTab())
            return this.getActiveTab().child();
        return null;
    },

    setActiveTabByInteractiveId : function(interactiveId) {
        var tabsInfo = this.getTabsInfo();
        var me = this;
        Ext.Array.each(tabsInfo, function(tabInfo) {
            if (('plot_'+tabInfo.object.get('tab-index')) == interactiveId) {
                me.setActiveTab(tabInfo.tabItem);
            }
        });
    },
	
    updateTimeObjects : function() {
        for (i = 0; i < this.items.getCount(); ++i)
        {
            this.items.getAt(i).items.getAt(0).updateTimeObject();
        }
    },
	
    getTreeFromPlotTab: function(plotTab)
    {
        return plotTab.child().treePlot;
    },

    getTabsInfo: function()
    {
        var tabsInfo = [];
        for (i = 0; i < this.items.getCount(); ++i) {
            var tabItem = this.items.getAt(i);
            var tabContent = tabItem.items.getAt(0);
            var plotNode = tabContent.plotNode;
            plotNode.get('object').set('tab-index', tabContent.tabIndex);
            plotNode.get('object').set('tab-title', tabContent.getPlotTitle());
            tabsInfo.push({
                tabItem: tabItem,
                name: plotNode.get('object').get('tab-title'),
                object: plotNode.get('object'),
                selected: (tabItem == this.getActiveTab()),
                tabContent: tabContent
            });
        }
        return tabsInfo;
    },

    refreshMultiPlot: function()
    {
        var plotModule = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.plot.id);
        plotModule.refreshMultiPlot();
    },

    enableTimeSelection: function(enable)
    {
        for (i = 0; i < this.items.getCount(); ++i)
        {
            this.items.getAt(i).items.getAt(0).enableTimeSelection(enable);
        }
    },
	
    init : function(config) {
        var me = this;

        this.plotUI = config.plotUI;

        var myConf = {
            plain: true,
            bodyStyle: { background : '#dfe8f6' },
            border : false,
            defaults: {
                border: false
            },	
            tabBar:{
                items:[
                    {
                        text:'+',
                        closable: false,
                        handler:function(btn,e){
                            var tabContent = me.addPlotNode(me.multiplot_object.createNewPlot(), true, true);
                        }
                    }
                ],
                listeners: {
                    beforedestroy: function(tabbar, eOpts) {
                        me.tabbar_destroy = true;
                    }
                }
            },
            listeners: {
                tabchange: function(tabPanel, newCard, oldCard, eOpts) {
			if (newCard) {
				var newTree = this.getTreeFromPlotTab(newCard);
				if (newTree) {
					if (newTree.plotElementPanel != null) {
						var selectedNode = newTree.getSelectedNode();
						if (selectedNode != null) {
							newTree.plotElementPanel.setElement(selectedNode.type, selectedNode.object, newTree);
						}
					}
				}
			}
                },
                scope: this
            }
        };

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