UNPKG

10 kBPlain TextView Raw
1import {Bean, Autowired} from "./context/context";
2import {Constants} from "./constants";
3import {ColumnController} from "./columnController/columnController";
4import {IRowModel} from "./interfaces/iRowModel";
5import {Utils as _} from "./utils";
6import {GridRow} from "./entities/gridRow";
7import {GridCell, GridCellDef} from "./entities/gridCell";
8import {GridOptionsWrapper} from "./gridOptionsWrapper";
9import {PinnedRowModel} from "./rowModels/pinnedRowModel";
10import {RowNode} from "./entities/rowNode";
11import {Column} from "./entities/column";
12
13@Bean('cellNavigationService')
14export class CellNavigationService {
15
16 @Autowired('columnController') private columnController: ColumnController;
17 @Autowired('rowModel') private rowModel: IRowModel;
18 @Autowired('pinnedRowModel') private pinnedRowModel: PinnedRowModel;
19 @Autowired('gridOptionsWrapper') private gridOptionsWrapper: GridOptionsWrapper;
20
21 // returns null if no cell to focus on, ie at the end of the grid
22 public getNextCellToFocus(key: any, lastCellToFocus: GridCell): GridCell {
23
24 // starting with the provided cell, we keep moving until we find a cell we can
25 // focus on.
26 let pointer = lastCellToFocus;
27 let finished = false;
28
29 // finished will be true when either:
30 // a) cell found that we can focus on
31 // b) run out of cells (ie the method returns null)
32 while (!finished) {
33
34 switch (key) {
35 case Constants.KEY_UP :
36 pointer = this.getCellAbove(pointer);
37 break;
38 case Constants.KEY_DOWN :
39 pointer = this.getCellBelow(pointer);
40 break;
41 case Constants.KEY_RIGHT :
42 if (this.gridOptionsWrapper.isEnableRtl()) {
43 pointer = this.getCellToLeft(pointer);
44 } else {
45 pointer = this.getCellToRight(pointer);
46 }
47 break;
48 case Constants.KEY_LEFT :
49 if (this.gridOptionsWrapper.isEnableRtl()) {
50 pointer = this.getCellToRight(pointer);
51 } else {
52 pointer = this.getCellToLeft(pointer);
53 }
54 break;
55 default : console.log('ag-Grid: unknown key for navigation ' + key);
56 pointer = null;
57 break;
58 }
59
60 if (pointer) {
61 finished = this.isCellGoodToFocusOn(pointer);
62 } else {
63 finished = true;
64 }
65 }
66
67 return pointer;
68 }
69
70 private isCellGoodToFocusOn(gridCell: GridCell): boolean {
71 let column: Column = gridCell.column;
72 let rowNode: RowNode;
73
74 switch (gridCell.floating) {
75 case Constants.PINNED_TOP:
76 rowNode = this.pinnedRowModel.getPinnedTopRow(gridCell.rowIndex);
77 break;
78 case Constants.PINNED_BOTTOM:
79 rowNode = this.pinnedRowModel.getPinnedBottomRow(gridCell.rowIndex);
80 break;
81 default:
82 rowNode = this.rowModel.getRow(gridCell.rowIndex);
83 break;
84 }
85
86 let suppressNavigable = column.isSuppressNavigable(rowNode);
87 return !suppressNavigable;
88 }
89
90 private getCellToLeft(lastCell: GridCell): GridCell {
91 let colToLeft = this.columnController.getDisplayedColBefore(lastCell.column);
92 if (!colToLeft) {
93 return null;
94 } else {
95 let gridCellDef = <GridCellDef> {rowIndex: lastCell.rowIndex, column: colToLeft, floating: lastCell.floating};
96 return new GridCell(gridCellDef);
97 }
98 }
99
100 private getCellToRight(lastCell: GridCell): GridCell {
101 let colToRight = this.columnController.getDisplayedColAfter(lastCell.column);
102 // if already on right, do nothing
103 if (!colToRight) {
104 return null;
105 } else {
106 let gridCellDef = <GridCellDef> {rowIndex: lastCell.rowIndex, column: colToRight, floating: lastCell.floating};
107 return new GridCell(gridCellDef);
108 }
109 }
110
111 public getRowBelow(lastRow: GridRow): GridRow {
112 // if already on top row, do nothing
113 if (this.isLastRowInContainer(lastRow)) {
114
115 if (lastRow.isFloatingBottom()) {
116 return null;
117 } else if (lastRow.isNotFloating()) {
118 if (this.pinnedRowModel.isRowsToRender(Constants.PINNED_BOTTOM)) {
119 return new GridRow(0, Constants.PINNED_BOTTOM);
120 } else {
121 return null;
122 }
123 } else {
124 if (this.rowModel.isRowsToRender()) {
125 return new GridRow(0, null);
126 } else if (this.pinnedRowModel.isRowsToRender(Constants.PINNED_BOTTOM)) {
127 return new GridRow(0, Constants.PINNED_BOTTOM);
128 } else {
129 return null;
130 }
131 }
132
133 } else {
134 return new GridRow(lastRow.rowIndex + 1, lastRow.floating);
135 }
136
137 }
138
139 private getCellBelow(lastCell: GridCell): GridCell {
140 let rowBelow = this.getRowBelow(lastCell.getGridRow());
141 if (rowBelow) {
142 let gridCellDef = <GridCellDef> {rowIndex: rowBelow.rowIndex, column: lastCell.column, floating: rowBelow.floating};
143 return new GridCell(gridCellDef);
144 } else {
145 return null;
146 }
147 }
148
149 private isLastRowInContainer(gridRow: GridRow): boolean {
150 if (gridRow.isFloatingTop()) {
151 let lastTopIndex = this.pinnedRowModel.getPinnedTopRowData().length - 1;
152 return lastTopIndex <= gridRow.rowIndex;
153 } else if (gridRow.isFloatingBottom()) {
154 let lastBottomIndex = this.pinnedRowModel.getPinnedBottomRowData().length - 1;
155 return lastBottomIndex <= gridRow.rowIndex;
156 } else {
157 let lastBodyIndex = this.rowModel.getPageLastRow();
158 return lastBodyIndex <= gridRow.rowIndex;
159 }
160 }
161
162 private getRowAbove(lastRow: GridRow): GridRow {
163 // if already on top row, do nothing
164 if (lastRow.rowIndex === 0) {
165 if (lastRow.isFloatingTop()) {
166 return null;
167 } else if (lastRow.isNotFloating()) {
168 if (this.pinnedRowModel.isRowsToRender(Constants.PINNED_TOP)) {
169 return this.getLastFloatingTopRow();
170 } else {
171 return null;
172 }
173 } else {
174 // last floating bottom
175 if (this.rowModel.isRowsToRender()) {
176 return this.getLastBodyCell();
177 } else if (this.pinnedRowModel.isRowsToRender(Constants.PINNED_TOP)) {
178 return this.getLastFloatingTopRow();
179 } else {
180 return null;
181 }
182 }
183
184 } else {
185 return new GridRow(lastRow.rowIndex - 1, lastRow.floating);
186 }
187
188 }
189
190 private getCellAbove(lastCell: GridCell): GridCell {
191 let rowAbove = this.getRowAbove(lastCell.getGridRow());
192 if (rowAbove) {
193 let gridCellDef = <GridCellDef> {rowIndex: rowAbove.rowIndex, column: lastCell.column, floating: rowAbove.floating};
194 return new GridCell(gridCellDef);
195 } else {
196 return null;
197 }
198 }
199
200 private getLastBodyCell(): GridRow {
201 let lastBodyRow = this.rowModel.getPageLastRow();
202 return new GridRow(lastBodyRow, null);
203 }
204
205 private getLastFloatingTopRow(): GridRow {
206 let lastFloatingRow = this.pinnedRowModel.getPinnedTopRowData().length - 1;
207 return new GridRow(lastFloatingRow, Constants.PINNED_TOP);
208 }
209
210 public getNextTabbedCell(gridCell: GridCell, backwards: boolean): GridCell {
211 if (backwards) {
212 return this.getNextTabbedCellBackwards(gridCell);
213 } else {
214 return this.getNextTabbedCellForwards(gridCell);
215 }
216 }
217
218 public getNextTabbedCellForwards(gridCell: GridCell): GridCell {
219
220 let displayedColumns = this.columnController.getAllDisplayedColumns();
221
222 let newRowIndex = gridCell.rowIndex;
223 let newFloating = gridCell.floating;
224
225 // move along to the next cell
226 let newColumn = this.columnController.getDisplayedColAfter(gridCell.column);
227
228 // check if end of the row, and if so, go forward a row
229 if (!newColumn) {
230 newColumn = displayedColumns[0];
231
232 let rowBelow = this.getRowBelow(gridCell.getGridRow());
233 if (_.missing(rowBelow)) {
234 return;
235 }
236 newRowIndex = rowBelow.rowIndex;
237 newFloating = rowBelow.floating;
238 }
239
240 let gridCellDef = <GridCellDef> {rowIndex: newRowIndex, column: newColumn, floating: newFloating};
241 return new GridCell(gridCellDef);
242 }
243
244 public getNextTabbedCellBackwards(gridCell: GridCell): GridCell {
245
246 let displayedColumns = this.columnController.getAllDisplayedColumns();
247
248 let newRowIndex = gridCell.rowIndex;
249 let newFloating = gridCell.floating;
250
251 // move along to the next cell
252 let newColumn = this.columnController.getDisplayedColBefore(gridCell.column);
253
254 // check if end of the row, and if so, go forward a row
255 if (!newColumn) {
256 newColumn = displayedColumns[displayedColumns.length - 1];
257
258 let rowAbove = this.getRowAbove(gridCell.getGridRow());
259 if (_.missing(rowAbove)) {
260 return;
261 }
262 newRowIndex = rowAbove.rowIndex;
263 newFloating = rowAbove.floating;
264 }
265
266 let gridCellDef = <GridCellDef> {rowIndex: newRowIndex, column: newColumn, floating: newFloating};
267 return new GridCell(gridCellDef);
268 }
269
270}
\No newline at end of file