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

// 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 : 800,
	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 ' + (name[name.length-1] == 's' ? name : name + 's').replace(/_/g, ' ').toLowerCase();
	},

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

		var filter_dic = new Array();
		if(this.dpt_cb.value == 'all') {
			for (var dpt in this.metadata) {
				for (var tc in this.metadata[dpt]) {
					for (tn in this.metadata[dpt][tc]) {
						for (serv in this.metadata[dpt][tc][tn]) {
							filter_dic[serv] = this.metadata[dpt][tc][tn][serv][0] + (serv in filter_dic ? filter_dic[serv] : 0);
						}
					}
				}
			}
		} else if (this.tc_cb.value == 'all') {
			for (var tc in this.metadata[this.dpt_cb.value]) {
				for (tn in this.metadata[this.dpt_cb.value][tc]) {
					for (serv in this.metadata[this.dpt_cb.value][tc][tn]) {
						filter_dic[serv] = this.metadata[this.dpt_cb.value][tc][tn][serv][0] + (serv in filter_dic ? filter_dic[serv] : 0);
					}
				}
			}
		} else if (this.tn_cb.value == 'all') {
			for (tn in this.metadata[this.dpt_cb.value][this.tc_cb.value]) {
				for (serv in this.metadata[this.dpt_cb.value][this.tc_cb.value][tn]) {
					filter_dic[serv] = this.metadata[this.dpt_cb.value][this.tc_cb.value][tn][serv][0] + (serv in filter_dic ? filter_dic[serv] : 0);
				}
			}
		} else {
			for (serv in this.metadata[this.dpt_cb.value][this.tc_cb.value][this.tn_cb.value]) {
				filter_dic[serv] = this.metadata[this.dpt_cb.value][this.tc_cb.value][this.tn_cb.value][serv][0] + (serv in filter_dic ? filter_dic[serv] : 0);
			}
		}

		var filter = Object.keys(filter_dic).map(function(key) {
			return [key, filter_dic[key]];
		});
		filter.sort(function(first, second) {
			return second[1] - first[1];
		});
		for (var s = 0; s < filter.length; s++) {
			this.sp_grid.getStore().add({'id': filter[s][0], 'nb_results': filter[s][1]});
		}
	},

	onWindowLoaded: function(services) {
		this.metadata = JSON.parse(loadTextFileAjaxSync('../../generic_data/EpnTapData/metadata.json', "application/json"));
		this.services = JSON.parse(loadTextFileAjaxSync('../../generic_data/EpnTapData/EpnTapServices.json', "application/json"));
		console.log(Object.keys(this.metadata).length + " dataproduct_type répertoriés.");

		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'});
		this.tc_cb.getStore().add({'id': 'all', 'name': 'All target names'});
		this.tn_cb.getStore().add({'id': 'all', 'name': 'All target classes'});

		for (var dpt_id in this.metadata) {
			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.tc_cb.select('all');
		this.tn_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.metadata[this.dpt_cb.value];

			if (Object.keys(target_classes).length == 1) {
				this.tc_cb.getStore().add({'id': Object.keys(target_classes)[0], 'name': this.prettify(Object.keys(target_classes)[0])});
				this.tc_cb.disable();
				this.tc_cb.select(this.tc_cb.getStore().getAt(0)['internalId']);
				this.onTargetClassCB();
			} 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.select('all');
				this.tc_cb.enable();
			}
		}
		this.tn_cb.getStore().add({'id': 'all', 'name': 'All target names'});
		this.tn_cb.select('all');
		this.update_services();
	},

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

		if (this.tc_cb.value == 'all') {
			this.tn_cb.getStore().add({'id': 'all', 'name': 'All target names'});
			this.tn_cb.select('all');
			this.tn_cb.disable();
		} else {
			var target_names = this.metadata[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.select(this.tn_cb.getStore().getAt(0)['internalId']);
				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.select('all');
				this.tn_cb.enable();
			}
		}
		this.update_services();
	},

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

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

	fillGranules: function(granules) {
		console.log(granules);

		Ext.getCmp('granulesPanel').getStore().removeAll();
		Ext.getCmp('granulesPanel').getStore().add(granules);
	},

	onServiceSelected: function(service) {
		var select = Array();
		for (var i_s = 0 ; i_s < this.services.length ; i_s++) {
			if (this.services[i_s]['schema'] === service['id']) {
				var columns = this.services[i_s]['columns'].split(',');
				for (var i_c=0 ; i_c<this.gp_grid.columns.length ; i_c++) {
					if (columns.indexOf(this.gp_grid.columns[i_c].dataIndex) != -1)
						select.push(this.gp_grid.columns[i_c].dataIndex)
				}
				var filter = Array(
					this.dpt_cb.value !== 'all' ? this.dpt_cb.value : null,
					this.tn_cb.value !== 'all' ? this.dpt_cb.value : null,
					Ext.getCmp('startTimeDF').rawValue !== '' ? this.dpt_cb.value : null,
					Ext.getCmp('stopTimeDF').rawValue !== '' ? this.dpt_cb.value : null);
				break;
			}
		}
		console.log("Getting granules of " + service['id'] + "...");
		AmdaAction.epnTapGetGranules(service['id'], this.services[i_s]['accessurl'], filter, select, this.fillGranules);
	},

	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
		};
	}
});