]> git.evergreen-ils.org Git - Evergreen.git/blob - Evergreen/staff_client/chrome/content/evergreen/util/widgets.js
fix progressmeter
[Evergreen.git] / Evergreen / staff_client / chrome / content / evergreen / util / widgets.js
1 sdump('D_WIDGETS',"Loading widgets.js\n");
2
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.
6 var treeitem_id = 0;
7 function append_treeitem() {
8         var e = arguments[0];
9         if (typeof(e) != 'object') { e = document.getElementById(e); }
10         if (typeof(e) != 'object') { throw('typeof e != object : typeof e = ' + typeof(e)); }
11         var treechildren = e;
12
13         if (!treechildren) { sdump('D_WIDGETS','No ' + id + ' to append to\n'); return; }
14
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++ ) {
18                 var treecell = elem(
19                         'treecell',
20                         { 'label': arguments[i], 'id' : 'treeitem_' + treeitem_id + '_' + i }
21                 );
22                 treerow.appendChild(treecell);
23                 //sdump('D_WIDGETS','treecell.label = ' + arguments[i] + '\n');
24         }
25         return treeitem_id++;
26 }
27
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) {
33         try {
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)); }
39
40                 var toggle = e.getAttribute(a);
41                 var next_one = false;
42                 sdump('D_WIDGETS','cycling ' + a + ' on ' + e.getAttribute('id') + ' to ');
43                 for (var i = 0; i < v.length; i++) {
44                         if (next_one) {
45                                 e.setAttribute(a,v[i]);
46                                 sdump('D_WIDGETS',v[i] + '\n');
47                                 return v[i];
48                         }
49                         if (toggle == v[i]) {
50                                 next_one = true;
51                         }
52                 }
53                 if (next_one) {
54                         e.setAttribute(a,v[0]);
55                         sdump('D_WIDGETS',v[0] + '\n');
56                         return v[0];
57                 } else {
58                         throw('current value not in list');
59                 }
60         } catch(E) {
61                 sdump('D_WIDGETS','cycle_attribute error: ' + js2JSON(E) + '\n');
62                 sdump('D_WIDGETS','null\n');
63                 return null;
64         }
65 }
66
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;
73                 } else {
74                         var w = document.getElementById( arguments[i] );
75                         if (w) { 
76                                 sdump('D_WIDGETS',w + '.disabled = true;\n');
77                                 w.disabled = true; 
78                         }
79                 }
80         }
81 }
82
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++) {
89                 e.removeChild(nl[i]);
90         }
91 }
92
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); }
98 }
99
100
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;
107                 } else {
108                         var w = document.getElementById( arguments[i] );
109                         if (w) { 
110                                 sdump('D_WIDGETS',w + '.disabled = false;\n');
111                                 w.disabled = false; 
112                         }
113                 }
114         }
115 }
116
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
120 // gives it focus.
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);
125         }
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'); 
134                         next_one = nl[i];
135                 }
136                 if (nl[i] === current) {
137                         sdump('D_WIDGETS','fake_tab_for_textboxes: Found current\n');
138                         flag = true;
139                 }
140         }
141         if (!next_one) {
142                 sdump('D_WIDGETS','fake_tab_for_textboxes: Out of loop, Setting next_one ' + nl[0] + '\n');     
143                 next_one = nl[0];
144         }
145         if (next_one) {
146                 next_one.focus(); next_one.select();
147         } else {
148                 sdump('D_WIDGETS','fake_tab_for_textboxes: next_one not set\n');
149         }
150 }
151
152
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);
158
159         var t = find_func(tree); if (t) return t;
160
161         var c = children_func(tree);
162
163         for (var i = 0; i < c.length; i++) {
164                 t = find_func( c[i] );
165                 if (t) return t;
166         }
167 }
168
169
170 // Give this element focus
171 function focus_widget(e) {
172         if (typeof(e) == 'object') {
173                 e.focus();
174         } else {
175                 var w = document.getElementById(e);
176                 if (w) { w.focus(); }
177         }
178 }
179
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');
183         var hitlist;
184         if (typeof(tree_w) != 'object') {
185                 hitlist = document.getElementById(tree_w);
186         } else {
187                 hitlist = tree_w;
188         }
189         var list = [];
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');
198                         list.push( i );
199                 }
200         }
201         sdump('D_WIDGETS','leaving get_list_from_tree...\n');
202         return list;
203 }
204
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')
210                 return;
211
212         var real = meter.getAttribute('_real');
213
214         if (!real)
215                 real = 0;
216         real = parseFloat( real ) + parseFloat( increment );
217
218         if (real > 100)
219                 real = 100;
220         else if ( real < 0)
221                 real = 0;
222
223         meter.setAttribute('_real',real);
224         meter.value = Math.ceil( real );
225 }
226
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');
233         if (group) {
234                 var nl = document.getElementsByTagName('checkbox');
235                 for (var i in nl) {
236                         if (typeof(nl[i])=='object') {
237                                 var c = nl[i];
238                                 var cgroup = c.getAttribute('group');
239                                 if (cgroup == group) {
240                                         c.checked = false;
241                                 }
242                         }
243                 }
244                 target.checked = true;
245         } else {
246                 sdump('D_WIDGETS','radio_checkbox: Checkbox must have a group attribute to find peers');
247         }
248 }
249
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] );
256         }
257 }
258
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');
269 }
270
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);
275         }
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');
285                         } else {
286                                 row.setAttribute('hidden','true');
287                         }
288                 }
289         }
290 }
291
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') {
295                 for (var e in el) {
296                         var w = document.getElementById(e);
297                         for (var a in attrs) {
298                                 w.setAttribute(a,attrs[a]);
299                         }
300                 }
301         } else {
302                 var w = document.getElementById(el);
303                 for (var a in attrs) {
304                         w.setAttribute(a,attrs[a]);
305                 }
306         }
307 }
308