SelectFilter2.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*global SelectBox, gettext, ngettext, 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. // Make sure the selector div is at the beginning so that the
  31. // add link would be displayed to the right of the widget.
  32. from_box.parentNode.prepend(selector_div);
  33. selector_div.className = is_stacked ? 'selector stacked' : 'selector';
  34. // <div class="selector-available">
  35. const selector_available = quickElement('div', selector_div);
  36. selector_available.className = 'selector-available';
  37. const title_available = quickElement('h2', selector_available, interpolate(gettext('Available %s') + ' ', [field_name]));
  38. quickElement(
  39. 'span', title_available, '',
  40. 'class', 'help help-tooltip help-icon',
  41. 'title', interpolate(
  42. gettext(
  43. 'This is the list of available %s. You may choose some by ' +
  44. 'selecting them in the box below and then clicking the ' +
  45. '"Choose" arrow between the two boxes.'
  46. ),
  47. [field_name]
  48. )
  49. );
  50. const filter_p = quickElement('p', selector_available, '', 'id', field_id + '_filter');
  51. filter_p.className = 'selector-filter';
  52. const search_filter_label = quickElement('label', filter_p, '', 'for', field_id + '_input');
  53. quickElement(
  54. 'span', search_filter_label, '',
  55. 'class', 'help-tooltip search-label-icon',
  56. 'title', interpolate(gettext("Type into this box to filter down the list of available %s."), [field_name])
  57. );
  58. filter_p.appendChild(document.createTextNode(' '));
  59. const filter_input = quickElement('input', filter_p, '', 'type', 'text', 'placeholder', gettext("Filter"));
  60. filter_input.id = field_id + '_input';
  61. selector_available.appendChild(from_box);
  62. 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');
  63. choose_all.className = 'selector-chooseall';
  64. // <ul class="selector-chooser">
  65. const selector_chooser = quickElement('ul', selector_div);
  66. selector_chooser.className = 'selector-chooser';
  67. const add_link = quickElement('a', quickElement('li', selector_chooser), gettext('Choose'), 'title', gettext('Choose'), 'href', '#', 'id', field_id + '_add_link');
  68. add_link.className = 'selector-add';
  69. const remove_link = quickElement('a', quickElement('li', selector_chooser), gettext('Remove'), 'title', gettext('Remove'), 'href', '#', 'id', field_id + '_remove_link');
  70. remove_link.className = 'selector-remove';
  71. // <div class="selector-chosen">
  72. const selector_chosen = quickElement('div', selector_div, '', 'id', field_id + '_selector_chosen');
  73. selector_chosen.className = 'selector-chosen';
  74. const title_chosen = quickElement('h2', selector_chosen, interpolate(gettext('Chosen %s') + ' ', [field_name]));
  75. quickElement(
  76. 'span', title_chosen, '',
  77. 'class', 'help help-tooltip help-icon',
  78. 'title', interpolate(
  79. gettext(
  80. 'This is the list of chosen %s. You may remove some by ' +
  81. 'selecting them in the box below and then clicking the ' +
  82. '"Remove" arrow between the two boxes.'
  83. ),
  84. [field_name]
  85. )
  86. );
  87. const filter_selected_p = quickElement('p', selector_chosen, '', 'id', field_id + '_filter_selected');
  88. filter_selected_p.className = 'selector-filter';
  89. const search_filter_selected_label = quickElement('label', filter_selected_p, '', 'for', field_id + '_selected_input');
  90. quickElement(
  91. 'span', search_filter_selected_label, '',
  92. 'class', 'help-tooltip search-label-icon',
  93. 'title', interpolate(gettext("Type into this box to filter down the list of selected %s."), [field_name])
  94. );
  95. filter_selected_p.appendChild(document.createTextNode(' '));
  96. const filter_selected_input = quickElement('input', filter_selected_p, '', 'type', 'text', 'placeholder', gettext("Filter"));
  97. filter_selected_input.id = field_id + '_selected_input';
  98. const to_box = quickElement('select', selector_chosen, '', 'id', field_id + '_to', 'multiple', '', 'size', from_box.size, 'name', from_box.name);
  99. to_box.className = 'filtered';
  100. const warning_footer = quickElement('div', selector_chosen, '', 'class', 'list-footer-display');
  101. quickElement('span', warning_footer, '', 'id', field_id + '_list-footer-display-text');
  102. quickElement('span', warning_footer, ' (click to clear)', 'class', 'list-footer-display__clear');
  103. 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');
  104. clear_all.className = 'selector-clearall';
  105. from_box.name = from_box.name + '_old';
  106. // Set up the JavaScript event handlers for the select box filter interface
  107. const move_selection = function(e, elem, move_func, from, to) {
  108. if (elem.classList.contains('active')) {
  109. move_func(from, to);
  110. SelectFilter.refresh_icons(field_id);
  111. SelectFilter.refresh_filtered_selects(field_id);
  112. SelectFilter.refresh_filtered_warning(field_id);
  113. }
  114. e.preventDefault();
  115. };
  116. choose_all.addEventListener('click', function(e) {
  117. move_selection(e, this, SelectBox.move_all, field_id + '_from', field_id + '_to');
  118. });
  119. add_link.addEventListener('click', function(e) {
  120. move_selection(e, this, SelectBox.move, field_id + '_from', field_id + '_to');
  121. });
  122. remove_link.addEventListener('click', function(e) {
  123. move_selection(e, this, SelectBox.move, field_id + '_to', field_id + '_from');
  124. });
  125. clear_all.addEventListener('click', function(e) {
  126. move_selection(e, this, SelectBox.move_all, field_id + '_to', field_id + '_from');
  127. });
  128. warning_footer.addEventListener('click', function(e) {
  129. filter_selected_input.value = '';
  130. SelectBox.filter(field_id + '_to', '');
  131. SelectFilter.refresh_filtered_warning(field_id);
  132. SelectFilter.refresh_icons(field_id);
  133. });
  134. filter_input.addEventListener('keypress', function(e) {
  135. SelectFilter.filter_key_press(e, field_id, '_from', '_to');
  136. });
  137. filter_input.addEventListener('keyup', function(e) {
  138. SelectFilter.filter_key_up(e, field_id, '_from');
  139. });
  140. filter_input.addEventListener('keydown', function(e) {
  141. SelectFilter.filter_key_down(e, field_id, '_from', '_to');
  142. });
  143. filter_selected_input.addEventListener('keypress', function(e) {
  144. SelectFilter.filter_key_press(e, field_id, '_to', '_from');
  145. });
  146. filter_selected_input.addEventListener('keyup', function(e) {
  147. SelectFilter.filter_key_up(e, field_id, '_to', '_selected_input');
  148. });
  149. filter_selected_input.addEventListener('keydown', function(e) {
  150. SelectFilter.filter_key_down(e, field_id, '_to', '_from');
  151. });
  152. selector_div.addEventListener('change', function(e) {
  153. if (e.target.tagName === 'SELECT') {
  154. SelectFilter.refresh_icons(field_id);
  155. }
  156. });
  157. selector_div.addEventListener('dblclick', function(e) {
  158. if (e.target.tagName === 'OPTION') {
  159. if (e.target.closest('select').id === field_id + '_to') {
  160. SelectBox.move(field_id + '_to', field_id + '_from');
  161. } else {
  162. SelectBox.move(field_id + '_from', field_id + '_to');
  163. }
  164. SelectFilter.refresh_icons(field_id);
  165. }
  166. });
  167. from_box.closest('form').addEventListener('submit', function() {
  168. SelectBox.filter(field_id + '_to', '');
  169. SelectBox.select_all(field_id + '_to');
  170. });
  171. SelectBox.init(field_id + '_from');
  172. SelectBox.init(field_id + '_to');
  173. // Move selected from_box options to to_box
  174. SelectBox.move(field_id + '_from', field_id + '_to');
  175. // Initial icon refresh
  176. SelectFilter.refresh_icons(field_id);
  177. },
  178. any_selected: function(field) {
  179. // Temporarily add the required attribute and check validity.
  180. field.required = true;
  181. const any_selected = field.checkValidity();
  182. field.required = false;
  183. return any_selected;
  184. },
  185. refresh_filtered_warning: function(field_id) {
  186. const count = SelectBox.get_hidden_node_count(field_id + '_to');
  187. const selector = document.getElementById(field_id + '_selector_chosen');
  188. const warning = document.getElementById(field_id + '_list-footer-display-text');
  189. selector.className = selector.className.replace('selector-chosen--with-filtered', '');
  190. warning.textContent = interpolate(ngettext(
  191. '%s selected option not visible',
  192. '%s selected options not visible',
  193. count
  194. ), [count]);
  195. if(count > 0) {
  196. selector.className += ' selector-chosen--with-filtered';
  197. }
  198. },
  199. refresh_filtered_selects: function(field_id) {
  200. SelectBox.filter(field_id + '_from', document.getElementById(field_id + "_input").value);
  201. SelectBox.filter(field_id + '_to', document.getElementById(field_id + "_selected_input").value);
  202. },
  203. refresh_icons: function(field_id) {
  204. const from = document.getElementById(field_id + '_from');
  205. const to = document.getElementById(field_id + '_to');
  206. // Active if at least one item is selected
  207. document.getElementById(field_id + '_add_link').classList.toggle('active', SelectFilter.any_selected(from));
  208. document.getElementById(field_id + '_remove_link').classList.toggle('active', SelectFilter.any_selected(to));
  209. // Active if the corresponding box isn't empty
  210. document.getElementById(field_id + '_add_all_link').classList.toggle('active', from.querySelector('option'));
  211. document.getElementById(field_id + '_remove_all_link').classList.toggle('active', to.querySelector('option'));
  212. SelectFilter.refresh_filtered_warning(field_id);
  213. },
  214. filter_key_press: function(event, field_id, source, target) {
  215. const source_box = document.getElementById(field_id + source);
  216. // don't submit form if user pressed Enter
  217. if ((event.which && event.which === 13) || (event.keyCode && event.keyCode === 13)) {
  218. source_box.selectedIndex = 0;
  219. SelectBox.move(field_id + source, field_id + target);
  220. source_box.selectedIndex = 0;
  221. event.preventDefault();
  222. }
  223. },
  224. filter_key_up: function(event, field_id, source, filter_input) {
  225. const input = filter_input || '_input';
  226. const source_box = document.getElementById(field_id + source);
  227. const temp = source_box.selectedIndex;
  228. SelectBox.filter(field_id + source, document.getElementById(field_id + input).value);
  229. source_box.selectedIndex = temp;
  230. SelectFilter.refresh_filtered_warning(field_id);
  231. SelectFilter.refresh_icons(field_id);
  232. },
  233. filter_key_down: function(event, field_id, source, target) {
  234. const source_box = document.getElementById(field_id + source);
  235. // right key (39) or left key (37)
  236. const direction = source === '_from' ? 39 : 37;
  237. // right arrow -- move across
  238. if ((event.which && event.which === direction) || (event.keyCode && event.keyCode === direction)) {
  239. const old_index = source_box.selectedIndex;
  240. SelectBox.move(field_id + source, field_id + target);
  241. SelectFilter.refresh_filtered_selects(field_id);
  242. SelectFilter.refresh_filtered_warning(field_id);
  243. source_box.selectedIndex = (old_index === source_box.length) ? source_box.length - 1 : old_index;
  244. return;
  245. }
  246. // down arrow -- wrap around
  247. if ((event.which && event.which === 40) || (event.keyCode && event.keyCode === 40)) {
  248. source_box.selectedIndex = (source_box.length === source_box.selectedIndex + 1) ? 0 : source_box.selectedIndex + 1;
  249. }
  250. // up arrow -- wrap around
  251. if ((event.which && event.which === 38) || (event.keyCode && event.keyCode === 38)) {
  252. source_box.selectedIndex = (source_box.selectedIndex === 0) ? source_box.length - 1 : source_box.selectedIndex - 1;
  253. }
  254. }
  255. };
  256. window.addEventListener('load', function(e) {
  257. document.querySelectorAll('select.selectfilter, select.selectfilterstacked').forEach(function(el) {
  258. const data = el.dataset;
  259. SelectFilter.init(el.id, data.fieldName, parseInt(data.isStacked, 10));
  260. });
  261. });
  262. }