|
1
|
'use strict'; |
|
2
|
{ |
|
3
|
const SelectBox = { |
|
4
|
cache: {}, |
|
5
|
init: function(id) { |
|
6
|
const box = document.getElementById(id); |
|
7
|
SelectBox.cache[id] = []; |
|
8
|
const cache = SelectBox.cache[id]; |
|
9
|
for (const node of box.options) { |
|
10
|
cache.push({value: node.value, text: node.text, displayed: 1}); |
|
11
|
} |
|
12
|
}, |
|
13
|
redisplay: function(id) { |
|
14
|
// Repopulate HTML select box from cache |
|
15
|
const box = document.getElementById(id); |
|
16
|
const scroll_value_from_top = box.scrollTop; |
|
17
|
box.innerHTML = ''; |
|
18
|
for (const node of SelectBox.cache[id]) { |
|
19
|
if (node.displayed) { |
|
20
|
const new_option = new Option(node.text, node.value, false, false); |
|
21
|
// Shows a tooltip when hovering over the option |
|
22
|
new_option.title = node.text; |
|
23
|
box.appendChild(new_option); |
|
24
|
} |
|
25
|
} |
|
26
|
box.scrollTop = scroll_value_from_top; |
|
27
|
}, |
|
28
|
filter: function(id, text) { |
|
29
|
// Redisplay the HTML select box, displaying only the choices containing ALL |
|
30
|
// the words in text. (It's an AND search.) |
|
31
|
const tokens = text.toLowerCase().split(/\s+/); |
|
32
|
for (const node of SelectBox.cache[id]) { |
|
33
|
node.displayed = 1; |
|
34
|
const node_text = node.text.toLowerCase(); |
|
35
|
for (const token of tokens) { |
|
36
|
if (!node_text.includes(token)) { |
|
37
|
node.displayed = 0; |
|
38
|
break; // Once the first token isn't found we're done |
|
39
|
} |
|
40
|
} |
|
41
|
} |
|
42
|
SelectBox.redisplay(id); |
|
43
|
}, |
|
44
|
get_hidden_node_count(id) { |
|
45
|
const cache = SelectBox.cache[id] || []; |
|
46
|
return cache.filter(node => node.displayed === 0).length; |
|
47
|
}, |
|
48
|
delete_from_cache: function(id, value) { |
|
49
|
let delete_index = null; |
|
50
|
const cache = SelectBox.cache[id]; |
|
51
|
for (const [i, node] of cache.entries()) { |
|
52
|
if (node.value === value) { |
|
53
|
delete_index = i; |
|
54
|
break; |
|
55
|
} |
|
56
|
} |
|
57
|
cache.splice(delete_index, 1); |
|
58
|
}, |
|
59
|
add_to_cache: function(id, option) { |
|
60
|
SelectBox.cache[id].push({value: option.value, text: option.text, displayed: 1}); |
|
61
|
}, |
|
62
|
cache_contains: function(id, value) { |
|
63
|
// Check if an item is contained in the cache |
|
64
|
for (const node of SelectBox.cache[id]) { |
|
65
|
if (node.value === value) { |
|
66
|
return true; |
|
67
|
} |
|
68
|
} |
|
69
|
return false; |
|
70
|
}, |
|
71
|
move: function(from, to) { |
|
72
|
const from_box = document.getElementById(from); |
|
73
|
for (const option of from_box.options) { |
|
74
|
const option_value = option.value; |
|
75
|
if (option.selected && SelectBox.cache_contains(from, option_value)) { |
|
76
|
SelectBox.add_to_cache(to, {value: option_value, text: option.text, displayed: 1}); |
|
77
|
SelectBox.delete_from_cache(from, option_value); |
|
78
|
} |
|
79
|
} |
|
80
|
SelectBox.redisplay(from); |
|
81
|
SelectBox.redisplay(to); |
|
82
|
}, |
|
83
|
move_all: function(from, to) { |
|
84
|
const from_box = document.getElementById(from); |
|
85
|
for (const option of from_box.options) { |
|
86
|
const option_value = option.value; |
|
87
|
if (SelectBox.cache_contains(from, option_value)) { |
|
88
|
SelectBox.add_to_cache(to, {value: option_value, text: option.text, displayed: 1}); |
|
89
|
SelectBox.delete_from_cache(from, option_value); |
|
90
|
} |
|
91
|
} |
|
92
|
SelectBox.redisplay(from); |
|
93
|
SelectBox.redisplay(to); |
|
94
|
}, |
|
95
|
sort: function(id) { |
|
96
|
SelectBox.cache[id].sort(function(a, b) { |
|
97
|
a = a.text.toLowerCase(); |
|
98
|
b = b.text.toLowerCase(); |
|
99
|
if (a > b) { |
|
100
|
return 1; |
|
101
|
} |
|
102
|
if (a < b) { |
|
103
|
return -1; |
|
104
|
} |
|
105
|
return 0; |
|
106
|
} ); |
|
107
|
}, |
|
108
|
select_all: function(id) { |
|
109
|
const box = document.getElementById(id); |
|
110
|
for (const option of box.options) { |
|
111
|
option.selected = true; |
|
112
|
} |
|
113
|
} |
|
114
|
}; |
|
115
|
window.SelectBox = SelectBox; |
|
116
|
} |
|
117
|
|