StepsManager.js 7.18 KB
Ext.define('treps.controller.Steps.StepsManager', {
	extend: 'Ext.app.Controller',
	
	requires: [
		'treps.view.Steps.SourceSelection.SourceSelectionPanel',
		'treps.view.Steps.TransformationDefinition.TransformationDefinitionPanel',
		'treps.view.Steps.ExportDefinition.ExportDefinitionPanel',
		'treps.view.Steps.ExportResult.ExportResultPanel'
	],
	
	views: [
		'Application.Central.CentralContainer',
		'Application.Footer.FooterToolbar'
	],
	
	stores: ['Steps'],

	isReceivedSrc : false,

	refs: [
		{
			ref: 'app_central',
			selector: 'app_central'
		},
		{
			ref: 'nextStepButton',
			selector: 'footer_toolbar > #nextStepButton'
		},
		{
			ref: 'prevStepButton',
			selector: 'footer_toolbar > #prevStepButton'
		},
		{
			ref: 'resetOpButton',
			selector: 'footer_toolbar > #resetOpButton'
		}
	],
	
	init: function() {
		var me = this;
		
		this.control({
			'footer_toolbar > #nextStepButton': {
				click: me.onNextStep,
				scope: me
			},
			'footer_toolbar > #prevStepButton': {
				click: me.onPrevStep,
				scope: me
			},
			'footer_toolbar > #resetOpButton': {
				click: me.onReset,
				scope: me
			}
		});
		
		this.application.on(
			{
				initapp: me.onReset,
				scope: me
			}
		);
		
		this.application.on(
			{
				receivesource: me.onReceiveSource,
				scope: me
			}
		);
	},

	onReset: function() {
		this.doResetRequest();
	},

	onReceiveSource: function(type, data) {
		var me = this;

		var steps = Ext.getStore('Steps');
		var crtStepIndex = steps.indexOf(this.getCrtStep());

		//block other source reception
		if (this.isReceivedSrc)
			return;
	
		this.isReceivedSrc = true;

		if (crtStepIndex != 0)
		{
			treps.Messages.showQuestion(
				"A notification has been received to start new operation. Do you accept?",
				function()
				{
					me.doResetRequest(function()
						{
							me.doRequest(function (success)
								{
									
									if ((success == null) || success)
									{
										var nextStep = me.getNextStep();
										if (nextStep != null)
											me.setStep(nextStep);
									}
									me.isReceivedSrc = false;
								},
								type,
								data
							);
						}
					);
				},
				function()
				{
					me.isReceivedSrc = false;
				}
			);
			return;
		}

		this.doRequest(function (success)
			{
				if ((success == null) || success)
				{
					var nextStep = me.getNextStep();
					if (nextStep != null)
						me.setStep(nextStep);
				}
				me.isReceivedSrc = false;
			},
			type,
			data
		);
	},
	
	onNextStep: function() {
		var me = this;
		this.doRequest(function (success)
			{
				if ((success == null) || success)
				{
					var nextStep = me.getNextStep();
					if (nextStep != null)
						me.setStep(nextStep);
				}
			}
		);
	},
	
	onPrevStep: function() {
		var steps = Ext.getStore('Steps');
                var firstStep = steps.getAt(0);

		var prevStep = this.getPreviousStep();
		if (prevStep != null)
		{
			if (firstStep.getId() == prevStep.getId())
				this.application.fireEvent('initapp');
			else
				this.setStep(prevStep);
		}
	},
	
	getCrtStep: function() {
		var steps = Ext.getStore('Steps');
		var session = treps.model.Session;
		return steps.getById(session.get('stepId'));
	},
	
	getNextStep: function() {
		var steps = Ext.getStore('Steps');
		var crtStepIndex = steps.indexOf(this.getCrtStep());
		if (crtStepIndex < steps.count()-1)
			return steps.getAt(crtStepIndex+1);
		return null;
	},
	
	getPreviousStep: function() {
		var steps = Ext.getStore('Steps');
		var crtStepIndex = steps.indexOf(this.getCrtStep());
		if (crtStepIndex > 0)
			return steps.getAt(crtStepIndex-1);
		return null;
	},
	
	getCrtStepPanel: function() {
		var crtStep = this.getCrtStep();
		if (crtStep == null)
			return null;
		var view = this.getApp_central();
		return view.child(crtStep.get('panel'));
	},
	
	getCrtStepController: function() {
		var crtStep = this.getCrtStep();
		if (crtStep == null)
			return null;
		return treps.app.getController(crtStep.get('controller'));
	},

	doResetRequest: function(onReady)
	{
		var steps = Ext.getStore('Steps');
		var firstStep = steps.getAt(0);
		var me = this;
		var session = treps.model.Session;

		//reset all ihm selection recorded values
		session.set('step2_vecs',null);
		session.set('step2_srcFrame',null);
		session.set('step2_dstFrame',null);
		

		if ((firstStep.getId() != session.get('stepId')) && (session.get('id') != ''))
		{
			treps.LoadMask.show('Reset operation. Please wait ... ');
			TREPSAction.resetOperation({'id' : session.get('id')}, function(result, event)
				{
					treps.LoadMask.hide();

					if (!result)
					{
						treps.Messages.showError("Internal error during resetOperation request");
						return;
					}
					else if (!result.success)
					{
						treps.Messages.showError(result.error);
						return;
					}

					me.setStep(firstStep);

					if (onReady != null)
						onReady.call();
				});
		}
		else
		{
			this.setStep(firstStep);
			if (onReady != null)
				onReady.call();
		}
	},
	
	doRequest: function(afterrequest,type,data) {
		var me = this;

		var crtController = this.getCrtStepController();
		
		if ((crtController == null) || (crtController.isValidStep == null))
			return;
	
		if ((data == null) && (!crtController.isValidStep()) || (crtController.doRequest == null))
			return;
		
		crtController.doRequest(function (success)
			{
				if (afterrequest != null)
					afterrequest.call(me,success);
			},
			type,
			data
		);
	},
	
	doInitStep: function() {
		var crtController = this.getCrtStepController();
		
		if (crtController == null)
			return;
	
		if (crtController.doInitStep == null)
			return;
		
		crtController.doInitStep();
	},
	
	setStep: function( step ) {
		var me = this,
			config;
	
		var steps = Ext.getStore('Steps');
        
		var firstStep = steps.getAt(0);
		if (firstStep.getId() == step.getId())
			treps.model.Session.set('id','');

		treps.model.Session.set('stepId',step.getId());
		
		var stepNb = steps.indexOf(step)+1;
		var stepsCount = steps.count();

		if(stepNb==1) {
			config = {
				xtype: step.get('panel'),
				title: 'Step '+stepNb+' / '+stepsCount+' - '+step.get('description')+' '+treps.Constants.UPLD_MAX_FILESIZE
			};
		} else {
			config = {
				xtype: step.get('panel'),
				title: 'Step '+stepNb+' / '+stepsCount+' - '+step.get('description')
			};
		}

		


		var view = this.getApp_central();

		view.removeAll(true);

		var newPanel = view.add(config);
	
		view.doLayout();

		var nextStep = this.getNextStep();
		var prevStep = this.getPreviousStep();
		
		var nextBtn = this.getNextStepButton();
		var prevBtn = this.getPrevStepButton();
		
		if (nextStep != null)
		{
			nextBtn.enable();
			nextBtn.setTooltip(nextStep.get('description'));
		}
		else
		{
			nextBtn.disable();
			nextBtn.setTooltip('Not available');
		}
		
		if (prevStep != null)
		{
			prevBtn.enable();
			prevBtn.setTooltip(prevStep.get('description'));
		}
		else
		{
			prevBtn.disable();
			prevBtn.setTooltip('Not available');
		}

		this.doInitStep();
	}
});