1
|
/*
|
2
|
Copyright (C) Philippe Meyer 2019-2021
|
3
|
Distributed under the MIT License
|
4
|
|
5
|
vanillaSelectBox : v0.58 : Bug fixes
|
6
|
vanillaSelectBox : v0.57 : Bug fix (minWidth option not honored)
|
7
|
vanillaSelectBox : v0.56 : The multiselect checkboxes are a little smaller, maxWidth option is now working + added minWidth option as well
|
8
|
The button has now a style attribute to protect its appearance
|
9
|
vanillaSelectBox : v0.55 : All attributes from the original select options are copied to the selectBox element
|
10
|
vanillaSelectBox : v0.54 : if all the options of the select are selected by the user then the check all checkbox is checked
|
11
|
vanillaSelectBox : v0.53 : if all the options of the select are selected then the check all checkbox is checked
|
12
|
vanillaSelectBox : v0.52 : Better support of select('all') => command is consistent with checkbox and selecting / deselecting while searching select / uncheck only the found items
|
13
|
vanillaSelectBox : v0.51 : Translations for select all/clear all + minor css corrections + don't select disabled items
|
14
|
vanillaSelectBox : v0.50 : PR by jaguerra2017 adding a select all/clear all check button + optgroup support !
|
15
|
vanillaSelectBox : v0.41 : Bug corrected, the menu content was misplaced if a css transform was applied on a parent
|
16
|
vanillaSelectBox : v0.40 : A click on one selectBox close the other opened boxes
|
17
|
vanillaSelectBox : v0.35 : You can enable and disable items
|
18
|
vanillaSelectBox : v0.30 : The menu stops moving around on window resize and scroll + z-index in order of creation for multiple instances
|
19
|
vanillaSelectBox : v0.26 : Corrected bug in stayOpen mode with disable() function
|
20
|
vanillaSelectBox : v0.25 : New option stayOpen, and the dropbox is no longer a dropbox but a nice multi-select
|
21
|
previous version : v0.24 : corrected bug affecting options with more than one class
|
22
|
https://github.com/PhilippeMarcMeyer/vanillaSelectBox
|
23
|
*/
|
24
|
|
25
|
|
26
|
function getValues(id) {
|
27
|
let result = [];
|
28
|
let collection = document.querySelectorAll("#" + id + " option");
|
29
|
collection.forEach(function (x) {
|
30
|
if (x.selected) {
|
31
|
result.push(x.value);
|
32
|
}
|
33
|
});
|
34
|
return result;
|
35
|
}
|
36
|
|
37
|
let VSBoxCounter = function () {
|
38
|
let count = 0;
|
39
|
let instances = [];
|
40
|
return {
|
41
|
set: function (instancePtr) {
|
42
|
instances.push({offset:++count,ptr:instancePtr});
|
43
|
return instances[instances.length-1].offset;
|
44
|
},
|
45
|
remove: function (instanceNr) {
|
46
|
let temp = instances.filter(function(x){
|
47
|
return x.offset != instanceNr;
|
48
|
})
|
49
|
instances = temp.splice(0);
|
50
|
temp = [];
|
51
|
},
|
52
|
closeAllButMe:function(instanceNr){
|
53
|
temp = [];
|
54
|
instances.forEach(function(x){
|
55
|
if(x.offset != instanceNr){
|
56
|
x.ptr.closeOrder();
|
57
|
}
|
58
|
});
|
59
|
}
|
60
|
};
|
61
|
}();
|
62
|
|
63
|
function vanillaSelectBox(domSelector, options) {
|
64
|
let self = this;
|
65
|
this.instanceOffset = VSBoxCounter.set(self);
|
66
|
this.domSelector = domSelector;
|
67
|
this.root = document.querySelector(domSelector)
|
68
|
this.main;
|
69
|
this.button;
|
70
|
this.title;
|
71
|
this.isMultiple = this.root.hasAttribute("multiple");
|
72
|
this.multipleSize = this.isMultiple && this.root.hasAttribute("size") ? parseInt(this.root.getAttribute("size")) : -1;
|
73
|
this.drop;
|
74
|
this.top;
|
75
|
this.left;
|
76
|
this.options;
|
77
|
this.listElements;
|
78
|
this.isDisabled = false;
|
79
|
this.search = false;
|
80
|
this.searchZone = null;
|
81
|
this.inputBox = null;
|
82
|
this.disabledItems = [];
|
83
|
this.ulminWidth = 140;
|
84
|
this.ulminHeight = 25;
|
85
|
this.forbidenAttributes = ["class","selected","disabled","data-text","data-value","style"];
|
86
|
this.forbidenClasses= ["active","disabled"];
|
87
|
this.userOptions = {
|
88
|
maxWidth: 500,
|
89
|
minWidth:-1,
|
90
|
maxHeight: 400,
|
91
|
translations: { "all": "All", "items": "items","selectAll":"Vybrat vše","clearAll":"Vyčistit filr"},
|
92
|
search: false,
|
93
|
placeHolder: "Nevybráno",
|
94
|
stayOpen:false,
|
95
|
disableSelectAll: false,
|
96
|
}
|
97
|
if (options) {
|
98
|
if (options.maxWidth != undefined) {
|
99
|
this.userOptions.maxWidth = options.maxWidth;
|
100
|
}
|
101
|
if (options.minWidth != undefined) {
|
102
|
this.userOptions.minWidth = options.minWidth;
|
103
|
}
|
104
|
if (options.maxHeight != undefined) {
|
105
|
this.userOptions.maxHeight = options.maxHeight;
|
106
|
}
|
107
|
if (options.translations != undefined) {
|
108
|
for (var property in options.translations) {
|
109
|
if (options.translations.hasOwnProperty(property)) {
|
110
|
if(this.userOptions.translations[property] ){
|
111
|
this.userOptions.translations[property] = options.translations[property];
|
112
|
}
|
113
|
}
|
114
|
}
|
115
|
}
|
116
|
if (options.placeHolder != undefined) {
|
117
|
this.userOptions.placeHolder = options.placeHolder;
|
118
|
}
|
119
|
if (options.search != undefined) {
|
120
|
this.search = options.search;
|
121
|
}
|
122
|
if (options.stayOpen != undefined) {
|
123
|
this.userOptions.stayOpen = options.stayOpen;
|
124
|
}
|
125
|
if (options.disableSelectAll != undefined) {
|
126
|
this.userOptions.disableSelectAll = options.disableSelectAll;
|
127
|
}
|
128
|
}
|
129
|
|
130
|
this.closeOrder=function(){
|
131
|
let self = this;
|
132
|
if(!self.userOptions.stayOpen){
|
133
|
self.drop.style.visibility = "hidden";
|
134
|
if(self.search){
|
135
|
self.inputBox.value = "";
|
136
|
Array.prototype.slice.call(self.listElements).forEach(function (x) {
|
137
|
x.classList.remove("hide");
|
138
|
});
|
139
|
}
|
140
|
}
|
141
|
}
|
142
|
|
143
|
this.getCssArray =function(selector){
|
144
|
let cssArray = [];
|
145
|
if(selector === ".vsb-main button"){
|
146
|
cssArray= [
|
147
|
{"key":"min-width","value":"120px"},
|
148
|
{"key":"border-radius","value":"0"},
|
149
|
{"key":"width","value":"100%"},
|
150
|
{"key":"text-align","value":"left"},
|
151
|
{"key":"z-index","value":"1"},
|
152
|
{"key":"color","value":"#333"},
|
153
|
{"key":"background","value":"white !important"},
|
154
|
{"key":"border","value":"1px solid #999 !important"},
|
155
|
{"key":"line-height","value":"20px"},
|
156
|
{"key":"font-size","value":"14px"},
|
157
|
{"key":"padding","value":"6px 12px"}
|
158
|
]
|
159
|
}
|
160
|
|
161
|
return cssArrayToString(cssArray);
|
162
|
|
163
|
function cssArrayToString(cssList){
|
164
|
let list = "";
|
165
|
cssList.forEach(function(x){
|
166
|
list += x.key + ":" + x.value + ";";
|
167
|
});
|
168
|
return list;
|
169
|
}
|
170
|
}
|
171
|
|
172
|
this.init = function () {
|
173
|
let self = this;
|
174
|
this.root.style.display = "none";
|
175
|
let already = document.getElementById("btn-group-" + self.domSelector);
|
176
|
if (already) {
|
177
|
already.remove();
|
178
|
}
|
179
|
|
180
|
this.main = document.createElement("div");
|
181
|
this.root.parentNode.insertBefore(this.main, this.root.nextSibling);
|
182
|
this.main.classList.add("vsb-main");
|
183
|
this.main.setAttribute("id", "btn-group-" + this.domSelector);
|
184
|
this.main.style.marginLeft = this.main.style.marginLeft;
|
185
|
if(self.userOptions.stayOpen){
|
186
|
this.main.style.minHeight = (this.userOptions.maxHeight+10) + "px";
|
187
|
}
|
188
|
|
189
|
if(self.userOptions.stayOpen){
|
190
|
this.button = document.createElement("div");
|
191
|
}else{
|
192
|
this.button = document.createElement("button");
|
193
|
var cssList = self.getCssArray(".vsb-main button");
|
194
|
this.button.setAttribute("style", cssList);
|
195
|
}
|
196
|
this.button.style.maxWidth = this.userOptions.maxWidth + "px";
|
197
|
if(this.userOptions.minWidth !== -1){
|
198
|
this.button.style.minWidth = this.userOptions.minWidth + "px";
|
199
|
}
|
200
|
|
201
|
this.main.appendChild(this.button);
|
202
|
this.title = document.createElement("span");
|
203
|
this.button.appendChild(this.title);
|
204
|
this.title.classList.add("title");
|
205
|
let caret = document.createElement("span");
|
206
|
this.button.appendChild(caret);
|
207
|
|
208
|
caret.classList.add("caret");
|
209
|
caret.style.position = "absolute";
|
210
|
caret.style.right = "8px";
|
211
|
caret.style.marginTop = "8px";
|
212
|
|
213
|
if(self.userOptions.stayOpen){
|
214
|
caret.style.display = "none";
|
215
|
this.title.style.paddingLeft = "20px";
|
216
|
this.title.style.fontStyle = "italic";
|
217
|
this.title.style.verticalAlign = "20%";
|
218
|
}
|
219
|
|
220
|
this.drop = document.createElement("div");
|
221
|
this.main.appendChild(this.drop);
|
222
|
this.drop.classList.add("vsb-menu");
|
223
|
this.drop.style.zIndex = 2000 - this.instanceOffset;
|
224
|
this.ul = document.createElement("ul");
|
225
|
this.drop.appendChild(this.ul);
|
226
|
|
227
|
this.ul.style.maxHeight = this.userOptions.maxHeight + "px";
|
228
|
this.ul.style.minWidth = this.ulminWidth + "px";
|
229
|
this.ul.style.minHeight = this.ulminHeight + "px";
|
230
|
if (this.isMultiple) {
|
231
|
this.ul.classList.add("multi");
|
232
|
if (!self.userOptions.disableSelectAll) {
|
233
|
let selectAll = document.createElement("option");
|
234
|
selectAll.setAttribute("value", 'all');
|
235
|
selectAll.innerText = self.userOptions.translations.selectAll;
|
236
|
this.root.insertBefore(selectAll,(this.root.hasChildNodes())
|
237
|
? this.root.childNodes[0]
|
238
|
: null);
|
239
|
}
|
240
|
}
|
241
|
let selectedTexts = "";
|
242
|
let sep = "";
|
243
|
let nrActives = 0;
|
244
|
|
245
|
if (this.search) {
|
246
|
this.searchZone = document.createElement("div");
|
247
|
this.ul.appendChild(this.searchZone);
|
248
|
this.searchZone.classList.add("vsb-js-search-zone");
|
249
|
this.searchZone.style.zIndex = 2001 - this.instanceOffset;
|
250
|
this.inputBox = document.createElement("input");
|
251
|
this.searchZone.appendChild(this.inputBox);
|
252
|
this.inputBox.setAttribute("type", "text");
|
253
|
this.inputBox.setAttribute("id", "search_" + this.domSelector);
|
254
|
|
255
|
let fontSizeForP = this.isMultiple ? "12px" : "6px";
|
256
|
var para = document.createElement("p");
|
257
|
this.ul.appendChild(para);
|
258
|
para.style.fontSize = fontSizeForP;
|
259
|
para.innerHTML = " ";
|
260
|
this.ul.addEventListener("scroll", function (e) {
|
261
|
var y = this.scrollTop;
|
262
|
self.searchZone.parentNode.style.top = y + "px";
|
263
|
});
|
264
|
}
|
265
|
|
266
|
this.options = document.querySelectorAll(this.domSelector + " > option");
|
267
|
Array.prototype.slice.call(this.options).forEach(function (x) {
|
268
|
let text = x.textContent;
|
269
|
let value = x.value;
|
270
|
let originalAttrs;
|
271
|
if (x.hasAttributes()) {
|
272
|
originalAttrs = Array.prototype.slice.call(x.attributes)
|
273
|
.filter(function(a){
|
274
|
return self.forbidenAttributes.indexOf(a.name) === -1
|
275
|
});
|
276
|
}
|
277
|
let classes = x.getAttribute("class");
|
278
|
if(classes)
|
279
|
{
|
280
|
classes=classes
|
281
|
.split(" ")
|
282
|
.filter(function(c){
|
283
|
return self.forbidenClasses.indexOf(c) === -1
|
284
|
});
|
285
|
}else
|
286
|
{
|
287
|
classes=[];
|
288
|
}
|
289
|
let li = document.createElement("li");
|
290
|
let isSelected = x.hasAttribute("selected");
|
291
|
let isDisabled = x.hasAttribute("disabled");
|
292
|
|
293
|
self.ul.appendChild(li);
|
294
|
li.setAttribute("data-value", value);
|
295
|
li.setAttribute("data-text", text);
|
296
|
|
297
|
if(originalAttrs !== undefined){
|
298
|
originalAttrs.forEach(function(a){
|
299
|
li.setAttribute(a.name, a.value);
|
300
|
});
|
301
|
}
|
302
|
|
303
|
classes.forEach(function(x){
|
304
|
li.classList.add(x);
|
305
|
});
|
306
|
|
307
|
if (isSelected) {
|
308
|
nrActives++;
|
309
|
selectedTexts += sep + text;
|
310
|
sep = ",";
|
311
|
li.classList.add("active");
|
312
|
if (!self.isMultiple) {
|
313
|
self.title.textContent = text;
|
314
|
if (classes.length != 0) {
|
315
|
classes.forEach(function(x){
|
316
|
self.title.classList.add(x);
|
317
|
});
|
318
|
}
|
319
|
}
|
320
|
}
|
321
|
if(isDisabled){
|
322
|
li.classList.add("disabled");
|
323
|
}
|
324
|
li.appendChild(document.createTextNode(text));
|
325
|
});
|
326
|
|
327
|
if (document.querySelector(this.domSelector + ' optgroup') !== null) {
|
328
|
this.options = document.querySelectorAll(this.domSelector + " option");
|
329
|
let groups = document.querySelectorAll(this.domSelector + ' optgroup');
|
330
|
Array.prototype.slice.call(groups).forEach(function(group) {
|
331
|
let groupOptions = group.querySelectorAll('option');
|
332
|
let li = document.createElement("li");
|
333
|
li.classList.add('grouped-option');
|
334
|
li.appendChild(document.createTextNode(group.label));
|
335
|
self.ul.appendChild(li);
|
336
|
|
337
|
Array.prototype.slice.call(groupOptions).forEach(function(x) {
|
338
|
let text = x.textContent;
|
339
|
let value = x.value;
|
340
|
let classes = x.getAttribute("class");
|
341
|
if(classes)
|
342
|
{
|
343
|
classes=classes.split(" ");
|
344
|
}
|
345
|
else
|
346
|
{
|
347
|
classes=[];
|
348
|
}
|
349
|
let li = document.createElement("li");
|
350
|
let isSelected = x.hasAttribute("selected");
|
351
|
self.ul.appendChild(li);
|
352
|
li.setAttribute("data-value", value);
|
353
|
li.setAttribute("data-text", text);
|
354
|
if (classes.length != 0) {
|
355
|
classes.forEach(function(x){
|
356
|
li.classList.add(x);
|
357
|
});
|
358
|
|
359
|
}
|
360
|
if (isSelected) {
|
361
|
nrActives++;
|
362
|
selectedTexts += sep + text;
|
363
|
sep = ",";
|
364
|
li.classList.add("active");
|
365
|
if (!self.isMultiple) {
|
366
|
self.title.textContent = text;
|
367
|
if (classes.length != 0) {
|
368
|
classes.forEach(function(x){
|
369
|
self.title.classList.add(x);
|
370
|
});
|
371
|
}
|
372
|
}
|
373
|
}
|
374
|
li.appendChild(document.createTextNode(text));
|
375
|
})
|
376
|
})
|
377
|
}
|
378
|
|
379
|
if (self.multipleSize != -1) {
|
380
|
if (nrActives > self.multipleSize) {
|
381
|
let wordForItems = self.userOptions.translations.items || "items"
|
382
|
selectedTexts = nrActives + " " + wordForItems;
|
383
|
}
|
384
|
}
|
385
|
if (self.isMultiple) {
|
386
|
self.title.innerHTML = selectedTexts;
|
387
|
}
|
388
|
if (self.userOptions.placeHolder != "" && self.title.textContent == "") {
|
389
|
self.title.textContent = self.userOptions.placeHolder;
|
390
|
}
|
391
|
this.listElements = this.drop.querySelectorAll("li:not(.grouped-option)");
|
392
|
if (self.search) {
|
393
|
self.inputBox.addEventListener("keyup", function (e) {
|
394
|
let searchValue = e.target.value.toUpperCase();
|
395
|
let searchValueLength = searchValue.length;
|
396
|
let nrFound = 0;
|
397
|
let nrChecked = 0;
|
398
|
let selectAll = null;
|
399
|
if (searchValueLength < 2) {
|
400
|
Array.prototype.slice.call(self.listElements).forEach(function (x) {
|
401
|
if (x.getAttribute('data-value') === 'all') {
|
402
|
selectAll = x;
|
403
|
}else{
|
404
|
x.classList.remove("hidden-search");
|
405
|
nrFound++;
|
406
|
nrChecked += x.classList.contains('active');
|
407
|
}
|
408
|
});
|
409
|
} else {
|
410
|
Array.prototype.slice.call(self.listElements).forEach(function (x) {
|
411
|
if (x.getAttribute('data-value') !== 'all') {
|
412
|
let text = x.getAttribute("data-text").toUpperCase();
|
413
|
if (text.indexOf(searchValue) === -1 && x.getAttribute('data-value') !== 'all') {
|
414
|
x.classList.add("hidden-search");
|
415
|
} else {
|
416
|
nrFound++;
|
417
|
x.classList.remove("hidden-search");
|
418
|
nrChecked += x.classList.contains('active');
|
419
|
}
|
420
|
}else{
|
421
|
selectAll = x;
|
422
|
}
|
423
|
});
|
424
|
}
|
425
|
if(selectAll){
|
426
|
if(nrFound === 0){
|
427
|
selectAll.classList.add('disabled');
|
428
|
}else{
|
429
|
selectAll.classList.remove('disabled');
|
430
|
}
|
431
|
if( nrChecked !== nrFound){
|
432
|
selectAll.classList.remove("active");
|
433
|
selectAll.innerText = self.userOptions.translations.selectAll;
|
434
|
selectAll.setAttribute('data-selected', 'false')
|
435
|
}else{
|
436
|
selectAll.classList.add("active");
|
437
|
selectAll.innerText = self.userOptions.translations.clearAll;
|
438
|
selectAll.setAttribute('data-selected', 'true')
|
439
|
}
|
440
|
}
|
441
|
});
|
442
|
}
|
443
|
|
444
|
if(self.userOptions.stayOpen){
|
445
|
self.drop.style.visibility = "visible";
|
446
|
self.drop.style.boxShadow = "none";
|
447
|
self.drop.style.minHeight = (this.userOptions.maxHeight+10) + "px";
|
448
|
self.drop.style.position = "relative";
|
449
|
self.drop.style.left = "0px";
|
450
|
self.drop.style.top = "0px";
|
451
|
self.button.style.border = "none";
|
452
|
}else{
|
453
|
this.main.addEventListener("click", function (e) {
|
454
|
if (self.isDisabled) return;
|
455
|
self.drop.style.left = self.left + "px";
|
456
|
self.drop.style.top = self.top + "px";
|
457
|
self.drop.style.visibility = "visible";
|
458
|
document.addEventListener("click", docListener);
|
459
|
e.preventDefault();
|
460
|
e.stopPropagation();
|
461
|
if(!self.userOptions.stayOpen ){
|
462
|
VSBoxCounter.closeAllButMe(self.instanceOffset);
|
463
|
}
|
464
|
});
|
465
|
}
|
466
|
this.drop.addEventListener("click", function (e) {
|
467
|
if (self.isDisabled) return;
|
468
|
|
469
|
if (!e.target.hasAttribute("data-value")) {
|
470
|
e.preventDefault();
|
471
|
e.stopPropagation();
|
472
|
return;
|
473
|
}
|
474
|
let choiceValue = e.target.getAttribute("data-value");
|
475
|
let choiceText = e.target.getAttribute("data-text");
|
476
|
let className = e.target.getAttribute("class");
|
477
|
|
478
|
if(className &&className.indexOf("disabled") != -1){
|
479
|
return;
|
480
|
}
|
481
|
|
482
|
if (choiceValue === 'all') {
|
483
|
if (e.target.hasAttribute('data-selected')
|
484
|
&& e.target.getAttribute('data-selected') === 'true') {
|
485
|
self.setValue('none')
|
486
|
} else {
|
487
|
self.setValue('all');
|
488
|
}
|
489
|
return;
|
490
|
}
|
491
|
|
492
|
if (!self.isMultiple) {
|
493
|
self.root.value = choiceValue;
|
494
|
self.title.textContent = choiceText;
|
495
|
if (className) {
|
496
|
self.title.setAttribute("class", className + " title");
|
497
|
} else {
|
498
|
self.title.setAttribute("class", "title");
|
499
|
}
|
500
|
Array.prototype.slice.call(self.listElements).forEach(function (x) {
|
501
|
x.classList.remove("active");
|
502
|
});
|
503
|
if (choiceText != "") {
|
504
|
e.target.classList.add("active");
|
505
|
}
|
506
|
self.privateSendChange();
|
507
|
if(!self.userOptions.stayOpen){
|
508
|
docListener();
|
509
|
}
|
510
|
} else {
|
511
|
let wasActive = false;
|
512
|
if (className) {
|
513
|
wasActive = className.indexOf("active") != -1;
|
514
|
}
|
515
|
if (wasActive) {
|
516
|
e.target.classList.remove("active");
|
517
|
} else {
|
518
|
e.target.classList.add("active");
|
519
|
}
|
520
|
let selectedTexts = "";
|
521
|
let sep = "";
|
522
|
let nrActives = 0;
|
523
|
let nrAll = 0;
|
524
|
for (let i = 0; i < self.options.length; i++) {
|
525
|
nrAll++;
|
526
|
if (self.options[i].value == choiceValue) {
|
527
|
self.options[i].selected = !wasActive;
|
528
|
}
|
529
|
if (self.options[i].selected) {
|
530
|
nrActives++;
|
531
|
selectedTexts += sep + self.options[i].textContent;
|
532
|
sep = ",";
|
533
|
}
|
534
|
}
|
535
|
if (nrAll == nrActives) {
|
536
|
let wordForAll = self.userOptions.translations.all || "all";
|
537
|
selectedTexts = wordForAll;
|
538
|
} else if (self.multipleSize != -1) {
|
539
|
if (nrActives > self.multipleSize) {
|
540
|
let wordForItems = self.userOptions.translations.items || "items"
|
541
|
selectedTexts = nrActives + " " + wordForItems;
|
542
|
}
|
543
|
}
|
544
|
//self.title.textContent = selectedTexts;
|
545
|
let result = nrActives;
|
546
|
if(nrActives < 1){
|
547
|
result = "Nevybráno";
|
548
|
} else if(nrActives === 1){
|
549
|
result += " rukopis";
|
550
|
} else if(nrActives < 5){
|
551
|
result += " rukopisy";
|
552
|
} else {
|
553
|
result += " rukopisů";
|
554
|
}
|
555
|
|
556
|
self.title.textContent = result;
|
557
|
|
558
|
self.checkUncheckAll();
|
559
|
self.privateSendChange();
|
560
|
}
|
561
|
e.preventDefault();
|
562
|
e.stopPropagation();
|
563
|
if (self.userOptions.placeHolder != "" && self.title.textContent == "") {
|
564
|
self.title.textContent = self.userOptions.placeHolder;
|
565
|
}
|
566
|
});
|
567
|
function docListener() {
|
568
|
document.removeEventListener("click", docListener);
|
569
|
self.drop.style.visibility = "hidden";
|
570
|
if(self.search){
|
571
|
self.inputBox.value = "";
|
572
|
Array.prototype.slice.call(self.listElements).forEach(function (x) {
|
573
|
x.classList.remove("hidden-search");
|
574
|
});
|
575
|
}
|
576
|
}
|
577
|
}
|
578
|
this.init();
|
579
|
this.checkUncheckAll();
|
580
|
}
|
581
|
|
582
|
vanillaSelectBox.prototype.disableItems = function (values) {
|
583
|
let self = this;
|
584
|
let foundValues = [];
|
585
|
if (vanillaSelectBox_type(values) == "string") {
|
586
|
values = values.split(",");
|
587
|
}
|
588
|
|
589
|
if(vanillaSelectBox_type(values) == "array"){
|
590
|
Array.prototype.slice.call(self.options).forEach(function (x) {
|
591
|
if (values.indexOf(x.value) != -1) {
|
592
|
foundValues.push(x.value);
|
593
|
x.setAttribute("disabled","");
|
594
|
}
|
595
|
});
|
596
|
}
|
597
|
Array.prototype.slice.call(self.listElements).forEach(function (x) {
|
598
|
let val = x.getAttribute("data-value");
|
599
|
if (foundValues.indexOf(val) != -1) {
|
600
|
x.classList.add("disabled");
|
601
|
}
|
602
|
});
|
603
|
}
|
604
|
|
605
|
vanillaSelectBox.prototype.enableItems = function (values) {
|
606
|
let self = this;
|
607
|
let foundValues = [];
|
608
|
if (vanillaSelectBox_type(values) == "string") {
|
609
|
values = values.split(",");
|
610
|
}
|
611
|
|
612
|
if(vanillaSelectBox_type(values) == "array"){
|
613
|
Array.prototype.slice.call(self.options).forEach(function (x) {
|
614
|
if (values.indexOf(x.value) != -1) {
|
615
|
foundValues.push(x.value);
|
616
|
x.removeAttribute("disabled");
|
617
|
}
|
618
|
});
|
619
|
}
|
620
|
|
621
|
Array.prototype.slice.call(self.listElements).forEach(function (x) {
|
622
|
if (foundValues.indexOf(x.getAttribute("data-value")) != -1) {
|
623
|
x.classList.remove("disabled");
|
624
|
}
|
625
|
});
|
626
|
}
|
627
|
|
628
|
vanillaSelectBox.prototype.checkUncheckAll = function () {
|
629
|
let self = this;
|
630
|
if (self.isMultiple) {
|
631
|
let nrChecked = 0;
|
632
|
let nrCheckable = 0;
|
633
|
let checkAllElement = null;
|
634
|
|
635
|
Array.prototype.slice.call(self.listElements).forEach(function (x) {
|
636
|
if (x.hasAttribute('data-value')){
|
637
|
if(x.getAttribute('data-value') === 'all'){
|
638
|
checkAllElement = x;
|
639
|
}
|
640
|
if (x.getAttribute('data-value') !== 'all'
|
641
|
&& !x.classList.contains('hidden-search')
|
642
|
&& !x.classList.contains('disabled')) {
|
643
|
nrCheckable++;
|
644
|
nrChecked += x.classList.contains('active');
|
645
|
}
|
646
|
}
|
647
|
});
|
648
|
|
649
|
if(checkAllElement ){
|
650
|
if(nrChecked === nrCheckable){
|
651
|
// check the checkAll checkbox
|
652
|
checkAllElement.classList.add("active");
|
653
|
checkAllElement.innerText = self.userOptions.translations.clearAll;
|
654
|
checkAllElement.setAttribute('data-selected', 'true')
|
655
|
}else if(nrChecked === 0){
|
656
|
// uncheck the checkAll checkbox
|
657
|
checkAllElement.classList.remove("active");
|
658
|
checkAllElement.innerText = self.userOptions.translations.selectAll;
|
659
|
checkAllElement.setAttribute('data-selected', 'false')
|
660
|
}
|
661
|
}
|
662
|
}
|
663
|
}
|
664
|
|
665
|
|
666
|
vanillaSelectBox.prototype.setValue = function (values) {
|
667
|
let self = this;
|
668
|
if (values == null || values == undefined || values == "") {
|
669
|
self.empty();
|
670
|
} else {
|
671
|
if (self.isMultiple) {
|
672
|
if (vanillaSelectBox_type(values) == "string") {
|
673
|
if (values === "all") {
|
674
|
values = [];
|
675
|
Array.prototype.slice.call(self.listElements).forEach(function (x) {
|
676
|
if (x.hasAttribute('data-value')){
|
677
|
let value = x.getAttribute('data-value');
|
678
|
if (value !== 'all'){
|
679
|
if(!x.classList.contains('hidden-search') && !x.classList.contains('disabled')) {
|
680
|
values.push(x.getAttribute('data-value'));
|
681
|
}
|
682
|
// already checked (but hidden by search)
|
683
|
if(x.classList.contains('active')){
|
684
|
if(x.classList.contains('hidden-search') || x.classList.contains('disabled')){
|
685
|
values.push(value);
|
686
|
}
|
687
|
}
|
688
|
}
|
689
|
}
|
690
|
});
|
691
|
} else if (values === "none") {
|
692
|
values = [];
|
693
|
Array.prototype.slice.call(self.listElements).forEach(function (x) {
|
694
|
if (x.hasAttribute('data-value')){
|
695
|
let value = x.getAttribute('data-value');
|
696
|
if (value !== 'all'){
|
697
|
if(x.classList.contains('active')){
|
698
|
if(x.classList.contains('hidden-search') || x.classList.contains('disabled')){
|
699
|
values.push(value);
|
700
|
}
|
701
|
}
|
702
|
}
|
703
|
}
|
704
|
});
|
705
|
}else {
|
706
|
values = values.split(",");
|
707
|
}
|
708
|
}
|
709
|
let foundValues = [];
|
710
|
if (vanillaSelectBox_type(values) == "array") {
|
711
|
Array.prototype.slice.call(self.options).forEach(function (x) {
|
712
|
if (values.indexOf(x.value) !== -1) {
|
713
|
x.selected = true;
|
714
|
foundValues.push(x.value);
|
715
|
} else {
|
716
|
x.selected = false;
|
717
|
}
|
718
|
});
|
719
|
let selectedTexts = ""
|
720
|
let sep = "";
|
721
|
let nrActives = 0;
|
722
|
let nrAll = 0;
|
723
|
Array.prototype.slice.call(self.listElements).forEach(function (x) {
|
724
|
nrAll++;
|
725
|
if (foundValues.indexOf(x.getAttribute("data-value")) != -1) {
|
726
|
x.classList.add("active");
|
727
|
nrActives++;
|
728
|
selectedTexts += sep + x.getAttribute("data-text");
|
729
|
sep = ",";
|
730
|
} else {
|
731
|
x.classList.remove("active");
|
732
|
}
|
733
|
});
|
734
|
if (nrAll == nrActives) {
|
735
|
let wordForAll = self.userOptions.translations.all || "all";
|
736
|
selectedTexts = wordForAll;
|
737
|
} else if (self.multipleSize != -1) {
|
738
|
if (nrActives > self.multipleSize) {
|
739
|
let wordForItems = self.userOptions.translations.items || "items"
|
740
|
selectedTexts = nrActives + " " + wordForItems;
|
741
|
}
|
742
|
}
|
743
|
//self.title.textContent = selectedTexts;
|
744
|
let result = nrActives;
|
745
|
if(nrActives < 1){
|
746
|
result = "Nevybráno";
|
747
|
} else if(nrActives === 1){
|
748
|
result += " rukopis";
|
749
|
} else if(nrActives < 5){
|
750
|
result += " rukopisy";
|
751
|
} else {
|
752
|
result += " rukopisů";
|
753
|
}
|
754
|
|
755
|
self.title.textContent = result;
|
756
|
self.privateSendChange();
|
757
|
}
|
758
|
self.checkUncheckAll();
|
759
|
} else {
|
760
|
let found = false;
|
761
|
let text = "";
|
762
|
let classNames = ""
|
763
|
Array.prototype.slice.call(self.listElements).forEach(function (x) {
|
764
|
if (x.getAttribute("data-value") == values) {
|
765
|
x.classList.add("active");
|
766
|
found = true;
|
767
|
text = x.getAttribute("data-text")
|
768
|
} else {
|
769
|
x.classList.remove("active");
|
770
|
}
|
771
|
});
|
772
|
Array.prototype.slice.call(self.options).forEach(function (x) {
|
773
|
if (x.value == values) {
|
774
|
x.selected = true;
|
775
|
className = x.getAttribute("class");
|
776
|
if (!className) className = "";
|
777
|
} else {
|
778
|
x.selected = false;
|
779
|
}
|
780
|
});
|
781
|
if (found) {
|
782
|
self.title.textContent = text;
|
783
|
if (self.userOptions.placeHolder != "" && self.title.textContent == "") {
|
784
|
self.title.textContent = self.userOptions.placeHolder;
|
785
|
}
|
786
|
if (className != "") {
|
787
|
self.title.setAttribute("class", className + " title");
|
788
|
} else {
|
789
|
self.title.setAttribute("class", "title");
|
790
|
}
|
791
|
}
|
792
|
}
|
793
|
}
|
794
|
}
|
795
|
|
796
|
vanillaSelectBox.prototype.privateSendChange = function () {
|
797
|
let event = document.createEvent('HTMLEvents');
|
798
|
event.initEvent('change', true, false);
|
799
|
this.root.dispatchEvent(event);
|
800
|
}
|
801
|
|
802
|
vanillaSelectBox.prototype.empty = function () {
|
803
|
Array.prototype.slice.call(this.listElements).forEach(function (x) {
|
804
|
x.classList.remove("active");
|
805
|
});
|
806
|
Array.prototype.slice.call(this.options).forEach(function (x) {
|
807
|
x.selected = false;
|
808
|
});
|
809
|
this.title.textContent = "";
|
810
|
if (this.userOptions.placeHolder != "" && this.title.textContent == "") {
|
811
|
this.title.textContent = this.userOptions.placeHolder;
|
812
|
}
|
813
|
this.checkUncheckAll();
|
814
|
this.privateSendChange();
|
815
|
}
|
816
|
|
817
|
vanillaSelectBox.prototype.destroy = function () {
|
818
|
let already = document.getElementById("btn-group-" + this.domSelector);
|
819
|
if (already) {
|
820
|
VSBoxCounter.remove(this.instanceOffset);
|
821
|
already.remove();
|
822
|
this.root.style.display = "inline-block";
|
823
|
}
|
824
|
}
|
825
|
vanillaSelectBox.prototype.disable = function () {
|
826
|
let already = document.getElementById("btn-group-" + this.domSelector);
|
827
|
if (already) {
|
828
|
button = already.querySelector("button")
|
829
|
if(button) button.classList.add("disabled");
|
830
|
this.isDisabled = true;
|
831
|
}
|
832
|
}
|
833
|
vanillaSelectBox.prototype.enable = function () {
|
834
|
let already = document.getElementById("btn-group-" + this.domSelector);
|
835
|
if (already) {
|
836
|
button = already.querySelector("button")
|
837
|
if(button) button.classList.remove("disabled");
|
838
|
this.isDisabled = false;
|
839
|
}
|
840
|
}
|
841
|
|
842
|
vanillaSelectBox.prototype.showOptions = function(){
|
843
|
console.log(this.userOptions);
|
844
|
}
|
845
|
// Polyfills for IE
|
846
|
if (!('remove' in Element.prototype)) {
|
847
|
Element.prototype.remove = function () {
|
848
|
if (this.parentNode) {
|
849
|
this.parentNode.removeChild(this);
|
850
|
}
|
851
|
};
|
852
|
}
|
853
|
|
854
|
function vanillaSelectBox_type(target) {
|
855
|
const computedType = Object.prototype.toString.call(target);
|
856
|
const stripped = computedType.replace("[object ", "").replace("]", "");
|
857
|
const lowercased = stripped.toLowerCase();
|
858
|
return lowercased;
|
859
|
}
|