1 | import {Bean, Autowired, PostConstruct} from "./context/context";
|
2 | import {EventService} from "./eventService";
|
3 | import {Column} from "./entities/column";
|
4 | import {CellFocusedEvent, Events} from "./events";
|
5 | import {GridOptionsWrapper} from "./gridOptionsWrapper";
|
6 | import {ColumnApi} from "./columnController/columnApi";
|
7 | import {ColumnController} from "./columnController/columnController";
|
8 | import {Utils as _} from "./utils";
|
9 | import {GridCell} from "./entities/gridCell";
|
10 | import {RowNode} from "./entities/rowNode";
|
11 | import {GridApi} from "./gridApi";
|
12 | import {CellComp} from "./rendering/cellComp";
|
13 |
|
14 | @Bean('focusedCellController')
|
15 | export class FocusedCellController {
|
16 |
|
17 | @Autowired('eventService') private eventService: EventService;
|
18 | @Autowired('gridOptionsWrapper') private gridOptionsWrapper: GridOptionsWrapper;
|
19 | @Autowired('columnController') private columnController: ColumnController;
|
20 | @Autowired('columnApi') private columnApi: ColumnApi;
|
21 | @Autowired('gridApi') private gridApi: GridApi;
|
22 |
|
23 | private focusedCell: GridCell;
|
24 |
|
25 | @PostConstruct
|
26 | private init(): void {
|
27 | this.eventService.addEventListener(Events.EVENT_COLUMN_PIVOT_MODE_CHANGED, this.clearFocusedCell.bind(this));
|
28 | this.eventService.addEventListener(Events.EVENT_COLUMN_EVERYTHING_CHANGED, this.clearFocusedCell.bind(this));
|
29 | this.eventService.addEventListener(Events.EVENT_COLUMN_GROUP_OPENED, this.clearFocusedCell.bind(this));
|
30 | this.eventService.addEventListener(Events.EVENT_COLUMN_MOVED, this.clearFocusedCell.bind(this));
|
31 | this.eventService.addEventListener(Events.EVENT_COLUMN_PINNED, this.clearFocusedCell.bind(this));
|
32 | this.eventService.addEventListener(Events.EVENT_COLUMN_ROW_GROUP_CHANGED, this.clearFocusedCell.bind(this));
|
33 | this.eventService.addEventListener(Events.EVENT_COLUMN_VISIBLE, this.clearFocusedCell.bind(this));
|
34 | }
|
35 |
|
36 | public clearFocusedCell(): void {
|
37 | this.focusedCell = null;
|
38 | this.onCellFocused(false);
|
39 | }
|
40 |
|
41 | public getFocusedCell(): GridCell {
|
42 | return this.focusedCell;
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | public getFocusCellToUseAfterRefresh(): GridCell {
|
53 | if (this.gridOptionsWrapper.isSuppressFocusAfterRefresh()) {
|
54 | return null;
|
55 | }
|
56 |
|
57 | if (!this.focusedCell) {
|
58 | return null;
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 | let browserFocusedCell = this.getGridCellForDomElement(document.activeElement);
|
64 | if (!browserFocusedCell) {
|
65 | return null;
|
66 | }
|
67 |
|
68 | return this.focusedCell;
|
69 | }
|
70 |
|
71 | private getGridCellForDomElement(eBrowserCell: Node): GridCell {
|
72 |
|
73 | let ePointer = eBrowserCell;
|
74 | while (ePointer) {
|
75 | let cellComp = <CellComp> this.gridOptionsWrapper.getDomData(ePointer, CellComp.DOM_DATA_KEY_CELL_COMP);
|
76 | if (cellComp) {
|
77 | return cellComp.getGridCell();
|
78 | }
|
79 | ePointer = ePointer.parentNode;
|
80 | }
|
81 |
|
82 | return null;
|
83 | }
|
84 |
|
85 | public setFocusedCell(rowIndex: number, colKey: string|Column, floating: string, forceBrowserFocus = false): void {
|
86 | let column = _.makeNull(this.columnController.getGridColumn(colKey));
|
87 | this.focusedCell = new GridCell({rowIndex: rowIndex,
|
88 | floating: _.makeNull(floating),
|
89 | column: column});
|
90 |
|
91 | this.onCellFocused(forceBrowserFocus);
|
92 | }
|
93 |
|
94 | public isCellFocused(gridCell: GridCell): boolean {
|
95 | if (_.missing(this.focusedCell)) { return false; }
|
96 | return this.focusedCell.column === gridCell.column && this.isRowFocused(gridCell.rowIndex, gridCell.floating);
|
97 | }
|
98 |
|
99 | public isRowNodeFocused(rowNode: RowNode): boolean {
|
100 | return this.isRowFocused(rowNode.rowIndex, rowNode.rowPinned);
|
101 | }
|
102 |
|
103 | public isAnyCellFocused(): boolean {
|
104 | return !!this.focusedCell;
|
105 | }
|
106 |
|
107 | public isRowFocused(rowIndex: number, floating: string): boolean {
|
108 | if (_.missing(this.focusedCell)) { return false; }
|
109 | let floatingOrNull = _.makeNull(floating);
|
110 | return this.focusedCell.rowIndex === rowIndex && this.focusedCell.floating === floatingOrNull;
|
111 | }
|
112 |
|
113 | private onCellFocused(forceBrowserFocus: boolean): void {
|
114 | let event: CellFocusedEvent = {
|
115 | type: Events.EVENT_CELL_FOCUSED,
|
116 | forceBrowserFocus: forceBrowserFocus,
|
117 | rowIndex: <number> null,
|
118 | column: <Column> null,
|
119 | floating: <string> null,
|
120 | api: this.gridApi,
|
121 | columnApi: this.columnApi,
|
122 | rowPinned: <string> null
|
123 | };
|
124 |
|
125 | if (this.focusedCell) {
|
126 | event.rowIndex = this.focusedCell.rowIndex;
|
127 | event.column = this.focusedCell.column;
|
128 | event.rowPinned = this.focusedCell.floating;
|
129 | }
|
130 |
|
131 | this.eventService.dispatchEvent(event);
|
132 | }
|
133 | } |
\ | No newline at end of file |