-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathListItem.js
325 lines (272 loc) · 6.78 KB
/
ListItem.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
import { isSpace, isEnter } from "@ui5/webcomponents-base/dist/Keys.js";
import "@ui5/webcomponents-icons/dist/icons/decline.js";
import "@ui5/webcomponents-icons/dist/icons/edit.js";
import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
import ListItemType from "./types/ListItemType.js";
import ListMode from "./types/ListMode.js";
import ListItemBase from "./ListItemBase.js";
import RadioButton from "./RadioButton.js";
import CheckBox from "./CheckBox.js";
import Button from "./Button.js";
import { DELETE, ARIA_LABEL_LIST_ITEM_CHECKBOX } from "./generated/i18n/i18n-defaults.js";
// Styles
import styles from "./generated/themes/ListItem.css.js";
/**
* @public
*/
const metadata = {
languageAware: true,
properties: /** @lends sap.ui.webcomponents.main.ListItem.prototype */ {
/**
* Defines the visual indication and behavior of the list items.
* Available options are <code>Active</code> (by default), <code>Inactive</code> and <code>Detail</code>.
* <br><br>
* <b>Note:</b> When set to <code>Active</code>, the item will provide visual response upon press and hover,
* while with type <code>Inactive</code> and <code>Detail</code> - will not.
*
* @type {ListItemType}
* @defaultvalue "Active"
* @public
*/
type: {
type: ListItemType,
defaultValue: ListItemType.Active,
},
/**
* Indicates if the list item is active, e.g pressed down with the mouse or the keyboard keys.
*
* @type {boolean}
* @private
*/
active: {
type: Boolean,
},
/**
* Indicates if the list item is actionable, e.g has hover and pressed effects.
*
* @type {boolean}
* @private
*/
actionable: {
type: Boolean,
},
/**
* Used to define the role of the list item.
*
* @private
* @type {String}
* @defaultvalue "option"
* @since 1.0.0-rc.9
*
*/
role: {
type: String,
defaultValue: "option",
},
_mode: {
type: ListMode,
defaultValue: ListMode.None,
noAttribute: true,
},
},
events: /** @lends sap.ui.webcomponents.main.ListItem.prototype */ {
/**
* Fired when the user clicks on the detail button when type is <code>Detail</code>.
*
* @event sap.ui.webcomponents.main.ListItem#detail-click
* @public
*/
"detail-click": {},
_press: {},
_focused: {},
"_selection-requested": {},
},
};
/**
* @class
* A class to serve as a base
* for the <code>StandardListItem</code> and <code>CustomListItem</code> classes.
*
* @constructor
* @author SAP SE
* @alias sap.ui.webcomponents.main.ListItem
* @extends ListItemBase
* @public
*/
class ListItem extends ListItemBase {
static get metadata() {
return metadata;
}
static get styles() {
return [ListItemBase.styles, styles];
}
static get dependencies() {
return [
Button,
RadioButton,
CheckBox,
];
}
constructor() {
super();
this.deactivateByKey = event => {
if (isEnter(event)) {
this.deactivate();
}
};
this.deactivate = () => {
if (this.active) {
this.active = false;
}
};
this.i18nBundle = getI18nBundle("@ui5/webcomponents");
}
onBeforeRendering(...params) {
this.actionable = (this.type === ListItemType.Active) && (this._mode !== ListMode.Delete);
}
onEnterDOM() {
document.addEventListener("mouseup", this.deactivate);
document.addEventListener("touchend", this.deactivate);
document.addEventListener("keyup", this.deactivateByKey);
}
onExitDOM() {
document.removeEventListener("mouseup", this.deactivate);
document.removeEventListener("keyup", this.deactivateByKey);
document.removeEventListener("touchend", this.deactivate);
}
_onkeydown(event) {
super._onkeydown(event);
const itemActive = this.type === ListItemType.Active;
if (isSpace(event)) {
event.preventDefault();
}
if ((isSpace(event) || isEnter(event)) && itemActive) {
this.activate();
}
if (isEnter(event)) {
this.fireItemPress(event);
}
}
_onkeyup(event) {
if (isSpace(event) || isEnter(event)) {
this.deactivate();
}
if (isSpace(event)) {
this.fireItemPress(event);
}
}
_onmousedown(event) {
if (event.isMarked === "button") {
return;
}
this.activate();
}
_onmouseup(event) {
if (event.isMarked === "button") {
return;
}
this.deactivate();
}
_ontouchstart(event) {
this._onmousedown(event);
}
_ontouchend(event) {
this._onmouseup(event);
}
_onfocusout() {
super._onfocusout();
this.deactivate();
}
_onclick(event) {
if (event.isMarked === "button") {
return;
}
this.fireItemPress(event);
}
/*
* Called when selection components in Single (ui5-radiobutton)
* and Multi (ui5-checkbox) selection modes are used.
*/
onMultiSelectionComponentPress(event) {
if (this.isInactive) {
return;
}
this.fireEvent("_selection-requested", { item: this, selected: event.target.checked, selectionComponentPressed: true });
}
onSingleSelectionComponentPress(event) {
if (this.isInactive) {
return;
}
this.fireEvent("_selection-requested", { item: this, selected: !event.target.selected, selectionComponentPressed: true });
}
activate() {
if (this.type === ListItemType.Active) {
this.active = true;
}
}
onDelete(event) {
this.fireEvent("_selection-requested", { item: this, selectionComponentPressed: false });
}
onDetailClick(event) {
this.fireEvent("detail-click", { item: this, selected: this.selected });
}
fireItemPress(event) {
if (this.isInactive) {
return;
}
this.fireEvent("_press", { item: this, selected: this.selected, key: event.key });
}
get isInactive() {
return this.type === ListItemType.Inactive || this.type === ListItemType.Detail;
}
get placeSelectionElementBefore() {
return this._mode === ListMode.MultiSelect
|| this._mode === ListMode.SingleSelectBegin;
}
get placeSelectionElementAfter() {
return !this.placeSelectionElementBefore
&& (this._mode === ListMode.SingleSelectEnd || this._mode === ListMode.Delete);
}
get modeSingleSelect() {
return [
ListMode.SingleSelectBegin,
ListMode.SingleSelectEnd,
ListMode.SingleSelect,
].includes(this._mode);
}
get modeMultiSelect() {
return this._mode === ListMode.MultiSelect;
}
get modeDelete() {
return this._mode === ListMode.Delete;
}
get typeDetail() {
return this.type === ListItemType.Detail;
}
get typeActive() {
return this.type === ListItemType.Active;
}
get ariaSelected() {
if (this.modeMultiSelect || this.modeSingleSelect) {
return this.selected;
}
return undefined;
}
get deleteText() {
return this.i18nBundle.getText(DELETE);
}
get _accInfo() {
return {
role: this.role,
ariaExpanded: undefined,
ariaLevel: undefined,
ariaLabel: this.i18nBundle.getText(ARIA_LABEL_LIST_ITEM_CHECKBOX),
};
}
static async onDefine() {
await Promise.all([
fetchI18nBundle("@ui5/webcomponents"),
]);
}
}
export default ListItem;