Fixes issue 27730: Pressing enter in the last editable grid line creates record
authorAugusto Mauch <augusto.mauch@openbravo.com>
Fri, 03 Oct 2014 13:46:05 +0200
changeset 24990 ff95c8c768e7
parent 24989 7d7b36e1e61e
child 24991 6e4c293df93c
Fixes issue 27730: Pressing enter in the last editable grid line creates record

The fix [1] of this issue [2] added a check to prevent creating a new line when, in an editable grid, a value was selected from a picklist by pressing the Enter key. The fix consisted in overwriting the ha
ndleKeyPress function of TextItem. The problem was that because of the way the fix was implemented, the new function was invoked but the original implementation of TextItem.handleKeyPress was never execute
d. This missing code was causing a strange behaviour in how the EventHandler set the items that are object of the events, and as a result of this the current issue was being reproducible.

To fix this, the issue [2] has been implemented differently. The root cause of that issue is that the keyDown event was done in a formitem (the selector) and the keyPress on another item (in this case the
description column). When this was detected, the call the handleKeyPress was intercepted to prevent adding a new line. Now, the interception is done in the cellEditEnd. This has two benefits:
- handleKeyPress is not overwritten, so the problems caused by the missing TextItem.handleKeyPress invocation disappear
- the interception is done in a more specific point. handleKeyPress is called constantly, but the cellEditEnd is only called when the focus leaves a grid cell.

[1] https://code.openbravo.com/erp/devel/pi/rev/b4ba48792e4850c804c42001d8f818f1e0575f81
[2] https://issues.openbravo.com/view.php?id=26817
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js
modules/org.openbravo.userinterface.smartclient/web/org.openbravo.userinterface.smartclient/js/ob-smartclient.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js	Fri Oct 03 13:12:58 2014 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js	Fri Oct 03 13:46:05 2014 +0200
@@ -3164,6 +3164,13 @@
     var newRow = nextEditCell && nextEditCell[0] !== rowNum;
     var enterKey = editCompletionEvent === 'enter';
 
+    // if event was triggered by pressing the enter key, do not continue if the current edit field is not the one focused when the enter key was pressed
+    // this happens for instance when a value is selected from a pick list by pressing enter. if that happens the value is selected, the focus is moved to the 
+    // next form item and the cellEditEnd function can be invoked for the form item that just got the focus
+    if (enterKey && editField.name !== this.getEditForm().lastKeyDownItem.name) {
+      return;
+    }
+
     // no newValue, compute it, this because in the super method there is a check
     // how many arguments are passed on, sometimes the newValue is not passed in
     // and then it must be recomputed, so if we then use the undefined newValue
--- a/modules/org.openbravo.userinterface.smartclient/web/org.openbravo.userinterface.smartclient/js/ob-smartclient.js	Fri Oct 03 13:12:58 2014 +0200
+++ b/modules/org.openbravo.userinterface.smartclient/web/org.openbravo.userinterface.smartclient/js/ob-smartclient.js	Fri Oct 03 13:46:05 2014 +0200
@@ -520,15 +520,12 @@
     return this.Super('useDisabledEventMask', arguments);
   },
 
-  // handles a corner case where the event of the previous field is called for the current field,
-  // refer issue https://issues.openbravo.com/view.php?id=26817. In this case, the event is not triggered.
-  handleKeyPress: function (event, eventInfo) {
-    var key = isc.EH.lastEvent.keyName;
-    if (key === 'Enter' && event.itemInfo && event.itemInfo.item && event.itemInfo.item.name && event.itemInfo.item.name !== this.name) {
-      return true;
-    } else {
-      return this.Super('handleKeyPress', arguments);
-    }
+  // store the item that was focused when the key is pressed
+  // this information is then used in the OBViewGrid.cellEditEnd function. See issue https://issues.openbravo.com/view.php?id=27730
+  _original_handleKeyDown: isc.TextItem.getPrototype().handleKeyDown,
+  handleKeyDown: function (event, eventInfo) {
+    this.form.lastKeyDownItem = this;
+    return this._original_handleKeyDown(event, eventInfo);
   }
 });