|
8 | 8 | * https://tldrlegal.com/l/mit |
9 | 9 | * |
10 | 10 | * @author Postcode.nl |
11 | | - * @version 1.2.3 |
| 11 | + * @version 1.3.0 |
12 | 12 | */ |
13 | 13 |
|
14 | 14 | (function (global, factory) { |
|
31 | 31 | const document = window.document, |
32 | 32 | $ = function (selector) { return document.querySelectorAll(selector); }, |
33 | 33 | elementData = new WeakMap(), |
| 34 | + VERSION = '1.3.0', |
34 | 35 | EVENT_NAMESPACE = 'autocomplete-', |
35 | 36 | PRECISION_ADDRESS = 'Address', |
36 | 37 | KEY_ESC = 'Escape', |
|
174 | 175 | language: { |
175 | 176 | writable: true, |
176 | 177 | }, |
| 178 | + |
177 | 179 | }); |
178 | 180 |
|
179 | 181 | /** |
|
672 | 674 | // Create options object that inherits from defaults. |
673 | 675 | options = extend(Object.create(defaults), options); |
674 | 676 |
|
| 677 | + // Make context lowercase as required by API. |
| 678 | + options.context = options.context.toLowerCase(); |
| 679 | + |
675 | 680 | // Expose options. |
676 | 681 | Object.defineProperty(this, 'options', { |
677 | 682 | get: function () { |
|
889 | 894 | return str; |
890 | 895 | } |
891 | 896 |
|
892 | | - var i = 0, |
| 897 | + let i = 0, |
893 | 898 | start = 0, |
894 | 899 | end = 0, |
895 | 900 | result = [], |
|
1028 | 1033 | }, |
1029 | 1034 | }); |
1030 | 1035 |
|
| 1036 | + if (false === element.hasAttribute('autocomplete')) |
| 1037 | + { |
| 1038 | + element.autocomplete = 'off'; |
| 1039 | + } |
| 1040 | + |
1031 | 1041 | element.spellcheck = false; |
1032 | | - element.autocomplete = 'off'; |
1033 | 1042 | element.setAttribute('aria-controls', liveRegion.id); |
1034 | 1043 | element.classList.add(options.cssPrefix + 'address-input'); |
1035 | 1044 | element.classList.toggle(inputBlankClassName, element.value === ''); |
|
1164 | 1173 |
|
1165 | 1174 | /** |
1166 | 1175 | * Search after input has stopped arriving for the amount of milliseconds specified by options.delay. |
| 1176 | + * Values of up to three characters have practically no delay because most of these queries will be served from cache. |
1167 | 1177 | * |
1168 | 1178 | * @param {HTMLElement} element - Associated input element. |
1169 | 1179 | */ |
1170 | 1180 | const searchDebounced = function (element) |
1171 | 1181 | { |
1172 | 1182 | window.clearTimeout(searchTimeoutId); |
1173 | | - searchTimeoutId = window.setTimeout(search, options.delay, element); |
| 1183 | + |
| 1184 | + let delay = 10; |
| 1185 | + |
| 1186 | + if (element.value.length > 3) |
| 1187 | + { |
| 1188 | + delay = options.delay; |
| 1189 | + } |
| 1190 | + |
| 1191 | + searchTimeoutId = window.setTimeout(search, delay, element); |
1174 | 1192 | } |
1175 | 1193 |
|
1176 | 1194 | /** |
|
1247 | 1265 | }, |
1248 | 1266 | }); |
1249 | 1267 |
|
| 1268 | + // Expose readonly version number. |
| 1269 | + Object.defineProperty(autocomplete, 'version', { |
| 1270 | + value: VERSION, |
| 1271 | + }); |
| 1272 | + |
1250 | 1273 | return autocomplete; |
1251 | 1274 | })); |
0 commit comments