forked from AlexWayfer/cccombo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcccombo.js
More file actions
395 lines (339 loc) · 9.13 KB
/
cccombo.js
File metadata and controls
395 lines (339 loc) · 9.13 KB
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
/*
Cccombo
Make select-box or combo-box by one module!
Author: Alexander Popov <alex.wayfer@gmail.com>
License: MIT
Version: 1.3.2
https://github.com/AlexWayfer/cccombo
*/
// Helpers functions
function dispatchCustomEvent(element, event_name) {
var event;
try {
event = new Event(event_name, { 'bubbles': true, 'cancelable': true });
} catch(e) {
event = document.createEvent('Event');
// Define that the event name is 'build'.
event.initEvent(event_name, true, true);
}
// target can be any Element or other EventTarget.
element.dispatchEvent(event);
}
// Cccombo Item of List class
function CccomboItem(element) {
// Constructor
this.element = element;
this._fromDataWithDefault(
'label',
(this.element.querySelector('*[data-label]') || this.element).innerHTML
);
this._fromDataWithDefault('value');
}
CccomboItem.prototype = {
hideClass: 'hidden',
hoverClass: 'hover',
selectClass: 'selected',
_fromDataWithDefault: function(key, defaultValue) {
if (defaultValue === undefined) defaultValue = this.element.innerHTML;
var dataValue = this.element.getAttribute('data-' + key);
return (
this[key] = (dataValue !== null) ? dataValue : defaultValue
);
},
_isHidden: function() {
return this.element.classList.contains(this.hideClass);
},
_isHovered: function() {
return this.element.classList.contains(this.hoverClass);
},
_isSelected: function() {
return this.element.classList.contains(this.selectClass);
},
_hide: function() {
this.element.classList.add(this.hideClass);
},
_show: function() {
this.element.classList.remove(this.hideClass);
},
_hover: function() {
this.element.classList.add(this.hoverClass);
},
_unhover: function() {
this.element.classList.remove(this.hoverClass);
},
_select: function() {
this.element.classList.add(this.selectClass);
},
_unselect: function() {
this.element.classList.remove(this.selectClass);
}
};
// Cccombo List class
function CccomboList(cccombo) {
// Constructor
this.cccombo = cccombo;
this.element = this.cccombo.element.getElementsByTagName('ul')[0];
var liArray = this.element.querySelectorAll('li:not(.group)');
this.items = [].map.call(liArray, function(item) {
return new CccomboItem(item);
});
}
CccomboList.prototype = {
visibleItems: function() {
return this.items.filter(function(item) {
return !item._isHidden();
});
},
hideItem: function(index) {
var item = this.items[index];
if (item._isHovered()) this.hover(null);
if (item._isSelected()) this.selectItem(null);
item._hide();
},
showItem: function(index) {
this.items[index]._show();
},
filterItems: function(callback) {
var list = this;
this.items.forEach(function(item, index) {
if (callback(item, index)) {
list.showItem(index);
} else {
list.hideItem(index);
}
});
// Hide empty list
this.cccombo.open();
// if (this.visibleItems().length === 0) {
// this.cccombo.close();
// }
},
hoveredItem: function() {
return this.items.filter(function(item) {
return item._isHovered();
})[0];
},
hover: function(item) {
var hoveredItem = this.hoveredItem();
if (hoveredItem) hoveredItem._unhover();
if (item) item._hover();
},
hoverMove: function(delta) {
var visibleItems = this.visibleItems();
var index = visibleItems.indexOf(this.hoveredItem()) + delta;
if (
(index >= visibleItems.length && delta == 1) ||
(index < 0 && delta != -1)
) {
index = 0;
} else if (
(index >= visibleItems.length && delta != 1) ||
(index < 0 && delta == -1)
) {
index = visibleItems.length - 1;
}
this.hover(visibleItems[index]);
},
hoverNext: function() {
this.hoverMove(+1);
},
hoverPrev: function() {
this.hoverMove(-1);
},
hoverFirst: function() {
this.hover(this.visibleItems()[0]);
},
selectedItem: function() {
return this.items.filter(function(item) {
return item._isSelected();
})[0];
},
selectItem: function(item) {
var selectedItem = this.selectedItem();
if (selectedItem) selectedItem._unselect();
if (item) item._select();
}
};
// Cccombo class
function Cccombo(element) {
// Constructor
this.element = element;
this.input = this.element.querySelector('input:not([type="hidden"])');
this.button = this.element.querySelector('button');
this.input_or_button = (this.input || this.button);
this.list = new CccomboList(this);
}
Cccombo.prototype = {
openClass: 'open',
init: function() {
var cccombo = this;
// Add hidden input for button
if (this.button) {
// Init node
this.hidden_input = document.createElement('input');
this.hidden_input.setAttribute('type', 'hidden');
['name', 'value', 'form'].forEach(function(attr) {
var value = cccombo.button.getAttribute(attr);
if (value) cccombo.hidden_input.setAttribute(attr, value);
});
// Events for node
this.button.addEventListener('change', function(event) {
cccombo.buttonOnChange();
});
// Append node
this.element.insertBefore(this.hidden_input, this.list.element);
}
// Open and close by focus
if (this.input) this.input.addEventListener('focus', function(event) {
cccombo.open();
});
if (this.button) this.button.addEventListener('click', function(event) {
cccombo.toggle();
event.preventDefault();
});
this.input_or_button.addEventListener('blur', function(event) {
if (cccombo.element.querySelector(':hover') !== cccombo.list.element) {
cccombo.close();
}
});
this.list.items.forEach(function(item) {
// Hover item event
item.element.addEventListener('mouseenter', function(event) {
cccombo.list.hover(item);
});
item.element.addEventListener('mouseleave', function(event) {
cccombo.list.hover(null);
});
// Select item event
item.element.addEventListener('click', function(event) {
cccombo.select(item);
if (cccombo.button) cccombo.button.focus();
});
// Select selected item
if (item._isSelected()) cccombo.select(item);
});
// Filtering items on input
if (this.input) this.input.addEventListener('input', function(event) {
// Hide items
var input_value = this.value.toLowerCase();
cccombo.list.filterItems(function(item) {
var item_value = item.element.innerHTML.toLowerCase();
return (item_value.indexOf(input_value) >= 0);
});
var selectedItem = cccombo.list.selectedItem();
if (selectedItem && selectedItem.value != this.value) {
selectedItem._unselect();
}
});
// Moving for items
this.input_or_button.addEventListener('keydown', function(event) {
// console.log(event.keyCode);
var arrow = { up: 38, down: 40 },
page = { up: 33, down: 34 },
escape = 27,
enter = 13;
if (cccombo.isOpen()) {
switch (event.keyCode) {
case arrow.up:
cccombo.list.hoverPrev();
break;
case arrow.down:
cccombo.list.hoverNext();
break;
case page.up:
cccombo.list.hoverMove(-5);
break;
case page.down:
cccombo.list.hoverMove(5);
break;
case escape:
cccombo.close();
break;
case enter:
cccombo.select();
break;
default: return; // exit
}
} else {
switch (event.keyCode) {
case enter:
case arrow.up:
case arrow.down:
case page.up:
case page.down:
cccombo.open();
break;
default: return; // exit
}
}
event.preventDefault();
});
// window.addEventListener('resize', function(event) {
// // Make width of button same as list width
// if (cccombo.button) {
// var bordeWidth = parseInt(
// getComputedStyle(
// cccombo.input_or_button, null
// ).getPropertyValue('border-width')
// );
// cccombo.element.style.width = (
// cccombo.list.element.offsetWidth + bordeWidth * 2 + 'px'
// );
// }
// });
// dispatchCustomEvent(window, 'resize');
},
buttonOnChange: function() {
this.hidden_input.value = this.button.value;
},
open: function() {
if (this.list.visibleItems().length === 0) {
this.close();
return false;
}
this.element.classList.add(this.openClass);
var selectedItem = this.list.selectedItem();
if (!selectedItem) {
this.list.hoverFirst();
} else {
this.list.hover(selectedItem);
}
},
close: function() {
this.element.classList.remove(this.openClass);
this.list.hover(null);
},
isOpen: function() {
return this.element.classList.contains(this.openClass);
},
toggle: function() {
if (this.isOpen()) {
this.close();
} else {
this.open();
}
},
select: function(item) {
if (item === undefined) {
item = this.list.hoveredItem();
}
this.list.selectItem(item);
this.input_or_button.value = item.value;
if (this.input) dispatchCustomEvent(this.input, 'input');
if (this.button) {
var label = (this.button.querySelector('*[data-label]') || this.button);
label.innerHTML = item.label;
}
dispatchCustomEvent(this.input_or_button, 'change');
// Hidden input has no value from button
// because disabled button doesn't recieve events (on Firefox)
// https://bugzilla.mozilla.org/show_bug.cgi?id=329509
if (this.button && this.button.getAttribute('disabled') !== undefined) {
this.buttonOnChange();
}
this.close();
},
selectFirst: function() {
this.select(this.list.visibleItems()[0]);
}
};