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

// for(let service of Ext.getCmp('servicesPanel').getStore().getRange()) {
// 	var filter = Array(service.data.schema, service.data.accessurl, targetName, productType, startTime, stopTime);
// 	AmdaAction.epnTapMgr('getServiceNbResults', filter, function(epnTapServices) {
// 		service.set('nb_responses', epnTapServices===null ? "-" : epnTapServices);
// 		console.log(epnTapServices);
// 	});
// }

// var grid = Ext.getCmp('servicesPanel');
// var selection = grid.getSelectionModel();
// access_url = [];
// table_name = [];
// for(i=0 ; i<grid.store.getCount() ; i++) {
// 	if(selection.isSelected(i)) {
// 		table_name.push(grid.store.getAt(i).data.table_name);
// 		access_url.push(grid.store.getAt(i).data.access_url);
// 	}
//
// }

// Load text with Ajax synchronously: takes path to file and optional MIME type
function loadTextFileAjaxSync(filePath, mimeType) {
	var xmlhttp=new XMLHttpRequest();
	xmlhttp.open("GET", filePath, false);
	if (mimeType != null) {
		if (xmlhttp.overrideMimeType) {
			xmlhttp.overrideMimeType(mimeType);
		}
	}
	xmlhttp.send();
	if (xmlhttp.status == 200) {
		return xmlhttp.responseText;
	} else {
		return null;
	}
}

Ext.define('amdaDesktop.EpnTapModule', {

	extend: 'amdaDesktop.AmdaModule',

	// requires: ['amdaUI.EpnTapUI', 'amdaReader.EpnTapReader'],
	requires: ['amdaUI.EpnTapUI'],
	contentId : 'EpnTapUI',

	/** The alias name of the module view. */
	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',

	width : 600,
	height: 550,

	dpt_dic: { "im": "Image", "ma": "Map", "sp": "Spectrum", "ds": "Dynamic spectrum", "sc": "Spectral cube", "pr": "Profile",
	"vo": "Volume", "mo": "Movie", "cu": "Cube", "ts": "Time series", "ca": "Catalogue", "ci": "Catalogue item"	},

	prettify: function(name) {
		return name.charAt(0).toUpperCase() + name.replace(/_/g, ' ').substr(1).toLowerCase();
	},

	all_prettify: function(name) {
		return 'All ' + this.prettify(name[name.length-1] == 's' ? name : name + 's');
	},

	update_services: function() {
		this.sp_grid.getStore().removeAll();
		this.gp_grid.getStore().removeAll();

		// TODO: mettre à jour la liste des services

		// var target_names = services[this.dpt_cb.value][this.tc_cb.value];
		// for (var res_key in results) {
		// 	sp_store.add({'id': res_key, 'nb_results': results[res_key][0]});
		// }
	},

	onWindowLoaded: function(services) {
		this.services = JSON.parse(loadTextFileAjaxSync('../../generic_data/EpnTapData/services.json', "application/json"));

		this.dpt_cb = Ext.getCmp('productTypeCB');
		this.tc_cb = Ext.getCmp('targetClassCB');
		this.tn_cb = Ext.getCmp('targetNameCB');
		this.sp_grid = Ext.getCmp('servicesPanel');
		this.gp_grid = Ext.getCmp('granulesPanel');

		this.dpt_cb.getStore().removeAll();
		this.tc_cb.getStore().removeAll();
		this.tn_cb.getStore().removeAll();
		this.tc_cb.disable();
		this.tn_cb.disable();

		this.dpt_cb.getStore().add({'id': 'all', 'name': 'All data product types'});

		for (var dpt_id in this.services) {
			if (dpt_id in this.dpt_dic) {
				this.dpt_cb.getStore().add({'id': dpt_id, 'name': this.prettify(this.dpt_dic[dpt_id])});
			} else {
				console.log("Unknown data product type '" + dpt_id + "'");
			}
		}
		this.dpt_cb.select('all');
		this.update_services();
	},

	onProductTypeCB: function() {
		this.tc_cb.getStore().removeAll();
		this.tn_cb.getStore().removeAll();
		this.tn_cb.disable();

		if (this.dpt_cb.value == 'all') {
			this.tc_cb.disable();
		} else {
			var target_classes = this.services[this.dpt_cb.value];

			if (Object.keys(target_classes).length == 1) {
				this.tc_cb.getStore().add({'id': target_classes[0], 'name': this.prettify(target_classes[0])});
				this.tc_cb.disable();
			} else {
				this.tc_cb.getStore().add({'id': 'all', 'name': this.all_prettify(this.dpt_dic[this.dpt_cb.value])});
				for (var tc_id in target_classes) {
					this.tc_cb.getStore().add({'id': tc_id, 'name': this.prettify(tc_id)});
				}
				this.tc_cb.enable();
			}
			this.tc_cb.select(this.tc_cb.getStore().getAt(0)['internalId']);
		}
		this.update_services();
	},

	onTargetClassCB: function() {
		this.tn_cb.getStore().removeAll();

		if (this.tc_cb.value == 'all') {
			this.tn_cb.disable();
		} else {
			var target_names = this.services[this.dpt_cb.value][this.tc_cb.value];

			if (Object.keys(target_names).length == 1) {
				this.tn_cb.getStore().add({'id': Object.keys(target_names)[0], 'name': this.prettify(Object.keys(target_names)[0])});
				this.tn_cb.disable();
			} else {
				this.tn_cb.getStore().add({'id': 'all', 'name': this.all_prettify(this.tc_cb.value)});
				for (var tn_id in target_names) {
					this.tn_cb.getStore().add({'id': tn_id, 'name': this.prettify(tn_id)});
				}
				this.tn_cb.enable();
			}
			this.tn_cb.select(this.tn_cb.getStore().getAt(0)['internalId']);
		}
		this.update_services();
	},

	onTargetNameCB: function() {
		this.update_services();
	},

	onSearchBtnClicked: function() {
		console.log("onSearchBtnClicked");
	},

	onServiceSelected: function(service) {
		var filter = Array(service['schema'], service['accessurl'], Ext.getCmp('targetNameCB').value,
				this.dpt_cb.value, Ext.getCmp('startTimeDF').rawValue, Ext.getCmp('stopTimeDF').rawValue);

		AmdaAction.epnTapMgr('getGranules', filter, function(granules) {
			console.log(granules);
		});
	},

	onGranuleSelected: function(granule) {
		console.log('selected granule: ' + granule.target_name);
	},

	/** @class Module initialisation. */
	init: function() {
		this.launcher = {
			text: this.title,
			iconCls: this.icon,
			handler: this.createWindow,
			scope: this
		};

		// this.onWindowLoaded = onWindowLoaded;
		// this.onSearchBtnClicked = onSearchBtnClicked;
		// this.onProductTypeCB = onProductTypeCB;
		// this.onGranuleSelected = onGranuleSelected;
		// this.onServiceSelected = onServiceSelected;
		// this.onTargetClassCB = onTargetClassCB;
		// this.onTargetNameCB = onTargetNameCB;
	}

});