SourceSelectionStep.js 7 KB
Ext.define('treps.controller.Steps.SourceSelectionStep', {
	extend: 'treps.controller.Steps.BaseStep',
	
	views: [
		'Steps.SourceSelection.SourceSelectionPanel'
	],

	requires: [
		'treps.controller.SAMP.SAMPManager'
	],

	sampButtonSelector : 'sourceselection_panel > panel > sampdata_panel > sampconnect_button',

	sampClientsDataViewSelector : 'sourceselection_panel > panel > sampdata_panel > sampclients_dataview',

	localFileSelector : 'sourceselection_panel > panel > localfile_panel > filefield',

	refs: [
		{
			ref: 'sourceSelectionPanel',
			selector: 'sourceselection_panel'
		},
		{
			ref: 'filefield',
                        selector: 'filedropzone'
                }
	],

	init: function() {
		var me = this;

		this.ref(
			[
				{
					ref: 'sampConnectButton',
					selector: this.sampButtonSelector
				},
				{
					ref: 'sampClientsDataView',
					selector: this.sampClientsDataViewSelector
				}
			]
		);

		var c = {};
		c[this.sampButtonSelector] = {
			click : me.onChangeSampConnection,
			scope : me
		};


		c[this.localFileSelector] = {
			change : me.onSelectLocalFile,
			scope  : me
		};

		this.control(c);


		this.application.on(
			{
				samp_connect: me.updateSAMPConnection,
                                scope: me
                        }
                );

		this.application.on(
			{
				samp_disconnect: me.updateSAMPConnection,
				scope: me
			}
		);

		this.application.on(
			{
				samp_clients: me.updateSAMPClients,
				scope: me
			}
		);
	},

	getCrtPanel: function() {
		var mainPanel = this.getSourceSelectionPanel();
		return mainPanel.child("[collapsed=false]").child("panel");
	},

	onChangeSampConnection: function()
	{
		var sampManager = treps.controller.SAMP.SAMPManager;

		if (!sampManager.isConnected())
			sampManager.connect();
		else
			sampManager.disconnect();
	},

	onSelectLocalFile : function()
	{
		treps.getApplication().fireEvent('receivesource',null,null);	
	},

	updateSAMPConnection: function()
	{
		var sampManager = treps.controller.SAMP.SAMPManager;
		var sampButton  = this.getSampConnectButton();

		if (sampButton == null)
			return;

		var connected = sampManager.isConnected();

		if (connected)
		{
			sampButton.setText('Disconnect to hub');
			sampButton.setIconCls('icons-24x24-disconnect');
		}
		else
		{
			sampButton.setText('Connect to hub');
			sampButton.setIconCls('icons-24x24-connect');
		}
		
	},

	updateSAMPClients: function()
	{
		var sampManager = treps.controller.SAMP.SAMPManager;
		var clientsStore = sampManager.getClientsStore();
		var clientsDataView = this.getSampClientsDataView();

		if (clientsDataView == null)
			return;

		if (clientsDataView.getStore().storeId != clientsStore.storeId)
			clientsDataView.bindStore(clientsStore);
	},

	doInitStep: function() {
		var editController = treps.app.getController('Data.Edit.EditDataGrid');
		editController.clearStore();
		var dropController = treps.app.getController('Drop.FileDrop');
		this.updateSAMPConnection();
		this.updateSAMPClients();
	},
	
	isValidStep: function() {
		var me = this;
		var crtPanel = this.getCrtPanel();

		var editController = treps.app.getController('Data.Edit.EditDataGrid');

		if (crtPanel == null)
			return false;
		
		switch (crtPanel.xtype)
		{
			case 'localfile_panel':
				if (!crtPanel.isValid())
				{
					treps.Messages.showError("Validation error. Cannot submit the request.");
					return false;
				}
				break;
			case 'sampdata_panel':
				treps.Messages.showInfo("Please send data with a SAMP notification.");
				return false;
			case 'webfile_panel':
				if (!crtPanel.isValid())
				{
					treps.Messages.showError("Validation error. Cannot submit the request.");
					return false;
				}
				break;
			case 'manualdata_panel':
				if (!editController.isValid())
				{
					treps.Messages.showInfo("Validation error. Cannot submit the request.");
					return false;
				}
				break;
		}
		
		return true;
	},
	
	doRequest: function(afterrequest,type,data) {
		var me = this;
		var crtPanel = this.getCrtPanel();
		if (crtPanel == null)
			return false;
		
		var editController = treps.app.getController('Data.Edit.EditDataGrid');

		//reinit source selection
		var session = treps.model.Session;
		session.set('in_filenameBase',null);

		//on SAMP notification or drag & drop selection
		if ((type != null) && (data != null))
		{
			me.callRequest(type,data,afterrequest);
			return;
		}

		switch (crtPanel.xtype)
		{
			case 'localfile_panel':
				crtPanel.getForm().submit(
					{
						url: treps.Constants.PHP_DIR+'UploadFile.php',
						waitMsg: 'Uploading your file...',
						timeout: 120000,
						success: function(form, action) {
							var filenameBase = action.result.fileName.substring(0,
								action.result.fileName.lastIndexOf('.'));
							console.log('localfile_panel filename: '+filenameBase);
							treps.model.Session.set('in_filenameBase',filenameBase);
							me.callRequest('local',action.result.fileName,afterrequest);
						},
						failure: function(form, action)
						{
							treps.LoadMask.hide();

							switch (action.failureType)
							{
								case Ext.form.action.Action.CLIENT_INVALID:
									treps.Messages.showError('Form fields may not be submitted with invalid values');
									break;
								case Ext.form.action.Action.CONNECT_FAILURE:
									treps.Messages.showError('Ajax communication failed');
									break;
								case Ext.form.action.Action.SERVER_INVALID:
									if (!action.result)
										treps.Messages.showError("Internal error during UploadFile request");
									else
										treps.Messages.showError(action.result.error);
									break;
								default:
									treps.Messages.showError('Unknown error');
							}
							afterrequest.call(me,false);

						}
					}
				);
				break;
			case 'sampdata_panel':
				//nothing to do
				break;
			case 'webfile_panel':
				var url = crtPanel.child("textfield").getRawValue();
				me.callRequest('url',url,afterrequest);
				break;
			case 'manualdata_panel':
				var data = editController.getData();
				me.callRequest('data',data,afterrequest);
				break;
		}
	},
	
	callRequest : function(type, location, afterrequest)
	{
		var me = this;
		treps.LoadMask.show("Get source data. Please Wait ...");
		TREPSAction.startNewOperation({'type' : type, 'data' : location}, function(result, event)
			{
				treps.LoadMask.hide();
			//	console.log('callRequestlocation: '+location); 
				
				if (!result)
				{
					treps.Messages.showError("Internal error during startNewOperation request");
					afterrequest.call(me,false);
					return;
				}
				else if (!result.success)
				{
					treps.Messages.showError(result.error);
					afterrequest.call(me,false);
					return;
				}
			
				var session_id = result.id;


				treps.model.Session.set('id',session_id);
			
				
				afterrequest.call(me,true);
			}
		);
	}
});