UNPKG

5.43 kBPlain TextView Raw
1import {Bean, Autowired, PostConstruct} from "./context/context";
2import {EventService} from "./eventService";
3import {Column} from "./entities/column";
4import {CellFocusedEvent, Events} from "./events";
5import {GridOptionsWrapper} from "./gridOptionsWrapper";
6import {ColumnApi} from "./columnController/columnApi";
7import {ColumnController} from "./columnController/columnController";
8import {Utils as _} from "./utils";
9import {GridCell} from "./entities/gridCell";
10import {RowNode} from "./entities/rowNode";
11import {GridApi} from "./gridApi";
12import {CellComp} from "./rendering/cellComp";
13
14@Bean('focusedCellController')
15export 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 // we check if the browser is focusing something, and if it is, and
46 // it's the cell we think is focused, then return the cell. so this
47 // methods returns the cell if a) we think it has focus and b) the
48 // browser thinks it has focus. this then returns nothing if we
49 // first focus a cell, then second click outside the grid, as then the
50 // grid cell will still be focused as far as the grid is concerned,
51 // however the browser focus will have moved somewhere else.
52 public getFocusCellToUseAfterRefresh(): GridCell {
53 if (this.gridOptionsWrapper.isSuppressFocusAfterRefresh()) {
54 return null;
55 }
56
57 if (!this.focusedCell) {
58 return null;
59 }
60
61 // we check that the browser is actually focusing on the grid, if it is not, then
62 // we have nothing to worry about
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