-
-
Notifications
You must be signed in to change notification settings - Fork 881
Expand file tree
/
Copy pathAutocompleteDropdown.js
More file actions
89 lines (75 loc) · 2.67 KB
/
Copy pathAutocompleteDropdown.js
File metadata and controls
89 lines (75 loc) · 2.67 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
import Fragment from 'flarum/common/Fragment';
export default class AutocompleteDropdown extends Fragment {
items = [];
active = false;
index = 0;
keyWasJustPressed = false;
view() {
return (
<ul className="Dropdown-menu EmojiDropdown">
<li className="Dropdown-header">{app.translator.trans('flarum-emoji.forum.composer.type_to_search_text')}</li>
{this.items.map((item) => (
<li key={item.attrs.key}>{item}</li>
))}
</ul>
);
}
show(left, top) {
const style = this.element.style;
style.display = 'block';
style.left = left + 'px';
style.top = top + 'px';
this.active = true;
}
hide() {
this.element.style.display = 'none';
this.active = false;
}
navigate(delta) {
this.keyWasJustPressed = true;
this.setIndex(this.index + delta, true);
clearTimeout(this.keyWasJustPressedTimeout);
this.keyWasJustPressedTimeout = setTimeout(() => (this.keyWasJustPressed = false), 500);
}
complete() {
this.element.querySelectorAll('li:not(.Dropdown-header)')[this.index].querySelector('button').click();
}
// todo: check if copied implementation matches the original behavior
setIndex(index, scrollToItem) {
if (this.keyWasJustPressed && !scrollToItem) return;
const dropdown = this.element;
const items = dropdown.querySelectorAll('li:not(.Dropdown-header)');
let rangedIndex = index;
if (rangedIndex < 0) {
rangedIndex = items.length - 1;
} else if (rangedIndex >= items.length) {
rangedIndex = 0;
}
this.index = rangedIndex;
items.forEach((el) => el.classList.remove('active'));
const item = items[rangedIndex];
item.classList.add('active');
if (scrollToItem) {
const documentScrollTop = document.documentElement.scrollTop;
const dropdownScroll = dropdown.scrollTop;
const dropdownRect = dropdown.getBoundingClientRect();
const dropdownTop = dropdownRect.top + documentScrollTop;
const dropdownBottom = dropdownTop + dropdownRect.height;
const itemRect = item.getBoundingClientRect();
const itemTop = itemRect.top + documentScrollTop;
const itemBottom = itemTop + itemRect.height;
let scrollTop;
if (itemTop < dropdownTop) {
scrollTop = dropdownScroll - dropdownTop + itemTop - parseInt(getComputedStyle(dropdown).paddingTop, 10);
} else if (itemBottom > dropdownBottom) {
scrollTop = dropdownScroll - dropdownBottom + itemBottom + parseInt(getComputedStyle(dropdown).paddingBottom, 10);
}
if (typeof scrollTop !== 'undefined') {
dropdown.scrollTo({
top: scrollTop,
behavior: 'smooth',
});
}
}
}
}