bgneal@1
|
1 /*
|
bgneal@1
|
2 SelectFilter2 - Turns a multiple-select box into a filter interface.
|
bgneal@1
|
3
|
bgneal@1
|
4 Different than SelectFilter because this is coupled to the admin framework.
|
bgneal@1
|
5
|
bgneal@1
|
6 Requires core.js, SelectBox.js and addevent.js.
|
bgneal@1
|
7 */
|
bgneal@1
|
8
|
bgneal@1
|
9 function findForm(node) {
|
bgneal@1
|
10 // returns the node of the form containing the given node
|
bgneal@1
|
11 if (node.tagName.toLowerCase() != 'form') {
|
bgneal@1
|
12 return findForm(node.parentNode);
|
bgneal@1
|
13 }
|
bgneal@1
|
14 return node;
|
bgneal@1
|
15 }
|
bgneal@1
|
16
|
bgneal@1
|
17 var SelectFilter = {
|
bgneal@1
|
18 init: function(field_id, field_name, is_stacked, admin_media_prefix) {
|
bgneal@1
|
19 var from_box = document.getElementById(field_id);
|
bgneal@1
|
20 from_box.id += '_from'; // change its ID
|
bgneal@1
|
21 from_box.className = 'filtered';
|
bgneal@1
|
22
|
bgneal@1
|
23 // Remove <p class="info">, because it just gets in the way.
|
bgneal@1
|
24 var ps = from_box.parentNode.getElementsByTagName('p');
|
bgneal@1
|
25 for (var i=0; i<ps.length; i++) {
|
bgneal@1
|
26 from_box.parentNode.removeChild(ps[i]);
|
bgneal@1
|
27 }
|
bgneal@1
|
28
|
bgneal@1
|
29 // <div class="selector"> or <div class="selector stacked">
|
bgneal@1
|
30 var selector_div = quickElement('div', from_box.parentNode);
|
bgneal@1
|
31 selector_div.className = is_stacked ? 'selector stacked' : 'selector';
|
bgneal@1
|
32
|
bgneal@1
|
33 // <div class="selector-available">
|
bgneal@1
|
34 var selector_available = quickElement('div', selector_div, '');
|
bgneal@1
|
35 selector_available.className = 'selector-available';
|
bgneal@1
|
36 quickElement('h2', selector_available, interpolate(gettext('Available %s'), [field_name]));
|
bgneal@1
|
37 var filter_p = quickElement('p', selector_available, '');
|
bgneal@1
|
38 filter_p.className = 'selector-filter';
|
bgneal@1
|
39 quickElement('img', filter_p, '', 'src', admin_media_prefix + 'img/admin/selector-search.gif');
|
bgneal@1
|
40 filter_p.appendChild(document.createTextNode(' '));
|
bgneal@1
|
41 var filter_input = quickElement('input', filter_p, '', 'type', 'text');
|
bgneal@1
|
42 filter_input.id = field_id + '_input';
|
bgneal@1
|
43 selector_available.appendChild(from_box);
|
bgneal@1
|
44 var choose_all = quickElement('a', selector_available, gettext('Choose all'), 'href', 'javascript: (function(){ SelectBox.move_all("' + field_id + '_from", "' + field_id + '_to"); })()');
|
bgneal@1
|
45 choose_all.className = 'selector-chooseall';
|
bgneal@1
|
46
|
bgneal@1
|
47 // <ul class="selector-chooser">
|
bgneal@1
|
48 var selector_chooser = quickElement('ul', selector_div, '');
|
bgneal@1
|
49 selector_chooser.className = 'selector-chooser';
|
bgneal@1
|
50 var add_link = quickElement('a', quickElement('li', selector_chooser, ''), gettext('Add'), 'href', 'javascript: (function(){ SelectBox.move("' + field_id + '_from","' + field_id + '_to");})()');
|
bgneal@1
|
51 add_link.className = 'selector-add';
|
bgneal@1
|
52 var remove_link = quickElement('a', quickElement('li', selector_chooser, ''), gettext('Remove'), 'href', 'javascript: (function(){ SelectBox.move("' + field_id + '_to","' + field_id + '_from");})()');
|
bgneal@1
|
53 remove_link.className = 'selector-remove';
|
bgneal@1
|
54
|
bgneal@1
|
55 // <div class="selector-chosen">
|
bgneal@1
|
56 var selector_chosen = quickElement('div', selector_div, '');
|
bgneal@1
|
57 selector_chosen.className = 'selector-chosen';
|
bgneal@1
|
58 quickElement('h2', selector_chosen, interpolate(gettext('Chosen %s'), [field_name]));
|
bgneal@1
|
59 var selector_filter = quickElement('p', selector_chosen, gettext('Select your choice(s) and click '));
|
bgneal@1
|
60 selector_filter.className = 'selector-filter';
|
bgneal@1
|
61 quickElement('img', selector_filter, '', 'src', admin_media_prefix + (is_stacked ? 'img/admin/selector_stacked-add.gif':'img/admin/selector-add.gif'), 'alt', 'Add');
|
bgneal@1
|
62 var to_box = quickElement('select', selector_chosen, '', 'id', field_id + '_to', 'multiple', 'multiple', 'size', from_box.size, 'name', from_box.getAttribute('name'));
|
bgneal@1
|
63 to_box.className = 'filtered';
|
bgneal@1
|
64 var clear_all = quickElement('a', selector_chosen, gettext('Clear all'), 'href', 'javascript: (function() { SelectBox.move_all("' + field_id + '_to", "' + field_id + '_from");})()');
|
bgneal@1
|
65 clear_all.className = 'selector-clearall';
|
bgneal@1
|
66
|
bgneal@1
|
67 from_box.setAttribute('name', from_box.getAttribute('name') + '_old');
|
bgneal@1
|
68
|
bgneal@1
|
69 // Set up the JavaScript event handlers for the select box filter interface
|
bgneal@1
|
70 addEvent(filter_input, 'keyup', function(e) { SelectFilter.filter_key_up(e, field_id); });
|
bgneal@1
|
71 addEvent(filter_input, 'keydown', function(e) { SelectFilter.filter_key_down(e, field_id); });
|
bgneal@1
|
72 addEvent(from_box, 'dblclick', function() { SelectBox.move(field_id + '_from', field_id + '_to'); });
|
bgneal@1
|
73 addEvent(to_box, 'dblclick', function() { SelectBox.move(field_id + '_to', field_id + '_from'); });
|
bgneal@1
|
74 addEvent(findForm(from_box), 'submit', function() { SelectBox.select_all(field_id + '_to'); });
|
bgneal@1
|
75 SelectBox.init(field_id + '_from');
|
bgneal@1
|
76 SelectBox.init(field_id + '_to');
|
bgneal@1
|
77 // Move selected from_box options to to_box
|
bgneal@1
|
78 SelectBox.move(field_id + '_from', field_id + '_to');
|
bgneal@1
|
79 },
|
bgneal@1
|
80 filter_key_up: function(event, field_id) {
|
bgneal@1
|
81 from = document.getElementById(field_id + '_from');
|
bgneal@1
|
82 // don't submit form if user pressed Enter
|
bgneal@1
|
83 if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {
|
bgneal@1
|
84 from.selectedIndex = 0;
|
bgneal@1
|
85 SelectBox.move(field_id + '_from', field_id + '_to');
|
bgneal@1
|
86 from.selectedIndex = 0;
|
bgneal@1
|
87 return false;
|
bgneal@1
|
88 }
|
bgneal@1
|
89 var temp = from.selectedIndex;
|
bgneal@1
|
90 SelectBox.filter(field_id + '_from', document.getElementById(field_id + '_input').value);
|
bgneal@1
|
91 from.selectedIndex = temp;
|
bgneal@1
|
92 return true;
|
bgneal@1
|
93 },
|
bgneal@1
|
94 filter_key_down: function(event, field_id) {
|
bgneal@1
|
95 from = document.getElementById(field_id + '_from');
|
bgneal@1
|
96 // right arrow -- move across
|
bgneal@1
|
97 if ((event.which && event.which == 39) || (event.keyCode && event.keyCode == 39)) {
|
bgneal@1
|
98 var old_index = from.selectedIndex;
|
bgneal@1
|
99 SelectBox.move(field_id + '_from', field_id + '_to');
|
bgneal@1
|
100 from.selectedIndex = (old_index == from.length) ? from.length - 1 : old_index;
|
bgneal@1
|
101 return false;
|
bgneal@1
|
102 }
|
bgneal@1
|
103 // down arrow -- wrap around
|
bgneal@1
|
104 if ((event.which && event.which == 40) || (event.keyCode && event.keyCode == 40)) {
|
bgneal@1
|
105 from.selectedIndex = (from.length == from.selectedIndex + 1) ? 0 : from.selectedIndex + 1;
|
bgneal@1
|
106 }
|
bgneal@1
|
107 // up arrow -- wrap around
|
bgneal@1
|
108 if ((event.which && event.which == 38) || (event.keyCode && event.keyCode == 38)) {
|
bgneal@1
|
109 from.selectedIndex = (from.selectedIndex == 0) ? from.length - 1 : from.selectedIndex - 1;
|
bgneal@1
|
110 }
|
bgneal@1
|
111 return true;
|
bgneal@1
|
112 }
|
bgneal@1
|
113 }
|