From 69dc27bdc5372563f5bf45e3bd9e46f7e9bb1ee4 Mon Sep 17 00:00:00 2001
From: Sarah McAlear and Tira Odhner <pair+smcalear+aodhner@pivotal.io>
Date: Thu, 30 Mar 2017 18:06:29 -0400
Subject: [PATCH 08/11] deselect the select-all checkbox when selection changes

---
 web/pgadmin/static/js/selection/grid_selector.js   | 26 +++++++++++++++-------
 .../javascript/selection/grid_selector_spec.js     | 15 ++++++++++++-
 2 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/web/pgadmin/static/js/selection/grid_selector.js b/web/pgadmin/static/js/selection/grid_selector.js
index c0deb071..dfe115f4 100644
--- a/web/pgadmin/static/js/selection/grid_selector.js
+++ b/web/pgadmin/static/js/selection/grid_selector.js
@@ -13,6 +13,8 @@ define(['jquery', 'sources/selection/column_selector', 'sources/selection/row_se
             toggleSelectAll(grid);
           }
         });
+        grid.getSelectionModel().onSelectedRangesChanged
+          .subscribe(handleSelectedRangesChanged.bind(null, grid));
         grid.registerPlugin(rowSelector);
         grid.registerPlugin(columnSelector);
       };
@@ -26,23 +28,26 @@ define(['jquery', 'sources/selection/column_selector', 'sources/selection/row_se
         return columnDefinitions;
       };
 
-      function toggleSelectAll(grid) {
+      function handleSelectedRangesChanged(grid) {
+        if (!isEntireGridSelected(grid)) {
+          $("[data-id='checkbox-select-all']").prop("checked", false)
+        }
+      }
+
+      function isEntireGridSelected(grid) {
         var selectionModel = grid.getSelectionModel();
         var selectedRanges = selectionModel.getSelectedRanges();
+        return selectedRanges.length == 1 && isSameRange(selectedRanges[0], getRangeOfWholeGrid(grid));
+      }
 
-        var entireGridIsSelected = selectedRanges.length == 1 && isSameRange(selectedRanges[0], getRangeOfWholeGrid(grid));
-        if (entireGridIsSelected){
+      function toggleSelectAll(grid) {
+        if (isEntireGridSelected(grid)) {
           deselect(grid);
         } else {
           selectAll(grid)
         }
       }
 
-      function deselect(grid) {
-        var selectionModel = grid.getSelectionModel();
-        selectionModel.setSelectedRanges([]);
-      }
-
       var isSameRange = function (range, otherRange) {
         return range.fromCell == otherRange.fromCell && range.toCell == otherRange.toCell &&
           range.fromRow == otherRange.fromRow && range.toRow == otherRange.toRow;
@@ -52,6 +57,11 @@ define(['jquery', 'sources/selection/column_selector', 'sources/selection/row_se
         return new Slick.Range(0, 1, grid.getDataLength() - 1, grid.getColumns().length - 1);
       }
 
+      function deselect(grid) {
+        var selectionModel = grid.getSelectionModel();
+        selectionModel.setSelectedRanges([]);
+      }
+
       function selectAll(grid) {
         var range = getRangeOfWholeGrid(grid);
         var selectionModel = grid.getSelectionModel();
diff --git a/web/regression/javascript/selection/grid_selector_spec.js b/web/regression/javascript/selection/grid_selector_spec.js
index 915e72a4..f9923e78 100644
--- a/web/regression/javascript/selection/grid_selector_spec.js
+++ b/web/regression/javascript/selection/grid_selector_spec.js
@@ -53,7 +53,7 @@ define(["jquery",
       });
 
       it("renders a checkbox for selecting all the cells", function () {
-          expect(container.find("[title='Select/Deselect All']").length).toBe(1);
+        expect(container.find("[title='Select/Deselect All']").length).toBe(1);
       });
 
       describe("when the main checkbox in the corner gets selected", function () {
@@ -88,6 +88,19 @@ define(["jquery",
             expect(selectedRanges.length).toBe(0);
           });
         });
+
+        describe("and then the underlying selection changes", function () {
+          beforeEach(function () {
+            container.find("[title='Select/Deselect All']").click();
+          });
+
+          it("unchecks the main checkbox", function () {
+            var ranges = [new Slick.Range(0, 0, 0, 1)];
+            rowSelectionModel.setSelectedRanges(ranges);
+
+            expect($(container.find("[title='Select/Deselect All']")).is(':checked')).toBeFalsy();
+          });
+        });
       });
     });
   });
\ No newline at end of file
-- 
2.12.0

