fixes issue 33588: extra DS calls having summary functions and lazy filtering
authorCarlos Aristu <carlos.aristu@openbravo.com>
Mon, 08 Aug 2016 12:32:51 +0200
changeset 30074 768f34426413
parent 30068 2f8cbe2f7611
child 30075 655bd2d28fba
fixes issue 33588: extra DS calls having summary functions and lazy filtering

When working with the grid having summary functions, several flows were performing datasource requests for recalculating the summaries:
- Open a tab having a grid that already contains a summary function.
- After saving a new record
- After updating an existing record
- After deleting a record
- After switching into form view and saving the record
- After selecting/replacing a summary function

Having the summary row displayed during those actions, Smartclient was recalculating the grid automatically. Now, if the lazy filtering is enabled, the summary row is hidden to prevent the recalculation. Thus, the summary function will be recalculated once the filter/sorting action is requested by the user.
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-grid.js
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/main/ob-standard-view.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-grid.js	Sun Aug 07 16:24:38 2016 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-grid.js	Mon Aug 08 12:32:51 2016 +0200
@@ -708,10 +708,14 @@
       this.showSortArrow = isc.ListGrid.BOTH;
       this.sorterDefaults = {
         click: function () {
-          var grid = this.parentElement;
+          var grid = this.parentElement,
+              alreadySorted;
           if (!this._iconEnabled) {
             return;
           }
+          if (grid.summaryFunctionsHaveChanged && !grid.showGridSummary) {
+            grid.setShowGridSummary(true);
+          }
           if (grid.filterHasChanged || grid.filterClauseJustRemoved) {
             // the filter clause can only be removed once
             delete grid.filterClauseJustRemoved;
@@ -721,15 +725,31 @@
             delete grid.filterHasChanged;
             delete grid.sortingHasChanged;
             delete grid._filteringAndSortingManually;
+            delete grid.summaryFunctionsHaveChanged;
           } else if (!isc.isA.ResultSet(grid.data) || grid.serverDataNotLoaded) {
             // The initial data has not been loaded yet, refreshGrid
             // refreshGrid applies also the current sorting
             grid.refreshGrid();
             delete grid.sortingHasChanged;
             delete grid.serverDataNotLoaded;
+            delete grid.summaryFunctionsHaveChanged;
           } else if (grid.sortingHasChanged) {
-            grid.setSort(grid.savedSortSpecifiers, true);
+            if (grid.summaryFunctionsHaveChanged) {
+              alreadySorted = grid.isSortApplied();
+              grid.setSort(grid.savedSortSpecifiers, true);
+              if (!grid.savedSortSpecifiers) {
+                grid.recalculateGridSummary();
+              } else if (alreadySorted) {
+                grid.data.resort();
+              }
+            } else {
+              grid.setSort(grid.savedSortSpecifiers, true);
+            }
             delete grid.sortingHasChanged;
+            delete grid.summaryFunctionsHaveChanged;
+          } else if (grid.summaryFunctionsHaveChanged) {
+            grid.recalculateGridSummary();
+            delete grid.summaryFunctionsHaveChanged;
           }
           if (grid && grid.sorter) {
             grid.sorter.disable();
@@ -755,6 +775,29 @@
     this.Super('initWidget', arguments);
   },
 
+  isSortApplied: function () {
+    var i, gridSortSpecifiers, gridDataSortSpecifiers;
+
+    if (!this.data || !this.data.getSort) {
+      return false;
+    }
+
+    gridSortSpecifiers = this.getSort() || [];
+    gridDataSortSpecifiers = this.data.getSort() || [];
+
+    if (gridSortSpecifiers.length !== gridDataSortSpecifiers.length) {
+      return false;
+    }
+
+    for (i = 0; i < gridSortSpecifiers.length; i++) {
+      if (gridSortSpecifiers[i].property !== gridDataSortSpecifiers[i].property || gridSortSpecifiers[i].direction !== gridDataSortSpecifiers[i].direction) {
+        return false;
+      }
+    }
+
+    return true;
+  },
+
   clearFilter: function (keepFilterClause, noPerformAction) {
     var i = 0,
         fld, length, groupState, forceRefresh;
@@ -846,6 +889,57 @@
     return ret;
   },
 
+  // overwritten to prevent undesired requests having summary functions and lazy filtering enabled
+  // creates (or updates) and returns the summaryRow autoChild
+  // not called directly -- call 'setShowGridSummary' instead
+  getSummaryRow: function () {
+    if (this.lazyFiltering && this.summaryRow) {
+      if (this.getSummaryRowDataSource && this.completeFields) {
+        this.summaryRow.setDataSource(this.getSummaryRowDataSource(), this.completeFields.duplicate());
+      }
+      if (this.summaryFunctionsHaveChanged) {
+        return this.summaryRow;
+      }
+      this.summaryFunctionsHaveChanged = true;
+      if (this.sorter) {
+        this.sorter.enable();
+      }
+      return this.summaryRow;
+    }
+    return this.Super('getSummaryRow');
+  },
+
+  // puts the grid in a state pending of recalculate summaries
+  markForCalculateSummaries: function () {
+    if (!this.lazyFiltering) {
+      return;
+    }
+    if (this.showGridSummary) {
+      this.setShowGridSummary(false);
+      if (!this.hasSummaryFunctions()) {
+        // the grid does not have any summary function
+        // not mark it as pending for recalculation
+        return;
+      }
+    }
+    if (!this.summaryFunctionsHaveChanged) {
+      this.summaryFunctionsHaveChanged = true;
+      if (this.sorter) {
+        this.sorter.enable();
+      }
+    }
+  },
+
+  hasSummaryFunctions: function () {
+    var i, fields = this.getFields() || [];
+    for (i = 0; i < fields.length; i++) {
+      if (fields[i].summaryFunction) {
+        return true;
+      }
+    }
+    return false;
+  },
+
   // show or hide the filter button
   filterEditorSubmit: function (criteria) {
     this.checkShowFilterFunnelIcon(criteria);
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js	Sun Aug 07 16:24:38 2016 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js	Mon Aug 08 12:32:51 2016 +0200
@@ -637,6 +637,10 @@
     if (this.isGrouped) {
       this.regroup();
     }
+    if (this.lazyFiltering) {
+      this.markForCalculateSummaries();
+      return;
+    }
     if (!clear) {
       if (!this.showGridSummary) {
         this.setShowGridSummary(true);
@@ -2978,6 +2982,7 @@
     }
 
     if (this.lazyFiltering && !isc.isA.ResultSet(this.data)) {
+      this.markForCalculateSummaries();
       OB.Utilities.createResultSetManually(this);
     }
 
@@ -3770,6 +3775,7 @@
     }
 
     this.view.messageBar.hide();
+    this.markForCalculateSummaries();
 
     delete this._showingEditor;
     return ret;
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/main/ob-standard-view.js	Sun Aug 07 16:24:38 2016 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/main/ob-standard-view.js	Mon Aug 08 12:32:51 2016 +0200
@@ -1484,6 +1484,7 @@
     this.viewForm.recordIdInForm = OB.Utilities.getTemporaryId();
 
     if (!this.isShowingForm) {
+      this.viewGrid.markForCalculateSummaries();
       this.switchFormGridVisibility();
     }
 
@@ -2299,6 +2300,7 @@
           selection = currentGrid.getSelection().duplicate();
           // deselect the current records
           currentGrid.deselectAllRecords();
+          view.viewGrid.markForCalculateSummaries();
 
           if (selection.length > 1) {
             deleteData = {};