FramesSelection.js 4.86 KB
Ext.define('treps.controller.Frames.FramesSelection', {
	extend: 'Ext.app.Controller',

	requires: [
		'treps.controller.Frames.FramesManager',
		'treps.controller.Bodies.BodySelection',
		'treps.controller.Data.Store.DataStoreManager'
	],

	views: [
		'Steps.TransformationDefinition.TransformationDefinitionPanel'
	],

	refs: [
        {
                ref: 'sourceCombo',
                selector: '#srcSysFrame'
        },
		{
			ref: 'destCombo',
			selector: '#destSysFrame'
		}
	],
	
	srcCenter : '',
	dstCenter : '',

	init: function() {
		var me = this;
		//timeDefController.init();
		
			this.control({
				'#srcSysFrame': {
					select: me.onSrcFrameSelect,
					scope: me
				}
			});
			this.control({
				'#destSysFrame': {
					select: me.onDstFrameSelect,
					scope: me
				}
			});
			
	},
	
	onSrcFrameSelect: function() {
		var me = this;
		var timeDefController   = treps.app.getController('Times.SourceTimeSelection');
		if((me.getSrcFrame()=='RTN') || (me.getSrcFrame()=='RTP')) {
			if(timeDefController.getTimeFormat() == "") {//check time is defined
				treps.Messages.showWarning("You must select a time field to use RTN/RTP coordinate system.");
				me.getSourceCombo().setValue(null);
			} else {
				me.selectRTBodies('Select bodies for '+me.getSrcFrame(),me.getSourceCombo());
			}
		}
	},
	onDstFrameSelect: function() {
		var me = this;
		var timeDefController   = treps.app.getController('Times.SourceTimeSelection');
		if((me.getDstFrame()=='RTN') || (me.getDstFrame()=='RTP')) {
			if(timeDefController.getTimeFormat() == "") {//check time is defined
				treps.Messages.showWarning("You must select a time field to use RTN/RTP coordinate system.");
				me.getDestCombo().setValue(null);
			} else {
				me.selectRTBodies('Select bodies for '+me.getDstFrame(),me.getDestCombo());
			}
		}
	},

	initStore: function(onReady)
	{
		var me = this;

		treps.controller.Frames.FramesManager.loadFramesStore(
			function(store)
			{
				me.getSourceCombo().store = store;
				me.getDestCombo().store   = store;
				if (onReady != null)
					onReady.call(me, store);
                        });
	},
	
	
	//show RTN/RTP bodies selection and returns center formated string as follow:
	// centerid centername scid scname
	
	ctrlAdded: false,
	currentCombo: undefined,
	currentBodyWin: undefined,
	
	selectRTBodies: function(pTitle,frameCombo) {
		var me = this;
		//var bodySelCtrl = treps.app.getController('treps.controller.Bodies.BodySelection');
		var bodySelCtrl = Ext.create('treps.controller.Bodies.BodySelection');
		treps.controller.Data.Store.DataStoreManager.getStore('SourceDataStore');
		var bodySelWin = bodySelCtrl.getBodySelWin(pTitle);
		this.currentCombo=frameCombo;
		this.currentBodyWin = bodySelWin;
		if(!this.ctrlAdded) {
			this.control({
				'bodies_panel > #okbtn': {
				click: function() {
					var center = bodySelCtrl.getCenterBody()+' '+bodySelCtrl.getScBody();
						if(this.currentCombo==me.getSourceCombo()) {
							me.srcCenter = center;
							console.log("set src center "+me.srcCenter+" dst="+me.dstCenter);
						}
						if(this.currentCombo==me.getDestCombo()) {
							me.dstCenter = center;
							console.log("set dst center "+me.dstCenter+" src="+me.srcCenter);
						}
					me.currentBodyWin.close();
				},
				scope: me
				}
			});
			this.ctrlAdded = true;
		}
		var srcStore = treps.controller.Data.Store.DataStoreManager.getStore('SourceDataStore');
		var gridController      = treps.app.getController('Data.Grid.SourceDataGrid');
		var timeCol = gridController.getFirstColumnDataIndex(gridController.getSourceGrid());
		bodySelCtrl.initStore(srcStore.getAt(0).get(timeCol), srcStore.getAt(srcStore.totalCount-1).get(timeCol),
				function(lBodies){
					console.log("combo Sc found? "+bodySelCtrl.getBodiesScCombo().store.count(false));
					console.log("combo Center found? "+bodySelCtrl.getBodiesCenterCombo().store.count(false));
					//for RTN, select default sun value
					if(frameCombo.getValue()=='RTN') bodySelCtrl.getBodiesCenterCombo().setValue('10');
					bodySelWin.add(bodySelCtrl.getBodiesScCombo().ownerCt);
					bodySelWin.doLayout();
					bodySelWin.show();
				}
		);
	
	},

	getSrcCenter: function() {
		return this.srcCenter;
	},
	getDstCenter: function() {
		return this.dstCenter;
	},

	getSrcFrame: function() {
		return this.getSourceCombo().getValue();
	},

	getDstFrame: function() {
		return this.getDestCombo().getValue();
	},

	setSrcFrame: function(srcFrame) {
		this.getSourceCombo().setValue(srcFrame);
	},

	isValid: function() {
		var srcFrame = this.getSrcFrame();
		var dstFrame = this.getDstFrame();

		if ((srcFrame == null) && (dstFrame == null))
		{
			//No transformation
			return true;
		}
		else if (srcFrame == null)
		{
			treps.Messages.showError('Please select a source frame');
			return false;
		}
		else if (dstFrame == null)
		{
			treps.Messages.showError('Please select a destination frame');
			return false;
		}
		

		return true;
	}
});