Commit a57e9877 authored by Simon's avatar Simon

autocomplete update

parent 038d9863
......@@ -34,6 +34,10 @@
background: #fff;
}
.awesomplete mark {
background: none;
}
.awesomplete > ul:empty {
display: none;
}
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -22,6 +22,43 @@ const swiper = new Swiper('.c-feedbacks--inner', {
}
});
const lisp = (str) => {
var symbols = {
'ą': 'a',
'č': 'c',
'ę': 'e',
'ė': 'e',
'į': 'i',
'š': 's',
'ų': 'u',
'ū': 'u',
'ž': 'z',
'ā': 'a',
'ē': 'e',
'ģ': 'g',
'ī': 'i',
'ķ': 'k',
'ļ': 'l',
'ņ': 'n',
'õ': 'o',
'ä': 'a',
'ö': 'o',
'ü': 'u'
};
str = str.trim();
str = str.toLowerCase();
str = str.replace(/[ąčęėįšųūžāēģīķļņõäöü]/g, (s) => {
return symbols[s];
});
return str;
};
const autocomplete = (str) => {
const node = document.getElementById(str);
const arrow = document.querySelector('.js-toggle--' + str);
......@@ -46,7 +83,10 @@ const autocomplete = (str) => {
const box = new Awesomplete(node, {
minChars: 0,
maxItems: 1000,
sort: false
sort: false,
filter: function (text, input) {
return lisp(text).indexOf(lisp(input)) !== -1;
}
});
node.addEventListener('input', () => {
......@@ -68,6 +108,18 @@ const autocomplete = (str) => {
}
});
node.addEventListener('blur', () => {
const suggestion = (Array.isArray(box.suggestions) && box.suggestions.length === 1) ? box.suggestions[0] : null;
if (!suggestion) {
return;
}
if (lisp(node.value) === lisp(suggestion.value)) {
node.value = suggestion.value;
}
});
node.addEventListener('awesomplete-selectcomplete', () => {
node.blur();
if (clear) {
......@@ -130,7 +182,9 @@ if (node) {
minChars: 0,
maxItems: 1000,
sort: false,
filter: function (text, input) {
return lisp(text).indexOf(lisp(input)) !== -1;
},
replace: function (suggestion) {
this.input.value = suggestion.label;
......@@ -143,6 +197,22 @@ if (node) {
node.addEventListener('focus', () => {
nodeBox.evaluate();
});
node.addEventListener('blur', () => {
const suggestion = (Array.isArray(nodeBox.suggestions) && nodeBox.suggestions.length === 1) ? nodeBox.suggestions[0] : null;
if (!suggestion) {
return;
}
if (lisp(node.value) === lisp(suggestion.label)) {
node.value = suggestion.label;
if (cityID) {
cityID.value = suggestion.value;
}
}
});
}
if (window.NodeList && !NodeList.prototype.forEach) {
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment