Mercurial > public > madeira
comparison media/django/js/SelectFilter.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 SelectFilter - Turns a multiple-select box into a filter interface. | |
3 | |
4 Requires SelectBox.js and addevent.js. | |
5 */ | |
6 | |
7 function findForm(node) { | |
8 // returns the node of the form containing the given node | |
9 if (node.tagName.toLowerCase() != 'form') { | |
10 return findForm(node.parentNode); | |
11 } | |
12 return node; | |
13 } | |
14 | |
15 var SelectFilter = { | |
16 init: function(field_id) { | |
17 var from_box = document.getElementById(field_id); | |
18 from_box.id += '_from'; // change its ID | |
19 // Create the INPUT input box | |
20 var input_box = document.createElement('input'); | |
21 input_box.id = field_id + '_input'; | |
22 input_box.setAttribute('type', 'text'); | |
23 from_box.parentNode.insertBefore(input_box, from_box); | |
24 from_box.parentNode.insertBefore(document.createElement('br'), input_box.nextSibling); | |
25 // Create the TO box | |
26 var to_box = document.createElement('select'); | |
27 to_box.id = field_id + '_to'; | |
28 to_box.setAttribute('multiple', 'multiple'); | |
29 to_box.setAttribute('size', from_box.size); | |
30 from_box.parentNode.insertBefore(to_box, from_box.nextSibling); | |
31 to_box.setAttribute('name', from_box.getAttribute('name')); | |
32 from_box.setAttribute('name', from_box.getAttribute('name') + '_old'); | |
33 // Give the filters a CSS hook | |
34 from_box.setAttribute('class', 'filtered'); | |
35 to_box.setAttribute('class', 'filtered'); | |
36 // Set up the JavaScript event handlers for the select box filter interface | |
37 addEvent(input_box, 'keyup', function(e) { SelectFilter.filter_key_up(e, field_id); }); | |
38 addEvent(input_box, 'keydown', function(e) { SelectFilter.filter_key_down(e, field_id); }); | |
39 addEvent(from_box, 'dblclick', function() { SelectBox.move(field_id + '_from', field_id + '_to'); }); | |
40 addEvent(from_box, 'focus', function() { input_box.focus(); }); | |
41 addEvent(to_box, 'dblclick', function() { SelectBox.move(field_id + '_to', field_id + '_from'); }); | |
42 addEvent(findForm(from_box), 'submit', function() { SelectBox.select_all(field_id + '_to'); }); | |
43 SelectBox.init(field_id + '_from'); | |
44 SelectBox.init(field_id + '_to'); | |
45 // Move selected from_box options to to_box | |
46 SelectBox.move(field_id + '_from', field_id + '_to'); | |
47 }, | |
48 filter_key_up: function(event, field_id) { | |
49 from = document.getElementById(field_id + '_from'); | |
50 // don't submit form if user pressed Enter | |
51 if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) { | |
52 from.selectedIndex = 0; | |
53 SelectBox.move(field_id + '_from', field_id + '_to'); | |
54 from.selectedIndex = 0; | |
55 return false; | |
56 } | |
57 var temp = from.selectedIndex; | |
58 SelectBox.filter(field_id + '_from', document.getElementById(field_id + '_input').value); | |
59 from.selectedIndex = temp; | |
60 return true; | |
61 }, | |
62 filter_key_down: function(event, field_id) { | |
63 from = document.getElementById(field_id + '_from'); | |
64 // right arrow -- move across | |
65 if ((event.which && event.which == 39) || (event.keyCode && event.keyCode == 39)) { | |
66 var old_index = from.selectedIndex; | |
67 SelectBox.move(field_id + '_from', field_id + '_to'); | |
68 from.selectedIndex = (old_index == from.length) ? from.length - 1 : old_index; | |
69 return false; | |
70 } | |
71 // down arrow -- wrap around | |
72 if ((event.which && event.which == 40) || (event.keyCode && event.keyCode == 40)) { | |
73 from.selectedIndex = (from.length == from.selectedIndex + 1) ? 0 : from.selectedIndex + 1; | |
74 } | |
75 // up arrow -- wrap around | |
76 if ((event.which && event.which == 38) || (event.keyCode && event.keyCode == 38)) { | |
77 from.selectedIndex = (from.selectedIndex == 0) ? from.length - 1 : from.selectedIndex - 1; | |
78 } | |
79 return true; | |
80 } | |
81 } |