-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcccombo.js
514 lines (443 loc) · 11.8 KB
/
cccombo.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
Cccombo
Make select-box or combo-box by one module!
Author: Alexander Popov <[email protected]>
License: MIT
Version: 2.0.0
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, element) {
// Constructor
this.cccombo = cccombo;
this.element = element;
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() {
var found = null;
for (var i = 0; i < this.items.length; i++) {
var item = this.items[i];
if (item._isHovered()) {
found = item;
break;
}
}
return found;
},
_hover: function(target_item) {
var hoveredItem = this._hoveredItem();
if (hoveredItem) hoveredItem._unhover();
if (target_item) {
this.items.forEach(function(item) {
if (item.value == target_item.value) {
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]);
},
_selectedItem: function() {
var found = null;
for (var i = 0; i < this.items.length; i++) {
var item = this.items[i];
if (item._isSelected()) {
found = item;
break;
}
}
return found;
},
selectItem: function(item) {
var selectedItem = this._selectedItem();
if (selectedItem) {
if (item && selectedItem.value == item.value) return;
selectedItem._unselect();
}
if (item) item._select();
},
isMain: function() {
return this.element.dataset.main == 'true';
}
};
// Cccombo class
function Cccombo(element) {
var cccombo = this;
// Constructor
this.element = element;
this.input =
this.element.querySelector('.cccombo > input:not([type="hidden"])');
this.button = this.element.querySelector('button');
this.input_or_button = (this.input || this.button);
this.lists = [].map.call(
this.element.querySelectorAll('ul'), function(list_element) {
return new CccomboList(cccombo, list_element);
}
);
var main_lists = this.lists.filter(function(list) {
return list.isMain();
});
if (main_lists.length > 1) {
console.error('There are more than 1 main list for Cccombo:');
console.error(main_lists);
}
this.main_list = (main_lists.length > 0 ? main_lists : this.lists)[0];
this.dropdown =
this.element.querySelector('.cccombo > ul, .cccombo > .dropdown');
}
Cccombo.container = function() {
return document.body;
};
Cccombo.prototype = {
openClass: 'open',
hoveredItems: function() {
this.lists.map(function(list) {
return list._hoveredItem();
});
},
selectedItem: function() {
return this.main_list._selectedItem();
},
hover: function(item) {
this.lists.forEach(function(list) {
list._hover(item);
});
},
hoverMove: function(delta) {
this.main_list._hoverMove(delta);
this.hover(this.main_list._hoveredItem());
},
hoverFirst: function() {
this.hover(this.main_list.visibleItems()[0]);
},
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.button.nextSibling
);
}
// 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('ul:hover')) {
cccombo.close();
}
});
// Filtering items on input
if (this.input) this.input.addEventListener('input', function(event) {
var value = this.value;
// Hide items
var input_value = value.toLowerCase();
cccombo.lists.forEach(function(list) {
list.filterItems(function(item) {
var item_value = item.element.innerHTML.toLowerCase();
return (item_value.indexOf(input_value) >= 0);
});
var selectedItem = list._selectedItem();
if (selectedItem && selectedItem.value != 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.hoverMove(-1);
break;
case arrow.down:
cccombo.hoverMove(+1);
break;
case page.up:
cccombo.hoverMove(-5);
break;
case page.down:
cccombo.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();
});
this.lists.forEach(function(list) {
list.items.forEach(function(item) {
// Hover item event
item.element.addEventListener('mouseenter', function(event) {
cccombo.hover(item);
});
item.element.addEventListener('mouseleave', function(event) {
cccombo.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()) {
item._unselect();
cccombo.select(item);
}
});
});
// 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.lists.every(function(list) {
return list.visibleItems().length === 0;
})
) {
this.close();
return false;
}
this.dropdown.style.visibility = 'hidden';
this.element.classList.add(this.openClass);
var
dropdown_bounding = this.dropdown.getBoundingClientRect(),
container = Cccombo.container(this.element),
container_bounding = container.getBoundingClientRect(),
body_bounding = document.body.getBoundingClientRect();
if (dropdown_bounding.bottom > body_bounding.bottom) {
this.dropdown.style.bottom = '100%';
}
if (dropdown_bounding.right > container_bounding.right) {
this.dropdown.style.left =
'calc(' +
window.getComputedStyle(this.dropdown).left +
' - ' +
window.getComputedStyle(container).paddingRight +
' + ' +
(container_bounding.right - dropdown_bounding.right + 'px') +
')';
}
this.dropdown.style.visibility = '';
var selectedItem = this.selectedItem();
if (!selectedItem) {
this.hoverFirst();
} else {
this.hover(selectedItem);
}
},
close: function() {
this.element.classList.remove(this.openClass);
this.hover(null);
this.dropdown.style.bottom = '';
this.dropdown.style.left = '';
},
isOpen: function() {
return this.element.classList.contains(this.openClass);
},
toggle: function() {
if (this.isOpen()) {
this.close();
} else {
this.open();
}
},
select: function(item) {
var
original_item_value = typeof item == 'string' ? item : item.value,
previous_selected_item = this.selectedItem();
this.lists.forEach(function(list) {
var list_item = null;
var visibleItems = list.visibleItems();
visibleItems.some(function(visibleItem) {
if (visibleItem.value == original_item_value) {
return item = list_item = visibleItem;
} else {
return;
}
});
list.selectItem(list_item);
});
if (item == undefined) {
item = this.hoveredItems()[0];
}
this.input_or_button.value = item.value;
if (this.button) {
var label = (this.button.querySelector('*[data-label]') || this.button);
label.innerHTML = item.label;
}
// 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();
}
if (!previous_selected_item || item.value != previous_selected_item.value) {
if (this.input) dispatchCustomEvent(this.input, 'input');
dispatchCustomEvent(this.input_or_button, 'change');
}
this.close();
}
};