2020-10-24 02:58:05 +00:00
|
|
|
'use strict';
|
|
|
|
{
|
|
|
|
const SelectBox = {
|
2018-10-12 03:11:57 +00:00
|
|
|
cache: {},
|
|
|
|
init: function(id) {
|
2020-10-24 02:58:05 +00:00
|
|
|
const box = document.getElementById(id);
|
2018-10-12 03:11:57 +00:00
|
|
|
SelectBox.cache[id] = [];
|
2020-10-24 02:58:05 +00:00
|
|
|
const cache = SelectBox.cache[id];
|
|
|
|
for (const node of box.options) {
|
2018-10-12 03:11:57 +00:00
|
|
|
cache.push({value: node.value, text: node.text, displayed: 1});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
redisplay: function(id) {
|
|
|
|
// Repopulate HTML select box from cache
|
2020-10-24 02:58:05 +00:00
|
|
|
const box = document.getElementById(id);
|
2021-04-11 19:51:12 +00:00
|
|
|
const scroll_value_from_top = box.scrollTop;
|
2020-10-24 02:58:05 +00:00
|
|
|
box.innerHTML = '';
|
|
|
|
for (const node of SelectBox.cache[id]) {
|
2018-10-12 03:11:57 +00:00
|
|
|
if (node.displayed) {
|
2020-10-24 02:58:05 +00:00
|
|
|
const new_option = new Option(node.text, node.value, false, false);
|
2018-10-12 03:11:57 +00:00
|
|
|
// Shows a tooltip when hovering over the option
|
2020-10-24 02:58:05 +00:00
|
|
|
new_option.title = node.text;
|
|
|
|
box.appendChild(new_option);
|
2018-10-12 03:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-11 19:51:12 +00:00
|
|
|
box.scrollTop = scroll_value_from_top;
|
2018-10-12 03:11:57 +00:00
|
|
|
},
|
|
|
|
filter: function(id, text) {
|
|
|
|
// Redisplay the HTML select box, displaying only the choices containing ALL
|
|
|
|
// the words in text. (It's an AND search.)
|
2020-10-24 02:58:05 +00:00
|
|
|
const tokens = text.toLowerCase().split(/\s+/);
|
|
|
|
for (const node of SelectBox.cache[id]) {
|
2018-10-12 03:11:57 +00:00
|
|
|
node.displayed = 1;
|
2020-10-24 02:58:05 +00:00
|
|
|
const node_text = node.text.toLowerCase();
|
|
|
|
for (const token of tokens) {
|
2021-04-11 19:51:12 +00:00
|
|
|
if (!node_text.includes(token)) {
|
2018-10-12 03:11:57 +00:00
|
|
|
node.displayed = 0;
|
2019-07-11 02:25:40 +00:00
|
|
|
break; // Once the first token isn't found we're done
|
2018-10-12 03:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SelectBox.redisplay(id);
|
|
|
|
},
|
|
|
|
delete_from_cache: function(id, value) {
|
2020-10-24 02:58:05 +00:00
|
|
|
let delete_index = null;
|
|
|
|
const cache = SelectBox.cache[id];
|
|
|
|
for (const [i, node] of cache.entries()) {
|
2018-10-12 03:11:57 +00:00
|
|
|
if (node.value === value) {
|
|
|
|
delete_index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cache.splice(delete_index, 1);
|
|
|
|
},
|
|
|
|
add_to_cache: function(id, option) {
|
|
|
|
SelectBox.cache[id].push({value: option.value, text: option.text, displayed: 1});
|
|
|
|
},
|
|
|
|
cache_contains: function(id, value) {
|
|
|
|
// Check if an item is contained in the cache
|
2020-10-24 02:58:05 +00:00
|
|
|
for (const node of SelectBox.cache[id]) {
|
2018-10-12 03:11:57 +00:00
|
|
|
if (node.value === value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
move: function(from, to) {
|
2020-10-24 02:58:05 +00:00
|
|
|
const from_box = document.getElementById(from);
|
|
|
|
for (const option of from_box.options) {
|
|
|
|
const option_value = option.value;
|
2018-10-12 03:11:57 +00:00
|
|
|
if (option.selected && SelectBox.cache_contains(from, option_value)) {
|
|
|
|
SelectBox.add_to_cache(to, {value: option_value, text: option.text, displayed: 1});
|
|
|
|
SelectBox.delete_from_cache(from, option_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SelectBox.redisplay(from);
|
|
|
|
SelectBox.redisplay(to);
|
|
|
|
},
|
|
|
|
move_all: function(from, to) {
|
2020-10-24 02:58:05 +00:00
|
|
|
const from_box = document.getElementById(from);
|
|
|
|
for (const option of from_box.options) {
|
|
|
|
const option_value = option.value;
|
2018-10-12 03:11:57 +00:00
|
|
|
if (SelectBox.cache_contains(from, option_value)) {
|
|
|
|
SelectBox.add_to_cache(to, {value: option_value, text: option.text, displayed: 1});
|
|
|
|
SelectBox.delete_from_cache(from, option_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SelectBox.redisplay(from);
|
|
|
|
SelectBox.redisplay(to);
|
|
|
|
},
|
|
|
|
sort: function(id) {
|
|
|
|
SelectBox.cache[id].sort(function(a, b) {
|
|
|
|
a = a.text.toLowerCase();
|
|
|
|
b = b.text.toLowerCase();
|
2020-10-24 02:58:05 +00:00
|
|
|
if (a > b) {
|
|
|
|
return 1;
|
2018-10-12 03:11:57 +00:00
|
|
|
}
|
2020-10-24 02:58:05 +00:00
|
|
|
if (a < b) {
|
|
|
|
return -1;
|
2018-10-12 03:11:57 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
select_all: function(id) {
|
2020-10-24 02:58:05 +00:00
|
|
|
const box = document.getElementById(id);
|
|
|
|
for (const option of box.options) {
|
|
|
|
option.selected = true;
|
2018-10-12 03:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
window.SelectBox = SelectBox;
|
2020-10-24 02:58:05 +00:00
|
|
|
}
|