-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathTableRow.ts
More file actions
277 lines (244 loc) · 7.37 KB
/
TableRow.ts
File metadata and controls
277 lines (244 loc) · 7.37 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
import { customElement, slotStrict as slot, property } from "@ui5/webcomponents-base/dist/decorators.js";
import { isEnter } from "@ui5/webcomponents-base/dist/Keys.js";
import getActiveElement from "@ui5/webcomponents-base/dist/util/getActiveElement.js";
import query from "@ui5/webcomponents-base/dist/decorators/query.js";
import { toggleAttribute } from "./TableUtils.js";
import TableRowTemplate from "./TableRowTemplate.js";
import TableRowBase from "./TableRowBase.js";
import TableRowCss from "./generated/themes/TableRow.css.js";
import type TableCell from "./TableCell.js";
import type TableRowActionBase from "./TableRowActionBase.js";
import type Button from "./Button.js";
import type { UI5CustomEvent } from "@ui5/webcomponents-base";
import type { Slot, DefaultSlot } from "@ui5/webcomponents-base/dist/UI5Element.js";
import {
TABLE_ROW_MULTIPLE_ACTIONS, TABLE_ROW_SINGLE_ACTION,
} from "./generated/i18n/i18n-defaults.js";
/**
* @class
*
* ### Overview
*
* The `ui5-table-row` component represents a row in the `ui5-table`.
*
* ### ES6 Module Import
*
* `import "@ui5/webcomponents/dist/TableRow.js";`
*
* @constructor
* @extends TableRowBase
* @since 2.0.0
* @public
*/
@customElement({
tag: "ui5-table-row",
styles: [TableRowBase.styles, TableRowCss],
template: TableRowTemplate,
})
class TableRow extends TableRowBase<TableCell> {
/**
* Defines the cells of the component.
*
* **Note:** Use `ui5-table-cell` for the intended design.
*
* @public
*/
@slot({
type: HTMLElement,
"default": true,
individualSlots: true,
invalidateOnChildChange: {
properties: ["merged", "_popin", "_popinHidden"],
slots: false,
},
})
cells!: DefaultSlot<TableCell>;
/**
* Defines the actions of the component.
*
* **Note:** Use `ui5-table-row-action` or `ui5-table-row-action-navigation` for the intended design.
*
* @since 2.7.0
* @public
*/
@slot({
type: HTMLElement,
individualSlots: true,
})
actions!: Slot<TableRowActionBase>;
/**
* Unique identifier of the row.
*
* **Note:** For selection features to work properly, this property is mandatory, and its value must not contain spaces.
*
* @default undefined
* @public
*/
@property()
rowKey?: string;
/**
* Defines the 0-based position of the row related to the total number of rows within the table when the `ui5-table-virtualizer` feature is used.
*
* @default undefined
* @since 2.5.0
* @public
*/
@property({ type: Number })
position?: number;
/**
* Defines the interactive state of the row.
*
* @default false
* @public
*/
@property({ type: Boolean })
interactive = false;
/**
* Defines the navigated state of the row.
*
* @default false
* @public
*/
@property({ type: Boolean })
navigated = false;
/**
* Defines whether the row is movable.
*
* @default false
* @since 2.6.0
* @public
*/
@property({ type: Boolean })
movable = false;
@query("#popin-cell")
_popinCell?: TableCell;
@query("#actions-cell")
_actionsCell?: TableCell;
onBeforeRendering() {
super.onBeforeRendering();
this.ariaRowIndex = (this.role === "row") ? `${this._rowIndex + 2}` : null;
toggleAttribute(this, "draggable", this.movable, "true");
toggleAttribute(this, "_interactive", this._isInteractive);
toggleAttribute(this, "_alternate", this._alternate);
}
async _onpointerdown(e: PointerEvent) {
if (e.button !== 0 || !this._isInteractive) {
return;
}
const composedPath = e.composedPath();
composedPath.splice(composedPath.indexOf(this));
await new Promise(resolve => setTimeout(resolve)); // wait for the focus to be set
const activeElement = getActiveElement() as Element;
if (!composedPath.includes(activeElement)) {
this._setActive("pointerup");
}
}
_onkeydown(e: KeyboardEvent, eventOrigin: HTMLElement) {
super._onkeydown(e, eventOrigin);
if (e.defaultPrevented) {
return;
}
if (eventOrigin === this && this._isInteractive && isEnter(e)) {
this._setActive("keyup");
this._onclick();
}
}
_onclick() {
if (this === getActiveElement()) {
if (this._isSelectable && !this._hasSelector) {
this._onSelectionChange();
} else if (this.interactive || this._isNavigable) {
this._table?._onRowClick(this);
}
}
}
_setActive(deactivationEvent: string) {
this.toggleAttribute("_active", true);
document.addEventListener(deactivationEvent, () => {
this.removeAttribute("_active");
}, { once: true });
}
_onOverflowButtonClick(e: UI5CustomEvent<Button, "click">) {
const ctor = this.actions[0].constructor as typeof TableRowActionBase;
ctor.showMenu(this._overflowActions, e.target as HTMLElement);
e.stopPropagation();
}
get _isInteractive() {
return this.interactive || (this._isSelectable && !this._hasSelector) || this._isNavigable;
}
get _isNavigable() {
return this._fixedActions.find(action => {
return action.hasAttribute("ui5-table-row-action-navigation") && !action.invisible && !action._isInteractive;
}) !== undefined;
}
get _rowIndex() {
if (this.position !== undefined) {
return this.position;
}
if (this._table) {
return this._table.rows.indexOf(this);
}
return -1;
}
get _hasOverflowActions() {
let renderableActionsCount = 0;
return this.actions.some(action => {
if (action.isFixedAction() || !action.invisible) {
renderableActionsCount++;
}
return renderableActionsCount > this._rowActionCount;
});
}
get _flexibleActions() {
const flexibleActions = this.actions.filter(action => !action.isFixedAction());
const fixedActionsCount = this.actions.length - flexibleActions.length;
let maxFlexibleActionsCount = this._rowActionCount - fixedActionsCount;
if (maxFlexibleActionsCount < 1) {
return []; // fixed actions occupy all the available space
}
if (flexibleActions.length <= maxFlexibleActionsCount) {
return flexibleActions; // all actions fit the available space
}
const visibleFlexibleActions = flexibleActions.filter(action => !action.invisible);
if (visibleFlexibleActions.length > maxFlexibleActionsCount) {
maxFlexibleActionsCount--; // preserve space for the overflow button
}
return visibleFlexibleActions.slice(0, maxFlexibleActionsCount);
}
get _fixedActions() {
let maxFixedActionsCount = this._rowActionCount;
if (this._hasOverflowActions) {
maxFixedActionsCount--;
}
const fixedActions = this.actions.filter(action => action.isFixedAction());
return fixedActions.slice(0, maxFixedActionsCount);
}
get _overflowActions() {
const fixedActions = this._fixedActions;
const flexibleActions = this._flexibleActions;
const overflowActions: Array<TableRowActionBase> = [];
this.actions.forEach(action => {
if (!action.invisible && !fixedActions.includes(action) && !flexibleActions.includes(action)) {
overflowActions.push(action);
}
});
return overflowActions;
}
get _availableActionsCount() {
if (this._rowActionCount < 1) {
return 0;
}
return [...this._flexibleActions, ...this._fixedActions].filter(action => {
return !action.invisible && action._isInteractive;
}).length + (this._hasOverflowActions ? 1 : 0);
}
get _actionCellAccText() {
const availableActionsCount = this._availableActionsCount;
if (availableActionsCount > 0) {
const bundleKey = availableActionsCount === 1 ? TABLE_ROW_SINGLE_ACTION : TABLE_ROW_MULTIPLE_ACTIONS;
return TableRowBase.i18nBundle.getText(bundleKey, availableActionsCount);
}
}
}
TableRow.define();
export default TableRow;