-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathTableRow.js
388 lines (332 loc) · 8.91 KB
/
TableRow.js
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import { isSpace, isEnter } from "@ui5/webcomponents-base/dist/Keys.js";
import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import TableMode from "./types/TableMode.js";
import TableRowType from "./types/TableRowType.js";
import TableRowTemplate from "./generated/templates/TableRowTemplate.lit.js";
import { ARIA_LABEL_ROW_SELECTION } from "./generated/i18n/i18n-defaults.js";
// Styles
import styles from "./generated/themes/TableRow.css.js";
/**
* @public
*/
const metadata = {
tag: "ui5-table-row",
managedSlots: true,
slots: /** @lends sap.ui.webcomponents.main.TableRow.prototype */ {
/**
* Defines the cells of the component.
* <br><br>
* <b>Note:</b> Use <code>ui5-table-cell</code> for the intended design.
*
* @type {sap.ui.webcomponents.main.ITableCell[]}
* @slot cells
* @public
*/
"default": {
propertyName: "cells",
type: HTMLElement,
individualSlots: true,
},
},
properties: /** @lends sap.ui.webcomponents.main.TableRow.prototype */ {
/**
* Defines the mode of the row (None, SingleSelect, MultiSelect).
* @type {TableMode}
* @defaultvalue "None"
* @since 1.0.0-rc.15
* @private
*/
mode: {
type: TableMode,
defaultValue: TableMode.None,
},
/**
* Defines the visual indication and behavior of the component.
* <br><br>
* Available options are:
* <ul>
* <li><code>Active</code></li>
* <li><code>Inactive</code></li>
* <ul>
* <br><br>
* <b>Note:</b> When set to <code>Active</code>, the item will provide visual response upon press,
* while with type <code>Inactive</code> - will not.
*
* @type {TableRowType}
* @defaultvalue "Inactive"
* @since 1.0.0-rc.15
* @public
*/
type: {
type: TableRowType,
defaultValue: TableRowType.Inactive,
},
/**
* Defines the row's selected state.
*
* @type {boolean}
* @defaultvalue false
* @since 1.0.0-rc.15
* @public
*/
selected: {
type: Boolean,
},
/**
* Indicates if the table row is active.
*
* @type {boolean}
* @defaultvalue false
* @since 1.0.0-rc.15
* @private
*/
active: {
type: Boolean,
},
_columnsInfo: {
type: Object,
multiple: true,
},
_tabIndex: {
type: String,
defaultValue: "-1",
},
_busy: {
type: Boolean,
},
},
events: /** @lends sap.ui.webcomponents.main.TableRow.prototype */ {
/**
* Fired when a row in <code>Active</code> mode is clicked or <code>Enter</code> key is pressed.
*
* @event sap.ui.webcomponents.main.TableRow#row-click
* @since 1.0.0-rc.15
* @private
*/
"row-click": {},
_focused: {},
/**
* Fired on selection change of an active row.
*
* @event sap.ui.webcomponents.main.TableRow#selection-requested
* @since 1.0.0-rc.15
* @private
*/
"selection-requested": {},
},
};
/**
* @class
*
* <h3 class="comment-api-title">Overview</h3>
*
* The <code>ui5-table-row</code> component represents a row in the <code>ui5-table</code>.
*
* <h3>CSS Shadow Parts</h3>
*
* <ui5-link target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/::part">CSS Shadow Parts</ui5-link> allow developers to style elements inside the Shadow DOM.
* <br>
* The <code>ui5-table-row</code> exposes the following CSS Shadow Parts:
* <ul>
* <li>row - Used to style the native <code>tr</code> element</li>
* <li>popin-row - Used to style the <code>tr</code> element</li> when a row pops in
* </ul>
*
* @constructor
* @author SAP SE
* @alias sap.ui.webcomponents.main.TableRow
* @extends sap.ui.webcomponents.base.UI5Element
* @tagname ui5-table-row
* @implements sap.ui.webcomponents.main.ITableRow
* @public
*/
class TableRow extends UI5Element {
static get metadata() {
return metadata;
}
static get styles() {
return styles;
}
static get render() {
return litRender;
}
static get template() {
return TableRowTemplate;
}
constructor() {
super();
this.i18nBundle = getI18nBundle("@ui5/webcomponents");
}
_onmouseup() {
this.deactivate();
}
_onkeydown(event) {
const itemActive = this.type === TableRowType.Active;
const isSingleSelect = this.isSingleSelect;
const itemSelectable = isSingleSelect || this.isMultiSelect;
const isRowFocused = this._getActiveElementTagName() === "ui5-table-row";
const checkboxPressed = event.target.classList.contains("ui5-multi-select-checkbox");
if (isSpace(event) && event.target.tagName.toLowerCase() === "tr") {
event.preventDefault();
}
if (isRowFocused && !checkboxPressed) {
if ((isSpace(event) && itemSelectable) || (isEnter(event) && isSingleSelect)) {
this.fireEvent("selection-requested", { row: this });
}
if (isEnter(event) && itemActive) {
this.fireEvent("row-click", { row: this });
if (!isSingleSelect) {
this.activate();
}
}
}
}
_onkeyup(event) {
if (isSpace(event) || isEnter(event)) {
this.deactivate();
}
}
_ontouchstart(event) {
this.activate();
}
_ontouchend() {
this.deactivate();
}
_onfocusout() {
this.deactivate();
}
_onfocusin(event, forceSelfFocus = false) {
if (forceSelfFocus || this._getActiveElementTagName() === "ui5-table-cell") {
this.shadowRoot.querySelector(".ui5-table-row-root").focus();
this.activate();
}
this.fireEvent("_focused", event);
}
_onrowclick(event) {
const checkboxPressed = event.target.classList.contains("ui5-multi-select-checkbox");
// If the user tab over a button on IOS device, the document.activeElement
// is the ui5-table-row. The check below ensure that, if a button within the row is pressed,
// the row will not be selected.
if (event.isMarked === "button") {
return;
}
if (!this.contains(document.activeElement)) {
// If the user clickes on non-focusable element within the ui5-table-cell,
// the focus goes to the body, se we have to bring it back to the row.
// If the user clicks on input, button or similar clickable element,
// the focus remains on that element.
this._onfocusin(event, true /* force row focus */);
this.deactivate();
}
if (this._getActiveElementTagName() === "ui5-table-row") {
if (this.isSingleSelect) {
this._handleSelection();
}
if (this.type === TableRowType.Active && !checkboxPressed) {
this.fireEvent("row-click", { row: this });
}
}
}
_handleSelection() {
this.fireEvent("selection-requested", { row: this });
}
_getActiveElementTagName() {
return document.activeElement.localName.toLocaleLowerCase();
}
activate() {
if (this.type === TableRowType.Active) {
this.active = true;
}
}
deactivate() {
if (this.active) {
this.active = false;
}
}
get shouldPopin() {
return this._columnsInfo.filter(el => {
return el.demandPopin;
}).length;
}
get allColumnsPoppedIn() {
return this._columnsInfo.every(el => el.demandPopin && !el.visible);
}
onBeforeRendering() {
if (!this.shouldPopin) {
return;
}
this.visibleCells = [];
this.popinCells = [];
if (this.cells.length === 0) {
return;
}
const allColumnsPoppedInClass = this.allColumnsPoppedIn ? "all-columns-popped-in" : "";
this._columnsInfo.forEach((info, index) => {
const cell = this.cells[index];
if (!cell) {
return;
}
if (info.visible) {
this.visibleCells.push(cell);
cell.popined = false;
} else if (info.demandPopin) {
const popinHeaderClass = this.popinCells.length === 0 ? "popin-header" : "";
this.popinCells.push({
cell,
popinText: info.popinText,
classes: `ui5-table-popin-row ${allColumnsPoppedInClass} ${popinHeaderClass}`,
});
cell.popined = true;
} else {
cell.popined = false;
}
}, this);
const lastVisibleCell = this.visibleCells[this.visibleCells.length - 1];
if (lastVisibleCell) {
lastVisibleCell.lastInRow = true;
}
}
get visibleCellsCount() {
let visibleCellsCount = this.visibleCells.length;
if (this.isMultiSelect) {
visibleCellsCount += 1;
}
return visibleCellsCount;
}
get ariaLabelText() {
return this.cells.map((cell, index) => {
const columText = this.getColumnTextByIdx(index);
const cellText = this.getCellText(cell);
return `${columText} ${cellText}`;
}).join(" ");
}
get ariaLabelRowSelection() {
return this.i18nBundle.getText(ARIA_LABEL_ROW_SELECTION);
}
get isSingleSelect() {
return this.mode === "SingleSelect";
}
get isMultiSelect() {
return this.mode === "MultiSelect";
}
getCellText(cell) {
return this.getNormilzedTextContent(cell.textContent);
}
getColumnTextByIdx(index) {
const columnInfo = this._columnsInfo[index];
if (!columnInfo) {
return "";
}
return this.getNormilzedTextContent(columnInfo.text);
}
getNormilzedTextContent(textContent) {
return textContent.replace(/[\n\r\t]/g, "").trim();
}
static async onDefine() {
await fetchI18nBundle("@ui5/webcomponents");
}
}
TableRow.define();
export default TableRow;