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