Hello site
[auf_framonde.git] / eggs / Django-1.4.5-py2.7.egg / django / contrib / admin / static / admin / js / SelectFilter2.js
CommitLineData
01b54c21
MN
1/*
2SelectFilter2 - Turns a multiple-select box into a filter interface.
3
4Requires core.js, SelectBox.js and addevent.js.
5*/
6(function($) {
7function 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
15window.SelectFilter = {
16 init: function(field_id, field_name, is_stacked, admin_media_prefix) {
17 if (field_id.match(/__prefix__/)){
18 // Don't intialize on empty forms.
19 return;
20 }
21 var from_box = document.getElementById(field_id);
22 from_box.id += '_from'; // change its ID
23 from_box.className = 'filtered';
24
25 var ps = from_box.parentNode.getElementsByTagName('p');
26 for (var i=0; i<ps.length; i++) {
27 if (ps[i].className.indexOf("info") != -1) {
28 // Remove <p class="info">, because it just gets in the way.
29 from_box.parentNode.removeChild(ps[i]);
30 } else if (ps[i].className.indexOf("help") != -1) {
31 // Move help text up to the top so it isn't below the select
32 // boxes or wrapped off on the side to the right of the add
33 // button:
34 from_box.parentNode.insertBefore(ps[i], from_box.parentNode.firstChild);
35 }
36 }
37
38 // <div class="selector"> or <div class="selector stacked">
39 var selector_div = quickElement('div', from_box.parentNode);
40 selector_div.className = is_stacked ? 'selector stacked' : 'selector';
41
42 // <div class="selector-available">
43 var selector_available = quickElement('div', selector_div, '');
44 selector_available.className = 'selector-available';
45 var title_available = quickElement('h2', selector_available, interpolate(gettext('Available %s') + ' ', [field_name]));
46 quickElement('img', title_available, '', 'src', admin_media_prefix + 'img/icon-unknown.gif', 'width', '10', 'height', '10', 'class', 'help help-tooltip', 'title', interpolate(gettext('This is the list of available %s. You may choose some by selecting them in the box below and then clicking the "Choose" arrow between the two boxes.'), [field_name]));
47
48 var filter_p = quickElement('p', selector_available, '', 'id', field_id + '_filter');
49 filter_p.className = 'selector-filter';
50
51 var search_filter_label = quickElement('label', filter_p, '', 'for', field_id + "_input");
52
53 var search_selector_img = quickElement('img', search_filter_label, '', 'src', admin_media_prefix + 'img/selector-search.gif', 'class', 'help-tooltip', 'alt', '', 'title', interpolate(gettext("Type into this box to filter down the list of available %s."), [field_name]));
54
55 filter_p.appendChild(document.createTextNode(' '));
56
57 var filter_input = quickElement('input', filter_p, '', 'type', 'text', 'placeholder', gettext("Filter"));
58 filter_input.id = field_id + '_input';
59
60 selector_available.appendChild(from_box);
61 var choose_all = quickElement('a', selector_available, gettext('Choose all'), 'title', interpolate(gettext('Click to choose all %s at once.'), [field_name]), 'href', 'javascript: (function(){ SelectBox.move_all("' + field_id + '_from", "' + field_id + '_to"); SelectFilter.refresh_icons("' + field_id + '");})()', 'id', field_id + '_add_all_link');
62 choose_all.className = 'selector-chooseall';
63
64 // <ul class="selector-chooser">
65 var selector_chooser = quickElement('ul', selector_div, '');
66 selector_chooser.className = 'selector-chooser';
67 var add_link = quickElement('a', quickElement('li', selector_chooser, ''), gettext('Choose'), 'title', gettext('Choose'), 'href', 'javascript: (function(){ SelectBox.move("' + field_id + '_from","' + field_id + '_to"); SelectFilter.refresh_icons("' + field_id + '");})()', 'id', field_id + '_add_link');
68 add_link.className = 'selector-add';
69 var remove_link = quickElement('a', quickElement('li', selector_chooser, ''), gettext('Remove'), 'title', gettext('Remove'), 'href', 'javascript: (function(){ SelectBox.move("' + field_id + '_to","' + field_id + '_from"); SelectFilter.refresh_icons("' + field_id + '");})()', 'id', field_id + '_remove_link');
70 remove_link.className = 'selector-remove';
71
72 // <div class="selector-chosen">
73 var selector_chosen = quickElement('div', selector_div, '');
74 selector_chosen.className = 'selector-chosen';
75 var title_chosen = quickElement('h2', selector_chosen, interpolate(gettext('Chosen %s') + ' ', [field_name]));
76 quickElement('img', title_chosen, '', 'src', admin_media_prefix + 'img/icon-unknown.gif', 'width', '10', 'height', '10', 'class', 'help help-tooltip', 'title', interpolate(gettext('This is the list of chosen %s. You may remove some by selecting them in the box below and then clicking the "Remove" arrow between the two boxes.'), [field_name]));
77
78 var to_box = quickElement('select', selector_chosen, '', 'id', field_id + '_to', 'multiple', 'multiple', 'size', from_box.size, 'name', from_box.getAttribute('name'));
79 to_box.className = 'filtered';
80 var clear_all = quickElement('a', selector_chosen, gettext('Remove all'), 'title', interpolate(gettext('Click to remove all chosen %s at once.'), [field_name]), 'href', 'javascript: (function() { SelectBox.move_all("' + field_id + '_to", "' + field_id + '_from"); SelectFilter.refresh_icons("' + field_id + '");})()', 'id', field_id + '_remove_all_link');
81 clear_all.className = 'selector-clearall';
82
83 from_box.setAttribute('name', from_box.getAttribute('name') + '_old');
84
85 // Set up the JavaScript event handlers for the select box filter interface
86 addEvent(filter_input, 'keyup', function(e) { SelectFilter.filter_key_up(e, field_id); });
87 addEvent(filter_input, 'keydown', function(e) { SelectFilter.filter_key_down(e, field_id); });
88 addEvent(from_box, 'change', function(e) { SelectFilter.refresh_icons(field_id) });
89 addEvent(to_box, 'change', function(e) { SelectFilter.refresh_icons(field_id) });
90 addEvent(from_box, 'dblclick', function() { SelectBox.move(field_id + '_from', field_id + '_to'); SelectFilter.refresh_icons(field_id); });
91 addEvent(to_box, 'dblclick', function() { SelectBox.move(field_id + '_to', field_id + '_from'); SelectFilter.refresh_icons(field_id); });
92 addEvent(findForm(from_box), 'submit', function() { SelectBox.select_all(field_id + '_to'); });
93 SelectBox.init(field_id + '_from');
94 SelectBox.init(field_id + '_to');
95 // Move selected from_box options to to_box
96 SelectBox.move(field_id + '_from', field_id + '_to');
97
98 if (!is_stacked) {
99 // In horizontal mode, give the same height to the two boxes.
100 var j_from_box = $(from_box);
101 var j_to_box = $(to_box);
102 var resize_filters = function() { j_to_box.height($(filter_p).outerHeight() + j_from_box.outerHeight()); }
103 if (j_from_box.outerHeight() > 0) {
104 resize_filters(); // This fieldset is already open. Resize now.
105 } else {
106 // This fieldset is probably collapsed. Wait for its 'show' event.
107 j_to_box.closest('fieldset').one('show.fieldset', resize_filters);
108 }
109 }
110
111 // Initial icon refresh
112 SelectFilter.refresh_icons(field_id);
113 },
114 refresh_icons: function(field_id) {
115 var from = $('#' + field_id + '_from');
116 var to = $('#' + field_id + '_to');
117 var is_from_selected = from.find('option:selected').length > 0;
118 var is_to_selected = to.find('option:selected').length > 0;
119 // Active if at least one item is selected
120 $('#' + field_id + '_add_link').toggleClass('active', is_from_selected);
121 $('#' + field_id + '_remove_link').toggleClass('active', is_to_selected);
122 // Active if the corresponding box isn't empty
123 $('#' + field_id + '_add_all_link').toggleClass('active', from.find('option').length > 0);
124 $('#' + field_id + '_remove_all_link').toggleClass('active', to.find('option').length > 0);
125 },
126 filter_key_up: function(event, field_id) {
127 var from = document.getElementById(field_id + '_from');
128 // don't submit form if user pressed Enter
129 if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {
130 from.selectedIndex = 0;
131 SelectBox.move(field_id + '_from', field_id + '_to');
132 from.selectedIndex = 0;
133 return false;
134 }
135 var temp = from.selectedIndex;
136 SelectBox.filter(field_id + '_from', document.getElementById(field_id + '_input').value);
137 from.selectedIndex = temp;
138 return true;
139 },
140 filter_key_down: function(event, field_id) {
141 var from = document.getElementById(field_id + '_from');
142 // right arrow -- move across
143 if ((event.which && event.which == 39) || (event.keyCode && event.keyCode == 39)) {
144 var old_index = from.selectedIndex;
145 SelectBox.move(field_id + '_from', field_id + '_to');
146 from.selectedIndex = (old_index == from.length) ? from.length - 1 : old_index;
147 return false;
148 }
149 // down arrow -- wrap around
150 if ((event.which && event.which == 40) || (event.keyCode && event.keyCode == 40)) {
151 from.selectedIndex = (from.length == from.selectedIndex + 1) ? 0 : from.selectedIndex + 1;
152 }
153 // up arrow -- wrap around
154 if ((event.which && event.which == 38) || (event.keyCode && event.keyCode == 38)) {
155 from.selectedIndex = (from.selectedIndex == 0) ? from.length - 1 : from.selectedIndex - 1;
156 }
157 return true;
158 }
159}
160
161})(django.jQuery);