Show:

File: ia\factories\AreaProfileFactory.js

/** 
 * Factory for creating area profiles.
 *
 * @author J Clare
 * @class ia.AreaProfileFactory
 * @param {ia.ComponentConfig} config The component config.
 * @param {ia.Report} report The report object.
 * @param {Object} componentGroup  Hash table containing the Data and  Interaction Groups that the component belongs to:
 * {dataGroup:ia.DataGroup, interactionGroup:ia.InteractionGroup, comparisonInteractionGroup:ia.InteractionGroup}.
 */
ia.AreaProfileFactory = function(config, report, componentGroup)
{
	var me = this;

	// Data and Interaction groups that the components belongs to.
	var interactionGroup = componentGroup.interactionGroup;
	var dataGroup = componentGroup.dataGroup;
	var comparisonInteractionGroup = componentGroup.comparisonInteractionGroup;

	// Event handlers.

	// This code executes every time a geography has changed.
	var geogChanged = true;
	dataGroup.addEventListener(ia.DataEvent.GEOG_CHANGED, function(event)
	{
		geogChanged = true;
		profile.geography = dataGroup.geography;
	});

	// This code executes every time the data groups data has changed.
	dataGroup.addEventListener(ia.DataEvent.DATA_CHANGED, function(event)
	{
		me.update();
	});

	// This code executes every time the comparison selection has changed.
	comparisonInteractionGroup.addEventListener(ia.InteractionEvent.SELECTION_CHANGED, function(event) 
	{
		me.update();
	});

	// Components.

	// Panel.
	var panel = report.getWidget(config.id); 

	// Profile.
	var profile;

	/** 
	 * Builds the component.
	 *
	 * @method build
 	 * @param {Function} callbackFunction Called on completion of function, with the component id as the parameter.
	 */
	this.build = function(callbackFunction)
	{
		// Empty panel.
		panel.content.empty();

		// Legend.
		var legendConfig = report.config.getComponent("profileLegend"+dataGroup.suffix);
		var legend;
		if (legendConfig)
		{
			legend = new ia.ProfileLegend(legendConfig.id);
			if (legendConfig.getProperty("layout")) legend.layout = legendConfig.getProperty("layout");

			var legendPanel = report.getWidget(legendConfig.id);
			legendPanel.append(legend.container);
			report.addComponent(legendConfig.id, legend);
		}

		// Profile
		var useMouseClick = config.getProperty("useMouseClick");
		if (useMouseClick === undefined) useMouseClick = true;

		profile = new ia.Profile(config.id, useMouseClick, function(id)
		{
			if (report.config.template !== ia.ELECTION_REPORT) 
			{
				// id of the form 'indicatorid~date'.
				var dataIds = id.split("~");
				dataGroup.setData(dataGroup.geography.id, dataIds[0], dataIds[1]);
			}
		});
		profile.geography = dataGroup.geography;

		var featureLegend = report.getComponent("featureLegend");
		if (featureLegend) 
		{
			comparisonInteractionGroup.addComponent(featureLegend);
			profile.colorPalette = featureLegend.colorPalette;
		}
		
		var legendClasses = []; // For profile key.

		// Symbols.
		var symbols = [];
		for (var i = 1; i <= 12; i++) 
		{
			if (config.getProperty("symbol_value_"+i) !== undefined)
			{
				var symbol = new Object();
				symbol.shape = config.getProperty("symbol_shape_"+i);
				symbol.color = config.getProperty("symbol_color_"+i);
				symbol.size = config.getProperty("symbol_size_"+i);
				symbol.label = config.getProperty("symbol_label_"+i);
				symbol.value = config.getProperty("symbol_value_"+i);
				symbols[symbols.length] = symbol;

				// For profile key.
				var legendClass = new ia.CategoricClass(symbol.label);
				legendClass.color = symbol.color;
				legendClass.symbol = symbol.shape;
				legendClass.size = symbol.size;
				legendClass.value = symbol.label;
				legendClasses[legendClasses.length] = legendClass;
			}
		}
		profile.symbols = symbols;
		
		// Targets.
		var targets = [];
		for (var i = 1; i <= 4; i++) 
		{
			if (config.getProperty("target_data_"+i) !== undefined)
			{
				var target = new Object();
				target.shape = config.getProperty("target_shape_"+i);
				target.color = config.getProperty("target_color_"+i);
				target.size = config.getProperty("target_size_"+i);
				target.label = config.getProperty("target_label_"+i);
				target.data = config.getProperty("target_data_"+i);
				targets[targets.length] = target;

				// For profile key.
				var legendClass = new ia.CategoricClass(target.label);
				legendClass.color = target.color;
				legendClass.symbol = target.shape;
				legendClass.size = target.size;
				legendClass.value = target.label;
				legendClasses[legendClasses.length] = legendClass;
			}
		}
		profile.targets = targets;

		// Bar.
		var bar = new Object();
		bar.data = config.getProperty("barData");
		bar.color = config.getProperty("barColor"); 
		bar.symbolValue = config.getProperty("symbolValue"); 
		bar.height = config.getProperty("barHeight"); 
		bar.minValue = config.getProperty("minValue"); 
		bar.midValue = config.getProperty("midValue"); 
		bar.maxValue = config.getProperty("maxValue"); 
		bar.breaksData = config.getProperty("breakData");
		bar.breaksFlip = config.getProperty("breakFlipData"); 
		profile.bar = bar;

		profile.displayMode = config.getProperty("displayMode");  
		profile.displayDatesInProfile = config.getProperty("displayDatesInProfile");  

		profile.useMouseClick = config.getProperty("useMouseClick"); 
		
		// Breaks.
		var breaks = [];
		for (var i = 1; i <= 5; i++) 
		{
			if (config.getProperty("break_label_"+i) !== undefined)
			{
				var breakObj = new Object();
				breakObj.color = config.getProperty("break_color_"+i);
				breakObj.label = config.getProperty("break_label_"+i);
				breaks[breaks.length] = breakObj;
				
				// For profile key.
				var legendClass = new ia.CategoricClass(breakObj.label);
				legendClass.color = breakObj.color;
				legendClass.symbol = ia.Shape.SQUARE;
				legendClass.size = 10;
				legendClass.value = breakObj.label;
				legendClasses[legendClasses.length] = legendClass;
			}
		}
		profile.breaks = breaks;

		if (legend)
		{
			legend.legendClasses = legendClasses;
			legend.render();
		}
		
		panel.append(profile.container);
		interactionGroup.addComponent(profile);
		comparisonInteractionGroup.addComponent(profile);
		report.addComponent(config.id, profile);
				
		if (callbackFunction !== undefined) callbackFunction.call(null, config.id);
	};

	/** 
	 * Updates the component.
	 *
	 * @method update
 	 * @param {Function} callbackFunction Called on completion of function, with the component id as the parameter.
	 */
	this.update = function(callbackFunction)
	{
		var indicator = dataGroup.indicator;
		profile.columns = config.getColumnsForIndicator(indicator, report.textSubstitution);
		profile.indicator = indicator;
		profile.render();

		if (geogChanged)
		{
			var expandedThemeIds = config.getProperty("expandedThemeIds");
			if (expandedThemeIds !== undefined) profile.expandThemes(expandedThemeIds);
			geogChanged = false;
		}

		if (callbackFunction !== undefined) callbackFunction.call(null, config.id);
	};

	/** 
	 * Renders the component.
	 *
	 * @method render
 	 * @param {Function} callbackFunction Called on completion of function, with the component id as the parameter.
	 */
	this.render = function(callbackFunction)
	{
		if (callbackFunction !== undefined) callbackFunction.call(null, config.id);
	};
};