1 sdump('D_WIDGETS',"Loading widgets.js\n");
3 // This was originally used in circ.js for checkin and checkout lists.
4 // The first argument is the treechildren element for the tree.
5 // Subsequent arguments are treated as textual values for treecells in that treeitem.
7 function append_treeitem() {
9 if (typeof(e) != 'object') { e = document.getElementById(e); }
10 if (typeof(e) != 'object') { throw('typeof e != object : typeof e = ' + typeof(e)); }
13 if (!treechildren) { sdump('D_WIDGETS','No ' + id + ' to append to\n'); return; }
15 var treeitem = elem('treeitem'); treechildren.appendChild(treeitem);
16 var treerow = elem('treerow'); treeitem.appendChild(treerow);
17 for (var i = 1; i < arguments.length ; i++ ) {
20 { 'label': arguments[i], 'id' : 'treeitem_' + treeitem_id + '_' + i }
22 treerow.appendChild(treecell);
23 //sdump('D_WIDGETS','treecell.label = ' + arguments[i] + '\n');
28 // This was used in browse_list.js as a more flexible alternative to swap_attribute.
29 // The first argument is the element, the second argument is the pertinant attribute,
30 // and the third argument is an array of values to cycle through for setting the
31 // element's attribute. Ex: var toggle = cycle_attribute( target,'toggle',['1','2','3'] );
32 function cycle_attribute(e,a,v) {
34 if (typeof(e) != 'object') { e = document.getElementById(e); }
35 if (typeof(e) != 'object') { throw('typeof e != object : typeof e = ' + typeof(e)); }
36 if (!a) { throw('!a : a = ' + a); }
37 if (! e.getAttribute(a) ) { throw(' ! e.getAttribute(a) : a = ' + a); }
38 if (typeof(v) != 'object') { throw('typeof v != object : typeof v = ' + typeof(v)); }
40 var toggle = e.getAttribute(a);
42 sdump('D_WIDGETS','cycling ' + a + ' on ' + e.getAttribute('id') + ' to ');
43 for (var i = 0; i < v.length; i++) {
45 e.setAttribute(a,v[i]);
46 sdump('D_WIDGETS',v[i] + '\n');
54 e.setAttribute(a,v[0]);
55 sdump('D_WIDGETS',v[0] + '\n');
58 throw('current value not in list');
61 sdump('D_WIDGETS','cycle_attribute error: ' + js2JSON(E) + '\n');
62 sdump('D_WIDGETS','null\n');
67 // Treats each argument as an element to disable
68 function disable_widgets() {
69 for (var i = 0; i < arguments.length; i++) {
70 if (typeof(arguments[i]) == 'object') {
71 sdump('D_WIDGETS',arguments[i] + '.disabled = true;\n');
72 arguments[i].disabled = true;
74 var w = document.getElementById( arguments[i] );
76 sdump('D_WIDGETS',w + '.disabled = true;\n');
83 // removes listitems from listboxes
84 function empty_listbox(e) {
85 if (typeof(e) != 'object') { e = document.getElementById(e); }
86 if (typeof(e) != 'object') { sdump('D_WIDGETS','Failed on empty_listbox\n'); return; }
87 var nl = e.getElementsByTagName('listitem');
88 for (var i = 0; i < nl.length; i++) {
93 // removes all of an element's children
94 function empty_widget(e) {
95 if (typeof(e) != 'object') { e = document.getElementById(e); }
96 if (typeof(e) != 'object') { sdump('D_WIDGETS','Failed on empty_widget\n'); return; }
97 while (e.lastChild) { e.removeChild(e.lastChild); }
101 // Treats each argument as an element to enable
102 function enable_widgets() {
103 for (var i = 0; i < arguments.length; i++) {
104 if (typeof(arguments[i]) == 'object') {
105 sdump('D_WIDGETS',arguments[i] + '.disabled = false;\n');
106 arguments[i].disabled = false;
108 var w = document.getElementById( arguments[i] );
110 sdump('D_WIDGETS',w + '.disabled = false;\n');
117 // Originally used in volume.js after intercepting Enter presses on the keyboard.
118 // The first argument is the element to search for textboxes, and the second
119 // argument is the current textbox. This function finds the next textbox and
121 function fake_tab_for_textboxes(w,current) {
122 var flag = false; var next_one;
123 if (typeof(w)!='object') {
124 w = document.getElementById(w);
126 sdump('D_WIDGETS', 'fake_tab_for_textboxes: Current ' + current + '\n');
127 var nl = w.getElementsByTagName('textbox');
128 //var nl = document.getElementsByTagName('textbox');
129 sdump('D_WIDGETS', 'fake_tab_for_textboxes: nl.length = ' + nl.length + '\n');
130 for (var i = 0; i < nl.length; i++) {
131 sdump('D_WIDGETS', 'fake_tab_for_textboxes: Considering ' + nl[i] + '...\n');
132 if (flag && !next_one) {
133 sdump('D_WIDGETS', 'fake_tab_for_textboxes: Setting next_one ' + nl[i] + '\n');
136 if (nl[i] === current) {
137 sdump('D_WIDGETS','fake_tab_for_textboxes: Found current\n');
142 sdump('D_WIDGETS','fake_tab_for_textboxes: Out of loop, Setting next_one ' + nl[0] + '\n');
146 next_one.focus(); next_one.select();
148 sdump('D_WIDGETS','fake_tab_for_textboxes: next_one not set\n');
153 // Not actually used anywhere. I'm not sure what this is :D
154 // Ah, looks like it could handle XUL trees and fieldmapper trees
155 // Ex. find( org_tree, function(o){return o.children();}, function(o){return (o.id == 'the winner');})
156 function find_tree_via_children(tree,children_func,find_func) {
157 if (typeof(tree)!='object') tree = document.getElementById(tree);
159 var t = find_func(tree); if (t) return t;
161 var c = children_func(tree);
163 for (var i = 0; i < c.length; i++) {
164 t = find_func( c[i] );
170 // Give this element focus
171 function focus_widget(e) {
172 if (typeof(e) == 'object') {
175 var w = document.getElementById(e);
176 if (w) { w.focus(); }
180 // Returns a list of selected treeitems from the specified tree
181 function get_list_from_tree_selection(tree_w) {
182 sdump('D_WIDGETS','entering get_list_from_tree...\n');
184 if (typeof(tree_w) != 'object') {
185 hitlist = document.getElementById(tree_w);
190 var start = new Object();
191 var end = new Object();
192 var numRanges = hitlist.view.selection.getRangeCount();
193 for (var t=0; t<numRanges; t++){
194 hitlist.view.selection.getRangeAt(t,start,end);
195 for (var v=start.value; v<=end.value; v++){
196 var i = hitlist.contentView.getItemAtIndex(v);
197 //sdump('D_WIDGETS',i.tagName + '\n');
201 sdump('D_WIDGETS','leaving get_list_from_tree...\n');
205 // Increment a XUL progressmeter
206 function incr_progressmeter(meter,increment) {
207 if (typeof(meter)!='object')
208 meter = document.getElementById(meter);
209 if (typeof(meter)!='object')
212 var real = meter.getAttribute('_real');
216 real = parseFloat( real ) + parseFloat( increment );
223 meter.setAttribute('_real',real);
224 meter.value = Math.ceil( real );
227 // Simulates radio buttons with checkboxes. Include this in command event listeners
228 // for the pertinent textboxes. For any set of checkboxes that have the same 'group'
229 // attribute, only one can be checked at a time.
230 function radio_checkbox(ev) {
231 var target = ev.target;
232 var group = target.getAttribute('group');
234 var nl = document.getElementsByTagName('checkbox');
236 if (typeof(nl[i])=='object') {
238 var cgroup = c.getAttribute('group');
239 if (cgroup == group) {
244 target.checked = true;
246 sdump('D_WIDGETS','radio_checkbox: Checkbox must have a group attribute to find peers');
250 // Takes a hash with key:value => deck element id : page index
251 // Sets each deck to the corresponding index
252 function set_decks(params) {
253 for (var deck_id in params) {
254 var deck = document.getElementById( deck_id )
255 if (deck) deck.setAttribute( 'selectedIndex', params[deck_id] );
259 // swaps the values of two attributes for an element
260 function swap_attributes(e,a1,a2) {
261 if (typeof(e) != 'object') { e = document.getElementById(e); }
262 if (typeof(e) != 'object') { sdump('D_WIDGETS','Failed on swap_attributes\n'); return; }
263 var a1_v = e.getAttribute(a1);
264 var a2_v = e.getAttribute(a2);
265 e.setAttribute(a1,a2_v);
266 e.setAttribute(a2, a1_v);
267 sdump('D_WIDGETS','before: a1 = ' + a1_v + ' a2 = ' + a2_v + ' and ');
268 sdump('D_WIDGETS','after: a1 = ' + a2_v + ' a2 = ' + a1_v + '\n');
271 // Flips the hidden value for each row in a grid
272 function toggle_hidden_grid_rows(grid) {
273 if (typeof(grid) != 'object') {
274 grid = document.getElementById(grid);
276 if (!grid) { return; }
277 var rows = grid.lastChild; if (!rows) { return; }
278 for (var r = 0; r < rows.childNodes.length; r++ ) {
279 var row = rows.childNodes[r];
280 if (typeof(row) == 'object') {
281 //sdump('D_WIDGETS','toggle row = ' + row + '\n');
282 var hidden = row.getAttribute('hidden');
283 if (hidden == 'true') {
284 row.setAttribute('hidden','false');
286 row.setAttribute('hidden','true');
292 /* The first parameter is the id of the element to set, or an array of ids for elements to set in batch. The second parameter is an object containing the attribute/value pairs to assign to the element or elements */
293 function xul_setAttributes(el,attrs) {
294 if (typeof(el) == 'object') {
296 var w = document.getElementById(e);
297 for (var a in attrs) {
298 w.setAttribute(a,attrs[a]);
302 var w = document.getElementById(el);
303 for (var a in attrs) {
304 w.setAttribute(a,attrs[a]);