1 /*global SelectBox, addEvent, gettext, interpolate, quickElement, SelectFilter*/
3 SelectFilter2 - Turns a multiple-select box into a filter interface.
5 Requires jQuery, core.js, and SelectBox.js.
9 function findForm(node) {
10 // returns the node of the form containing the given node
11 if (node.tagName.toLowerCase() !== 'form') {
12 return findForm(node.parentNode);
17 window.SelectFilter = {
18 init: function(field_id, field_name, is_stacked) {
19 if (field_id.match(/__prefix__/)) {
20 // Don't initialize on empty forms.
23 var from_box = document.getElementById(field_id);
24 from_box.id += '_from'; // change its ID
25 from_box.className = 'filtered';
27 var ps = from_box.parentNode.getElementsByTagName('p');
28 for (var i = 0; i < ps.length; i++) {
29 if (ps[i].className.indexOf("info") !== -1) {
30 // Remove <p class="info">, because it just gets in the way.
31 from_box.parentNode.removeChild(ps[i]);
32 } else if (ps[i].className.indexOf("help") !== -1) {
33 // Move help text up to the top so it isn't below the select
34 // boxes or wrapped off on the side to the right of the add
36 from_box.parentNode.insertBefore(ps[i], from_box.parentNode.firstChild);
40 // <div class="selector"> or <div class="selector stacked">
41 var selector_div = quickElement('div', from_box.parentNode);
42 selector_div.className = is_stacked ? 'selector stacked' : 'selector';
44 // <div class="selector-available">
45 var selector_available = quickElement('div', selector_div);
46 selector_available.className = 'selector-available';
47 var title_available = quickElement('h2', selector_available, interpolate(gettext('Available %s') + ' ', [field_name]));
49 'span', title_available, '',
50 'class', 'help help-tooltip help-icon',
53 'This is the list of available %s. You may choose some by ' +
54 'selecting them in the box below and then clicking the ' +
55 '"Choose" arrow between the two boxes.'
61 var filter_p = quickElement('p', selector_available, '', 'id', field_id + '_filter');
62 filter_p.className = 'selector-filter';
64 var search_filter_label = quickElement('label', filter_p, '', 'for', field_id + '_input');
67 'span', search_filter_label, '',
68 'class', 'help-tooltip search-label-icon',
69 'title', interpolate(gettext("Type into this box to filter down the list of available %s."), [field_name])
72 filter_p.appendChild(document.createTextNode(' '));
74 var filter_input = quickElement('input', filter_p, '', 'type', 'text', 'placeholder', gettext("Filter"));
75 filter_input.id = field_id + '_input';
77 selector_available.appendChild(from_box);
78 var 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');
79 choose_all.className = 'selector-chooseall';
81 // <ul class="selector-chooser">
82 var selector_chooser = quickElement('ul', selector_div);
83 selector_chooser.className = 'selector-chooser';
84 var add_link = quickElement('a', quickElement('li', selector_chooser), gettext('Choose'), 'title', gettext('Choose'), 'href', '#', 'id', field_id + '_add_link');
85 add_link.className = 'selector-add';
86 var remove_link = quickElement('a', quickElement('li', selector_chooser), gettext('Remove'), 'title', gettext('Remove'), 'href', '#', 'id', field_id + '_remove_link');
87 remove_link.className = 'selector-remove';
89 // <div class="selector-chosen">
90 var selector_chosen = quickElement('div', selector_div);
91 selector_chosen.className = 'selector-chosen';
92 var title_chosen = quickElement('h2', selector_chosen, interpolate(gettext('Chosen %s') + ' ', [field_name]));
94 'span', title_chosen, '',
95 'class', 'help help-tooltip help-icon',
98 'This is the list of chosen %s. You may remove some by ' +
99 'selecting them in the box below and then clicking the ' +
100 '"Remove" arrow between the two boxes.'
106 var to_box = quickElement('select', selector_chosen, '', 'id', field_id + '_to', 'multiple', 'multiple', 'size', from_box.size, 'name', from_box.getAttribute('name'));
107 to_box.className = 'filtered';
108 var 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');
109 clear_all.className = 'selector-clearall';
111 from_box.setAttribute('name', from_box.getAttribute('name') + '_old');
113 // Set up the JavaScript event handlers for the select box filter interface
114 var move_selection = function(e, elem, move_func, from, to) {
115 if (elem.className.indexOf('active') !== -1) {
117 SelectFilter.refresh_icons(field_id);
121 addEvent(choose_all, 'click', function(e) { move_selection(e, this, SelectBox.move_all, field_id + '_from', field_id + '_to'); });
122 addEvent(add_link, 'click', function(e) { move_selection(e, this, SelectBox.move, field_id + '_from', field_id + '_to'); });
123 addEvent(remove_link, 'click', function(e) { move_selection(e, this, SelectBox.move, field_id + '_to', field_id + '_from'); });
124 addEvent(clear_all, 'click', function(e) { move_selection(e, this, SelectBox.move_all, field_id + '_to', field_id + '_from'); });
125 addEvent(filter_input, 'keypress', function(e) { SelectFilter.filter_key_press(e, field_id); });
126 addEvent(filter_input, 'keyup', function(e) { SelectFilter.filter_key_up(e, field_id); });
127 addEvent(filter_input, 'keydown', function(e) { SelectFilter.filter_key_down(e, field_id); });
128 addEvent(selector_div, 'change', function(e) {
129 if (e.target.tagName === 'SELECT') {
130 SelectFilter.refresh_icons(field_id);
133 addEvent(selector_div, 'dblclick', function(e) {
134 if (e.target.tagName === 'OPTION') {
135 if (e.target.closest('select').id === field_id + '_to') {
136 SelectBox.move(field_id + '_to', field_id + '_from');
138 SelectBox.move(field_id + '_from', field_id + '_to');
140 SelectFilter.refresh_icons(field_id);
143 addEvent(findForm(from_box), 'submit', function() { SelectBox.select_all(field_id + '_to'); });
144 SelectBox.init(field_id + '_from');
145 SelectBox.init(field_id + '_to');
146 // Move selected from_box options to to_box
147 SelectBox.move(field_id + '_from', field_id + '_to');
150 // In horizontal mode, give the same height to the two boxes.
151 var j_from_box = $(from_box);
152 var j_to_box = $(to_box);
153 var resize_filters = function() { j_to_box.height($(filter_p).outerHeight() + j_from_box.outerHeight()); };
154 if (j_from_box.outerHeight() > 0) {
155 resize_filters(); // This fieldset is already open. Resize now.
157 // This fieldset is probably collapsed. Wait for its 'show' event.
158 j_to_box.closest('fieldset').one('show.fieldset', resize_filters);
162 // Initial icon refresh
163 SelectFilter.refresh_icons(field_id);
165 any_selected: function(field) {
166 var any_selected = false;
168 // Temporarily add the required attribute and check validity.
169 // This is much faster in WebKit browsers than the fallback.
170 field.attr('required', 'required');
171 any_selected = field.is(':valid');
172 field.removeAttr('required');
174 // Browsers that don't support :valid (IE < 10)
175 any_selected = field.find('option:selected').length > 0;
179 refresh_icons: function(field_id) {
180 var from = $('#' + field_id + '_from');
181 var to = $('#' + field_id + '_to');
182 // Active if at least one item is selected
183 $('#' + field_id + '_add_link').toggleClass('active', SelectFilter.any_selected(from));
184 $('#' + field_id + '_remove_link').toggleClass('active', SelectFilter.any_selected(to));
185 // Active if the corresponding box isn't empty
186 $('#' + field_id + '_add_all_link').toggleClass('active', from.find('option').length > 0);
187 $('#' + field_id + '_remove_all_link').toggleClass('active', to.find('option').length > 0);
189 filter_key_press: function(event, field_id) {
190 var from = document.getElementById(field_id + '_from');
191 // don't submit form if user pressed Enter
192 if ((event.which && event.which === 13) || (event.keyCode && event.keyCode === 13)) {
193 from.selectedIndex = 0;
194 SelectBox.move(field_id + '_from', field_id + '_to');
195 from.selectedIndex = 0;
196 event.preventDefault();
200 filter_key_up: function(event, field_id) {
201 var from = document.getElementById(field_id + '_from');
202 var temp = from.selectedIndex;
203 SelectBox.filter(field_id + '_from', document.getElementById(field_id + '_input').value);
204 from.selectedIndex = temp;
207 filter_key_down: function(event, field_id) {
208 var from = document.getElementById(field_id + '_from');
209 // right arrow -- move across
210 if ((event.which && event.which === 39) || (event.keyCode && event.keyCode === 39)) {
211 var old_index = from.selectedIndex;
212 SelectBox.move(field_id + '_from', field_id + '_to');
213 from.selectedIndex = (old_index === from.length) ? from.length - 1 : old_index;
216 // down arrow -- wrap around
217 if ((event.which && event.which === 40) || (event.keyCode && event.keyCode === 40)) {
218 from.selectedIndex = (from.length === from.selectedIndex + 1) ? 0 : from.selectedIndex + 1;
220 // up arrow -- wrap around
221 if ((event.which && event.which === 38) || (event.keyCode && event.keyCode === 38)) {
222 from.selectedIndex = (from.selectedIndex === 0) ? from.length - 1 : from.selectedIndex - 1;
228 addEvent(window, 'load', function(e) {
229 $('select.selectfilter, select.selectfilterstacked').each(function() {
232 SelectFilter.init($el.attr('id'), data.fieldName, parseInt(data.isStacked, 10));