Passed
Push — master ( 17fae3...e5a7e5 )
by Jesús
02:17
created

src/modules/wordInput/infrastructure/suggestions.ts   A

Complexity

Total Complexity 8
Complexity/F 1.33

Size

Lines of Code 72
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 57
mnd 2
bc 2
fnc 6
dl 0
loc 72
rs 10
bpm 0.3333
cpm 1.3333
noi 0
c 0
b 0
f 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A suggestions.ts ➔ clearSuggestionSelection 0 4 1
A suggestions.ts ➔ updateSuggestionSelection 0 8 2
A suggestions.ts ➔ showSuggestions 0 31 1
A suggestions.ts ➔ getSelectedIndex 0 3 1
A suggestions.ts ➔ hideSuggestions 0 10 2
A suggestions.ts ➔ setSelectedIndex 0 3 1
1
import { elements, state } from '../../bip39';
2
3
let selectedSuggestionIndex = -1;
4
let hideSuggestionsTimeout: NodeJS.Timeout | null = null;
5
6
export function getSelectedIndex(): number {
7
  return selectedSuggestionIndex;
8
}
9
10
export function setSelectedIndex(index: number): void {
11
  selectedSuggestionIndex = index;
12
}
13
14
export function showSuggestions(matches: string[], onSelect: (word: string) => void): void {
15
  elements.wordSuggestions.innerHTML = '';
16
17
  matches.forEach((word, index) => {
18
    const wordIndex = state.wordlist.indexOf(word);
19
    const item = document.createElement('div');
20
    item.className = 'suggestion-item';
21
    item.setAttribute('role', 'option');
22
    item.setAttribute('data-index', index.toString());
23
24
    item.innerHTML = `
25
      <span class="suggestion-word">${word}</span>
26
      <span class="suggestion-index">#${wordIndex + 1}</span>
27
    `;
28
29
    item.addEventListener('mousedown', e => {
30
      e.preventDefault();
31
      onSelect(word);
32
    });
33
34
    item.addEventListener('mouseenter', () => {
35
      clearSuggestionSelection();
36
      selectedSuggestionIndex = index;
37
      item.setAttribute('aria-selected', 'true');
38
    });
39
40
    elements.wordSuggestions.appendChild(item);
41
  });
42
43
  elements.wordSuggestions.removeAttribute('hidden');
44
}
45
46
export function hideSuggestions(): void {
47
  if (hideSuggestionsTimeout) {
48
    clearTimeout(hideSuggestionsTimeout);
49
  }
50
51
  hideSuggestionsTimeout = setTimeout(() => {
52
    elements.wordSuggestions.setAttribute('hidden', '');
53
    selectedSuggestionIndex = -1;
54
  }, 200);
55
}
56
57
export function updateSuggestionSelection(suggestions: NodeListOf<Element>): void {
58
  clearSuggestionSelection();
59
60
  if (selectedSuggestionIndex >= 0 && selectedSuggestionIndex < suggestions.length) {
61
    const selectedItem = suggestions[selectedSuggestionIndex] as HTMLElement;
62
    selectedItem.setAttribute('aria-selected', 'true');
63
    selectedItem.scrollIntoView({ block: 'nearest' });
64
  }
65
}
66
67
export function clearSuggestionSelection(): void {
68
  elements.wordSuggestions.querySelectorAll('.suggestion-item').forEach(item => {
69
    item.setAttribute('aria-selected', 'false');
70
  });
71
}
72