SelectFilter2.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*global SelectBox, gettext, interpolate, quickElement, SelectFilter*/
  2. /*
  3. SelectFilter2 - Turns a multiple-select box into a filter interface.
  4. Requires core.js and SelectBox.js.
  5. */
  6. 'use strict';
  7. {
  8. window.SelectFilter = {
  9. init: function(field_id, field_name, is_stacked) {
  10. if (field_id.match(/__prefix__/)) {
  11. // Don't initialize on empty forms.
  12. return;
  13. }
  14. const from_box = document.getElementById(field_id);
  15. from_box.id += '_from'; // change its ID
  16. from_box.className = 'filtered';
  17. for (const p of from_box.parentNode.getElementsByTagName('p')) {
  18. if (p.classList.contains("info")) {
  19. // Remove <p class="info">, because it just gets in the way.
  20. from_box.parentNode.removeChild(p);
  21. } else if (p.classList.contains("help")) {
  22. // Move help text up to the top so it isn't below the select
  23. // boxes or wrapped off on the side to the right of the add
  24. // button:
  25. from_box.parentNode.insertBefore(p, from_box.parentNode.firstChild);
  26. }
  27. }
  28. // <div class="selector"> or <div class="selector stacked">
  29. const selector_div = quickElement('div', from_box.parentNode);
  30. selector_div.className = is_stacked ? 'selector stacked' : 'selector';
  31. // <div class="selector-available">
  32. const selector_available = quickElement('div', selector_div);
  33. selector_available.className = 'selector-available';
  34. const title_available = quickElement('h2', selector_available, interpolate(gettext('Available %s') + ' ', [field_name]));
  35. quickElement(
  36. 'span', title_available, '',
  37. 'class', 'help help-tooltip help-icon',
  38. 'title', interpolate(
  39. gettext(
  40. 'This is the list of available %s. You may choose some by ' +
  41. 'selecting them in the box below and then clicking the ' +
  42. '"Choose" arrow between the two boxes.'
  43. ),
  44. [field_name]
  45. )
  46. );
  47. const filter_p = quickElement('p', selector_available, '', 'id', field_id + '_filter');
  48. filter_p.className = 'selector-filter';
  49. const search_filter_label = quickElement('label', filter_p, '', 'for', field_id + '_input');
  50. quickElement(
  51. 'span', search_filter_label, '',
  52. 'class', 'help-tooltip search-label-icon',
  53. 'title', interpolate(gettext("Type into this box to filter down the list of available %s."), [field_name])
  54. );
  55. filter_p.appendChild(document.createTextNode(' '));
  56. const filter_input = quickElement('input', filter_p, '', 'type', 'text', 'placeholder', gettext("Filter"));
  57. filter_input.id = field_id + '_input';
  58. selector_available.appendChild(from_box);
  59. const choose_all = quickElement('a', selector_available, gettext('Choose all'), 'title', interpolate(gettext('Click to choose all %s at once.'), [field_name]), 'href', '#', 'id', field_id + '_add_all_link');
  60. choose_all.className = 'selector-chooseall';
  61. // <ul class="selector-chooser">
  62. const selector_chooser = quickElement('ul', selector_div);
  63. selector_chooser.className = 'selector-chooser';
  64. const add_link = quickElement('a', quickElement('li', selector_chooser), gettext('Choose'), 'title', gettext('Choose'), 'href', '#', 'id', field_id + '_add_link');
  65. add_link.className = 'selector-add';
  66. const remove_link = quickElement('a', quickElement('li', selector_chooser), gettext('Remove'), 'title', gettext('Remove'), 'href', '#', 'id', field_id + '_remove_link');
  67. remove_link.className = 'selector-remove';
  68. // <div class="selector-chosen">
  69. const selector_chosen = quickElement('div', selector_div);
  70. selector_chosen.className = 'selector-chosen';
  71. const title_chosen = quickElement('h2', selector_chosen, interpolate(gettext('Chosen %s') + ' ', [field_name]));
  72. quickElement(
  73. 'span', title_chosen, '',
  74. 'class', 'help help-tooltip help-icon',
  75. 'title', interpolate(
  76. gettext(
  77. 'This is the list of chosen %s. You may remove some by ' +
  78. 'selecting them in the box below and then clicking the ' +
  79. '"Remove" arrow between the two boxes.'
  80. ),
  81. [field_name]
  82. )
  83. );
  84. const to_box = quickElement('select', selector_chosen, '', 'id', field_id + '_to', 'multiple', '', 'size', from_box.size, 'name', from_box.name);
  85. to_box.className = 'filtered';
  86. const clear_all = quickElement('a', selector_chosen, gettext('Remove all'), 'title', interpolate(gettext('Click to remove all chosen %s at once.'), [field_name]), 'href', '#', 'id', field_id + '_remove_all_link');
  87. clear_all.className = 'selector-clearall';
  88. from_box.name = from_box.name + '_old';
  89. // Set up the JavaScript event handlers for the select box filter interface
  90. const move_selection = function(e, elem, move_func, from, to) {
  91. if (elem.classList.contains('active')) {
  92. move_func(from, to);
  93. SelectFilter.refresh_icons(field_id);
  94. }
  95. e.preventDefault();
  96. };
  97. choose_all.addEventListener('click', function(e) {
  98. move_selection(e, this, SelectBox.move_all, field_id + '_from', field_id + '_to');
  99. });
  100. add_link.addEventListener('click', function(e) {
  101. move_selection(e, this, SelectBox.move, field_id + '_from', field_id + '_to');
  102. });
  103. remove_link.addEventListener('click', function(e) {
  104. move_selection(e, this, SelectBox.move, field_id + '_to', field_id + '_from');
  105. });
  106. clear_all.addEventListener('click', function(e) {
  107. move_selection(e, this, SelectBox.move_all, field_id + '_to', field_id + '_from');
  108. });
  109. filter_input.addEventListener('keypress', function(e) {
  110. SelectFilter.filter_key_press(e, field_id);
  111. });
  112. filter_input.addEventListener('keyup', function(e) {
  113. SelectFilter.filter_key_up(e, field_id);
  114. });
  115. filter_input.addEventListener('keydown', function(e) {
  116. SelectFilter.filter_key_down(e, field_id);
  117. });
  118. selector_div.addEventListener('change', function(e) {
  119. if (e.target.tagName === 'SELECT') {
  120. SelectFilter.refresh_icons(field_id);
  121. }
  122. });
  123. selector_div.addEventListener('dblclick', function(e) {
  124. if (e.target.tagName === 'OPTION') {
  125. if (e.target.closest('select').id === field_id + '_to') {
  126. SelectBox.move(field_id + '_to', field_id + '_from');
  127. } else {
  128. SelectBox.move(field_id + '_from', field_id + '_to');
  129. }
  130. SelectFilter.refresh_icons(field_id);
  131. }
  132. });
  133. from_box.closest('form').addEventListener('submit', function() {
  134. SelectBox.select_all(field_id + '_to');
  135. });
  136. SelectBox.init(field_id + '_from');
  137. SelectBox.init(field_id + '_to');
  138. // Move selected from_box options to to_box
  139. SelectBox.move(field_id + '_from', field_id + '_to');
  140. if (!is_stacked) {
  141. // In horizontal mode, give the same height to the two boxes.
  142. const j_from_box = document.getElementById(field_id + '_from');
  143. const j_to_box = document.getElementById(field_id + '_to');
  144. let height = filter_p.offsetHeight + j_from_box.offsetHeight;
  145. const j_to_box_style = window.getComputedStyle(j_to_box);
  146. if (j_to_box_style.getPropertyValue('box-sizing') === 'border-box') {
  147. // Add the padding and border to the final height.
  148. height += parseInt(j_to_box_style.getPropertyValue('padding-top'), 10)
  149. + parseInt(j_to_box_style.getPropertyValue('padding-bottom'), 10)
  150. + parseInt(j_to_box_style.getPropertyValue('border-top-width'), 10)
  151. + parseInt(j_to_box_style.getPropertyValue('border-bottom-width'), 10);
  152. }
  153. j_to_box.style.height = height + 'px';
  154. }
  155. // Initial icon refresh
  156. SelectFilter.refresh_icons(field_id);
  157. },
  158. any_selected: function(field) {
  159. // Temporarily add the required attribute and check validity.
  160. field.required = true;
  161. const any_selected = field.checkValidity();
  162. field.required = false;
  163. return any_selected;
  164. },
  165. refresh_icons: function(field_id) {
  166. const from = document.getElementById(field_id + '_from');
  167. const to = document.getElementById(field_id + '_to');
  168. // Active if at least one item is selected
  169. document.getElementById(field_id + '_add_link').classList.toggle('active', SelectFilter.any_selected(from));
  170. document.getElementById(field_id + '_remove_link').classList.toggle('active', SelectFilter.any_selected(to));
  171. // Active if the corresponding box isn't empty
  172. document.getElementById(field_id + '_add_all_link').classList.toggle('active', from.querySelector('option'));
  173. document.getElementById(field_id + '_remove_all_link').classList.toggle('active', to.querySelector('option'));
  174. },
  175. filter_key_press: function(event, field_id) {
  176. const from = document.getElementById(field_id + '_from');
  177. // don't submit form if user pressed Enter
  178. if ((event.which && event.which === 13) || (event.keyCode && event.keyCode === 13)) {
  179. from.selectedIndex = 0;
  180. SelectBox.move(field_id + '_from', field_id + '_to');
  181. from.selectedIndex = 0;
  182. event.preventDefault();
  183. }
  184. },
  185. filter_key_up: function(event, field_id) {
  186. const from = document.getElementById(field_id + '_from');
  187. const temp = from.selectedIndex;
  188. SelectBox.filter(field_id + '_from', document.getElementById(field_id + '_input').value);
  189. from.selectedIndex = temp;
  190. },
  191. filter_key_down: function(event, field_id) {
  192. const from = document.getElementById(field_id + '_from');
  193. // right arrow -- move across
  194. if ((event.which && event.which === 39) || (event.keyCode && event.keyCode === 39)) {
  195. const old_index = from.selectedIndex;
  196. SelectBox.move(field_id + '_from', field_id + '_to');
  197. from.selectedIndex = (old_index === from.length) ? from.length - 1 : old_index;
  198. return;
  199. }
  200. // down arrow -- wrap around
  201. if ((event.which && event.which === 40) || (event.keyCode && event.keyCode === 40)) {
  202. from.selectedIndex = (from.length === from.selectedIndex + 1) ? 0 : from.selectedIndex + 1;
  203. }
  204. // up arrow -- wrap around
  205. if ((event.which && event.which === 38) || (event.keyCode && event.keyCode === 38)) {
  206. from.selectedIndex = (from.selectedIndex === 0) ? from.length - 1 : from.selectedIndex - 1;
  207. }
  208. }
  209. };
  210. window.addEventListener('load', function(e) {
  211. document.querySelectorAll('select.selectfilter, select.selectfilterstacked').forEach(function(el) {
  212. const data = el.dataset;
  213. SelectFilter.init(el.id, data.fieldName, parseInt(data.isStacked, 10));
  214. });
  215. });
  216. }