SelectBox.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. delete_from_cache: function(id, value) {
  45. let delete_index = null;
  46. const cache = SelectBox.cache[id];
  47. for (const [i, node] of cache.entries()) {
  48. if (node.value === value) {
  49. delete_index = i;
  50. break;
  51. }
  52. }
  53. cache.splice(delete_index, 1);
  54. },
  55. add_to_cache: function(id, option) {
  56. SelectBox.cache[id].push({value: option.value, text: option.text, displayed: 1});
  57. },
  58. cache_contains: function(id, value) {
  59. // Check if an item is contained in the cache
  60. for (const node of SelectBox.cache[id]) {
  61. if (node.value === value) {
  62. return true;
  63. }
  64. }
  65. return false;
  66. },
  67. move: function(from, to) {
  68. const from_box = document.getElementById(from);
  69. for (const option of from_box.options) {
  70. const option_value = option.value;
  71. if (option.selected && SelectBox.cache_contains(from, option_value)) {
  72. SelectBox.add_to_cache(to, {value: option_value, text: option.text, displayed: 1});
  73. SelectBox.delete_from_cache(from, option_value);
  74. }
  75. }
  76. SelectBox.redisplay(from);
  77. SelectBox.redisplay(to);
  78. },
  79. move_all: function(from, to) {
  80. const from_box = document.getElementById(from);
  81. for (const option of from_box.options) {
  82. const option_value = option.value;
  83. if (SelectBox.cache_contains(from, option_value)) {
  84. SelectBox.add_to_cache(to, {value: option_value, text: option.text, displayed: 1});
  85. SelectBox.delete_from_cache(from, option_value);
  86. }
  87. }
  88. SelectBox.redisplay(from);
  89. SelectBox.redisplay(to);
  90. },
  91. sort: function(id) {
  92. SelectBox.cache[id].sort(function(a, b) {
  93. a = a.text.toLowerCase();
  94. b = b.text.toLowerCase();
  95. if (a > b) {
  96. return 1;
  97. }
  98. if (a < b) {
  99. return -1;
  100. }
  101. return 0;
  102. } );
  103. },
  104. select_all: function(id) {
  105. const box = document.getElementById(id);
  106. for (const option of box.options) {
  107. option.selected = true;
  108. }
  109. }
  110. };
  111. window.SelectBox = SelectBox;
  112. }