From ed8604e6a48a431c4ceac350fff2cf6f0a119c28 Mon Sep 17 00:00:00 2001
From: Joao Pereira <pair+jpereira@pivotal.io>
Date: Fri, 31 Mar 2017 16:41:28 -0400
Subject: [PATCH 09/11] Select the whole grid when clicking anywhere in the
 select-all cell

---
 web/pgadmin/static/js/selection/grid_selector.js   | 12 +++---
 .../javascript/selection/column_selector_spec.js   | 43 ++++++++--------------
 .../javascript/selection/grid_selector_spec.js     | 22 ++++++++++-
 3 files changed, 43 insertions(+), 34 deletions(-)

diff --git a/web/pgadmin/static/js/selection/grid_selector.js b/web/pgadmin/static/js/selection/grid_selector.js
index dfe115f4..31aee69f 100644
--- a/web/pgadmin/static/js/selection/grid_selector.js
+++ b/web/pgadmin/static/js/selection/grid_selector.js
@@ -8,11 +8,12 @@ define(['jquery', 'sources/selection/column_selector', 'sources/selection/row_se
 
       var init = function (grid) {
         this.grid = grid;
-        grid.onHeaderClick.subscribe(function (event, eventArgument) {
-          if (event.target == $("[data-id='checkbox-select-all']")[0]) {
+        grid.onHeaderClick.subscribe(function (event, eventArguments) {
+          if (eventArguments.column.selectAllOnClick) {
             toggleSelectAll(grid);
           }
         });
+
         grid.getSelectionModel().onSelectedRangesChanged
           .subscribe(handleSelectedRangesChanged.bind(null, grid));
         grid.registerPlugin(rowSelector);
@@ -23,15 +24,14 @@ define(['jquery', 'sources/selection/column_selector', 'sources/selection/row_se
         columnDefinitions = columnSelector.getColumnDefinitionsWithCheckboxes(columnDefinitions);
         columnDefinitions = rowSelector.getColumnDefinitionsWithCheckboxes(columnDefinitions);
 
+        columnDefinitions[0].selectAllOnClick = true;
         columnDefinitions[0].name = '<input type="checkbox" data-id="checkbox-select-all" ' +
           'title="Select/Deselect All"/>' + columnDefinitions[0].name;
         return columnDefinitions;
       };
 
       function handleSelectedRangesChanged(grid) {
-        if (!isEntireGridSelected(grid)) {
-          $("[data-id='checkbox-select-all']").prop("checked", false)
-        }
+        $("[data-id='checkbox-select-all']").prop("checked", isEntireGridSelected(grid));
       }
 
       function isEntireGridSelected(grid) {
@@ -76,4 +76,4 @@ define(['jquery', 'sources/selection/column_selector', 'sources/selection/row_se
     };
 
     return GridSelector;
-  });
\ No newline at end of file
+  });
diff --git a/web/regression/javascript/selection/column_selector_spec.js b/web/regression/javascript/selection/column_selector_spec.js
index ff991cd1..7c7a2127 100644
--- a/web/regression/javascript/selection/column_selector_spec.js
+++ b/web/regression/javascript/selection/column_selector_spec.js
@@ -39,42 +39,20 @@ define(
           };
           columns.push(checkboxColumn);
 
-          var columnSelector = new ColumnSelector();
-          columns = columnSelector.getColumnDefinitionsWithCheckboxes(columns);
-          var grid = new SlickGrid(container, data, columns, options);
-
-          var rowSelectionModel = new RowSelectionModel();
-          grid.setSelectionModel(rowSelectionModel);
-          grid.registerPlugin(columnSelector);
-          grid.invalidate();
+          setupGrid(columns);
 
           expect(container.find('.slick-header-columns input').length).toBe(2)
         });
       });
 
       it("renders a checkbox in the column header", function () {
-        var columnSelector = new ColumnSelector();
-        columns = columnSelector.getColumnDefinitionsWithCheckboxes(columns);
-        var grid = new SlickGrid(container, data, columns, options);
-
-        var rowSelectionModel = new RowSelectionModel();
-        grid.setSelectionModel(rowSelectionModel);
-
-        grid.registerPlugin(columnSelector);
-        grid.invalidate();
+        setupGrid(columns);
 
         expect(container.find('.slick-header-columns input').length).toBe(2)
       });
 
       it("displays the name of the column", function () {
-        var columnSelector = new ColumnSelector();
-        columns = columnSelector.getColumnDefinitionsWithCheckboxes(columns);
-        var grid = new SlickGrid(container, data, columns, options);
-
-        var rowSelectionModel = new RowSelectionModel();
-        grid.setSelectionModel(rowSelectionModel);
-        grid.registerPlugin(columnSelector);
-        grid.invalidate();
+        setupGrid(columns);
 
         expect($(container.find('.slick-header-columns .slick-column-name')[0]).text()).toBe(' some-column-name');
         expect($(container.find('.slick-header-columns .slick-column-name')[1]).text()).toBe(' second column');
@@ -144,7 +122,6 @@ define(
           container.find('.slick-header-columns input')[1].click();
 
           expect($(container.find('.slick-header-columns input')[1]).is(':checked')).toBeFalsy();
-
         });
 
         it("checks the checkbox", function () {
@@ -213,5 +190,17 @@ define(
           });
         });
       });
+
+      var setupGrid = function (columns) {
+        var columnSelector = new ColumnSelector();
+        columns = columnSelector.getColumnDefinitionsWithCheckboxes(columns);
+        var grid = new SlickGrid(container, data, columns, options);
+
+        var rowSelectionModel = new RowSelectionModel();
+        grid.setSelectionModel(rowSelectionModel);
+
+        grid.registerPlugin(columnSelector);
+        grid.invalidate();
+      };
     });
-  });
\ No newline at end of file
+  });
diff --git a/web/regression/javascript/selection/grid_selector_spec.js b/web/regression/javascript/selection/grid_selector_spec.js
index f9923e78..a74a66f9 100644
--- a/web/regression/javascript/selection/grid_selector_spec.js
+++ b/web/regression/javascript/selection/grid_selector_spec.js
@@ -56,6 +56,26 @@ define(["jquery",
         expect(container.find("[title='Select/Deselect All']").length).toBe(1);
       });
 
+      describe("when the cell for the select/deselect all is clicked", function () {
+        it("selects the whole grid", function () {
+          container.find("[title='Select/Deselect All']").parent().click();
+
+          var selectedRanges = rowSelectionModel.getSelectedRanges();
+          expect(selectedRanges.length).toBe(1);
+          var selectedRange = selectedRanges[0];
+          expect(selectedRange.fromCell).toBe(1);
+          expect(selectedRange.toCell).toBe(2);
+          expect(selectedRange.fromRow).toBe(0);
+          expect(selectedRange.toRow).toBe(9);
+        });
+
+        it("checks the checkbox", function () {
+          container.find("[title='Select/Deselect All']").parent().click();
+
+          expect($(container.find("[data-id='checkbox-select-all']")).is(':checked')).toBeTruthy();
+        })
+      });
+
       describe("when the main checkbox in the corner gets selected", function () {
         it("unchecks all the columns", function () {
           container.find("[title='Select/Deselect All']").click();
@@ -103,4 +123,4 @@ define(["jquery",
         });
       });
     });
-  });
\ No newline at end of file
+  });
-- 
2.12.0

