← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2831: Data entry section form: Move to previous/next field by pressing arrow up/down and enter keys.

 

Merge authors:
  Jan Henrik Øverland (janhenrik-overland)
------------------------------------------------------------
revno: 2831 [merge]
committer: Jan Henrik Overland <janhenrik.overland@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2011-02-10 12:58:55 +0100
message:
  Data entry section form: Move to previous/next field by pressing arrow up/down and enter keys.
modified:
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/defaultForm.vm
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.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/defaultForm.vm'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/defaultForm.vm	2011-01-25 22:15:46 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/defaultForm.vm	2011-02-10 11:57:10 +0000
@@ -64,7 +64,7 @@
       <option value="false" #if( $dataValue.value == "false" ) selected="selected" #end>$i18n.getString( "no" )</option>
     </select>
     #else
-    <input name="entryfield" id="$dataEntryId" type="text" value="$!encoder.htmlEncode( $dataValue.value )" #if( $auth.hasAccess( "dhis-web-dataentry", "saveValue") ) onchange="saveVal(${dataElement.id},${optionCombo.id})" ondblclick="viewHist(${dataElement.id},${optionCombo.id})" #else disabled="disabled"#end onkeypress="return keyPress(event,this)" class="entryField #if( $minMaxError ) minmax#end" tabindex="$tabIndex" #if( $locked )disabled="disabled"#end>
+    a<input name="entryfield" id="$dataEntryId" type="text" value="$!encoder.htmlEncode( $dataValue.value )" #if( $auth.hasAccess( "dhis-web-dataentry", "saveValue") ) onchange="saveVal(${dataElement.id},${optionCombo.id})" ondblclick="viewHist(${dataElement.id},${optionCombo.id})" #else disabled="disabled"#end onkeypress="return keyPress(event,this)" class="entryField #if( $minMaxError ) minmax#end" tabindex="$tabIndex" #if( $locked )disabled="disabled"#end>
     #end
     </td>
     #set( $tabIndex = $tabIndex + 1 )

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js	2011-02-06 07:08:06 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js	2011-02-10 11:57:10 +0000
@@ -314,78 +314,30 @@
 
 function keyPress( event, field )
 {
-    var key = 0;
-    if ( event.charCode )
-    {
-    	key = event.charCode; /* Safari2 (Mac) (and probably Konqueror on Linux, untested) */
-    }
-    else
-    {
-		if ( event.keyCode )
-		{
-			key = event.keyCode; /* Firefox1.5 (Mac/Win), Opera9 (Mac/Win), IE6, IE7Beta2, Netscape7.2 (Mac) */
-		}
-		else
-		{
-			if ( event.which )
-			{
-				key = event.which; /* Older Netscape? (No browsers triggered yet) */
-			}
-	    }
-	}
-    
-    if ( key == 13 ) /* CR */
-    {
-		nextField = getNextEntryField( field );
-        if ( nextField )
-        {
-            nextField.focus(); /* Does not seem to actually work in Safari, unless you also have an Alert in between */
-        }
-        return true;
-    }
-    
-    /* Illegal characters can be removed with a new if-block and return false */
+    var key = event.keyCode || event.charCode || event.which;
+    
+    var focusField = ( key == 13 || key == 40 ) ? getNextEntryField( field ) : ( key == 38 ) ? getPreviousEntryField( field ) : false;
+    
+    if ( focusField )
+    {
+        focusField.focus();
+    }
+    
     return true;
 }
 
 function getNextEntryField( field )
 {
-    var inputs = document.getElementsByName( "entryfield" );
-    
-    // Simple bubble sort
-    for ( i = 0; i < inputs.length - 1; ++i )
-    {
-        for ( j = i + 1; j < inputs.length; ++j )
-        {
-            if ( inputs[i].tabIndex > inputs[j].tabIndex )
-            {
-                tmp = inputs[i];
-                inputs[i] = inputs[j];
-                inputs[j] = tmp;
-            }
-        }
-    }
-    
-    i = 0;
-    for ( ; i < inputs.length; ++i )
-    {
-        if ( inputs[i] == field )
-        {
-            break;
-        }
-    }
-    
-    if ( i == inputs.length - 1 )
-    {
-    	// No more fields after this:
-    	return false;
-    	// First field:
-        //return inputs[0];
-    }
-    else
-    {
-        return inputs[i + 1];
-    }
+    var fields = $('input[name="entryfield"]');
+    
+    return fields[field.tabIndex];
+}
+
+function getPreviousEntryField( field )
+{
+    var fields = $('input[name="entryfield"]');
+    
+    return fields[field.tabIndex - 2];
 }
 
 // -----------------------------------------------------------------------------