|
| 1 | +import { CLASSES } from '../css-classes'; |
| 2 | +import { assertExists } from '../types/utils'; |
| 3 | +import { isVisible } from '../utils/dom'; |
| 4 | +import { ResizeEvent } from './make-responsive'; |
| 5 | + |
| 6 | +type Cells = HTMLElement[][]; |
| 7 | + |
| 8 | +type Position = { row: number; column: number }; |
| 9 | + |
| 10 | +const DEFAULT_POSITION = { row: 0, column: 0 }; |
| 11 | + |
| 12 | +class GridFocusManager { |
| 13 | + private cells: Cells; |
| 14 | + |
| 15 | + constructor(private grid: HTMLElement) { |
| 16 | + this.cells = setupCells(grid); |
| 17 | + this.setup(); |
| 18 | + } |
| 19 | + |
| 20 | + reset(): void { |
| 21 | + this.cells = setupCells(this.grid); |
| 22 | + this.currentPosition = DEFAULT_POSITION; |
| 23 | + } |
| 24 | + |
| 25 | + private setup(): void { |
| 26 | + this.grid.addEventListener('keydown', this.onKeydown.bind(this)); |
| 27 | + this.grid.addEventListener('click', this.onClick.bind(this)); |
| 28 | + } |
| 29 | + |
| 30 | + private currentPosition: Position = DEFAULT_POSITION; |
| 31 | + |
| 32 | + private get row(): number { |
| 33 | + return this.currentPosition.row; |
| 34 | + } |
| 35 | + |
| 36 | + private get lastRow(): number { |
| 37 | + return this.cells.length - 1; |
| 38 | + } |
| 39 | + |
| 40 | + private get column(): number { |
| 41 | + return this.currentPosition.column; |
| 42 | + } |
| 43 | + |
| 44 | + private get lastColumn(): number { |
| 45 | + // Assumes all rows have the same number of columns. |
| 46 | + return assertExists(this.cells[0]).length - 1; |
| 47 | + } |
| 48 | + |
| 49 | + private onKeydown(event: KeyboardEvent): void { |
| 50 | + const displayedAsGrid = this.grid.classList.contains(CLASSES.displayAsGrid); |
| 51 | + |
| 52 | + switch (event.key) { |
| 53 | + case 'ArrowRight': |
| 54 | + this.updatePosition({ column: this.column + 1 }); |
| 55 | + break; |
| 56 | + |
| 57 | + case 'ArrowLeft': |
| 58 | + this.updatePosition({ column: this.column - 1 }); |
| 59 | + break; |
| 60 | + |
| 61 | + case 'ArrowDown': |
| 62 | + if (displayedAsGrid) { |
| 63 | + this.updatePosition({ row: this.row + 1 }); |
| 64 | + } else { |
| 65 | + this.updatePosition({ column: this.column + 1 }); |
| 66 | + } |
| 67 | + break; |
| 68 | + |
| 69 | + case 'ArrowUp': |
| 70 | + if (displayedAsGrid) { |
| 71 | + this.updatePosition({ row: this.row - 1 }); |
| 72 | + } else { |
| 73 | + this.updatePosition({ column: this.column - 1 }); |
| 74 | + } |
| 75 | + break; |
| 76 | + |
| 77 | + case 'Home': |
| 78 | + this.updatePosition({ |
| 79 | + column: 0, |
| 80 | + row: displayedAsGrid && event.ctrlKey ? 0 : this.row, |
| 81 | + }); |
| 82 | + break; |
| 83 | + |
| 84 | + case 'End': |
| 85 | + this.updatePosition({ |
| 86 | + column: this.lastColumn, |
| 87 | + row: displayedAsGrid && event.ctrlKey ? this.lastRow : this.row, |
| 88 | + }); |
| 89 | + break; |
| 90 | + |
| 91 | + default: |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private onClick(event: MouseEvent) { |
| 97 | + if (event.target instanceof HTMLElement) { |
| 98 | + const cell = event.target.closest(`div.${CLASSES.cell}`); |
| 99 | + if (cell instanceof HTMLElement) { |
| 100 | + this.updatePosition(this.getPosition(cell)); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Focus new position. |
| 107 | + * Ensure only the latest position is focusable by keyboard. |
| 108 | + * Keep track of latest position. |
| 109 | + */ |
| 110 | + private updatePosition(updates: Partial<Position>) { |
| 111 | + const row = |
| 112 | + updates.row === undefined |
| 113 | + ? this.row |
| 114 | + : Math.max(Math.min(updates.row, this.lastRow), 0); |
| 115 | + const column = |
| 116 | + updates.column === undefined |
| 117 | + ? this.column |
| 118 | + : Math.max(Math.min(updates.column, this.lastColumn), 0); |
| 119 | + |
| 120 | + this.currentCell.tabIndex = -1; |
| 121 | + this.currentPosition = { row, column }; |
| 122 | + this.focusCurrent(); |
| 123 | + } |
| 124 | + |
| 125 | + private focusCurrent(): void { |
| 126 | + this.currentCell.focus(); |
| 127 | + this.currentCell.tabIndex = 0; |
| 128 | + } |
| 129 | + |
| 130 | + private get currentCell(): HTMLElement { |
| 131 | + return assertExists(this.getCell(this.row, this.column)); |
| 132 | + } |
| 133 | + |
| 134 | + private getCell( |
| 135 | + rowIndex: number, |
| 136 | + columnIndex: number |
| 137 | + ): HTMLElement | undefined { |
| 138 | + const row = this.cells[rowIndex]; |
| 139 | + return row && row[columnIndex]; |
| 140 | + } |
| 141 | + |
| 142 | + private getPosition(cell: HTMLElement): Position { |
| 143 | + // eslint-disable-next-line no-restricted-syntax |
| 144 | + for (const row of this.cells) { |
| 145 | + const index = row.indexOf(cell); |
| 146 | + if (index > -1) { |
| 147 | + return { |
| 148 | + column: index, |
| 149 | + row: this.cells.indexOf(row), |
| 150 | + }; |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + throw new Error('Position not found for cell'); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +export default function enableKeyboardNav( |
| 160 | + grid: HTMLElement, |
| 161 | + isResponsive: boolean |
| 162 | +): void { |
| 163 | + const manager = new GridFocusManager(grid); |
| 164 | + |
| 165 | + function maybeResetFocusManager(event: ResizeEvent) { |
| 166 | + if (!manager || event.detail.displayChanged) { |
| 167 | + manager.reset(); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + if (isResponsive) { |
| 172 | + grid.addEventListener('grid-resize', (event) => { |
| 173 | + maybeResetFocusManager(event as ResizeEvent); |
| 174 | + }); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +function setupCells(grid: HTMLElement) { |
| 179 | + const cells: Cells = []; |
| 180 | + [...grid.querySelectorAll(`div.${CLASSES.row}`)].forEach((row, rowIndex) => { |
| 181 | + cells.push(setupRowCells(row, rowIndex)); |
| 182 | + }); |
| 183 | + return cells; |
| 184 | +} |
| 185 | + |
| 186 | +function setupRowCells(row: Element, rowIndex: number) { |
| 187 | + const rowCells = [ |
| 188 | + ...row.querySelectorAll(`div.${CLASSES.cell}`), |
| 189 | + ] as HTMLElement[]; |
| 190 | + |
| 191 | + rowCells.forEach((cell) => { |
| 192 | + // eslint-disable-next-line no-param-reassign |
| 193 | + cell.tabIndex = -1; |
| 194 | + }); |
| 195 | + |
| 196 | + if (rowIndex === 0) { |
| 197 | + const firstCell = () => assertExists(rowCells[0]); |
| 198 | + if (!isVisible(firstCell())) { |
| 199 | + rowCells.shift(); |
| 200 | + } |
| 201 | + firstCell().tabIndex = 0; |
| 202 | + } |
| 203 | + return rowCells; |
| 204 | +} |
0 commit comments