PlotTabContent.js 10.2 KB
/**
 * Project   : AMDA-NG
 * Name      : PlotTab.js
 * @class   amdaPlotComp.PlotTab
 * @extends Ext.form.Panel
 * @brief   Tab content to define a plot
 * @author  Benjamin Renard
 * @version $Id: PlotTab.js benjamin $
 */

Ext.define('amdaPlotComp.PlotTabContent', {
    extend: 'Ext.form.Panel',

    requires: [
        'amdaUI.TimeSelectorUI',
        'amdaUI.DescriptionField',
        'amdaPlotComp.PlotTree',
        'amdaPlotComp.PlotOutputForm',
        'amdaPlotComp.PlotElementPanel'
    ],
	
    //Link to the Plot Node
    plotNode: null,

    //Save initial request data to have the possibility to reject all modifications
    initialObjectData: null,

    timeSelector: null,
    plotTree: null,
    plotElement: null,
    plotOutput: null,

    tabIndex: 0,
    plotTabPanel: null,
    
    constructor: function(config) {
		this.init(config);	    
		this.callParent(arguments);
    },

    setTime : function(startDate, stopDate) {
        this.timeSelector.intervalSel.setInterval(startDate, stopDate);
        this.timeSelector.setActiveTimeSource(amdaModel.AmdaTimeObject.inputTimeSrc[1]);
        
    },
    getTimeSelector: function(){
        return this.timeSelector;
    },
	
    updateTimeObject : function() {
        var timeSource = this.timeSelector.getActiveTimeSource();
        var tabForm = this.getForm();
        tabForm.updateRecord(this.plotNode.get('object'));
        this.plotNode.get('object').set('timesrc', timeSource);
        if (timeSource === amdaModel.AmdaTimeObject.inputTimeSrc[0])
            this.plotNode.get('object').set('timeTables',this.timeSelector.TTGrid.getStore().data.items);
    },

    doPlot : function() {
        this.plotNode.get('object').set('tab-index', this.tabIndex);
        this.plotNode.get('object').set('tab-title', this.getPlotTitle());
        this.plotNode.get('object').set('multi-selected', false);
        var plotModule = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.plot.id);
        plotModule.refreshMultiPlot();
        this.plotNode.execute();
    },

    getPlotTitle: function() {
       return (this.plotNode.get('text') != '') ? this.plotNode.get('text') : 'Plot '+(this.tabIndex + 1); 
    },

    savePlot : function() {
        var me = this;

        var object = this.plotNode.get('object');
        if (!object)
            return;

        if ((object.get('id') != '') && (this.plotNode.get('text') == object.get('name'))) {
            //update existing request
            this.updateTimeObject();
            this.plotNode.update({plot: true, callback: function() {
                me.setPlotNode(me.plotNode); //to update initial request data
                me.plotTabPanel.updatePlotTabs();
            }});
            return;
        }

        //save new request
        this.plotNode.isValidName(object.get('name'), function (res) {
            if (!res) {
                myDesktopApp.errorMsg('Error during object validation');
                return;
            }
            if (!res.valid) {
                if (res.error) {
                    if (res.error.search('subtree') != -1) {
                        Ext.Msg.show( { title : 'Warning',
                            msg: res.error + '<br/>Do you want to overwrite it?',
                            width: 300,
                            buttons: Ext.Msg.OKCANCEL,
                            icon: Ext.Msg.WARNING,
                            fn :  function(btn) {
                                if (btn == 'cancel') return;
                                me.saveProcess(true);
                            },
                            scope : me
                        });
                    }
                    else {
                        myDesktopApp.errorMsg(res.error);
                    }
                }
                else {
                    myDesktopApp.errorMsg('Invalid object name');
                }
                return;
            }
            me.saveProcess(false);
        });
    },

    saveProcess : function(overwriteExistingNode) {
        var plotModule = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.plot.id);
        if (!plotModule)
            return;

        var me = this;
        if (overwriteExistingNode) {
            this.updateTimeObject();
            amdaModel.InteractiveNode.preloadNodes(this.plotNode.getRootNode(), function () {
                var targetPlotNode = me.plotNode.getRootNode().findChild('text', me.plotNode.get('object').get('name'), true);
                if (!targetPlotNode) {
                    myDesktopApp.errorMsg('Cannot retrieve request node');
                    return;
                }
                targetPlotNode = plotModule.linkedNode.get('object').overwritePlot(me.plotNode, targetPlotNode);
                if (targetPlotNode) {
                    //Reject modifications in old plot node
                    me.resetModif();
                    targetPlotNode.update({plot: true, callback: function() {
                        me.setPlotNode(targetPlotNode);
                        me.plotTabPanel.updatePlotTabs();
                        me.updateUI();
			plotModule.updatePlotResultTitle(me.tabIndex, targetPlotNode.get('text'));
                    }});
                }
            });
        }
        else {
            if (this.plotNode.get('object').get('id') != '') {
                //Duplicate request
                var newPlotNode = this.plotTabPanel.duplicatePlotNode(this.plotNode);
                if (newPlotNode) {
                    //Reject modifications in old plot node
                    this.resetModif();
                    //Set new plot node to the current tab
                    this.setPlotNode(newPlotNode);
                }
            }
            this.updateTimeObject();
            this.plotNode.create({plot: true, callback: function() {
                me.plotNode.commit();
                me.setPlotNode(me.plotNode); //to update initial request data
                me.plotTabPanel.updatePlotTabs();
                me.updateUI();
		plotModule.updatePlotResultTitle(me.tabIndex, me.plotNode.get('text'));
            }});
        }
    },

    resetModif : function() {
        this.plotNode.set('object', Ext.create('amdaPlotObj.PlotRequestObject', this.initialObjectData));
    },

    getDataProcess : function() {
	var plotModule = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.plot.id);
	plotModule.editInDownloadModule(this.plotNode);
    },

    isValidRequest : function(acceptEmptyTTList = true) {
        this.updateTimeObject();
        if (!this.timeSelector.isValid(acceptEmptyTTList)) {
            myDesktopApp.errorMsg('Error in Time definition');
            return false;
        }
        return true;
    },

    updateUI : function() {
        this.plotOutput.setObject(this.plotNode.get('object'));
        var descField = this.descriptionCase.items.items[0];
        descField.setValue(this.plotNode.get('object').get('description'));
        this.timeSelector.intervalSel.setInterval(this.plotNode.get('object').get('startDate'), this.plotNode.get('object').get('stopDate'));
	this.timeSelector.intervalSel.updateStop();
        this.timeSelector.setTTTab(this.plotNode.get('object').get('timeTables'));
        this.timeSelector.timeSrc.setActiveTab(this.plotNode.get('object').get('timesrc'));
        this.treePlot.buildTree(this.plotNode.get('object'));
    },

    setPlotNode : function(plotNode) {
        this.initialObjectData = plotNode.get('object').getJsonValues();
        this.plotNode = plotNode;
    },

    enableTimeSelection : function(enable) {
        this.timeSelector.setDisabled(!enable);
    },

    init : function(config) {
        this.setPlotNode(config.plotNode);
        this.tabIndex = config.tabIndex;
        this.plotTabPanel = config.plotTabPanel;

        this.timeSelector = new amdaUI.TimeSelectorUI( { id: Ext.id()/*'plotTimeSelectorTab' + this.plotNode.id*/, border : false, flex: 6, collapsible: true, collapseDirection : 'bottom'} );
        this.plotElement = new amdaPlotComp.PlotElementPanel({flex: 12});
        this.treePlot = new amdaPlotComp.PlotTree({flex: 11, plotElementPanel: this.plotElement});
        this.plotOutput = new amdaPlotComp.PlotOutputForm({flex: 8, collapseDirection : 'bottom', collapsible : true });
        this.descriptionCase = new Ext.form.FieldSet({
            title:'Request description',
            collapsible:true,
            collapsed:true,
            flex:3,
            bodyStyle: {background: '#dfe8f6',padding: '1', },
            border:false, 
            layout:'fit',
            items:[{ xtype: 'descriptionfield', name: 'description', height:50, 
            emptyText:'Description', fieldLabel: ''}]
        });

        var treePanel = new Ext.form.Panel({
            flex: 1,
            layout: {
                type: 'vbox',
                pack: 'start',
                align: 'stretch'
            },
            bodyStyle: { background : '#dfe8f6' },
            defaults: {
                border: false
            },
            items: [
                this.treePlot,
                this.timeSelector
            ]
        });

        var optionsPanel = new Ext.form.Panel({
            flex: 1,
            layout: {
                type: 'vbox',
                pack: 'start',
                align: 'stretch'
            },
            bodyStyle: { background : '#dfe8f6' },
            defaults: {
                border: false
            },
            items: [
                this.plotElement,
                this.plotOutput,
                this.descriptionCase
            ]
        });

		
        var myConf = {
            bodyStyle: { background : '#dfe8f6' },
            border : false,
            defaults: {
                border: false
            },
            layout: {
                type: 'hbox',
                pack: 'start',
                align: 'stretch'
            },
            items: [
                treePanel,
                optionsPanel
            ],
            listeners: {
                afterrender: function(comp, eOpts) {
                    this.updateUI();
                },
                beforedestroy: function(comp, eOpts) {
                    this.resetModif();
                },
                scope: this
            }
        };

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