From b33c846197e7ca4a1064fa199c831b63d1ce6807 Mon Sep 17 00:00:00 2001 From: Joao Pereira Date: Wed, 26 Apr 2017 16:36:08 -0400 Subject: [PATCH 1/2] Add tests for cell selection model --- tests/plugins/cellselectionmodel.html | 35 ++++++ tests/plugins/cellselectionmodel_spec.js | 143 +++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 tests/plugins/cellselectionmodel.html create mode 100644 tests/plugins/cellselectionmodel_spec.js diff --git a/tests/plugins/cellselectionmodel.html b/tests/plugins/cellselectionmodel.html new file mode 100644 index 00000000..63682a1e --- /dev/null +++ b/tests/plugins/cellselectionmodel.html @@ -0,0 +1,35 @@ + + + + SlickGrid - Cell Selection Model plugin tests + + + + + +

SlickGrid - Cell Selection Model Test Suite

+

+
+

+
    + +


    +
    + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/plugins/cellselectionmodel_spec.js b/tests/plugins/cellselectionmodel_spec.js new file mode 100644 index 00000000..6209175f --- /dev/null +++ b/tests/plugins/cellselectionmodel_spec.js @@ -0,0 +1,143 @@ +(function($) { + + var grid, // The SlickGrid instance + cols = [ // The column definitions + { name: "Short", field: "short", width: 100 }, + { name: "Medium", field: "medium", width: 100 }, + { name: "Long", field: "long", width: 100 }, + { name: "Mixed", field: "mixed", width: 100 }, + { name: "Long header creates tooltip", field: "header", width: 50 }, + { name: "Long header with predefined tooltip", field: "tooltipHeader", width: 50, tooltip: "Already have a tooltip!" } + ], + data = [], // The grid data + LONG_VALUE = "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong", + MEDIUM_VALUE = "mediummmmmmm", + SHORT_VALUE = "short", + $container = $("#container"), + keys = { + LEFT_ARROW: 37, + UP_ARROW: 38, + RIGHT_ARROW: 39, + DOWN_ARROW: 40 + } + + // Create data + for (var i = 0; i < 10; i++) { + data.push({ + "id": "row" + i, + "short": SHORT_VALUE, + "medium": MEDIUM_VALUE, + "long": LONG_VALUE, + "mixed": ( i % 2 == 0 ? SHORT_VALUE : LONG_VALUE ), + "header": i, + "tooltipHeader": i + }); + } + + function setupGrid() { + $('
    ').appendTo($container); + grid = new Slick.Grid("#grid", data, cols); + grid.setSelectionModel(new Slick.CellSelectionModel()); + grid.render(); + } + + function teardownGrid() { + $container.empty(); + } + + function getCell(row, column) { + return $($("#grid .slick-cell.l" + column)[row]); + } + + function assertColumnRange(range, fromRow, fromCell, toRow, toCell) { + strictEqual(range.fromRow, fromRow, "start row"); + strictEqual(range.toRow, toRow, "end row"); + strictEqual(range.fromCell, fromCell, "start column"); + strictEqual(range.toCell, toCell, "end column"); + } + + function keyDownOnCell($cell, controlKeyPressed, commandKeyPressed, shiftKeyPressed, keyPressed) { + var $event = $.Event('keydown'); + $event.ctrlKey = controlKeyPressed; + $event.metaKey = commandKeyPressed; + $event.shiftKey = shiftKeyPressed; + $event.which = keyPressed; + $cell.trigger($event); + } + + module("plugins - cellselectionmodel - KeyDownHandler no active cell", { + setup: function () { + setupGrid({}); + }, + teardown: teardownGrid + }); + + test("press right arrow do not change selection", function () { + var $cell = getCell(0, 0); + var $event = $.Event('keydown'); + $event.which = keys.RIGHT_ARROW; + $cell.trigger($event); + + var selectedRanges = grid.getSelectionModel().getSelectedRanges(); + strictEqual(selectedRanges.length, 0, "number of ranges is incorrect"); + }); + + module("plugins - cellselectionmodel - KeyDownHandler with active cell", { + setup: function () { + setupGrid({}); + }, + teardown: teardownGrid + }); + + test("press right arrow do not change selection", function () { + var $cell = getCell(1, 3); + $cell.click(); + + keyDownOnCell($cell, false, false, false, keys.RIGHT_ARROW); + + var selectedRanges = grid.getSelectionModel().getSelectedRanges(); + strictEqual(selectedRanges.length, 1, "number of ranges is incorrect"); + var range = selectedRanges[0]; + + assertColumnRange(range, 1, 4, 1, 4); + }); + + test("press shift plus left arrow add second cell to selection", function () { + var $cell = getCell(1, 3); + + $cell.click(); + keyDownOnCell($cell, false, false, true, keys.LEFT_ARROW); + + var selectedRanges = grid.getSelectionModel().getSelectedRanges(); + strictEqual(selectedRanges.length, 1, "number of ranges is incorrect"); + var range = selectedRanges[0]; + + assertColumnRange(range, 1, 2, 1, 3); + }); + + test("press control plus shift plus up arrow do not change selection", function () { + var $cell = getCell(1, 3); + $cell.click(); + keyDownOnCell($cell, true, false, true, keys.UP_ARROW); + + var selectedRanges = grid.getSelectionModel().getSelectedRanges(); + strictEqual(selectedRanges.length, 1, "number of ranges"); + + var range = selectedRanges[0]; + assertColumnRange(range, 1, 3, 1, 3); + }); + + + test("press command plus shift plus down arrow do not change selection", function () { + var $cell = getCell(1, 3); + $cell.click(); + keyDownOnCell($cell, false, true, true, keys.DOWN_ARROW); + + var selectedRanges = grid.getSelectionModel().getSelectedRanges(); + strictEqual(selectedRanges.length, 1, "number of ranges"); + + var range = selectedRanges[0]; + assertColumnRange(range, 1, 3, 1, 3); + }); + +})(jQuery); \ No newline at end of file From 1dc3554621549d2f01f4c68d5cddc311edc82457 Mon Sep 17 00:00:00 2001 From: Joao Pereira Date: Wed, 26 Apr 2017 16:37:16 -0400 Subject: [PATCH 2/2] Add command to list of keys that change the behavior of onKeyDown --- plugins/slick.cellselectionmodel.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/slick.cellselectionmodel.js b/plugins/slick.cellselectionmodel.js index 3d9e2af0..39f27776 100644 --- a/plugins/slick.cellselectionmodel.js +++ b/plugins/slick.cellselectionmodel.js @@ -93,9 +93,10 @@ * 40 down */ var ranges, last; - var active = _grid.getActiveCell(); + var active = _grid.getActiveCell(); + var metaKey = e.ctrlKey || e.metaKey; - if ( active && e.shiftKey && !e.ctrlKey && !e.altKey && + if ( active && e.shiftKey && !metaKey && !e.altKey && (e.which == 37 || e.which == 39 || e.which == 38 || e.which == 40) ) { ranges = getSelectedRanges();