/**
* 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',
'amdaUI.TimeSelectorUI',
'amdaPlotComp.PlotTabPanel',
'amdaPlotComp.PlotOutputForm',
'amdaPlotComp.PlotElementPanel'
],
formPanel: null,
multiPlotIntervalPanel : null,
plotOutput: null,
plotTabs : null,
plotElement : null,
constructor: function(config) {
this.init(config);
this.callParent(arguments);
if (this.object)
this.setObject(this.object);
},
setObject : function(object) {
this.object = object;
this.plotOutput.setObject(this.object);
this.plotTabs.setRequestObject(this.object);
this.timeSelector.intervalSel.setInterval(this.object.get('startDate'), this.object.get('stopDate'));
this.addTTs(this.object.get('timeTables'));
},
/**
* overwrite metod called by Save button
*/
overwriteProcess : function(btn)
{
if (btn == 'cancel') return;
this.saveProcess(true);
},
/**
* save method called by Save button to launch the save process
*/
saveProcess : function(toRename) {
var plotModule = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.plot.id);
if (!plotModule)
return;
if (toRename) {
plotModule.linkedNode.set('object',this.object);
plotModule.linkedNode.update();
}
else {
//Save
if (this.object.get('id') != '') {
//Duplicate request
plotModule.createLinkedNode();
plotModule.linkedNode.set('object',this.object);
}
plotModule.linkedNode.create();
}
},
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();
// plot was not called - form only
if (this.object.get('last-plotted-tab') == 0) {
this.object.set('last-plotted-tab', this.plotTabs.getSelectedTabId());
}
var downObject = amdaModel.DownloadNode.decodeObject(this.object);
amdaModel.DownloadNode.set('object',Ext.create('amdaModel.Download',downObject));
amdaModel.DownloadNode.editInModule();
},
addParameter : function(node){
var crtTree = this.plotTabs.getTreeFromPlotTab(this.plotTabs.getActiveTab());
crtTree.dropRecord(node,null,'append');
},
/**
* plot method called by 'Do Plot' button to launch the plot process
*/
doPlot : function(){
this.updateObject();
this.object.set('last-plotted-tab', this.plotTabs.getSelectedTabId());
var plotModule = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.plot.id);
if (plotModule)
plotModule.linkedNode.execute();
},
/**
* 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(){
var timeSource = this.timeSelector.getActiveTimeSource();
var multiPlotForm = this.timeSelector.getForm();
multiPlotForm.updateRecord(this.object);
this.object.set('timesrc', timeSource);
if (timeSource === amdaModel.AmdaTimeObject.inputTimeSrc[0])
this.object.set('timeTables',this.timeSelector.TTGrid.getStore().data.items);
this.plotTabs.updateTimeObject();
},
addTT : function(newTTName,newTTid,timeSelectorId) {
var crtTimeSelector = Ext.getCmp(timeSelectorId);
if (crtTimeSelector)
crtTimeSelector.addTT(newTTName,newTTid);
},
addTTs : function(TTarray) {
// set TTTab
this.timeSelector.setTTTab(TTarray);
},
/**
* 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,'\/'));
for (var i = 0; i < this.plotTabs.items.getCount(); ++i) {
var plotTab = this.plotTabs.items.getAt(i).items.getAt(0);
plotTab.items.getAt(1).intervalSel.setInterval(dateStart, dateStop);
//TBD plotTab.updateTimeObject();
}
this.timeSelector.intervalSel.setInterval(dateStart, dateStop);
},
forceActiveTab : function(tabId) {
for (var i = 0; i < this.plotTabs.items.getCount(); ++i) {
var plotTab = this.plotTabs.items.getAt(i).items.getAt(0);
if (plotTab.tabId == tabId) {
this.plotTabs.setActiveTab(i);
return;
}
}
},
updatePlotTabName: function(tabId, name) {
var me = this;
this.object.tabs().each(function (tabObject) {
if (tabId == tabObject.getId()) {
tabObject.set('tab-name', name);
me.plotTabs.updatePlotTabs();
}
});
},
insertPlotTab : function(tabData) {
var newTab = this.object.createNewTab(tabData);
this.plotTabs.addPlotTab(newTab,true);
},
updateLinkedToMultiPlotMode : function(isLinkedToMultiPlotMode) {
this.timeSelector.setVisible(isLinkedToMultiPlotMode);
},
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.
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(allTabs) {
var plotModule = myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.plot.id);
if (!plotModule)
return;
this.updateObject();
this.object.set('active-tab-id', this.plotTabs.getSelectedTabId());
var me = this;
if ((this.object.get('id') != '') && (plotModule.linkedNode.get('text') == this.object.get('name'))) {
//update existing request
if (!allTabs) {
this.keepOnlySelectedTabInObject(true, function() {
plotModule.linkedNode.update();
});
}
else {
plotModule.linkedNode.update();
}
return;
}
//save new request
var me = this;
plotModule.linkedNode.isValidName(this.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 + '
Do you want to overwrite it?',
width: 300,
buttons: Ext.Msg.OKCANCEL,
icon: Ext.Msg.WARNING,
fn : me.overwriteProcess,
scope : me
});
}
else {
myDesktopApp.errorMsg(res.error);
}
}
else {
myDesktopApp.errorMsg('Invalid object name');
}
return;
}
if (!allTabs) {
me.keepOnlySelectedTabInObject((me.object.get('id') == ''), function() {
me.saveProcess(false);
me.setObject(me.object);
});
}
else {
me.saveProcess(false);
}
});
},
init : function(config) {
this.timeSelector = new amdaUI.TimeSelectorUI( { id: 'multiPlotTimeSelector' + config.id, title : 'MultiPlot Time Selection', border : false, collapsible : true, collapseDirection : 'bottom', visible : false, flex: 2 } );
this.plotOutput = new amdaPlotComp.PlotOutputForm({flex: 2, collapseDirection : 'bottom', collapsible : true });
this.plotElement = new amdaPlotComp.PlotElementPanel({flex: 4});
this.plotTabs = new amdaPlotComp.PlotTabPanel({flex: 4, plotElementPanel : this.plotElement, plotUI : this});
this.optionsPanel = new Ext.form.Panel({
layout: {
type: 'vbox',
pack: 'start',
align: 'stretch'
},
bodyStyle: { background : '#dfe8f6' },
defaults: {
border: false
},
items: [
this.plotElement,
this.plotOutput
]
});
this.formPanel = new Ext.form.Panel({
region: 'center',
layout: {
type: 'hbox',
pack: 'start',
align: 'stretch'
},
bodyStyle: { background : '#dfe8f6' },
defaults: {
border: false
},
items: [
{
xtype : 'panel',
layout: {
type: 'vbox',
pack: 'start',
align: 'stretch'
},
bodyStyle: { background : '#dfe8f6' },
defaults: {
border: false
},
flex: 1,
items: [
this.plotTabs,
this.timeSelector // this.multiPlotIntervalPanel
]
},
{
xtype : 'panel',
layout: 'fit',
bodyStyle: { background : '#dfe8f6' },
defaults: {
border: false
},
flex: 1,
items: [
this.optionsPanel
]
}
],
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: 'splitbutton',
text: 'Save All Tabs',
menu: {
items: [
{
text: 'Save Current Tab',
scope: this,
handler: function() {
this.savePlotRequest(false);
}
},
]
},
scope: this,
handler: function(button) {
this.savePlotRequest(true);
}
}
]
});
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));
}
});