PlotUI.js 7.07 KB
/**
 * Project   : AMDA-NG
 * Name      : PlotUI.js
 * @class   amdaUI.PlotUI
 * @extends Ext.container.Container
 * @brief   New Plot Module UI definition (View)
 * @author  Benjamin Renard
 * @version $Id: PlotUI.js benjamin $
 */


Ext.define('amdaUI.PlotUI', {
    extend: 'Ext.container.Container',
    alias: 'widget.newPanelPlot',
	
    requires: [
        'amdaModel.DownloadNode',
        'amdaModel.Download',
        'amdaPlotComp.PlotTabPanel'
    ],
	
    formPanel: null,	
    plotTabs : null,

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

    setObject : function(object) {
        this.object = object;
        if (this.object.plots().count() == 0) {
            this.object.createNewPlot();
        }
        this.plotTabs.setMultiplotObject(this.object);
    },
	
    resetProcess : function(){
        var plotModule = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.plot.id);			
        plotModule.createLinkedNode();
        var obj = null;
        if ((arguments.length > 0) && (arguments[0] != null)) {
            obj = arguments[0];
        }
        plotModule.createObject(obj);					  
        this.setObject(plotModule.linkedNode.get('object'));
    },
	
    getDataProcess : function(){
        this.updateObject();
        var plotTab = this.plotTabs.getCurrentPlotTabContent();
        if (!plotTab || !plotTab.plotNode)
            return;
        var downObject = amdaModel.DownloadNode.decodeObject(plotTab.plotNode.get('object'));
        amdaModel.DownloadNode.set('object',Ext.create('amdaModel.Download',downObject));
        amdaModel.DownloadNode.editInModule();
    },
    
    addParameter : function(node, updateTime){
        var crtTree = this.plotTabs.getTreeFromPlotTab(this.plotTabs.getActiveTab());
        if (crtTree) {
            crtTree.dropRecord(node,null,'append');
            if (updateTime) {
                if((node.get('globalStart') != null) && (node.get('globalStop') != null) && node.get('globalStart') != 'depending on mission' && node.get('isParameter')) {
                    this.setTimeFromData(node.getTimeFromNode(node));
                }
            }
        }
    },
    
    /**
     * plot method called by 'Do Plot' button to launch the plot process
     */
    doPlot : function(){
        var plotTab = this.plotTabs.getCurrentPlotTabContent();
        if (plotTab)
            plotTab.doPlot();
    },  
	
	/**
	 * Check if changes were made before closing window 
	 * @return false
	 */	
	fclose : function() {
		var module = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.plot.id);
		if (module)
			module.closeInteractiveSession();
		
		return this.object.isDirty();
	},
	
    /**
     * update time selector of this.object from form
     */
    updateObject : function(){
        this.plotTabs.updateTimeObjects();
    },
	
    addTT : function(newTTName,newTTid,timeSelectorId) {
        var crtTimeSelector = Ext.getCmp(timeSelectorId);
        if (crtTimeSelector)
        crtTimeSelector.addTT(newTTName,newTTid);
    },
	
    /**
     * Set Start-Stop from parameter info (Local & MyData)
     */ 
    setTimeFromData : function(obj) {                                                               
        if (!obj.start || !obj.stop)
            return;
        var dateStart = new Date(obj.start.replace(/[T|Z]/g,' ').replace(/\-/g,'\/')); 
        var dateStop = new Date(obj.stop.replace(/[T|Z]/g,' ').replace(/\-/g,'\/')); 

        var plotTab = this.plotTabs.getCurrentPlotTabContent();
        if (plotTab)
            plotTab.setTime(dateStart, dateStop);
    },

	keepOnlySelectedTabInObject: function(showWarning, onSuccess) {
		if (this.object.tabs().count() == 1) {
			if (onSuccess) {
				onSuccess();
			}
			return;
		}

		var me = this;
		var removeFunc = function() {
			var tabsToRemove = [];
			var selectedTabFound = false;
			me.object.tabs().each(function(tab) {
				if (tab.get('id') != me.object.get('active-tab-id')) {
					tabsToRemove.push(tab);
				}
				else {
					selectedTabFound = true;
				}
			});
			if (!selectedTabFound) {
				myDesktopApp.errorMsg('Cannot retrieve selected tab');
				return false;
			}
			if (tabsToRemove.length > 0) {
				me.object.tabs().remove(tabsToRemove);
			}
			return true;
		};

		if (!showWarning) {
			if (removeFunc()) {
				if (onSuccess) {
					onSuccess();
				}
			}
			return;
		}


		Ext.Msg.show( { title : 'Warning',
			msg: 'Active plot will be saved, but other ones will be lost.<br/>Do you want to continue?',
			width: 300,
			buttons: Ext.Msg.OKCANCEL,
			fn: function(btn) {
				if (btn == 'cancel') return;

				if (removeFunc()) {
					if (onSuccess) {
						onSuccess();
					}
				}
				return;
			},
			scope: me
		});
	},

    savePlotRequest : function() {
        var plotTab = this.plotTabs.getCurrentPlotTabContent();
        if (plotTab)
            plotTab.savePlot();
    },
    
	init : function(config) {
		this.plotTabs = new amdaPlotComp.PlotTabPanel({plotUI : this});

                this.formPanel = new Ext.form.Panel({
                    region: 'center',
                    layout: 'fit',
                    bodyStyle: { background : '#dfe8f6' },
                    defaults: {
                        border: false
                    },
                    items: [
                        this.plotTabs
                    ],
                    fbar: [
                        {
                            xtype: 'button',
                            text: 'Plot',
                            scope: this,
                            handler: function(button) {
                                this.doPlot();
                            }
                        },' ', {
                            xtype: 'button',
                            text: 'Get Data',
                            scope: this,
                            handler: function(button) {
                                this.getDataProcess();
                            }
                        },' ', {
                            xtype: 'button',
                            text: 'Reset',
                            scope: this,
                            handler: function(button) {
                                this.resetProcess();
                            }
                        },'->', '-', {
                            xtype: 'button',
                            text: 'Save',
                            scope: this,
                            handler: function(button) {
                                this.savePlotRequest();
                            }
                        }
                    ]
                });

		var myConf = {
			layout: 'border',
			items: [
				this.formPanel,
				{
					xtype: 'panel', 
					region: 'south',
					title: 'Information',
					collapsible: true,
					collapseMode: 'header',
					height: 100,
					autoHide: false,
					bodyStyle: 'padding:5px',
					iconCls: 'icon-information',
					loader: {
						autoLoad: true,
						url: helpDir+'plotHOWTO'
					}
				}
			]
		};
		
		Ext.apply(this, Ext.apply(arguments, myConf));
	}
});