This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 230
/
places.js
111 lines (95 loc) · 3.16 KB
/
places.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
/* eslint no-console:0 */
import EventEmitter from 'events';
import algoliasearch from 'algoliasearch/lite';
import autocomplete from 'autocomplete.js';
import './navigatorLanguage.js';
import createAutocompleteDataset from './createAutocompleteDataset.js';
import './places.scss';
import clearIcon from './icons/clear.svg';
import pinIcon from './icons/address.svg';
export default function places(options) {
const {
container,
style
} = options;
const placesInstance = new EventEmitter();
// https://github.com/algolia/autocomplete.js#options
const autocompleteOptions = {
autoselect: true,
hint: false,
cssClasses: {
root: 'algolia-places' + (style === false ? '-nostyle' : ''),
prefix: 'ap' + (style === false ? '-nostyle' : '')
}
};
if (process.env.NODE_ENV === 'development') {
autocompleteOptions.debug = true;
}
const autocompleteDataset = createAutocompleteDataset({
...options,
algoliasearch,
onHits: ({hits, rawAnswer, query}) => placesInstance.emit('suggestions', {
rawAnswer,
query,
suggestions: hits
})
});
const autocompleteInstance = autocomplete(container, autocompleteOptions, autocompleteDataset);
const autocompleteContainer = container.parentNode;
const autocompleteChangeEvents = ['selected', 'autocompleted'];
autocompleteChangeEvents.forEach(eventName => {
autocompleteInstance.on(`autocomplete:${eventName}`, (_, suggestion) => {
placesInstance.emit('change', {
rawAnswer: suggestion.rawAnswer,
query: suggestion.query,
suggestion,
suggestionIndex: suggestion.hitIndex
});
});
});
autocompleteInstance.on('autocomplete:cursorchanged', (_, suggestion) => {
placesInstance.emit('cursorchanged', {
rawAnswer: suggestion.rawAnswer,
query: suggestion.query,
suggestion,
suggestionIndex: suggestion.hitIndex
});
});
const clear = document.createElement('button');
clear.setAttribute('type', 'button');
clear.classList.add('ap-input-icon');
clear.classList.add('ap-input-icon-clear');
clear.innerHTML = clearIcon;
autocompleteContainer.appendChild(clear);
clear.style.display = 'none';
const pin = document.createElement('button');
pin.setAttribute('type', 'button');
pin.classList.add('ap-input-icon');
pin.classList.add('ap-input-icon-pin');
pin.innerHTML = pinIcon;
autocompleteContainer.appendChild(pin);
pin.addEventListener('click', () => autocompleteInstance.focus());
clear.addEventListener('click', () => {
autocompleteInstance.autocomplete.setVal('');
autocompleteInstance.focus();
clear.style.display = 'none';
pin.style.display = '';
placesInstance.emit('clear');
});
let previousQuery = '';
autocompleteContainer.querySelector('.ap-input').addEventListener('input', () => {
const query = autocompleteInstance.val();
if (query === '') {
pin.style.display = '';
clear.style.display = 'none';
if (previousQuery !== query) {
placesInstance.emit('clear');
}
} else {
clear.style.display = '';
pin.style.display = 'none';
}
previousQuery = query;
});
return placesInstance;
}