-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcndk.autocomplete.js
443 lines (367 loc) · 14.6 KB
/
cndk.autocomplete.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// MIT License
// Copyright (c) 2021 Ilker Cindik
// Version 0.1.3
(function () {
// Self-service
var self = null;
var form = null;
var currentElement = { text: '', id: '' };
this.field = null;
var staticContent = '';
var currentIndex = 0;
// Define our constructor
this.CndkAutoComplete = function () {
// Default options
var defaults = {
input: '', /* Input selector with classname (.) or ID (#) */
ajaxFile: '', /* Static or dynamic AjaxFile URL */
type: 'static', /* static | dynamic */
minCharsToSearch: 1, /* Min chars to search in items */
itemsShow: 5, /* Max items to show on list */
autoFocusWhenSelect: null, /* Focus another input element when any item has been selected */
disableInputOnSelect: false, /* Disable input if any item has been selected */
showAllOnInputClick: true, /* Shows all items when value length = 0 and options is = true */
submitFormOnEnter: false, /* Submit form when press Enter key on the keyboard */
submitFormOnItemSelect: false, /* Submit form when any item has been selected */
submitValueType: '${id}', /* $text | $id */
itemLayout: '${text}', /* Can be used of any json value like in .JSON response => $id | $text | $url */
itemInputLayout: '${text}', /* Can be used of any json value like in .JSON response => $id | $text | $url */
rootDivId: "cndkAutoComplete",
itemClass: 'cndk-item',
itemClassActive: 'cndk-item-active',
theme: 'light' /* light | dark */
}
// Override default options by extending defaults with the passed in arugments
if (arguments[0] && typeof arguments[0] === "object") {
this.options = overrideDefaults(defaults, arguments[0]);
}
// Get input element with #id or .classname
window.onload = (event) => {
this.field = getElement(this.options.input);
self = this;
if (this.field == undefined) {
throwError('Element cannot find. Please use "." or "#" selector on element.');
}
else if (this.field.tagName !== 'INPUT') {
throwError('Element must a <input> element.');
}
else {
this.run();
}
}
}
/* ### PUBLIC METHODS ### */
// Run
CndkAutoComplete.prototype.run = function () {
// Set attributes and listeners
this.field.setAttribute('cndkAutoComplete', true);
this.field.setAttribute('itemsShow', this.options.itemsShow);
this.field.setAttribute('autocomplete', 'off');
this.field.addEventListener('input', textChange);
this.field.addEventListener('keydown', preventSpecialKeys);
if (this.options.showAllOnInputClick && this.options.type == 'static') {
this.field.addEventListener('click', showAllOnInputClick);
}
// Get parent closest form
form = this.field.closest("form");
form.addEventListener('submit', formOnSubmit);
// Load .JSON file (Preload) - if type static
if (this.options.type == 'static') {
loadJSON(this.options.ajaxFile, function (data) {
staticContent = data;
});
}
// Get current widh and height of input
var _width = this.field.offsetWidth;
var _height = this.field.offsetHeight;
// Create root div
var container = document.createElement("DIV");
container.setAttribute("id", self.options.rootDivId + "Container");
container.setAttribute("class", this.options.itemClass + '-' + this.options.theme);
container.setAttribute("style", "position:relative");
var root = document.createElement("DIV");
root.setAttribute("id", self.options.rootDivId);
root.setAttribute("style", "top:" + _height + "px");
this.field.parentNode.insertBefore(container, this.field);
container.appendChild(this.field);
container.appendChild(root);
}
/* ### PRIVATE METHODS ### */
// Ovverride options helper
function overrideDefaults(source, properties) {
var property;
for (property in properties) {
if (properties.hasOwnProperty(property)) {
source[property] = properties[property];
}
}
return source;
}
// Show all items on input click [handler]
function showAllOnInputClick(e) {
var value = e.target.value;
clearList();
if(value.length == 0) showAllItems();
}
// This method shows all items in static json file
function showAllItems() {
// Reset current index
currentIndex = 0;
// Root Div
var root = document.getElementById(self.options.rootDivId);
// List
for (i = 0; i < staticContent.length; i++) {
// Item layout
var item = staticContent[i].text;
// Item div
var div = document.createElement("DIV");
div.setAttribute("class", self.options.itemClass + " " + self.options.itemClass + "-" + staticContent[i].id);
div.setAttribute("data-id", staticContent[i].id);
div.setAttribute("data-index", i);
div.addEventListener("click", clickItem);
// Rendered
var itemVariables = extractVariable(self.options.itemLayout);
var itemRender = '';
var layout = self.options.itemLayout;
// Resolve variables
for (var d = 0; d < itemVariables.length; d++) {
if (itemVariables[d] == 'text') {
layout = layout.replace('${' + itemVariables[d] + '}', item);
};
layout = layout.replace('${' + itemVariables[d] + '}', staticContent[i][itemVariables[d]]);
}
itemRender = layout;
// Assign
div.innerHTML = itemRender;
root.appendChild(div);
// Set active item (currently first item)
setActiveItem();
}
}
// On text-change target input
function textChange(e) {
// Value
var value = this.value.toLowerCase();
// Clear
clearList();
// Search
searchItems(value);
}
// Search entered text in items
function searchItems(value) {
// Reset current index
currentIndex = 0;
// Limit chars to search
var length = value.length;
if (length < self.options.minCharsToSearch) { clearList(); return; }
// Root Div
var root = document.getElementById(self.options.rootDivId);
// Get items by type
if (self.options.type == 'static') {
var c = 1;
for (i = 0; i < staticContent.length; i++) {
// Get values
var search = staticContent[i].text.toLowerCase();
var searchId = staticContent[i].id;
// Add items
if (search.includes(value) && elementIsExists(searchId) == 0) {
var startIndex = search.indexOf(value);
var lastIndex = startIndex + value.length;
// Item layout
var item = staticContent[i].text.slice(0, startIndex);
item += "<b>" + staticContent[i].text.slice(startIndex, lastIndex) + "</b>";
item += staticContent[i].text.slice(lastIndex)
// Item div
var div = document.createElement("DIV");
div.setAttribute("class", self.options.itemClass + " " + self.options.itemClass + "-" + searchId);
div.setAttribute("data-id", searchId);
div.setAttribute("data-index", i);
div.addEventListener("click", clickItem);
// Rendered
var itemVariables = extractVariable(self.options.itemLayout);
var itemRender = '';
var layout = self.options.itemLayout;
// Resolve variables
for (var d = 0; d < itemVariables.length; d++) {
if (itemVariables[d] == 'text') {
layout = layout.replace('${' + itemVariables[d] + '}', item);
};
layout = layout.replace('${' + itemVariables[d] + '}', staticContent[i][itemVariables[d]]);
}
itemRender = layout;
// Assign
div.innerHTML = itemRender;
root.appendChild(div);
// Set active item (currently first item)
setActiveItem();
// Limit showed items
if (c >= self.options.itemsShow) break;
else c++;
}
}
}
else {
// No char to search
clearList();
}
}
// Forms is submitting
function formOnSubmit(e) {
e.preventDefault();
if (self.options.submitValueType == '${id}' && currentElement.id != '') {
self.field.value = currentElement.id;
}
form.submit();
}
// Remove all neccesary elements
function clearList() {
var x = document.getElementsByClassName(self.options.itemClass);
while (x.length > 0) {
x[0].parentNode.removeChild(x[0]);
}
}
// Any item of selected
function itemSelected(e) {
// Disable on select
if (self.options.disableInputOnSelect) {
self.field.setAttribute('disabled', 'disabled');
}
// Input layout
var itemVariables = extractVariable(self.options.itemInputLayout);
var layout = self.options.itemInputLayout;
// Resolve variables
for (var d = 0; d < itemVariables.length; d++) {
layout = layout.replace('${' + itemVariables[d] + '}', staticContent[currentIndex][itemVariables[d]]);
}
// Set Value of Input
self.field.value = layout;
// Focus another input
if(self.options.autoFocusWhenSelect != null){
var anotherInput = getElement(self.options.autoFocusWhenSelect);
anotherInput.focus();
}
}
// Clicked item in results
function clickItem(e) {
// Set selected element
currentElement.name = e.target.innerText;
currentElement.id = e.target.attributes["data-id"].value;
currentIndex = parseInt(e.target.attributes["data-index"].value);
itemSelected();
}
// Prevent special keys (UP-DOWN-ENTER)
function preventSpecialKeys(e) {
if (e.keyCode == 38) {
// Up-Arrow Key
if (currentIndex > 0) currentIndex--;
else currentIndex = (self.options.itemsShow - 1);
}
else if (e.keyCode == 40) {
// Down-Arrow Key
if (currentIndex < (getItemCount() - 1)) currentIndex++;
else currentIndex = 0;
}
else if (e.keyCode == 27) {
// ESC Key
currentIndex = 0;
clearList();
}
else if (e.keyCode == 13) {
// Enter Key
if (!self.options.submitFormOnEnter && getActiveItem() != null) {
e.preventDefault();
}
// If selected item exists
var selected = getActiveItem();
if (selected != null) {
// Click item
selected.click();
// Set selected element
currentElement.name = selected.innerText;
currentElement.id = selected.attributes["data-id"].value;
itemSelected();
// Clear list
clearList();
// Form submit
if (self.options.submitFormOnItemSelect) {
var form = document.getElementsByTagName("form")[0];
form.submit();
}
}
}
// Remove active items
removeActiveItems();
// Set active item
setActiveItem();
}
// Load .JSON file functions
function loadJSON(path, callback) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
var data = JSON.parse(httpRequest.responseText);
if (callback) callback(data);
}
}
};
httpRequest.open('GET', path);
httpRequest.send();
}
// Get element classname or id
function getElement(input) {
var firstChar = input.charAt(0);
if (firstChar === '#') {
return document.getElementById(input.substr(1));
}
else if (firstChar === '.') {
return document.getElementsByClassName(input.substr(1))[0];
}
}
// Find and extract a variable
function extractVariable(variable) {
var regex = new RegExp(/\$\{(\w+?)\}/g),
text = variable,
result,
out = [];
while (result = regex.exec(text)) {
out.push(result[1]);
}
return out;
}
// Returns an integer - currently showing items
function getItemCount() {
return document.getElementsByClassName(self.options.itemClass).length;
}
// Get selected item
function getActiveItem() {
return document.getElementsByClassName(self.options.itemClassActive)[0];
}
// Set active item
function setActiveItem() {
if (getItemCount() > 0) {
var activated = document.getElementsByClassName(self.options.itemClass)[currentIndex];
activated.classList.add(self.options.itemClassActive);
}
}
// Remove active items
function removeActiveItems() {
for (i = 0; i < getItemCount(); i++) {
document.getElementsByClassName(self.options.itemClass)[i].classList.remove(self.options.itemClassActive);
}
}
// Prevent duplicate items
function elementIsExists(searchId) {
return document.getElementsByClassName(self.options.itemClass + "-" + searchId).length;
}
// Throws a error
function throwError(error) {
throw '(Cndk.AutoComplete.js) => ' + error;
}
// Hide results when click outside
document.addEventListener("click", function (e) {
if (e.target != getElement(self.options.input)) {
clearList();
}
});
}());