LP#1777207: add duplicate row detection to prepend()
authorGalen Charlton <gmc@equinoxinitiative.org>
Fri, 12 Jul 2019 14:51:28 +0000 (10:51 -0400)
committerGalen Charlton <gmc@equinoxinitiative.org>
Fri, 12 Jul 2019 14:51:28 +0000 (10:51 -0400)
This patch ensures that if a visible grid item that might be
added via prepend() would duplicate an existing row entry, a
full collect() is done instead. This patch also converts a
prepend() to a full collect() if sorting is in effect, so we
cannot know whether the most recently added item should be
displayed in the visible rows otherwise.

Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/web/js/ui/default/staff/services/grid.js

index a12e47b..0d2ce9b 100644 (file)
@@ -1328,6 +1328,17 @@ angular.module('egGridMod',
             }
 
             grid.prepend = function(limit) {
+                var ran_into_duplicate = false;
+                var sort = grid.dataProvider.sort;
+                if (sort && sort.length) {
+                    // if sorting is in effect, we have no
+                    // have no way here of knowing which row
+                    // was most recently added nor that it
+                    // belongs on top of the visible set of rows,
+                    // so we default to a full collect()
+                    grid.collect();
+                    return;
+                }
                 if (grid.offset > 0) {
                     // if we're prepending, we're forcing the
                     // offset back to zero to display the top
@@ -1344,6 +1355,14 @@ angular.module('egGridMod',
                 null,
                 function(item) {
                     if (item) {
+                        var newIdx = grid.indexValue(item);
+                        angular.forEach($scope.items, function(existing) {
+                            if (grid.indexValue(existing) == newIdx) {
+                                console.debug('egGrid.prepend(): refusing to add duplicate item ' + newIdx);
+                                ran_into_duplicate = true;
+                                return;
+                            }
+                        });
                         $scope.items.unshift(item);
                         if (limit && $scope.items.length > limit) {
                             // this accommodates the checkin grid that
@@ -1363,6 +1382,9 @@ angular.module('egGridMod',
                     console.debug('egGrid.prepend() complete');
                     grid.collecting = false;
                     $scope.selected = angular.copy($scope.selected);
+                    if (ran_into_duplicate) {
+                        grid.collect();
+                    }
                 });
             }