UNPKG

smart-table-keyboard

Version:

provide keyboard navigation to widget implementing the grid pattern

67 lines (62 loc) 2.54 kB
import {createCell} from './cell'; import {createRow} from './row'; import {findContainer} from './util'; export function keyGrid (grid, options) { const {rowSelector, cellSelector} = options; return { moveRight(target){ const cell = createCell(target, options); let newCell = createCell(cell.next(), options); while (newCell !== null && newCell.selectFromBefore === void 0) { newCell = createCell(newCell.next(), options); } return newCell !== null ? newCell.selectFromBefore() : target; }, moveLeft(target){ const cell = createCell(target, options); let newCell = createCell(cell.previous(), options); while (newCell !== null && newCell.selectFromAfter === void 0) { newCell = createCell(newCell.previous(), options); } return newCell !== null ? newCell.selectFromAfter() : target; }, moveUp(target){ const rowElement = findContainer(target, rowSelector); const cells = [...rowElement.querySelectorAll(cellSelector)]; const row = createRow(rowElement, grid, options); let newRow = createRow(row.previous(), grid, options); while (newRow !== null && newRow.item === void 0) { newRow = createRow(newRow.previous(), grid, options); } if (newRow === null) { return target; } let askedIndex = cells.indexOf(findContainer(target, cellSelector)); let newCell = createCell(newRow.item(askedIndex), options); while (newCell === null || newCell.selectFromBefore === void 0 && askedIndex > 0) { askedIndex--; newCell = createCell(newRow.item(askedIndex), options); } return newCell.selectFromBefore(); }, moveDown(target){ const rowElement = findContainer(target, rowSelector); const cells = [...rowElement.querySelectorAll(cellSelector)]; const row = createRow(rowElement, grid, options); let newRow = createRow(row.next(), grid, options); while (newRow !== null && newRow.item === void 0) { newRow = createRow(newRow.next(), grid, options); } if (newRow === null) { return target; } let askedIndex = cells.indexOf(findContainer(target, cellSelector)); let newCell = createCell(newRow.item(askedIndex), options); while (newCell === null || newCell.selectFromBefore === void 0 && askedIndex > 0) { askedIndex--; newCell = createCell(newRow.item(askedIndex), options); } return newCell.selectFromBefore(); } } }