-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdadata-address-suggests.ts
240 lines (216 loc) · 7.35 KB
/
dadata-address-suggests.ts
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
const DEBOUNCE_TIMEOUT = 500;
const DADATA_FORM_SELECTOR = '.js-dadata-form';
const DADATA_SELECTOR = '.js-address';
const DADATA_LOCATIONS = [{ region: 'москва' }, { region: 'московская' }];
const COMPLEX_COORDS = [55.497536, 36.918034]; // Как в contacts-map.js
const SUGGESTIONS_CONTAINER_CLASS = 'dadata__suggestions-container';
const fetchingMonitor: { isFetching: boolean; abort?: () => void } = {
isFetching: false,
};
const url =
'https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address';
const token = 'Use Your Own API Token Obtained From https://dadata.ru/api/suggest/address/'
async function getDaDataAddressSuggests(
query: string,
locations?: { region: string }[]
) {
// The body of this function is omitted from the repo as noted in the Readme. Please use the API access instructions from the official website.
}
function updateInputValue(
activeLine: HTMLParagraphElement,
inputElement: HTMLInputElement
) {
const clickedText = activeLine.textContent;
if (!clickedText) return;
/* eslint-disable no-param-reassign */
inputElement.value = clickedText;
/* eslint-enable no-param-reassign */
}
function getFirstElement(divElement: HTMLDivElement) {
let firstEl = divElement.firstElementChild;
while (firstEl && firstEl.tagName !== 'P') {
firstEl = firstEl.nextElementSibling;
}
return firstEl as HTMLParagraphElement;
}
function moveUp(activeLine: HTMLParagraphElement, divElement: HTMLDivElement) {
if (
activeLine.previousElementSibling instanceof HTMLParagraphElement &&
activeLine.dataset.index !== '0'
) {
activeLine.previousElementSibling.focus();
} else {
(divElement.lastElementChild as HTMLParagraphElement).focus();
}
}
function moveDown(
activeLine: HTMLParagraphElement,
divElement: HTMLDivElement
) {
if (activeLine.nextElementSibling instanceof HTMLParagraphElement) {
activeLine.nextElementSibling.focus();
} else {
(getFirstElement(divElement) as HTMLParagraphElement).focus();
}
}
function populateAddressSuggestions(
input: string,
suggestions: { value: string }[]
): string {
let innerHTML =
'<span class="dadata__subtitle">Выберите вариант или продолжите ввод</span>';
suggestions.forEach(({ value }, index: number) => {
const startIndex = value.toLowerCase().indexOf(input.toLowerCase());
if (startIndex >= 0) {
const beginning = value.substring(0, startIndex);
const middle = `<strong>${value.substring(
startIndex,
startIndex + input.length
)}</strong>`;
const end = value.substring(startIndex + input.length);
const valueWithHighlight = beginning + middle + end;
innerHTML += `<p class="dadata__suggestion" tabindex="0" data-index="${index}">${valueWithHighlight}</p>`;
} else {
innerHTML += `<p class="dadata__suggestion" tabindex="0" data-index="${index}">${value}</p>`;
}
});
return innerHTML;
}
function initContainer(inputElement: HTMLElement) {
const { parentElement } = inputElement;
if (!parentElement) return null;
let div = parentElement.querySelector(`.${SUGGESTIONS_CONTAINER_CLASS}`);
let isMounted = true;
if (!div) {
isMounted = false;
div = document.createElement('div');
div.classList.add(SUGGESTIONS_CONTAINER_CLASS);
}
return {
div: div as HTMLDivElement,
isMounted,
mount() {
if (!this.isMounted) {
parentElement.appendChild(this.div);
this.isMounted = true;
}
},
unmount() {
if (this.isMounted) {
parentElement.removeChild(this.div);
this.isMounted = false;
}
},
};
}
async function handleInput(inputEvent: Event) {
const inputElement: HTMLInputElement = inputEvent.target as HTMLInputElement;
const suggestionsContainer = initContainer(inputElement);
if (!suggestionsContainer) return;
const input: string = inputElement.value.trim();
if (input === '' || !input) {
suggestionsContainer.unmount();
return;
}
if (fetchingMonitor.isFetching && fetchingMonitor.abort) {
fetchingMonitor.abort();
}
fetchingMonitor.isFetching = true;
const { promise, abort } = await getDaDataAddressSuggests(input, DADATA_LOCATIONS);
fetchingMonitor.abort = abort;
promise
.then((resolve) => {
const { suggestions } = resolve as { suggestions: { value: string }[] };
if (!suggestions.length) return;
suggestionsContainer.div.innerHTML = populateAddressSuggestions(
input,
suggestions
);
if (suggestionsContainer.isMounted) return;
suggestionsContainer.mount();
suggestionsContainer.div.addEventListener('click', (evt) => {
const activeLine = (evt.target as HTMLElement).closest(
'p'
) as HTMLParagraphElement;
updateInputValue(activeLine, inputElement);
suggestionsContainer.unmount();
});
suggestionsContainer.div.addEventListener('keydown', (evt) => {
const activeLine = (evt.target as HTMLElement).closest(
'p'
) as HTMLParagraphElement;
const { key } = evt;
switch (key) {
case 'Enter': {
evt.preventDefault();
updateInputValue(activeLine, inputElement);
suggestionsContainer.unmount();
inputElement.focus();
break;
}
case 'Escape':
suggestionsContainer.unmount();
inputElement.focus();
break;
case 'ArrowUp':
moveUp(activeLine, suggestionsContainer.div);
break;
case 'ArrowDown':
moveDown(activeLine, suggestionsContainer.div);
break;
case 'Tab':
evt.preventDefault();
if (evt.shiftKey) {
moveUp(activeLine, suggestionsContainer.div);
} else {
moveDown(activeLine, suggestionsContainer.div);
}
break;
default:
break;
}
});
})
.catch(() => {
// console.log('Произошла ошибка', err);
})
.finally(() => {
fetchingMonitor.isFetching = false;
});
}
export function debounce(func: any, timeout: number | undefined) {
let timeoutId: NodeJS.Timeout;
return function (this: any, ...args: any[]) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func.apply(this, args);
}, timeout);
};
}
function openYandexMap(fromAddress: string) {
const encodedAddress = encodeURIComponent(fromAddress);
const endpoint = `${COMPLEX_COORDS[0]},${COMPLEX_COORDS[1]}`;
const mapsUrl = `https://yandex.ru/maps/?rtext=${encodedAddress}~${endpoint}`;
window.open(mapsUrl, '_blank');
}
function handleDadataSubmit(
inputElement: HTMLInputElement,
submitEvent: Event
) {
submitEvent.preventDefault();
openYandexMap(inputElement.value);
}
function initDadataForm() {
const debouncedInputHandler = debounce(handleInput, DEBOUNCE_TIMEOUT);
const dadataForm: HTMLFormElement | null =
document.querySelector(DADATA_FORM_SELECTOR);
if (!dadataForm) return;
const addressInputElement: HTMLInputElement | null =
dadataForm.querySelector(DADATA_SELECTOR);
if (!addressInputElement) return;
addressInputElement.addEventListener('input', debouncedInputHandler);
dadataForm.addEventListener('submit', (evt) =>
handleDadataSubmit(addressInputElement, evt)
);
}
document.addEventListener('DOMContentLoaded', initDadataForm);