SelectBox.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. (function() {
  2. 'use strict';
  3. var SelectBox = {
  4. cache: {},
  5. init: function(id) {
  6. var box = document.getElementById(id);
  7. var node;
  8. SelectBox.cache[id] = [];
  9. var cache = SelectBox.cache[id];
  10. for (var i = 0, j = box.options.length; i < j; i++) {
  11. node = box.options[i];
  12. cache.push({value: node.value, text: node.text, displayed: 1});
  13. }
  14. },
  15. redisplay: function(id) {
  16. // Repopulate HTML select box from cache
  17. var box = document.getElementById(id);
  18. var node;
  19. box.options.length = 0; // clear all options
  20. var cache = SelectBox.cache[id];
  21. for (var i = 0, j = cache.length; i < j; i++) {
  22. node = cache[i];
  23. if (node.displayed) {
  24. var new_option = new Option(node.text, node.value, false, false);
  25. // Shows a tooltip when hovering over the option
  26. new_option.setAttribute("title", node.text);
  27. box.options[box.options.length] = new_option;
  28. }
  29. }
  30. },
  31. filter: function(id, text) {
  32. // Redisplay the HTML select box, displaying only the choices containing ALL
  33. // the words in text. (It's an AND search.)
  34. var tokens = text.toLowerCase().split(/\s+/);
  35. var node, token;
  36. var cache = SelectBox.cache[id];
  37. for (var i = 0, j = cache.length; i < j; i++) {
  38. node = cache[i];
  39. node.displayed = 1;
  40. var numTokens = tokens.length;
  41. for (var k = 0; k < numTokens; k++) {
  42. token = tokens[k];
  43. if (node.text.toLowerCase().indexOf(token) === -1) {
  44. node.displayed = 0;
  45. }
  46. }
  47. }
  48. SelectBox.redisplay(id);
  49. },
  50. delete_from_cache: function(id, value) {
  51. var node, delete_index = null;
  52. var cache = SelectBox.cache[id];
  53. for (var i = 0, j = cache.length; i < j; i++) {
  54. node = cache[i];
  55. if (node.value === value) {
  56. delete_index = i;
  57. break;
  58. }
  59. }
  60. var k = cache.length - 1;
  61. for (i = delete_index; i < k; i++) {
  62. cache[i] = cache[i + 1];
  63. }
  64. cache.length--;
  65. },
  66. add_to_cache: function(id, option) {
  67. SelectBox.cache[id].push({value: option.value, text: option.text, displayed: 1});
  68. },
  69. cache_contains: function(id, value) {
  70. // Check if an item is contained in the cache
  71. var node;
  72. var cache = SelectBox.cache[id];
  73. for (var i = 0, j = cache.length; i < j; i++) {
  74. node = cache[i];
  75. if (node.value === value) {
  76. return true;
  77. }
  78. }
  79. return false;
  80. },
  81. move: function(from, to) {
  82. var from_box = document.getElementById(from);
  83. var option;
  84. var boxOptions = from_box.options;
  85. for (var i = 0, j = boxOptions.length; i < j; i++) {
  86. option = boxOptions[i];
  87. if (option.selected && 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. move_all: function(from, to) {
  96. var from_box = document.getElementById(from);
  97. var option;
  98. var boxOptions = from_box.options;
  99. for (var i = 0, j = boxOptions.length; i < j; i++) {
  100. option = boxOptions[i];
  101. if (SelectBox.cache_contains(from, option.value)) {
  102. SelectBox.add_to_cache(to, {value: option.value, text: option.text, displayed: 1});
  103. SelectBox.delete_from_cache(from, option.value);
  104. }
  105. }
  106. SelectBox.redisplay(from);
  107. SelectBox.redisplay(to);
  108. },
  109. sort: function(id) {
  110. SelectBox.cache[id].sort(function(a, b) {
  111. a = a.text.toLowerCase();
  112. b = b.text.toLowerCase();
  113. try {
  114. if (a > b) {
  115. return 1;
  116. }
  117. if (a < b) {
  118. return -1;
  119. }
  120. }
  121. catch (e) {
  122. // silently fail on IE 'unknown' exception
  123. }
  124. return 0;
  125. } );
  126. },
  127. select_all: function(id) {
  128. var box = document.getElementById(id);
  129. for (var i = 0; i < box.options.length; i++) {
  130. box.options[i].selected = 'selected';
  131. }
  132. }
  133. };
  134. window.SelectBox = SelectBox;
  135. })();