-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathInputSuggestions.js
433 lines (345 loc) · 10.8 KB
/
InputSuggestions.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import { registerFeature } from "@ui5/webcomponents-base/dist/FeaturesRegistry.js";
import { getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
import List from "../List.js";
import ResponsivePopover from "../ResponsivePopover.js";
import SuggestionItem from "../SuggestionItem.js";
import Button from "../Button.js";
import GroupHeaderListItem from "../GroupHeaderListItem.js";
import SuggestionListItem from "../SuggestionListItem.js";
import {
LIST_ITEM_POSITION,
LIST_ITEM_SELECTED,
} from "../generated/i18n/i18n-defaults.js";
/**
* A class to manage the <code>Input</code suggestion items.
*
* @class
* @private
* @author SAP SE
*/
class Suggestions {
constructor(component, slotName, highlight, handleFocus) {
// The component, that the suggestion would plug into.
this.component = component;
// Defines the items` slot name.
this.slotName = slotName;
// Defines, if the focus will be moved via the arrow keys.
this.handleFocus = handleFocus;
// Defines, if the suggestions should highlight.
this.highlight = highlight;
// Press and Focus handlers
this.fnOnSuggestionItemPress = this.onItemPress.bind(this);
this.fnOnSuggestionItemFocus = this.onItemFocused.bind(this);
this.fnOnSuggestionItemMouseOver = this.onItemMouseOver.bind(this);
this.fnOnSuggestionItemMouseOut = this.onItemMouseOut.bind(this);
// An integer value to store the currently selected item position,
// that changes due to user interaction.
this.selectedItemIndex = null;
this.i18nBundle = getI18nBundle("@ui5/webcomponents");
this.accInfo = {};
}
/* Public methods */
defaultSlotProperties(hightlightValue) {
const inputSuggestionItems = this._getComponent().suggestionItems;
const highlight = this.highlight && !!hightlightValue;
const suggestions = [];
inputSuggestionItems.map((suggestion, idx) => {
const text = highlight ? this.getHighlightedText(suggestion, hightlightValue) : this.getRowText(suggestion);
const description = highlight ? this.getHighlightedDesc(suggestion, hightlightValue) : this.getRowDesc(suggestion);
return suggestions.push({
text,
description,
image: suggestion.image || undefined,
icon: suggestion.icon || undefined,
type: suggestion.type || undefined,
additionalText: suggestion.additionalText || undefined,
additionalTextState: suggestion.additionalTextState,
group: suggestion.group,
key: idx,
});
});
return suggestions;
}
onUp(event) {
event.preventDefault();
this._handleItemNavigation(false /* forward */);
return true;
}
onDown(event) {
event.preventDefault();
this._handleItemNavigation(true /* forward */);
return true;
}
onSpace(event) {
if (this._isItemOnTarget()) {
event.preventDefault();
this.onItemSelected(null, true /* keyboardUsed */);
return true;
}
return false;
}
onEnter(event) {
if (this._isItemOnTarget()) {
this.onItemSelected(null, true /* keyboardUsed */);
return true;
}
return false;
}
toggle(bToggle, { preventFocusRestore }) {
const toggle = bToggle !== undefined ? bToggle : !this.isOpened();
if (toggle) {
this.open();
} else {
this.close(preventFocusRestore);
}
}
async _isScrollable() {
const sc = await this._getScrollContainer();
return sc.offsetHeight < sc.scrollHeight;
}
async open() {
this.responsivePopover = await this._respPopover();
this._beforeOpen();
if (this._getItems().length) {
this.responsivePopover.open(this._getComponent());
}
}
async close(preventFocusRestore = false) {
this.responsivePopover = await this._respPopover();
this.responsivePopover.close(false, false, preventFocusRestore);
}
updateSelectedItemPosition(pos) {
this.selectedItemIndex = pos;
}
/* Interface methods */
onItemFocused() {
this._getComponent().onItemFocused();
}
onItemMouseOver(event) {
this._getComponent().onItemMouseOver(event);
}
onItemMouseOut(event) {
this._getComponent().onItemMouseOut(event);
}
onItemSelected(selectedItem, keyboardUsed) {
const allItems = this._getItems();
const item = selectedItem || allItems[this.selectedItemIndex];
this.selectedItemIndex = allItems.indexOf(item);
this.accInfo = {
currentPos: this.selectedItemIndex + 1,
listSize: allItems.length,
itemText: item.textContent,
};
// If the item is "Inactive", prevent selection with SPACE or ENTER
// to have consistency with the way "Inactive" items behave in the ui5-list
if (item.type === "Inactive") {
return;
}
if (item.group) {
return;
}
this._getComponent().onItemSelected(this._getRealItems()[this.selectedItemIndex], keyboardUsed);
item.selected = false;
this.close();
}
onItemPreviewed(item) {
this._getComponent().onItemPreviewed(item);
}
/* Private methods */
onItemPress(oEvent) {
this.onItemSelected(oEvent.detail.item, false /* keyboardUsed */);
}
_beforeOpen() {
this._attachItemsListeners();
this._attachPopupListeners();
}
async _attachItemsListeners() {
const list = await this._getList();
list.removeEventListener("ui5-item-press", this.fnOnSuggestionItemPress);
list.addEventListener("ui5-item-press", this.fnOnSuggestionItemPress);
list.removeEventListener("ui5-item-focused", this.fnOnSuggestionItemFocus);
list.addEventListener("ui5-item-focused", this.fnOnSuggestionItemFocus);
list.removeEventListener("mouseover", this.fnOnSuggestionItemMouseOver);
list.addEventListener("mouseover", this.fnOnSuggestionItemMouseOver);
list.removeEventListener("mouseout", this.fnOnSuggestionItemMouseOut);
list.addEventListener("mouseout", this.fnOnSuggestionItemMouseOut);
}
_attachPopupListeners() {
if (!this.handleFocus) {
return;
}
if (!this.attachedAfterOpened) {
this._respPopover.addEventListener("ui5-after-open", this._onOpen.bind(this));
this.attachedAfterOpened = true;
}
if (!this.attachedAfterClose) {
this._respPopover.addEventListener("ui5-after-close", this._onClose.bind(this));
this.attachedAfterClose = true;
}
}
_onOpen() {
this._applyFocus();
this._getComponent().onOpen();
}
_onClose() {
this._getComponent().onClose();
}
_applyFocus() {
if (this.selectedItemIndex) {
this._getItems()[this.selectedItemIndex].focus();
}
}
_isItemOnTarget() {
return this.isOpened() && this.selectedItemIndex !== null;
}
isOpened() {
return !!(this.responsivePopover && this.responsivePopover.opened);
}
_handleItemNavigation(forward) {
if (!this._getItems().length) {
return;
}
if (forward) {
this._selectNextItem();
} else {
this._selectPreviousItem();
}
}
_selectNextItem() {
const itemsCount = this._getItems().length;
const previousSelectedIdx = this.selectedItemIndex;
if ((this.selectedItemIndex === null) || (++this.selectedItemIndex > itemsCount - 1)) {
this.selectedItemIndex = 0;
}
this._moveItemSelection(previousSelectedIdx, this.selectedItemIndex);
}
_selectPreviousItem() {
const itemsCount = this._getItems().length;
const previousSelectedIdx = this.selectedItemIndex;
if ((this.selectedItemIndex === null) || (--this.selectedItemIndex < 0)) {
this.selectedItemIndex = itemsCount - 1;
}
this._moveItemSelection(previousSelectedIdx, this.selectedItemIndex);
}
_moveItemSelection(previousIdx, nextIdx) {
const items = this._getItems();
const currentItem = items[nextIdx];
const previousItem = items[previousIdx];
this.accInfo = {
currentPos: nextIdx + 1,
listSize: items.length,
itemText: currentItem.textContent,
};
if (previousItem) {
previousItem.selected = false;
}
if (currentItem) {
currentItem.selected = true;
if (this.handleFocus) {
currentItem.focus();
}
}
this.onItemPreviewed(currentItem);
if (!this._isItemIntoView(currentItem)) {
this._scrollItemIntoView(currentItem);
}
}
_deselectItems() {
const items = this._getItems();
items.forEach(item => {
item.selected = false;
});
}
_isItemIntoView(item) {
const rectItem = item.getDomRef().getBoundingClientRect();
const rectInput = this._getComponent().getDomRef().getBoundingClientRect();
const windowHeight = (window.innerHeight || document.documentElement.clientHeight);
return (rectItem.top + Suggestions.SCROLL_STEP <= windowHeight) && (rectItem.top >= rectInput.top);
}
async _scrollItemIntoView(item) {
const pos = item.getDomRef().offsetTop;
const scrollContainer = await this._getScrollContainer();
scrollContainer.scrollTop = pos;
}
async _getScrollContainer() {
if (!this._scrollContainer) {
await this._respPopover();
this._scrollContainer = this.responsivePopover.shadowRoot.querySelector(".ui5-popup-content");
}
return this._scrollContainer;
}
_getItems() {
return [...this.responsivePopover.querySelector("[ui5-list]").children];
}
_getComponent() {
return this.component;
}
async _getList() {
this.responsivePopover = await this._respPopover();
return this.responsivePopover.querySelector("[ui5-list]");
}
async _getListWidth() {
const list = await this._getList();
return list.offsetWidth;
}
_getRealItems() {
return this._getComponent().getSlottedNodes(this.slotName);
}
async _respPopover() {
if (this.responsivePopover) {
return this.responsivePopover;
}
const staticAreaItem = await this._getComponent().getStaticAreaItemDomRef();
this.responsivePopover = staticAreaItem.querySelector("[ui5-responsive-popover]");
return this.responsivePopover;
}
get itemSelectionAnnounce() {
const i18nBundle = this.i18nBundle,
itemPositionText = i18nBundle.getText(LIST_ITEM_POSITION, [this.accInfo.currentPos], [this.accInfo.listSize]),
itemSelectionText = i18nBundle.getText(LIST_ITEM_SELECTED);
return `${itemPositionText} ${this.accInfo.itemText} ${itemSelectionText}`;
}
getRowText(suggestion) {
return this.sanitizeText(suggestion.text || suggestion.textContent);
}
getRowDesc(suggestion) {
if (suggestion.description) {
return this.sanitizeText(suggestion.description);
}
}
getHighlightedText(suggestion, input) {
let text = suggestion.text || suggestion.textContent;
text = this.sanitizeText(text);
return this.hightlightInput(text, input);
}
getHighlightedDesc(suggestion, input) {
let text = suggestion.description;
text = this.sanitizeText(text);
return this.hightlightInput(text, input);
}
hightlightInput(text, input) {
if (!text) {
return text;
}
const inputEscaped = input.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const regEx = new RegExp(inputEscaped, "ig");
return text.replace(regEx, match => `<b>${match}</b>`);
}
sanitizeText(text) {
return text && text.replace("<", "<");
}
static get dependencies() {
return [
SuggestionItem,
ResponsivePopover,
List,
SuggestionListItem,
GroupHeaderListItem,
Button,
];
}
}
Suggestions.SCROLL_STEP = 60;
// Add suggestions support to the global features registry so that Input.js can use it
registerFeature("InputSuggestions", Suggestions);
export default Suggestions;