Show:

File: ia\factories\DataTableFactory.js

/** 
 * Factory for creating data tables.
 *
 * @author J Clare
 * @class ia.DataTableFactory
 * @param {ia.ComponentConfig} config The component config.
 * @param {ia.Report} report The report object.
 * @param {ia.DataGroup[]} dataGroups The associated data groups.
 * @param {ia.InteractionGroup} interactionGroup The associated interaction group.
 * @param {ia.InteractionGroup} comparisonInteractionGroup The associated comparison interaction group.
 */
ia.DataTableFactory = function(config, report, dataGroups, interactionGroup, comparisonInteractionGroup)
{
	var me = this;

	// Event handlers.

	for (var i = 0; i < dataGroups.length; i++)
	{
		var dataGroup = dataGroups[i];

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

		// This code executes every time the data groups thematic has changed.
		// The thematic will change after any data change so only render
		// here to avoid multiple rendering.
		dataGroup.addEventListener(ia.Event.THEMATIC_CHANGED, function(event)
		{
			triggerRender();
		});
	}

	// Trigger a table update. Use this to prevent multiple table updates
	// when multiple data groups are updated at the same time.
	var updateTimeout = null;
	function triggerUpdate()
	{
		if (!updateTimeout) 
		{
			updateTimeout = setTimeout(function()
			{
				me.update();
			}, 250);
		}
	};

	// Trigger a table render. Use this to prevent multiple table renders
	// when multiple data groups are updated at the same time.
	var renderTimeout = null;
	function triggerRender()
	{
		if (!renderTimeout) 
		{
			renderTimeout = setTimeout(function()
			{
				me.render();
			}, 250);
		}
	};

	// Components.

	// Panel.
	var panel = report.getWidget(config.id); 
	panel.exportFunction = function() {table.exportData(report.config.getProperty("saveTableText"));}; 

	// Table.
	var table;

	/** 
	 * 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();

		// Table.
		table = new ia.Table(config.id);
		table.highlightColor = report.highlightColor;
		table.selectionColor = report.selectionColor;
		if (report.url.params["stickToTop"] !== undefined) 
		{
			var arr = report.url.params["stickToTop"].toString().split(",");
			table.stickToTop(arr);
		}
		panel.append(table.container);
		interactionGroup.addComponent(table);
		report.addComponent(config.id, table);

		// Center feature on map after selection
		var zoomOnSelection = config.getProperty("zoomOnSelection");
		if (zoomOnSelection)
		{
			table.addEventListener(ia.ItemEvent.ITEM_CLICK, function (event)
			{
				/*var item = event.item;
				if (item.state === ia.ItemLayer.SELECTED
				|| item.state === ia.ItemLayer.ROLLOVER_SELECTED) 
				{
					var activeMap = report.getComponent("activeMap"+dataGroup.suffix);
					if (activeMap) activeMap.zoomToFeatureWithId(item.id);
				}*/

				// Zoom to all selected features - zoom full if selection is cleared.
				var activeMap = report.getComponent("activeMap"+dataGroup.suffix);
				if (activeMap)
				{
					var selectedIds = interactionGroup.getSelection();
					if (selectedIds.length > 0)
						activeMap.zoomToFeatures(selectedIds, [dataGroups[0].mapData.baseLayer]); 	// Zoom to selected features.
					else 
						activeMap.controller.zoomFull(); 											// Zoom full when selection is cleared.
				}
			});
		}

		// Table Tools - dont add them if theres none defined.
		if (config.getProperty("clearButtonText") !== undefined 
			|| config.getProperty("filterButtonText") !== undefined)
		{
			var tableTools = new ia.TableTools(dataGroups[0], interactionGroup);
			tableTools.clearButtonText = config.getProperty("clearButtonText");
			tableTools.filterButtonText = config.getProperty("filterButtonText");
			tableTools.filterFunction = function()
			{
				if (interactionGroup.getSelection().length > 0)
					dataGroups[0].setFilteredFeatures(interactionGroup.getSelection());
				else if (dataGroups[0].getFilteredFeatures().length > 0)
					dataGroups[0].clearFilter();
			}
			tableTools.clearFunction = function()
			{
				interactionGroup.clearSelection();
				
				var activeMap = report.getComponent("activeMap"+dataGroup.suffix);
				if (activeMap) activeMap.controller.zoomFull(); 	// Zoom full when selection is cleared.
			}
			panel.appendToFooter(tableTools.container);
			tableTools.render();
		}

		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)
	{
		updateTimeout = null;

		table.showLegendColor = config.getProperty("showLegendColor");
		table.allowUserSorting = config.getProperty("allowUserSorting");

		// Check for custom columns.
		config.customColumns = dataGroups[0].indicator.customColumns;

		// Get the column info from the indicators.
		var indicators = [];
		for (var i = 0; i < dataGroups.length; i++) 
		{
			var dataGroup = dataGroups[i];
			indicators[i] = dataGroup.indicator;
		}
		table.columns = config.getColumnsForIndicators(indicators, report.textSubstitution);

		// Update the data.
		table.setData(dataGroups[0].indicatorData);
		for (var i = 1; i < dataGroups.length; i++)  // i = 1,#; Dont need to set first data group.
		{
			var dataGroup = dataGroups[i];
			var index = i + 1;
			table["data"+index] = dataGroup.indicatorData;
		}

		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)
	{
		renderTimeout = null;
		table.render();
		
		if (callbackFunction !== undefined) 
		{
			if (config.getProperty("sortColumnName") !== "") // Only sort on initialization because table handles sorting after that.
				table.sort(config.getProperty("sortColumnName"), config.getProperty("sortDirection"));
			callbackFunction.call(null, config.id);
		}
	};
};