-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathwidget.ts
More file actions
170 lines (135 loc) · 5.53 KB
/
widget.ts
File metadata and controls
170 lines (135 loc) · 5.53 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
/**
* @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 {KeyboardEventManager, Modifier} from '../behaviors/event-manager';
import {ListNavigationItem} from '../behaviors/list-navigation/list-navigation';
import {
SignalLike,
computed,
signal,
WritableSignalLike,
} from '../behaviors/signal-like/signal-like';
import type {GridCellPattern} from './cell';
/** The inputs for the `GridCellWidgetPattern`. */
export interface GridCellWidgetInputs extends Omit<ListNavigationItem, 'index'> {
/** The `GridCellPattern` that this widget belongs to. */
cell: SignalLike<GridCellPattern>;
/** The html element that should receive focus. */
element: SignalLike<HTMLElement>;
/** The type of widget, which determines how it is activated. */
widgetType: SignalLike<'simple' | 'complex' | 'editable'>;
/** The element that will receive focus when the widget is activated. */
focusTarget: SignalLike<HTMLElement | undefined>;
}
/** The UI pattern for a widget inside a grid cell. */
export class GridCellWidgetPattern implements ListNavigationItem {
/** A unique identifier for the widget. */
readonly id: SignalLike<string> = () => this.inputs.id();
/** The html element that should receive focus. */
readonly element: SignalLike<HTMLElement> = () => this.inputs.element();
/** The element that should receive focus. */
readonly widgetHost: SignalLike<HTMLElement> = computed(
() => this.inputs.focusTarget() ?? this.element(),
);
/** The index of the widget within the cell. */
readonly index: SignalLike<number> = computed(() =>
this.inputs.cell().inputs.widgets().indexOf(this),
);
/** Whether the widget is disabled. */
readonly disabled: SignalLike<boolean> = computed(
() => this.inputs.disabled() || this.inputs.cell().disabled(),
);
/** The tab index for the widget. */
readonly tabIndex: SignalLike<-1 | 0> = computed(() => this.inputs.cell().widgetTabIndex());
/** Whether the widget is the active item in the widget list. */
readonly active: SignalLike<boolean> = computed(
() => this.inputs.cell().active() && this.inputs.cell().activeWidget() === this,
);
/** Whether the widget is currently activated. */
readonly isActivated: WritableSignalLike<boolean> = signal(false);
/** The last event that caused the widget to be activated. */
readonly lastActivateEvent: WritableSignalLike<KeyboardEvent | FocusEvent | undefined> =
signal(undefined);
/** The last event that caused the widget to be deactivated. */
readonly lastDeactivateEvent: WritableSignalLike<KeyboardEvent | FocusEvent | undefined> =
signal(undefined);
/** The keyboard event manager for the widget. */
readonly keydown = computed(() => {
const manager = new KeyboardEventManager();
// Simple widget does not need to pause default grid behaviors.
// However, it does need to capture Enter key and trigger a click on the host element
// since the browser won't do it for us in activedescendant mode.
if (this.inputs.widgetType() === 'simple') {
console.log('simple widget keydown');
manager.on('Enter', () => this.element().click());
return manager;
}
// If a widget is activated, only listen to events that exits activate state.
if (this.isActivated()) {
manager.on('Escape', e => {
this.deactivate(e);
this.focus();
});
if (this.inputs.widgetType() === 'editable') {
manager.on('Enter', e => {
this.deactivate(e);
this.focus();
});
}
return manager;
}
// Enter key is used to activate widget for both complex and editable type.
manager.on('Enter', e => this.activate(e));
if (this.inputs.widgetType() === 'editable') {
manager.on([Modifier.Shift, Modifier.None], /^[a-zA-Z0-9]$/, e => this.activate(e), {
preventDefault: false,
});
}
return manager;
});
constructor(readonly inputs: GridCellWidgetInputs) {}
/** Handles keydown events for the widget. */
onKeydown(event: KeyboardEvent): void {
if (this.disabled()) return;
console.log('keydown of widget.ts');
this.keydown().handle(event);
}
/** Handles focusin events for the widget. */
onFocusIn(event: FocusEvent): void {
// Simple widget does not have activate state.
if (this.inputs.widgetType() === 'simple') return;
// Set activate state if the focus is inside of widget.
const focusTarget = event.target as Element;
if (this.widgetHost().contains(focusTarget) && this.widgetHost() !== focusTarget) {
this.activate(event);
}
}
/** Handles focusout events for the widget. */
onFocusOut(event: FocusEvent): void {
const focusTarget = event.relatedTarget as Element;
if (this.widgetHost().contains(focusTarget)) return;
// Reset states when focus leaving widget.
this.deactivate(event);
}
/** Focuses the widget's host element. */
focus(): void {
this.widgetHost().focus();
}
/** Activates the widget. */
activate(event?: KeyboardEvent | FocusEvent): void {
if (this.isActivated()) return;
if (this.inputs.widgetType() === 'simple') return;
this.isActivated.set(true);
this.lastActivateEvent.set(event);
}
/** Deactivates the widget and restores focus to the widget's host element. */
deactivate(event?: KeyboardEvent | FocusEvent): void {
if (!this.isActivated()) return;
this.isActivated.set(false);
this.lastDeactivateEvent.set(event);
}
}