|
| 1 | +import { |
| 2 | + Directive, |
| 3 | + Input, |
| 4 | + ElementRef, |
| 5 | + HostListener, |
| 6 | + Output, |
| 7 | + EventEmitter |
| 8 | +} from "@angular/core"; |
| 9 | +import { Table } from "./table.component"; |
| 10 | +import { getFocusElementList, tabbableSelectorIgnoreTabIndex } from "../common/tab.service"; |
| 11 | + |
| 12 | +@Directive({ |
| 13 | + selector: "[ibmDataGridFocus]" |
| 14 | +}) |
| 15 | +export class DataGridFocus { |
| 16 | + @Input() ibmDataGridFocus: boolean; |
| 17 | + @Input() set columnIndex(value: number) { |
| 18 | + const shouldEmit = value !== this._columnIndex; |
| 19 | + this._columnIndex = value; |
| 20 | + if (shouldEmit) { |
| 21 | + this.columnIndexChange.emit(value); |
| 22 | + } |
| 23 | + } |
| 24 | + get columnIndex(): number { |
| 25 | + return this._columnIndex; |
| 26 | + } |
| 27 | + |
| 28 | + @Output() columnIndexChange: EventEmitter<number> = new EventEmitter(); |
| 29 | + |
| 30 | + protected _columnIndex: number; |
| 31 | + |
| 32 | + constructor(protected elementRef: ElementRef) {} |
| 33 | + |
| 34 | + focus(element) { |
| 35 | + const focusElementList = getFocusElementList(element, tabbableSelectorIgnoreTabIndex); |
| 36 | + if (element.firstElementChild && element.firstElementChild.classList.contains("bx--table-sort-v2")) { |
| 37 | + focusElementList[1].focus(); |
| 38 | + } else if (focusElementList.length > 0) { |
| 39 | + focusElementList[0].focus(); |
| 40 | + } else { |
| 41 | + element.focus(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @HostListener("keyup", ["$event"]) |
| 46 | + keyUp(event: KeyboardEvent) { |
| 47 | + if (!this.ibmDataGridFocus) { |
| 48 | + return; |
| 49 | + } |
| 50 | + const element = this.elementRef.nativeElement; |
| 51 | + const rows = element.closest("table").rows; |
| 52 | + const closestTr = element.closest("tr"); |
| 53 | + let rowIndex = Array.from(rows).indexOf(closestTr); |
| 54 | + |
| 55 | + const headerRow = rows[0].querySelectorAll("th"); |
| 56 | + |
| 57 | + switch (event.key) { |
| 58 | + case "Right": // IE specific value |
| 59 | + case "ArrowRight": |
| 60 | + if (element.nextElementSibling && Array.from(headerRow).indexOf(element.nextElementSibling) < headerRow.length - 1) { |
| 61 | + this.columnIndex++; |
| 62 | + const nextSibling = element.nextElementSibling; |
| 63 | + Table.setTabIndex(element, -1); |
| 64 | + Table.setTabIndex(nextSibling, 0); |
| 65 | + this.focus(nextSibling); |
| 66 | + } |
| 67 | + break; |
| 68 | + case "Left": // IE specific value |
| 69 | + case "ArrowLeft": |
| 70 | + if (element.previousElementSibling) { |
| 71 | + this.columnIndex--; |
| 72 | + const previousSibling = element.previousElementSibling; |
| 73 | + Table.setTabIndex(element, -1); |
| 74 | + Table.setTabIndex(previousSibling, 0); |
| 75 | + this.focus(previousSibling); |
| 76 | + } |
| 77 | + break; |
| 78 | + case "Down": // IE specific value |
| 79 | + case "ArrowDown": |
| 80 | + if (rowIndex < rows.length - 1) { |
| 81 | + rowIndex++; |
| 82 | + const row = rows[rowIndex].querySelectorAll("td"); |
| 83 | + Table.setTabIndex(element, -1); |
| 84 | + if (rows[rowIndex].classList.contains("bx--expandable-row-v2") && !rows[rowIndex].classList.contains("bx--parent-row-v2")) { |
| 85 | + Table.setTabIndex(row[0], 0); |
| 86 | + this.focus(row[0]); |
| 87 | + } else { |
| 88 | + if (this.columnIndex > row.length - 1) { |
| 89 | + this.columnIndex = row.length - 1; |
| 90 | + } |
| 91 | + Table.setTabIndex(row[this.columnIndex], 0); |
| 92 | + this.focus(row[this.columnIndex]); |
| 93 | + } |
| 94 | + } |
| 95 | + break; |
| 96 | + case "Up": // IE specific value |
| 97 | + case "ArrowUp": |
| 98 | + if ((rowIndex === 1 && Array.from(headerRow).every(th => getFocusElementList(th, tabbableSelectorIgnoreTabIndex).length === 0)) || |
| 99 | + rowIndex === 0) { |
| 100 | + return; |
| 101 | + } |
| 102 | + Table.setTabIndex(element, -1); |
| 103 | + rowIndex--; |
| 104 | + const row = rows[rowIndex].querySelectorAll("td, th"); |
| 105 | + if (rows[rowIndex].classList.contains("bx--expandable-row-v2") && !rows[rowIndex].classList.contains("bx--parent-row-v2")) { |
| 106 | + Table.setTabIndex(row[0], 0); |
| 107 | + this.focus(row[0]); |
| 108 | + } else { |
| 109 | + if (this.columnIndex > row.length - 1) { |
| 110 | + this.columnIndex = row.length - 1; |
| 111 | + } |
| 112 | + Table.setTabIndex(row[this.columnIndex], 0); |
| 113 | + this.focus(row[this.columnIndex]); |
| 114 | + } |
| 115 | + break; |
| 116 | + case "Home": |
| 117 | + this.columnIndex = 0; |
| 118 | + Table.setTabIndex(element, -1); |
| 119 | + if (event.ctrlKey) { |
| 120 | + if (Array.from(headerRow).some(th => getFocusElementList(th, tabbableSelectorIgnoreTabIndex).length > 0)) { |
| 121 | + Table.setTabIndex(headerRow[0], 0); |
| 122 | + this.focus(headerRow[0]); |
| 123 | + } else { |
| 124 | + const firstBodyCell = rows[1].querySelectorAll("td")[0]; |
| 125 | + Table.setTabIndex(firstBodyCell, 0); |
| 126 | + this.focus(firstBodyCell); |
| 127 | + } |
| 128 | + } else { |
| 129 | + const firstRowCell = rows[rowIndex].querySelectorAll("th, td")[0]; |
| 130 | + Table.setTabIndex(firstRowCell, 0); |
| 131 | + this.focus(firstRowCell); |
| 132 | + } |
| 133 | + break; |
| 134 | + case "End": |
| 135 | + const lastRow = rows[rows.length - 1].querySelectorAll("td"); |
| 136 | + Table.setTabIndex(element, -1); |
| 137 | + if (event.ctrlKey) { |
| 138 | + this.columnIndex = lastRow.length - 1; |
| 139 | + Table.setTabIndex(lastRow[this.columnIndex], 0); |
| 140 | + this.focus(lastRow[this.columnIndex]); |
| 141 | + } else { |
| 142 | + const currentRow = rows[rowIndex].querySelectorAll("th, td"); |
| 143 | + this.columnIndex = currentRow.length - 1; |
| 144 | + Table.setTabIndex(currentRow[this.columnIndex], 0); |
| 145 | + this.focus(currentRow[this.columnIndex]); |
| 146 | + } |
| 147 | + break; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @HostListener("click", ["$event"]) |
| 152 | + onClick() { |
| 153 | + if (!this.ibmDataGridFocus) { |
| 154 | + return; |
| 155 | + } |
| 156 | + const focusElementList = getFocusElementList(this.elementRef.nativeElement.closest("table"), tabbableSelectorIgnoreTabIndex); |
| 157 | + focusElementList.forEach(element => Table.setTabIndex(element, -1)); |
| 158 | + Table.setTabIndex(this.elementRef.nativeElement, 0); |
| 159 | + this.focus(this.elementRef.nativeElement); |
| 160 | + } |
| 161 | +} |
0 commit comments