1 import { DataGridCoordinates } from "../types"
2 import { generateCellId } from "../utils"
3
4 export class DataGridQueryTool {
5 private container: HTMLElement | null
6
7 constructor(container: HTMLElement | null) {
8 this.container = container
9 }
10
11 getInput(cell: DataGridCoordinates) {
12 const id = this._getCellId(cell)
13
14 const input = this.container?.querySelector(`[data-cell-id="${id}"]`)
15
16 if (!input) {
17 return null
18 }
19
20 return input as HTMLElement
21 }
22
23 getInputByField(field: string) {
24 const input = this.container?.querySelector(`[data-field="${field}"]`)
25
26 if (!input) {
27 return null
28 }
29
30 return input as HTMLElement
31 }
32
33 getCoordinatesByField(field: string): DataGridCoordinates | null {
34 const cell = this.container?.querySelector(
35 `[data-field="${field}"][data-cell-id]`
36 )
37
38 if (!cell) {
39 return null
40 }
41
42 const cellId = cell.getAttribute("data-cell-id")
43
44 if (!cellId) {
45 return null
46 }
47
48 const [row, col] = cellId.split(":").map((n) => parseInt(n, 10))
49
50 if (isNaN(row) || isNaN(col)) {
51 return null
52 }
53
54 return { row, col }
55 }
56
57 getContainer(cell: DataGridCoordinates) {
58 const id = this._getCellId(cell)
59
60 const container = this.container?.querySelector(
61 `[data-container-id="${id}"]`
62 )
63
64 if (!container) {
65 return null
66 }
67
68 return container as HTMLElement
69 }
70
71 private _getCellId(cell: DataGridCoordinates): string {
72 return generateCellId(cell)
73 }
74 }