/django/contrib/admin/static/admin/js/SelectBox.js

http://github.com/django/django · JavaScript · 110 lines · 105 code · 0 blank · 5 comment · 18 complexity · 46d59670a7a76413ba5319c22ef2ea2d MD5 · raw file

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