Added horizontal keyboard selection

This commit is contained in:
Dallon Feldner
2012-10-02 12:00:59 -07:00
parent ab7386e9ab
commit 85dbd0a8b7

View File

@@ -162,7 +162,6 @@
}, vm);
vm.view.scrollToRow = function(row) {
// debugger;
var $table = vm.view.$table()
, rowIndex = vm.data.indexOf(row);
@@ -171,7 +170,7 @@
rowIndex = vm.data().length - 1;
}
if (rowIndex > -1) {
if (rowIndex >= 0 && rowIndex < $table.find('tbody tr').length) {
var $row = $table.find('tbody tr').eq(rowIndex + 1) //account for header-spacer
, headerHeight = $table.find('thead').outerHeight()
, top = $row.position().top
@@ -192,8 +191,37 @@
};
vm.view.scrollToColumn = function(prop) {
var $table = vm.view.$table()
, index = vm.properties.indexOf(prop)
, $columns = $table.find('thead th');
if (index === -1) index = 0;
if (index >= 0 && index < $columns.length + 2) {
var $col = $columns.eq(index + 2) // account for margin and id columns
, left = $col.position().left
, right = left + $col.outerWidth()
, marginWidth = $columns.filter('.margin').outerWidth()
, tableWidth = vm.view.dimensions().width - vm.view.scrollWidth()
, leftOffset = left - marginWidth
, rightOffset = right - tableWidth;
if (rightOffset > 0) {
vm.view.scrollX(vm.view.scrollX() + rightOffset);
}
if (leftOffset < 0) {
vm.view.scrollX(vm.view.scrollX() + leftOffset);
}
}
};
vm.view.scrollToSelected = function() {
vm.view.scrollToRow(vm.selectedRow());
vm.view.scrollToColumn(vm.selectedProp());
};
function createRow(data) {
@@ -247,6 +275,12 @@
case 34:
selectionVertical(e);
return false;
case 37: // left/right arrows
case 39:
case 36: // home/end
case 35:
selectionHorizontal(e);
return false;
}
return true;
@@ -288,7 +322,28 @@
}
vm.view.scrollToSelected();
}
function selectionHorizontal(e) {
var props = vm.properties()
, index = props.indexOf(vm.selectedProp());
if (e.which == 36 || (e.which == 37 && e.ctrlKey)) { // home or ctrl-left
index = 0;
if (e.which == 36) vm.view.scrollX(0);
} else if (e.which == 37) index -= 1;
if (e.which == 35 || (e.which == 39 && e.ctrlKey)) { // end or ctrl-right
index = props.length - 1;
} else if (e.which == 39) index += 1;
if (index <= 0 && props.indexOf(vm.selectedProp()) === 0) {
vm.view.scrollX(0);
} else if (index < props.length && index >= 0) {
vm.selectedProp(props[index]);
vm.view.scrollToSelected();
}
}
}