SelectFilter2.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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, '', 'id', field_id + '_selector_chosen');
  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 filter_selected_p = quickElement('p', selector_chosen, '', 'id', field_id + '_filter_selected');
  85. filter_selected_p.className = 'selector-filter';
  86. const search_filter_selected_label = quickElement('label', filter_selected_p, '', 'for', field_id + '_selected_input');
  87. quickElement(
  88. 'span', search_filter_selected_label, '',
  89. 'class', 'help-tooltip search-label-icon',
  90. 'title', interpolate(gettext("Type into this box to filter down the list of selected %s."), [field_name])
  91. );
  92. filter_selected_p.appendChild(document.createTextNode(' '));
  93. const filter_selected_input = quickElement('input', filter_selected_p, '', 'type', 'text', 'placeholder', gettext("Filter"));
  94. filter_selected_input.id = field_id + '_selected_input';
  95. const to_box = quickElement('select', selector_chosen, '', 'id', field_id + '_to', 'multiple', '', 'size', from_box.size, 'name', from_box.name);
  96. to_box.className = 'filtered';
  97. const warning_footer = quickElement('div', selector_chosen, '', 'class', 'list-footer-display');
  98. quickElement('span', warning_footer, '', 'id', field_id + '_list-footer-display-text');
  99. quickElement('span', warning_footer, ' (click to clear)', 'class', 'list-footer-display__clear');
  100. 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');
  101. clear_all.className = 'selector-clearall';
  102. from_box.name = from_box.name + '_old';
  103. // Set up the JavaScript event handlers for the select box filter interface
  104. const move_selection = function(e, elem, move_func, from, to) {
  105. if (elem.classList.contains('active')) {
  106. move_func(from, to);
  107. SelectFilter.refresh_icons(field_id);
  108. SelectFilter.refresh_filtered_selects(field_id);
  109. SelectFilter.refresh_filtered_warning(field_id);
  110. }
  111. e.preventDefault();
  112. };
  113. choose_all.addEventListener('click', function(e) {
  114. move_selection(e, this, SelectBox.move_all, field_id + '_from', field_id + '_to');
  115. });
  116. add_link.addEventListener('click', function(e) {
  117. move_selection(e, this, SelectBox.move, field_id + '_from', field_id + '_to');
  118. });
  119. remove_link.addEventListener('click', function(e) {
  120. move_selection(e, this, SelectBox.move, field_id + '_to', field_id + '_from');
  121. });
  122. clear_all.addEventListener('click', function(e) {
  123. move_selection(e, this, SelectBox.move_all, field_id + '_to', field_id + '_from');
  124. });
  125. warning_footer.addEventListener('click', function(e) {
  126. filter_selected_input.value = '';
  127. SelectBox.filter(field_id + '_to', '');
  128. SelectFilter.refresh_filtered_warning(field_id);
  129. SelectFilter.refresh_icons(field_id);
  130. });
  131. filter_input.addEventListener('keypress', function(e) {
  132. SelectFilter.filter_key_press(e, field_id, '_from', '_to');
  133. });
  134. filter_input.addEventListener('keyup', function(e) {
  135. SelectFilter.filter_key_up(e, field_id, '_from');
  136. });
  137. filter_input.addEventListener('keydown', function(e) {
  138. SelectFilter.filter_key_down(e, field_id, '_from', '_to');
  139. });
  140. filter_selected_input.addEventListener('keypress', function(e) {
  141. SelectFilter.filter_key_press(e, field_id, '_to', '_from');
  142. });
  143. filter_selected_input.addEventListener('keyup', function(e) {
  144. SelectFilter.filter_key_up(e, field_id, '_to', '_selected_input');
  145. });
  146. filter_selected_input.addEventListener('keydown', function(e) {
  147. SelectFilter.filter_key_down(e, field_id, '_to', '_from');
  148. });
  149. selector_div.addEventListener('change', function(e) {
  150. if (e.target.tagName === 'SELECT') {
  151. SelectFilter.refresh_icons(field_id);
  152. }
  153. });
  154. selector_div.addEventListener('dblclick', function(e) {
  155. if (e.target.tagName === 'OPTION') {
  156. if (e.target.closest('select').id === field_id + '_to') {
  157. SelectBox.move(field_id + '_to', field_id + '_from');
  158. } else {
  159. SelectBox.move(field_id + '_from', field_id + '_to');
  160. }
  161. SelectFilter.refresh_icons(field_id);
  162. }
  163. });
  164. from_box.closest('form').addEventListener('submit', function() {
  165. SelectBox.filter(field_id + '_to', '');
  166. SelectBox.select_all(field_id + '_to');
  167. });
  168. SelectBox.init(field_id + '_from');
  169. SelectBox.init(field_id + '_to');
  170. // Move selected from_box options to to_box
  171. SelectBox.move(field_id + '_from', field_id + '_to');
  172. // Initial icon refresh
  173. SelectFilter.refresh_icons(field_id);
  174. },
  175. any_selected: function(field) {
  176. // Temporarily add the required attribute and check validity.
  177. field.required = true;
  178. const any_selected = field.checkValidity();
  179. field.required = false;
  180. return any_selected;
  181. },
  182. refresh_filtered_warning: function(field_id) {
  183. const count = SelectBox.get_hidden_node_count(field_id + '_to');
  184. const selector = document.getElementById(field_id + '_selector_chosen');
  185. const warning = document.getElementById(field_id + '_list-footer-display-text');
  186. selector.className = selector.className.replace('selector-chosen--with-filtered', '');
  187. warning.textContent = interpolate(ngettext(
  188. '%s selected option not visible',
  189. '%s selected options not visible',
  190. count
  191. ), [count]);
  192. if(count > 0) {
  193. selector.className += ' selector-chosen--with-filtered';
  194. }
  195. },
  196. refresh_filtered_selects: function(field_id) {
  197. SelectBox.filter(field_id + '_from', document.getElementById(field_id + "_input").value);
  198. SelectBox.filter(field_id + '_to', document.getElementById(field_id + "_selected_input").value);
  199. },
  200. refresh_icons: function(field_id) {
  201. const from = document.getElementById(field_id + '_from');
  202. const to = document.getElementById(field_id + '_to');
  203. // Active if at least one item is selected
  204. document.getElementById(field_id + '_add_link').classList.toggle('active', SelectFilter.any_selected(from));
  205. document.getElementById(field_id + '_remove_link').classList.toggle('active', SelectFilter.any_selected(to));
  206. // Active if the corresponding box isn't empty
  207. document.getElementById(field_id + '_add_all_link').classList.toggle('active', from.querySelector('option'));
  208. document.getElementById(field_id + '_remove_all_link').classList.toggle('active', to.querySelector('option'));
  209. SelectFilter.refresh_filtered_warning(field_id);
  210. },
  211. filter_key_press: function(event, field_id, source, target) {
  212. const source_box = document.getElementById(field_id + source);
  213. // don't submit form if user pressed Enter
  214. if ((event.which && event.which === 13) || (event.keyCode && event.keyCode === 13)) {
  215. source_box.selectedIndex = 0;
  216. SelectBox.move(field_id + source, field_id + target);
  217. source_box.selectedIndex = 0;
  218. event.preventDefault();
  219. }
  220. },
  221. filter_key_up: function(event, field_id, source, filter_input) {
  222. const input = filter_input || '_input';
  223. const source_box = document.getElementById(field_id + source);
  224. const temp = source_box.selectedIndex;
  225. SelectBox.filter(field_id + source, document.getElementById(field_id + input).value);
  226. source_box.selectedIndex = temp;
  227. SelectFilter.refresh_filtered_warning(field_id);
  228. SelectFilter.refresh_icons(field_id);
  229. },
  230. filter_key_down: function(event, field_id, source, target) {
  231. const source_box = document.getElementById(field_id + source);
  232. // right key (39) or left key (37)
  233. const direction = source === '_from' ? 39 : 37;
  234. // right arrow -- move across
  235. if ((event.which && event.which === direction) || (event.keyCode && event.keyCode === direction)) {
  236. const old_index = source_box.selectedIndex;
  237. SelectBox.move(field_id + source, field_id + target);
  238. SelectFilter.refresh_filtered_selects(field_id);
  239. SelectFilter.refresh_filtered_warning(field_id);
  240. source_box.selectedIndex = (old_index === source_box.length) ? source_box.length - 1 : old_index;
  241. return;
  242. }
  243. // down arrow -- wrap around
  244. if ((event.which && event.which === 40) || (event.keyCode && event.keyCode === 40)) {
  245. source_box.selectedIndex = (source_box.length === source_box.selectedIndex + 1) ? 0 : source_box.selectedIndex + 1;
  246. }
  247. // up arrow -- wrap around
  248. if ((event.which && event.which === 38) || (event.keyCode && event.keyCode === 38)) {
  249. source_box.selectedIndex = (source_box.selectedIndex === 0) ? source_box.length - 1 : source_box.selectedIndex - 1;
  250. }
  251. }
  252. };
  253. window.addEventListener('load', function(e) {
  254. document.querySelectorAll('select.selectfilter, select.selectfilterstacked').forEach(function(el) {
  255. const data = el.dataset;
  256. SelectFilter.init(el.id, data.fieldName, parseInt(data.isStacked, 10));
  257. });
  258. });
  259. }