# HG changeset patch
# User Carlos Aristu <carlos.aristu@openbravo.com>
# Date 1506422509 -7200
#      Tue Sep 26 12:41:49 2017 +0200
# Node ID b8938a580022b5bd7e2e8b8353c2786d59624417
# Parent  978c90782e02b688decf897dfd142319f04a1136
fixes bug 36927: Multiple DS requests having a date item as first focused item

  When directly opening a record in a tab which has a date item as the first focused item, extra DS calls were fired which could prevent the record to be opened properly (the form displayed in blank)

  This has happening because a combination of different facts:

   - When the grid is redrawn, Smarclient fires an update of the first focused item. In the case of date items this causes that false item edition is fired because it detects that the value of the date item filter changes from undefined to empty string.

   - If the previous scenario happens while opening a record directly, the grid datasource detects that it needs to fetch data because at this point the number of cached rows is different from the total rows (see [1]).

  To avoid those extra DS requests we now ignore those false item editions while a target record is being opened.

[1] https://code.openbravo.com/erp/mods/org.openbravo.userinterface.smartclient.dev/file/66d2b640b66e/web/org.openbravo.userinterface.smartclient/isomorphic/client/application/ResultSet.js#l833

diff --git a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-minidaterange.js b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-minidaterange.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-minidaterange.js
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-minidaterange.js
@@ -667,6 +667,18 @@
     return this.Super('setElementValue', arguments);
   },
 
+  compareValues: function (value1, value2) {
+    if (this.targetRecordBeingOpened() && value1 === '' && value2 === undefined) {
+      // prevent extra DS requests when opening a record directly by ignoring false updates in the item value
+      return true;
+    }
+    return (0 === isc.Date.compareLogicalDates(value1, value2));
+  },
+
+  targetRecordBeingOpened: function () {
+    return this.grid && this.grid.parentElement && this.grid.parentElement.targetRecordId;
+  },
+
   mapDisplayToValue: function (display) {
     return display;
   },
