← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 3543: Work in progress on calculated indicators in custom data entry forms

 

------------------------------------------------------------
revno: 3543
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2011-05-09 00:03:05 +0200
message:
  Work in progress on calculated indicators in custom data entry forms
modified:
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.js
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataset/src/main/webapp/dhis-web-maintenance-dataset/javascript/viewDataEntryForm.js


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.js'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.js	2011-05-08 19:22:16 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.js	2011-05-08 22:03:05 +0000
@@ -7,6 +7,75 @@
 var COLOR_YELLOW = '#fffe8c';
 var COLOR_RED = '#ff8a8a';
 
+var indicatorFormulas = new Array();
+
+var FORMULA_PATTERN = /\[.+?\]/g;
+var SEPARATOR = '.';
+
+/**
+ * Updates all indicator input fields with the calculated value based on the
+ * values in the input entry fields in the form.
+ */
+function updateIndicators()
+{
+	var entryFieldValues = getEntryFieldValues();
+
+	$( 'input[name="indicator"]' ).each( function( index ) {
+		var indicatorId = $( this ).attr( 'indicatorId' );
+		var formula = indicatorFormulas[ indicatorId ];
+		
+		var expression = generateExpression( formula );		
+		var value = eval( expression );
+		
+		if ( value )
+		{
+			$( this ).attr( 'value', value );
+		}
+	} );
+}
+
+/**
+ * Returns an associative array with an entry for each entry input field in the
+ * form where the key is the input field id and the value is the input field value.
+ */
+function getEntryFieldValues()
+{
+	var entryFieldValues = new Array();
+	
+	$( 'input[name="entryfield"]' ).each( function( index ) {
+		entryFieldValues[ $( this ).attr( 'id' ) ] = $( this ).attr( 'value' );
+	} );
+	
+	return entryFieldValues;
+}
+
+/**
+ * Parses the expression and substitues the operand identifiers with the value of
+ * the corresponding input entry field.
+ */
+function generateExpression( expression )
+{
+	var matcher = expression.match( FORMULA_PATTERN );
+	
+	for ( k in matcher )
+	{
+		var match = matcher[k];
+		var operand = match.replace( /[\[\]]/g, '' ); // Remove brackets from expression to simplify extraction of identifiers
+		
+		var dataElementId = operand.substring( 0, operand.indexOf( SEPARATOR ) );
+		var categoryOptionComboId = operand.substring( operand.indexOf( SEPARATOR ) + 1, operand.length );
+		
+		var entryFieldId = 'value[' + dataElementId + '].value:value[' + categoryOptionComboId + '].value';		
+		var entryField = document.getElementById( entryFieldId );
+		
+		var value = entryField && entryField.value ? entryField.value : '0';
+		
+		expression = expression.replace( match, value );
+	}
+	
+	return expression;
+}
+
 /**
 /* Used by default and section forms.
 */

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataset/src/main/webapp/dhis-web-maintenance-dataset/javascript/viewDataEntryForm.js'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataset/src/main/webapp/dhis-web-maintenance-dataset/javascript/viewDataEntryForm.js	2011-05-08 18:59:01 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataset/src/main/webapp/dhis-web-maintenance-dataset/javascript/viewDataEntryForm.js	2011-05-08 22:03:05 +0000
@@ -66,13 +66,13 @@
 		id = boolDataEntryId;
 		html = "<input title=\"" + titleValue
 				+ "\" value=\"" + displayName + "\" id=\"" + boolDataEntryId
-				+ "\" style=\"width:4em;text-align:center\"/>";
+				+ "\" style=\"width:10em;text-align:center\"/>";
 	} 
 	else {
 		id = dataEntryId;
 		html = "<input title=\"" + titleValue
 				+ "\" value=\"" + displayName + "\" id=\"" + dataEntryId
-				+ "\" style=\"width:4em;text-align:center\"/>";
+				+ "\" style=\"width:10em;text-align:center\"/>";
 	}
 
 	if (checkExisted(id)) {