|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Directionality} from '@angular/cdk/bidi'; |
| 10 | +import {LEFT_ARROW, UP_ARROW, RIGHT_ARROW, DOWN_ARROW} from '@angular/cdk/keycodes'; |
| 11 | +import {Injectable} from '@angular/core'; |
| 12 | +import {PartialObserver} from 'rxjs'; |
| 13 | + |
| 14 | +import {EDITABLE_CELL_SELECTOR, ROW_SELECTOR, TABLE_SELECTOR} from './constants'; |
| 15 | +import {closest} from './polyfill'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Service responsible for moving cell focus around in response to keyboard events. |
| 19 | + * May be overridden to customize the keyboard behavior of popover edit. |
| 20 | + */ |
| 21 | +@Injectable({providedIn: 'root'}) |
| 22 | +export class FocusDispatcher { |
| 23 | + /** Observes keydown events triggered from the table. */ |
| 24 | + readonly keyObserver: PartialObserver<KeyboardEvent>; |
| 25 | + |
| 26 | + constructor(protected readonly directionality: Directionality) { |
| 27 | + this.keyObserver = {next: (event) => this.handleKeyboardEvent(event)}; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Moves focus to earlier or later cells (in dom order) by offset cells relative to |
| 32 | + * currentCell. |
| 33 | + */ |
| 34 | + moveFocusHorizontally(currentCell: HTMLElement, offset: number): void { |
| 35 | + const cells = Array.from(closest(currentCell, TABLE_SELECTOR)!.querySelectorAll( |
| 36 | + EDITABLE_CELL_SELECTOR)) as HTMLElement[]; |
| 37 | + const currentIndex = cells.indexOf(currentCell); |
| 38 | + const newIndex = currentIndex + offset; |
| 39 | + |
| 40 | + if (cells[newIndex]) { |
| 41 | + cells[newIndex].focus(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** Moves focus to up or down by row by offset cells relative to currentCell. */ |
| 46 | + moveFocusVertically(currentCell: HTMLElement, offset: number): void { |
| 47 | + const currentRow = closest(currentCell, ROW_SELECTOR)!; |
| 48 | + const rows = Array.from(closest(currentRow, TABLE_SELECTOR)!.querySelectorAll(ROW_SELECTOR)); |
| 49 | + const currentRowIndex = rows.indexOf(currentRow); |
| 50 | + const currentIndexWithinRow = |
| 51 | + Array.from(currentRow.querySelectorAll(EDITABLE_CELL_SELECTOR)).indexOf(currentCell); |
| 52 | + const newRowIndex = currentRowIndex + offset; |
| 53 | + |
| 54 | + if (rows[newRowIndex]) { |
| 55 | + const rowToFocus = |
| 56 | + Array.from(rows[newRowIndex].querySelectorAll(EDITABLE_CELL_SELECTOR)) as HTMLElement[]; |
| 57 | + |
| 58 | + if (rowToFocus[currentIndexWithinRow]) { |
| 59 | + rowToFocus[currentIndexWithinRow].focus(); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** Translates arrow keydown events into focus move operations. */ |
| 65 | + protected handleKeyboardEvent(event: KeyboardEvent): void { |
| 66 | + const cell = closest(event.target, EDITABLE_CELL_SELECTOR) as HTMLElement | null; |
| 67 | + |
| 68 | + if (!cell) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + switch (event.keyCode) { |
| 73 | + case UP_ARROW: |
| 74 | + this.moveFocusVertically(cell, -1); |
| 75 | + break; |
| 76 | + case DOWN_ARROW: |
| 77 | + this.moveFocusVertically(cell, 1); |
| 78 | + break; |
| 79 | + case LEFT_ARROW: |
| 80 | + this.moveFocusHorizontally(cell, this.directionality.value === 'ltr' ? -1 : 1); |
| 81 | + break; |
| 82 | + case RIGHT_ARROW: |
| 83 | + this.moveFocusHorizontally(cell, this.directionality.value === 'ltr' ? 1 : -1); |
| 84 | + break; |
| 85 | + default: |
| 86 | + // If the keyboard event is not handled, return now so that we don't `preventDefault`. |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + event.preventDefault(); |
| 91 | + } |
| 92 | +} |
0 commit comments