=== 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 = ""; + + "\" style=\"width:10em;text-align:center\"/>"; } else { id = dataEntryId; html = ""; + + "\" style=\"width:10em;text-align:center\"/>"; } if (checkExisted(id)) {