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(d,e) {
8 if (typeof(e) != 'object') { e = d.getElementById(e); }
9 if (typeof(e) != 'object') { throw('typeof e != object : typeof e = ' + typeof(e)); }
12 if (!treechildren) { sdump('D_WIDGETS','No ' + id + ' to append to\n'); return; }
14 var treeitem = elem('treeitem'); treechildren.appendChild(treeitem);
15 var treerow = elem('treerow'); treeitem.appendChild(treerow);
16 for (var i = 2; i < arguments.length ; i++ ) {
19 { 'label': arguments[i], 'id' : 'treeitem_' + treeitem_id + '_' + i }
21 treerow.appendChild(treecell);
22 //sdump('D_WIDGETS','treecell.label = ' + arguments[i] + '\n');
27 // This was used in browse_list.js as a more flexible alternative to swap_attribute.
28 // The first argument is the element, the second argument is the pertinant attribute,
29 // and the third argument is an array of values to cycle through for setting the
30 // element's attribute. Ex: var toggle = cycle_attribute( target,'toggle',['1','2','3'] );
31 function cycle_attribute(d,e,a,v) {
33 if (typeof(e) != 'object') { e = d.getElementById(e); }
34 if (typeof(e) != 'object') { throw('typeof e != object : typeof e = ' + typeof(e)); }
35 if (!a) { throw('!a : a = ' + a); }
36 if (! e.getAttribute(a) ) { throw(' ! e.getAttribute(a) : a = ' + a); }
37 if (typeof(v) != 'object') { throw('typeof v != object : typeof v = ' + typeof(v)); }
39 var toggle = e.getAttribute(a);
41 sdump('D_WIDGETS','cycling ' + a + ' on ' + e.getAttribute('id') + ' to ');
42 for (var i = 0; i < v.length; i++) {
44 e.setAttribute(a,v[i]);
45 sdump('D_WIDGETS',v[i] + '\n');
53 e.setAttribute(a,v[0]);
54 sdump('D_WIDGETS',v[0] + '\n');
57 throw('current value not in list');
60 sdump('D_WIDGETS','cycle_attribute error: ' + js2JSON(E) + '\n');
61 sdump('D_WIDGETS','null\n');
66 // Treats each argument as an element to disable
67 function disable_widgets(d) {
68 for (var i = 1; i < arguments.length; i++) {
69 if (typeof(arguments[i]) == 'object') {
70 sdump('D_WIDGETS',arguments[i] + '.disabled = true;\n');
71 arguments[i].disabled = true;
73 var w = d.getElementById( arguments[i] );
75 sdump('D_WIDGETS',w + '.disabled = true;\n');
82 // removes listitems from listboxes
83 function empty_listbox(d,e) {
84 if (typeof(e) != 'object') { e = d.getElementById(e); }
85 if (typeof(e) != 'object') { sdump('D_WIDGETS','Failed on empty_listbox\n'); return; }
86 var nl = e.getElementsByTagName('listitem');
87 for (var i = 0; i < nl.length; i++) {
92 // removes all of an element's children
93 function empty_widget(d,e) {
94 if (typeof(e) != 'object') { e = d.getElementById(e); }
95 if (typeof(e) != 'object') { sdump('D_WIDGETS','Failed on empty_widget\n'); return; }
96 while (e.lastChild) { e.removeChild(e.lastChild); }
100 // Treats each argument as an element to enable
101 function enable_widgets(d) {
102 for (var i = 1; i < arguments.length; i++) {
103 if (typeof(arguments[i]) == 'object') {
104 sdump('D_WIDGETS',arguments[i] + '.disabled = false;\n');
105 arguments[i].disabled = false;
107 var w = d.getElementById( arguments[i] );
109 sdump('D_WIDGETS',w + '.disabled = false;\n');
116 // Originally used in volume.js after intercepting Enter presses on the keyboard.
117 // The first argument is the element to search for textboxes, and the second
118 // argument is the current textbox. This function finds the next textbox and
120 function fake_tab_for_textboxes(d,w,current) {
121 var flag = false; var next_one;
122 if (typeof(w)!='object') {
123 w = d.getElementById(w);
125 sdump('D_WIDGETS', 'fake_tab_for_textboxes: Current ' + current + '\n');
126 var nl = w.getElementsByTagName('textbox');
127 //var nl = d.getElementsByTagName('textbox');
128 sdump('D_WIDGETS', 'fake_tab_for_textboxes: nl.length = ' + nl.length + '\n');
129 for (var i = 0; i < nl.length; i++) {
130 sdump('D_WIDGETS', 'fake_tab_for_textboxes: Considering ' + nl[i] + '...\n');
131 if (flag && !next_one) {
132 sdump('D_WIDGETS', 'fake_tab_for_textboxes: Setting next_one ' + nl[i] + '\n');
135 if (nl[i] === current) {
136 sdump('D_WIDGETS','fake_tab_for_textboxes: Found current\n');
141 sdump('D_WIDGETS','fake_tab_for_textboxes: Out of loop, Setting next_one ' + nl[0] + '\n');
145 next_one.focus(); next_one.select();
147 sdump('D_WIDGETS','fake_tab_for_textboxes: next_one not set\n');
152 // Not actually used anywhere. I'm not sure what this is :D
153 // Ah, looks like it could handle XUL trees and fieldmapper trees
154 // Ex. find( org_tree, function(o){return o.children();}, function(o){return (o.id == 'the winner');})
155 function find_tree_via_children(d,tree,children_func,find_func) {
156 if (typeof(tree)!='object') tree = d.getElementById(tree);
158 var t = find_func(tree); if (t) return t;
160 var c = children_func(tree);
162 for (var i = 0; i < c.length; i++) {
163 t = find_func( c[i] );
169 // Give this element focus
170 function focus_widget(d,e) {
171 if (typeof(e) == 'object') {
174 var w = d.getElementById(e);
175 if (w) { w.focus(); }
179 // Returns a list of selected treeitems from the specified tree
180 function get_list_from_tree_selection(d,tree_w) {
181 sdump('D_WIDGETS','entering get_list_from_tree...\n');
183 if (typeof(tree_w) != 'object') {
184 hitlist = d.getElementById(tree_w);
189 var start = new Object();
190 var end = new Object();
191 var numRanges = hitlist.view.selection.getRangeCount();
192 for (var t=0; t<numRanges; t++){
193 hitlist.view.selection.getRangeAt(t,start,end);
194 for (var v=start.value; v<=end.value; v++){
195 var i = hitlist.contentView.getItemAtIndex(v);
196 //sdump('D_WIDGETS',i.tagName + '\n');
200 sdump('D_WIDGETS','leaving get_list_from_tree...\n');
204 // Make sure we a widget
205 function get_widget(d,e) {
206 if (typeof(e) == 'object') {
209 var w = d.getElementById(e);
215 // Increment a XUL progressmeter
216 function incr_progressmeter(d,meter,increment) {
217 if (typeof(meter)!='object')
218 meter = d.getElementById(meter);
219 if (typeof(meter)!='object')
222 var real = meter.getAttribute('_real');
226 real = parseFloat( real ) + parseFloat( increment );
233 meter.setAttribute('_real',real);
234 meter.value = Math.ceil( real );
237 // Simulates radio buttons with checkboxes. Include this in command event listeners
238 // for the pertinent textboxes. For any set of checkboxes that have the same 'group'
239 // attribute, only one can be checked at a time.
240 function radio_checkbox(d,ev) {
241 var target = ev.target;
242 var group = target.getAttribute('group');
244 var nl = d.getElementsByTagName('checkbox');
246 if (typeof(nl[i])=='object') {
248 var cgroup = c.getAttribute('group');
249 if (cgroup == group) {
254 target.checked = true;
256 sdump('D_WIDGETS','radio_checkbox: Checkbox must have a group attribute to find peers');
260 // Takes a hash with key:value => deck element id : page index
261 // Sets each deck to the corresponding index
262 function set_decks(d,params) {
263 for (var deck_id in params) {
265 if (typeof(deck) != 'object')
266 deck = d.getElementById( deck_id )
267 if (deck) deck.setAttribute( 'selectedIndex', params[deck_id] );
271 // swaps the values of two attributes for an element
272 function swap_attributes(d,e,a1,a2) {
273 if (typeof(e) != 'object') { e = d.getElementById(e); }
274 if (typeof(e) != 'object') { sdump('D_WIDGETS','Failed on swap_attributes\n'); return; }
275 var a1_v = e.getAttribute(a1);
276 var a2_v = e.getAttribute(a2);
277 e.setAttribute(a1,a2_v);
278 e.setAttribute(a2, a1_v);
279 sdump('D_WIDGETS','before: a1 = ' + a1_v + ' a2 = ' + a2_v + ' and ');
280 sdump('D_WIDGETS','after: a1 = ' + a2_v + ' a2 = ' + a1_v + '\n');
283 // Flips the hidden value for each row in a grid
284 function toggle_hidden_grid_rows(d,grid) {
285 if (typeof(grid) != 'object') {
286 grid = d.getElementById(grid);
288 if (!grid) { return; }
289 var rows = grid.lastChild; if (!rows) { return; }
290 for (var r = 0; r < rows.childNodes.length; r++ ) {
291 var row = rows.childNodes[r];
292 if (typeof(row) == 'object') {
293 //sdump('D_WIDGETS','toggle row = ' + row + '\n');
294 var hidden = row.getAttribute('hidden');
295 if (hidden == 'true') {
296 row.setAttribute('hidden','false');
298 row.setAttribute('hidden','true');
304 /* 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 */
305 function xul_setAttributes(d,el,attrs) {
306 if (typeof(el) == 'object') {
308 var w = d.getElementById(e);
309 for (var a in attrs) {
310 w.setAttribute(a,attrs[a]);
314 var w = d.getElementById(el);
315 for (var a in attrs) {
316 w.setAttribute(a,attrs[a]);