comparison media/django/js/SelectFilter2.js @ 1:0dcfcdf50c62

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