PlotElementPanel.js 4.4 KB
/**
 * Project   : AMDA-NG
 * Name      : PlotElementPanel.js
 * @class   amdaPlotComp.PlotElementPanel
 * @extends Ext.form.Panel
 * @brief   Element of a plot request definition
 * @author  Benjamin Renard
 * @version $Id: PlotElementPanel.js benjamin $
 */

Ext.define('amdaPlotComp.PlotElementPanel', {
	extend: 'Ext.form.Panel',

	
	elementFormsManager : new Ext.AbstractManager(),
	
	crtTree   : null,
	
	constructor: function(config) {
		this.init(config);	    
		this.callParent(arguments);
	},
	
	destroy: function() {
		this.elementFormsManager.each(function (key, value, length) {
			this.elementFormsManager.unregister(value);
			value.destroy();
    		}, this);
		this.callParent();
	},
	
	setElement: function(type, object, tree) {
		this.removeAll(false);
		
		this.crtTree   = tree;
		
		var me =this;
		this.getElementForm(type, function (elementForm) {
			me.add(elementForm);
			elementForm.crtTree = tree;
			if (elementForm.setObject)
				elementForm.setObject(object);
		});
	},
	
	resetElement: function() {
		this.setElement('',null,null);
	},
	
	getElementForm: function(type, onFormReady) {
		var formId = type;
		if (type == '')
			formId = 'none';

		formId += '-element-form';
		
		if (this.rendered)
			this.getEl().mask();
			
		var me = this;
		if (!this.elementFormsManager.get(formId))
		{
			//Create element form
			var element_name = '';
			var element_text = '';
			switch (type)
			{
			case 'page' :
				element_name = 'amdaPlotComp.PlotPageForm';
				break;
			case 'panel' :
				element_name = 'amdaPlotComp.PlotPanelForm';
				break;
			case 'time-axis' :
				element_name = 'amdaPlotComp.PlotTimeAxisForm';
				break;
			case 'epoch-axis' :
				element_name = 'amdaPlotComp.PlotEpochAxisForm';
				break;
			case 'color-axis' :
				element_name = 'amdaPlotComp.PlotColorAxisForm';
				break;
			case 'x-axis' :
			case 'y-left-axis' :
			case 'y-right-axis' :
				element_name = 'amdaPlotComp.PlotBaseAxisForm';
				break;
			case 'param' :
				element_name = 'amdaPlotComp.PlotParamForm';
				break;
			case 'layout' :
				element_name = 'amdaPlotComp.PlotLayoutForm';
				break;
			case 'series-legend' :
				element_name = 'amdaPlotComp.PlotLegendSeriesForm';
				break;
			case 'text-legend' :
				element_name = 'amdaPlotComp.PlotLegendTextForm';
				break;
			case 'text-legends' :
				element_name = 'amdaPlotComp.PlotLegendsTextForm';
				break;
			case 'drawing-objects' :
				element_name = 'amdaPlotComp.PlotDrawingObjectForm';
				break;
			case 'constant' :
				element_name = 'amdaPlotComp.PlotConstantForm';
				break;
			case 'text-obj' :
				element_name = 'amdaPlotComp.PlotTextForm';
				break;
			case 'curve' :
				element_name = 'amdaPlotComp.PlotCurveForm';
				break;
			case 'fills' :
				element_name = 'amdaPlotComp.PlotFillsForm';
				break;
			case 'fill' :
				element_name = 'amdaPlotComp.PlotFillForm';
				break;
			case '' :
				element_name = 'Ext.form.Label';
				element_text = 'Select an element in the tree to show options';
				break;
			default :
				element_name = 'Ext.form.Label';
				element_text = 'No available options for this element';
			}
			
			var create_func = function() {
				var formArgs = {
						id: formId
				};
				
				if (element_text != '')
					formArgs.text = element_text;
				
				var new_element_form = Ext.create(element_name, formArgs);
				me.elementFormsManager.register(new_element_form);
				
				if (new_element_form.updateElement) {
					new_element_form.updateElement(function () {
						if (onFormReady != null)
							onFormReady(me.elementFormsManager.get(formId));
						if (me.rendered)
							me.getEl().unmask();
					});
				}
				else {
					if (onFormReady != null)
						onFormReady(me.elementFormsManager.get(formId));
					if (me.rendered)
						me.getEl().unmask();
				}
			};
			
			if (!Ext.ClassManager.get(element_name)) {
				var me = this;
				//Load class source code
				Ext.require(element_name, function() {
					//Create element
					create_func();
				});
			}
			else
				//Create element
				create_func();
				
			return;
		}
		
		if (this.rendered)
			this.getEl().unmask();
		
		if (onFormReady != null)
			onFormReady(this.elementFormsManager.get(formId));
	},
	
	init : function(config) {
		var me = this;
		
		var myConf = {
				title : 'Selected element options',
	        	bodyStyle: { background : '#dfe8f6' },
	        	autoScroll: true,
				defaults: {
					border: false
				}
		};
		
		Ext.apply (this , Ext.apply (arguments, myConf));
	}
});