PlotModule.js 12.1 KB
/** 
 * Project   : AMDA-NG
 * Name      : PlotModule.js
 * @class    amdaDesktop.PlotModule
 * @extends  amdaDesktop.InteractiveModule
 * @brief    New Plot Module controller definition 
 * @author   Benjamin Renard
 * $Id: PlotModule.js benjamin $
 */

Ext.define('amdaDesktop.PlotModule', {
    extend: 'amdaDesktop.InteractiveModule',

    requires: [
        'amdaUI.PlotUI',
        'amdaPlotObj.MultiplotRequestObject',
        'amdaModel.MultiplotNode',
        'amdaUI.PlotTabResultUI',
        'amdaPlotComp.PlotPreviewUI',
        'amdaUI.MultiPlotUI'
    ],

    contentId: 'plotUI',
    linkedNode: null,
    panelResultInstance: null,

    /**
     * @cfg {String} data models
     * @required
     */
    nodeDataModel: 'amdaModel.MultiplotNode',

    /**
     * @cfg {String} window definitions
     * @required
     */
    width: 650,
    height: 670,
    uiType: 'newPanelPlot',
    helpTitle: 'Help on Plot Module',
    helpFile: 'plotHelp',

    plotResultWindowsManager: new Ext.AbstractManager(),

    multiPlotWin: null,

    computeResultWindowSize: function (panelResult) {
        var size = panelResult.getImageSize();
        size.width += 30;
        size.height += 95;
        return size;
    },

    computePreviewWindowSize: function (previewContent) {
        var size = previewContent.getImageSize();
        size.width += 30;
        size.height += 100;
        return size;
    },

    updateInteractiveSession: function (session, newplot, panelId_, time_) {
        var me = this;


        Ext.each(session.result, function (tabResult, index) {
            if (logExecTime && tabResult.exectime) {
                console.log("CMD EXEC TIME FOR " + tabResult.plot + " = " + tabResult.exectime + "ms");
            }

            if (tabResult.preview) {
                var plotPreviewConfig = {
                    folder: session.folder,
                    plotFile: tabResult.plot,
                    context: tabResult.context,
                    time: time_,
                    panelId: panelId_,
                    interactiveId: tabResult.id
                };
                me.updatePreview(plotPreviewConfig);
                return;
            }

            var winResultId = tabResult.id + "-win";

            var winResult = me.getWindowResult(winResultId);

            var plotTabConfig = {
                folder: session.folder,
                plotFile: tabResult.plot,
                context: tabResult.context,
                interactiveId: tabResult.id,
                isInterval: tabResult.isInterval,
                ttName: tabResult.ttName,
                ttIndex: tabResult.ttIndex,
                ttNbIntervals: tabResult.ttNbIntervals,
                ttFileIndex: tabResult.ttFileIndex,
                multiplot: tabResult.multiplot
            };

            if (winResult == null) {
                var x = 50 + tabResult.index * 50;
                var y = 100 + tabResult.index * 20;
                //create new result win
                var panelResult = new amdaUI.PlotTabResultUI(plotTabConfig);
                me.panelResultInstance = panelResult;
                var size = me.computeResultWindowSize(panelResult);

                var win = myDesktopApp.getDesktop().createWindow({
                    id: tabResult.id + "-win",
                    title: tabResult.title,
                    width: size.width,
                    height: size.height,
                    x: x,
                    y: y,
                    layout: 'fit',
                    items: [
                        panelResult
                    ],
                    listeners: {
                        scope: me,
                        beforeclose: function (win, opt) {
                            me.plotResultWindowsManager.unregister(win);
                        },
                        afterrender: function (win, opt) {
                            win.getPanelResult().updateConfig(tabResult.title, tabResult.multiplot);
                        }
                    },
                    getPanelResult: function () {
                        return panelResult;
                    },
                    updateTitle: function (multiplot) {
                        win.setTitle(tabResult.title + (multiplot ? ' - Synchronized to multiplot' : ''));
                    }
                });
                win.show();
                me.plotResultWindowsManager.register(win);
            }
            else {
                //update result
                winResult.getPanelResult().updateConfig(tabResult.title, tabResult.multiplot);
                winResult.getPanelResult().updatePlotImage(plotTabConfig, newplot);
                //update window size => not done see https://projects.irap.omp.eu/issues/9010
                //var size = me.computeResultWindowSize(winResult.getPanelResult());
                //winResult.setSize(size.width, size.height);
                winResult.toFront();
            }
        });
    },

    closeInteractiveSession: function () {
        var me = this;
        this.plotResultWindowsManager.each(function (key, value, length) {
            value.close();
        });
        if (this.multiPlotWin) {
            this.multiPlotWin.close();
        }
    },

    updatePlotResultTitle: function (tabIndex, title) {
        var winResultId = 'plot_' + tabIndex + '-win';
        var winResult = this.getWindowResult(winResultId);
        if (winResult) {
            winResult.setTitle(title);
            myDesktopApp.getDesktop().taskbar.windowBar.items.each(function (item) {
                if (winResult == item.win) {
                    item.setText(title);
                }
            });
        }
    },

    updatePreview: function (plotPreviewConfig) {
        var winPreviewId = "plot-preview-win";

        var winPreview = this.getWindowResult(winPreviewId);

        if (winPreview == null) {
            //create new preview win
            var previewContent = new amdaPlotComp.PlotPreviewUI(plotPreviewConfig);
            previewContent.setPanelResultInstance(this.panelResultInstance);
            //previewContent.setPanelId
            var size = this.computePreviewWindowSize(previewContent);

            var win = myDesktopApp.getDesktop().createWindow({
                id: winPreviewId,
                title: 'Plot Preview',
                width: size.width,
                height: size.height,
                layout: 'fit',
                items: [
                    previewContent
                ],
                listeners: {
                    scope: this,
                    beforeclose: function (win, opt) {
                        this.plotResultWindowsManager.unregister(win);
                    }
                },
                getPreviewContent: function () {
                    return previewContent;
                }
            });
            win.show();
            this.plotResultWindowsManager.register(win);
        }
        else {
            //update result
            winPreview.getPreviewContent().updatePlotImage(plotPreviewConfig);
            //update window size
            var size = this.computePreviewWindowSize(winPreview.getPreviewContent());
            winPreview.setSize(size.width, size.height);
            winPreview.toFront();
        }
    },

    getWindowResult: function (winResultId) {
        if (!this.plotResultWindowsManager.get(winResultId)) return null;
        return this.plotResultWindowsManager.get(winResultId);
    },

    setTimeInterval: function (timeObj) {
        var me = this;
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow(this.id);
        if (win) {
            me.getUiContent().setTimeFromData(timeObj);
            win.show();
        }
        else {
            this.createWindow(function () {
                me.getUiContent().setTimeFromData(timeObj);
            });
        }
    },

    addParameter: function (paramNode) {
        var me = this;
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow(this.id);
        if (win) {
            me.getUiContent().addParameter(paramNode, false);
            win.show();
        }
        else {
            this.createWindow(function () {
                me.getUiContent().addParameter(paramNode, true);
            });
        }
    },
    editPlot: function (plotNode) {
        var me = this;
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow(this.id);
        if (win) {
            // Plot UI is opened => add plot in a new tab
            me.linkedNode.get('object').plots().add(plotNode);
            me.getUiContent().editPlot(plotNode);
            win.show();
        }
        else {
            // Plot UI is closed
            this.createWindow(null, function () {
                //This is the onAfterCreateObject callback
                //Add plot node to the multiplot object
                me.linkedNode.get('object').plots().removeAll();
                me.linkedNode.get('object').plots().add(plotNode);
            });
        }
    },

    syncAfterRename: function (renamedNode) {
        var me = this;
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow(this.id);
        if (win) {
            me.getUiContent().updateTabs();
            me.getUiContent().updateRequestName(renamedNode);
        }
    },

    showMultiplotWin: function () {
        var me = this;
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow(this.id);

        if (!win) {
            return;
        }

        if (!this.multiPlotWin) {
            var multiPlotPanel = Ext.create('amdaUI.MultiPlotUI',
                {
                    plotWin: me.getUiContent()
                }
            );
            this.multiPlotWin = myDesktopApp.getDesktop().createWindow({
                id: "multiplot-win",
                title: "Multi Plot Manager",
                width: 320,
                height: 320,
                minHeight: 300,
                minWidth: 320,
                layout: 'fit',
                items: [
                    multiPlotPanel
                ],
                listeners: {
                    beforeclose: function (win, opt) {
                        me.multiPlotWin = null;
                        me.getUiContent().enableSinglePlot(true);
                        me.plotResultWindowsManager.each(function (key, value, length) {
                            value.getPanelResult().updateConfig(value.getPanelResult().plotName, false);
                        });
                    }
                }
            });
        }

        this.getUiContent().enableSinglePlot(false);
        this.multiPlotWin.show();

    },

    refreshMultiPlot: function () {
        if (this.multiPlotWin) {
            this.multiPlotWin.items.items[0].refreshMultiPlot();
        }
        this.getUiContent().enableSinglePlot(!this.isMultiPlot());
    },

    isMultiPlot: function () {
        return this.multiPlotWin && !this.multiPlotWin.isHidden();
    },

    editInDownloadModule: function (plotNode) {
        var plotValues = plotNode.get('object').getJsonValues();
        var downloadValues = new Object();
        downloadValues.timesrc = plotValues.timesrc;
        downloadValues.startDate = plotValues.startDate;
        downloadValues.stopDate = plotValues.stopDate;
        downloadValues.durationDay = plotValues.durationDay;
        downloadValues.durationHour = plotValues.durationHour;
        downloadValues.durationMin = plotValues.durationMin;
        downloadValues.durationSec = plotValues.durationSec;
        downloadValues.durationMs = plotValues.durationMs;
        if (plotValues.timeTables)
            downloadValues.timeTables = plotValues.timeTables;
        downloadValues.list = [];
        plotNode.get('object').panels().each(function (panel) {
            panel.params().each(function (param) {
                paramObj = param.getJsonValues();
                paramObj.id = "";
                downloadValues.list.push(paramObj);
            });
        });
        myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.download.id, true, function (module) {
            module.editFromJsonData(downloadValues);
        });
    }
});