EpnTapModule.js 6.09 KB
/**
 * Project  : AMDA-NG
 * Name	 : EpnTapModule.js
 * @class   amdaDesktop.EpnTapModule
 * @extends amdaDesktop.AmdaModule
 * @brief   EpnTap Module controller definition
 * @author  Nathanael Jourdane
 */

Ext.define('amdaDesktop.EpnTapModule', {

	extend: 'amdaDesktop.AmdaModule',
	requires: ['amdaUI.EpnTapUI'],
	contentId : 'EpnTapUI',

	/** The alias name of the module view. */
	// uiType: 'panelEpnTap',
	uiType : 'panelEpnTap',

	/** The text displayed on the *help button* tooltip. */
	helpTitle: 'Help on EPN-TAP Module',

	/** The name of the documentation file related to the module. */
	helpFile : 'epnTapHelp',

	/**
	Module initialisation. Called the first time that the user open the epnTap module.
	*/
	init: function() {
		this.selectedService = null;

		this.launcher = {
			text: this.title,
			iconCls: this.icon,
			handler: this.createWindow,
			scope: this
		};
	},

	/**
	Called each time the epntap module is loaded.
	- `target`: an array of 3 values: [target_name, dataproduct_type]; or null.
	*/
	loadTarget: function(target) {
		this.target = target;
		this.aquireElements();
		this.addListeners();

		// this.servicesStore.clearFilter();
		this.granulesStore.removeAll();

		if(target) {
			var name = target[0].charAt(0).toUpperCase() + target[0].replace(/_/g, ' ').substr(1).toLowerCase();
			this.targetNameCB.getStore().add({'id': target[0], 'name': name});
			this.targetNameCB.select(target[0]);
			this.productTypeCB.select(target[1]);
			this.updateNbResults();
		}
	},

	aquireElements: function() {
		// UI elements
		this.servicesGrid = Ext.getCmp('epnTapServicesGrid');
		this.granulesGrid = Ext.getCmp('epnTapGranulesGrid');
		this.productTypeCB = Ext.getCmp('epnTapProductTypeCB');
		this.targetNameCB = Ext.getCmp('epnTapTargetNameCB');
		this.pagingTB = Ext.getCmp('epnTapPagingTB');
		this.timeSelector = Ext.getCmp('epnTapTimeSelector');
		this.getBtn = Ext.getCmp('epnTapGetBtn');

		// stores elements
		this.servicesStore = Ext.data.StoreManager.lookup('servicesStore');
		this.granulesStore = Ext.data.StoreManager.lookup('granulesStore');
		this.productTypesStore = Ext.data.StoreManager.lookup('productTypesStore');
		this.targetNamesStore = Ext.data.StoreManager.lookup('targetNamesStore');
	},

	addListeners: function() {
		this.targetNameCB.on('change', function() {
			this.updateGetBtnStatus();
		}, this);

		this.productTypeCB.on('change', function() {
			this.updateGetBtnStatus();
		}, this);

		this.servicesGrid.on('cellclick', function(grid, td, cellIndex, record) {
			this.onServiceSelected(record);
		}, this);

		this.pagingTB.on('beforechange', function(grid, td, cellIndex, record) {
			this.onPageChanged();
		}, this);

		this.getBtn.on('click', function() {
			this.updateNbResults();
		}, this);
	},

	/**********************
	*** Utils functions ***
	**********************/

	updateGetBtnStatus: function() {
		var shouldEnabled = this.targetNameCB.rawValue.length > 0 && this.productTypeCB.rawValue.length > 0;
		if(shouldEnabled) {
			this.getBtn.enable();
		} else {
			this.getBtn.disable();
		}
	},

	/*************
	*** Events ***
	*************/

	/**
	Trigerred when the 'Get results' button is clicked.
	*/
	updateNbResults: function() {
		var targetName = this.targetNameCB.rawValue;
		var productTypes = this.productTypeCB.value.join(';');
		var timeMin = Ext.Date.format(this.timeSelector.getStartTime(), 'd/m/Y H:i:s'); // start time
		var timeMax = Ext.Date.format(this.timeSelector.getStopTime(), 'd/m/Y H:i:s'); // stop time

		loadMask.show();
		this.servicesStore.each(function(record) {
			// TODO: use store.load() method instead and add 'success' and 'enable' columns in the store
			Ext.Ajax.request({
				url: 'php/epntap.php',
				method: 'GET',
				headers: {'Content-Type': 'application/json'},
				params: {
					'action': 'getNbResults',
					'serviceId': record.data['id'],
					'url': record.data['access_url'],
					'tableName': record.data['table_name'],
					'targetName': targetName,
					'productTypes': productTypes,
					'timeMin': timeMin,
					'timeMax': timeMax
				},
				// timeout: 3000,
				success: function(response, options) {
					var record = this.servicesStore.getById(options.params['serviceId']);
					var responseObj = Ext.decode(response.responseText);
					this.updateService(record, responseObj['success'] ? responseObj['data'] : -2, responseObj['msg']);
				},
				failure: function(response, options) {
					var record = this.servicesStore.getById(options.params['serviceId']);
					this.updateService(record, -1, response.statusText);
				},
				scope: this
			});
		}, this);
	},

	/**
	Update the nb_result field of the services store (see `EpnTapUI.servicesStore`), according to the field values in `serviceFilterPanel`.
	*/
	updateService: function(record, nbRes, info) {
		record.set('nb_results', nbRes);
		record.set('info', info);
		this.servicesStore.sort();
		loadMask.hide();
	},

	getGranuleParams: function() {
		return {
			'action': 'getGranules',
			'url': this.selectedService.data['access_url'],
			'tableName': this.selectedService.data['table_name'],
			'targetName': this.targetNameCB.rawValue,
			'productTypes': this.productTypeCB.value.join(';'),
			'timeMin': Ext.Date.format(this.timeSelector.getStartTime(), 'd/m/Y H:i:s'),
			'timeMax': Ext.Date.format(this.timeSelector.getStopTime(), 'd/m/Y H:i:s'),
			'nbRes': this.selectedService.get('nb_results')
		}
	},

	onPageChanged: function() {
		this.granulesStore.getProxy().extraParams = this.getGranuleParams();
	},

	/**
	Trigerred when a row is clicked in `servicesGrid` table (see `EpnTapUI.createServicesGrid()`). Among other things,
	send a new query and fill `granulesGrid`.
	*/
	onServiceSelected: function(record) {
		// Ext.Ajax.abortAll();
		this.selectedService = record;
		var nbRes = this.selectedService.get('nb_results');

		if(nbRes > 0 && !isNaN(nbRes)) { // TODO replace !isNaN(nbRes) by this.selectedService.get('success')
			this.granulesStore.load({
				params: this.getGranuleParams(),
				// callback: function (records, operation, success) { console.log(Ext.decode(operation.response.responseText)); },
				start: 0,
				limit: this.granulesStore.pageSize,
				scope: this
			});
		}
	}
});