-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
176 lines (146 loc) · 5.43 KB
/
index.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
'use strict';
class Menu {
constructor(el, parent) {
this.parent = parent;
this.el = el;
this.el.className = 'in-suggest_menu is-hidden';
this.el.addEventListener('mouseenter', () => {
this.el.classList.add('is-hover');
});
this.el.addEventListener('mouseleave', () => {
this.el.classList.remove('is-hover');
});
}
hidden(bool = true) {
if (bool) this.el.classList.add('is-hidden');
else this.el.classList.remove('is-hidden');
this.clear();
}
clear() {
this.parent.index = -1;
this.el.innerHTML = '';
}
}
class Item {
constructor(item, data, index, parent) {
if (!(item instanceof HTMLElement)) return this.logError('"createItem" should be return a HTMLElement')
this.parent = parent;
this.data = data;
this.id = index;
this.el = document.createElement('div');
this.el.className = 'in-suggest_menu_item';
this.el.appendChild(item);
this.el.addEventListener('click', this.parent.click.bind(this.parent, this.data));
this.el.addEventListener('mouseenter', () => {
this.parent.index = index;
this.el.classList.add('is-active');
});
this.el.addEventListener('mouseleave', () => {
this.parent.index = -1;
this.el.classList.remove('is-active');
});
this.parent.menu.el.appendChild(this.el);
}
active(bool = true) {
if (!bool) {
this.el.classList.remove('is-active');
return
}
this.parent.items.forEach(item => item.active(false));
this.el.classList.add('is-active');
}
}
class InSuggest {
constructor(id, options = {}) {
this.input = document.getElementById(id);
if (!this.input)
return this.logError(`element with "${id}" is not found`)
if (this.input.getAttribute('type') !== 'text')
return this.logError('element input should be have type text')
if (!options.action || !options.createItem || !options.selected)
return this.logError('options "action", "createItem" and "selected" is required')
const originClasses = this.input.className;
this.input.className = 'in-suggest_input';
this.wrapper = document.createElement('div');
this.wrapper.className = originClasses;
this.wrapper.classList.add('in-suggest');
this.wrapper.id = this.input.id;
this.input.removeAttribute('id');
this.menu = new Menu(document.createElement('div'), this);
this.input.parentNode.insertBefore(this.wrapper, this.input);
this.wrapper.appendChild(this.input);
this.wrapper.appendChild(this.menu.el);
this.items = [];
this.index = -1;
this.timer = null;
this.loadState = false;
this.options = options;
this.input.addEventListener('input', this.action.bind(this, options.action));
this.input.addEventListener('keydown', this.down.bind(this));
this.input.addEventListener('keydown', this.up.bind(this));
this.input.addEventListener('keyup', this.enter.bind(this));
this.input.addEventListener('keyup', this.escape.bind(this));
this.input.addEventListener('focus', this.action.bind(this, options.action));
this.input.addEventListener('blur', this.blur.bind(this));
}
blur() {
if (!this.menu.el.classList.contains('is-hover')) this.menu.hidden();
}
down(e) {
if (e.key !== 'ArrowDown') return
if (this.index < this.items.length - 1) this.index++;
else this.index = 0;
this.items.find(item => item.id === this.index).active();
e.preventDefault();
}
up(e) {
if (e.key !== 'ArrowUp') return
if (this.index > 0) this.index--;
else this.index = this.items.length - 1;
this.items.find(item => item.id === this.index).active();
e.preventDefault();
}
enter(e) {
if (e.key !== 'Enter' || this.index === -1 || this.loadState) return
this.click(this.items[this.index].data);
}
escape(e) {
if (e.key !== 'Escape') return
this.input.value = '';
this.menu.hidden();
}
click(item) {
this.options.selected(item, str => {
if (typeof str !== 'string')
return this.logError('"click" should be return a callback with string parameter')
this.input.value = str;
this.menu.hidden();
});
}
action(callback) {
clearTimeout(this.timer);
this.loading(false);
this.menu.hidden(false);
this.loading(true);
this.timer = setTimeout(() => {
callback.call(this, this.input.value, results => {
if (!Array.isArray(results)) this.items = [results];
else this.items = results;
this.loading(false);
this.menu.clear();
this.items = this.items.map((item, index) =>
new Item(this.options.createItem.call(this, item), item, index, this)
);
});
}, 200);
}
loading(bool) {
this.loadState = bool;
if (bool) this.wrapper.classList.add('is-loading');
else this.wrapper.classList.remove('is-loading');
}
logError(message) {
throw new Error(`[in-suggest] ${message}`)
}
}
module.exports = InSuggest;