Skip to content

Commit

Permalink
Merge pull request mleibman#534 from fetis/master
Browse files Browse the repository at this point in the history
Shift+arrow keys support for selection
  • Loading branch information
mleibman committed Feb 25, 2013
2 parents 30f1a1a + 252e53b commit 1af8606
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion plugins/slick.cellselectionmodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
_grid = grid;
_canvas = _grid.getCanvasNode();
_grid.onActiveCellChanged.subscribe(handleActiveCellChange);
_grid.onKeyDown.subscribe(handleKeyDown);
grid.registerPlugin(_selector);
_selector.onCellRangeSelected.subscribe(handleCellRangeSelected);
_selector.onBeforeCellRangeSelected.subscribe(handleBeforeCellRangeSelected);
}

function destroy() {
_grid.onActiveCellChanged.unsubscribe(handleActiveCellChange);
_grid.onKeyDown.unsubscribe(handleKeyDown);
_selector.onCellRangeSelected.unsubscribe(handleCellRangeSelected);
_selector.onBeforeCellRangeSelected.unsubscribe(handleBeforeCellRangeSelected);
_grid.unregisterPlugin(_selector);
Expand Down Expand Up @@ -78,6 +80,64 @@
setSelectedRanges([new Slick.Range(args.row, args.cell)]);
}
}

function handleKeyDown(e) {
/***
* Кey codes
* 37 left
* 38 up
* 39 right
* 40 down
*/
var ranges, last;
var active = _grid.getActiveCell();

if ( active && e.shiftKey && !e.ctrlKey && !e.altKey &&
(e.which == 37 || e.which == 39 || e.which == 38 || e.which == 40) ) {

ranges = getSelectedRanges();
if (!ranges.length)
ranges.push(new Slick.Range(active.row, active.cell));

// keyboard can work with last range only
last = ranges.pop();

// can't handle selection out of active cell
if (!last.contains(active.row, active.cell))
last = new Slick.Range(active.row, active.cell);

var dRow = last.toRow - last.fromRow,
dCell = last.toCell - last.fromCell,
// walking direction
dirRow = active.row == last.fromRow ? 1 : -1,
dirCell = active.cell == last.fromCell ? 1 : -1;

if (e.which == 37) {
dCell -= dirCell;
} else if (e.which == 39) {
dCell += dirCell ;
} else if (e.which == 38) {
dRow -= dirRow;
} else if (e.which == 40) {
dRow += dirRow;
}

// define new selection range
var new_last = new Slick.Range(active.row, active.cell, active.row + dirRow*dRow, active.cell + dirCell*dCell);
if (removeInvalidRanges([new_last]).length) {
ranges.push(new_last);
_grid.scrollRowIntoView(dirRow > 0 ? new_last.toRow : new_last.fromRow);
_grid.scrollCellIntoView(new_last.fromRow, dirCell > 0 ? new_last.toCell : new_last.fromCell);
}
else
ranges.push(last);

setSelectedRanges(ranges);

e.preventDefault();
e.stopPropagation();
}
}

$.extend(this, {
"getSelectedRanges": getSelectedRanges,
Expand All @@ -89,4 +149,4 @@
"onSelectedRangesChanged": new Slick.Event()
});
}
})(jQuery);
})(jQuery);

0 comments on commit 1af8606

Please sign in to comment.