-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathgrid.ts
More file actions
304 lines (252 loc) · 8.66 KB
/
grid.ts
File metadata and controls
304 lines (252 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {_IdGenerator} from '@angular/cdk/a11y';
import {
afterRenderEffect,
booleanAttribute,
computed,
contentChild,
contentChildren,
Directive,
ElementRef,
inject,
input,
model,
Signal,
} from '@angular/core';
import {Directionality} from '@angular/cdk/bidi';
import {
GridPattern,
GridRowPattern,
GridCellPattern,
GridCellWidgetPattern,
GridFocusMode,
GridWrapStrategy,
GridSelectionMode,
} from '../private';
export {GridFocusMode, GridWrapStrategy, GridSelectionMode};
/**
* A directive that provides grid-based navigation and selection behavior.
*
* @developerPreview 21.0
*/
@Directive({
selector: '[ngGrid]',
exportAs: 'ngGrid',
host: {
'class': 'grid',
'role': 'grid',
'[tabindex]': '_pattern.tabIndex()',
'[attr.aria-disabled]': '_pattern.disabled()',
'[attr.aria-activedescendant]': '_pattern.activeDescendant()',
'(keydown)': '_pattern.onKeydown($event)',
'(pointerdown)': '_pattern.onPointerdown($event)',
'(pointermove)': '_pattern.onPointermove($event)',
'(pointerup)': '_pattern.onPointerup($event)',
'(focusin)': '_pattern.onFocusIn()',
'(focusout)': '_pattern.onFocusOut($event)',
},
})
export class Grid {
/** A reference to the host element. */
private readonly _elementRef = inject(ElementRef);
/** The rows that make up the grid. */
private readonly _rows = contentChildren(GridRow, {descendants: true});
/** The UI patterns for the rows in the grid. */
private readonly _rowPatterns: Signal<GridRowPattern[]> = computed(() =>
this._rows().map(r => r._pattern),
);
/** Text direction. */
readonly textDirection = inject(Directionality).valueSignal;
/** The host native element. */
readonly element = computed(() => this._elementRef.nativeElement);
/** Whether selection is enabled for the grid. */
readonly enableSelection = input(false, {transform: booleanAttribute});
/** Whether the grid is disabled. */
readonly disabled = input(false, {transform: booleanAttribute});
/** Whether to allow disabled items to receive focus. */
readonly softDisabled = input(true, {transform: booleanAttribute});
/** The focus strategy used by the grid. */
readonly focusMode = input<GridFocusMode>('roving');
/** The wrapping behavior for keyboard navigation along the row axis. */
readonly rowWrap = input<GridWrapStrategy>('loop');
/** The wrapping behavior for keyboard navigation along the column axis. */
readonly colWrap = input<GridWrapStrategy>('loop');
/** Whether multiple cells in the grid can be selected. */
readonly multi = input(false, {transform: booleanAttribute});
/** The selection strategy used by the grid. */
readonly selectionMode = input<GridSelectionMode>('follow');
/** Whether enable range selections (with modifier keys or dragging). */
readonly enableRangeSelection = input(false, {transform: booleanAttribute});
/** The UI pattern for the grid. */
readonly _pattern = new GridPattern({
...this,
rows: this._rowPatterns,
getCell: e => this._getCell(e),
});
constructor() {
afterRenderEffect(() => this._pattern.setDefaultStateEffect());
afterRenderEffect(() => this._pattern.resetStateEffect());
afterRenderEffect(() => this._pattern.focusEffect());
}
/** Gets the cell pattern for a given element. */
private _getCell(element: Element): GridCellPattern | undefined {
const cellElement = element.closest('[ngGridCell]');
if (cellElement === undefined) return;
const widgetElement = element.closest('[ngGridCellWidget]');
for (const row of this._rowPatterns()) {
for (const cell of row.inputs.cells()) {
if (
cell.element() === cellElement ||
(widgetElement !== undefined && cell.element() === widgetElement)
) {
return cell;
}
}
}
return;
}
}
export type GridRowRole = 'row' | 'rowheader';
/**
* A directive that represents a row in a grid.
*
* @developerPreview 21.0
*/
@Directive({
selector: '[ngGridRow]',
exportAs: 'ngGridRow',
host: {
'class': 'grid-row',
'[attr.role]': 'role()',
},
})
export class GridRow {
/** A reference to the host element. */
private readonly _elementRef = inject(ElementRef);
/** The cells that make up this row. */
private readonly _cells = contentChildren(GridCell, {descendants: true});
/** The UI patterns for the cells in this row. */
private readonly _cellPatterns: Signal<GridCellPattern[]> = computed(() =>
this._cells().map(c => c._pattern),
);
/** The parent grid. */
private readonly _grid = inject(Grid);
/** The parent grid UI pattern. */
readonly grid = computed(() => this._grid._pattern);
/** The host native element. */
readonly element = computed(() => this._elementRef.nativeElement);
/** The ARIA role for the row. */
readonly role = input<GridRowRole>('row');
/** The index of this row within the grid. */
readonly rowIndex = input<number>();
/** The UI pattern for the grid row. */
readonly _pattern = new GridRowPattern({
...this,
cells: this._cellPatterns,
});
}
export type GridCellRole = 'gridcell' | 'columnheader';
/**
* A directive that represents a cell in a grid.
*
* @developerPreview 21.0
*/
@Directive({
selector: '[ngGridCell]',
exportAs: 'ngGridCell',
host: {
'class': 'grid-cell',
'[attr.role]': 'role()',
'[attr.id]': '_pattern.id()',
'[attr.rowspan]': '_pattern.rowSpan()',
'[attr.colspan]': '_pattern.colSpan()',
'[attr.data-active]': '_pattern.active()',
'[attr.data-anchor]': '_pattern.anchor()',
'[attr.aria-disabled]': '_pattern.disabled()',
'[attr.aria-rowspan]': '_pattern.rowSpan()',
'[attr.aria-colspan]': '_pattern.colSpan()',
'[attr.aria-rowindex]': '_pattern.ariaRowIndex()',
'[attr.aria-colindex]': '_pattern.ariaColIndex()',
'[attr.aria-selected]': '_pattern.ariaSelected()',
'[tabindex]': '_pattern.tabIndex()',
},
})
export class GridCell {
/** A reference to the host element. */
private readonly _elementRef = inject(ElementRef);
/** The widget contained within this cell, if any. */
private readonly _widgets = contentChild(GridCellWidget);
/** The UI pattern for the widget in this cell. */
private readonly _widgetPattern: Signal<GridCellWidgetPattern | undefined> = computed(
() => this._widgets()?._pattern,
);
/** The parent row. */
private readonly _row = inject(GridRow);
/** A unique identifier for the cell. */
private readonly _id = inject(_IdGenerator).getId('ng-grid-cell-', true);
/** The host native element. */
readonly element = computed(() => this._elementRef.nativeElement);
/** The ARIA role for the cell. */
readonly role = input<GridCellRole>('gridcell');
/** The number of rows the cell should span. */
readonly rowSpan = input<number>(1);
/** The number of columns the cell should span. */
readonly colSpan = input<number>(1);
/** The index of this cell's row within the grid. */
readonly rowIndex = input<number>();
/** The index of this cell's column within the grid. */
readonly colIndex = input<number>();
/** Whether the cell is disabled. */
readonly disabled = input(false, {transform: booleanAttribute});
/** Whether the cell is selected. */
readonly selected = model<boolean>(false);
/** Whether the cell is selectable. */
readonly selectable = input<boolean>(true);
/** The UI pattern for the grid cell. */
readonly _pattern = new GridCellPattern({
...this,
id: () => this._id,
grid: this._row.grid,
row: () => this._row._pattern,
widget: this._widgetPattern,
});
}
/**
* A directive that represents a widget inside a grid cell.
*
* @developerPreview 21.0
*/
@Directive({
selector: '[ngGridCellWidget]',
exportAs: 'ngGridCellWidget',
host: {
'class': 'grid-cell-widget',
'[attr.data-active]': '_pattern.active()',
'[tabindex]': '_pattern.tabIndex()',
},
})
export class GridCellWidget {
/** A reference to the host element. */
private readonly _elementRef = inject(ElementRef);
/** The parent cell. */
private readonly _cell = inject(GridCell);
/** The host native element. */
readonly element = computed(() => this._elementRef.nativeElement);
/** Whether the widget is activated and the grid navigation should be paused. */
readonly activate = model<boolean>(false);
/** The UI pattern for the grid cell widget. */
readonly _pattern = new GridCellWidgetPattern({
...this,
cell: () => this._cell._pattern,
});
/** Focuses the widget. */
focus(): void {
this.element().focus();
}
}