]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/list.js
toward easier row refreshing
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / util / list.js
1 dump('entering util.list.js\n');
2
3 if (typeof main == 'undefined') main = {};
4 util.list = function (id) {
5
6     this.node = document.getElementById(id);
7
8     this.row_count = { 'total' : 0, 'fleshed' : 0 };
9
10     this.unique_row_counter = 0;
11
12     if (!this.node) throw('Could not find element ' + id);
13     switch(this.node.nodeName) {
14         case 'listbox' : 
15         case 'tree' : break;
16         case 'richlistbox' :
17             throw(this.node.nodeName + ' not yet supported'); break;
18         default: throw(this.node.nodeName + ' not supported'); break;
19     }
20
21     JSAN.use('util.error'); this.error = new util.error();
22
23     return this;
24 };
25
26 util.list.prototype = {
27
28     'init' : function (params) {
29
30         var obj = this;
31
32         JSAN.use('util.widgets');
33
34         if (typeof params.map_row_to_column == 'function') obj.map_row_to_column = params.map_row_to_column;
35         if (typeof params.map_row_to_columns == 'function') obj.map_row_to_columns = params.map_row_to_columns;
36         if (typeof params.retrieve_row == 'function') obj.retrieve_row = params.retrieve_row;
37
38         obj.prebuilt = false;
39         if (typeof params.prebuilt != 'undefined') obj.prebuilt = params.prebuilt;
40
41         if (typeof params.columns == 'undefined') throw('util.list.init: No columns');
42         obj.columns = params.columns;
43
44         switch(obj.node.nodeName) {
45             case 'tree' : obj._init_tree(params); break;
46             case 'listbox' : obj._init_listbox(params); break;
47             default: throw('NYI: Need ._init() for ' + obj.node.nodeName); break;
48         }
49     },
50
51     '_init_tree' : function (params) {
52         var obj = this;
53         if (this.prebuilt) {
54         
55             this.treechildren = this.node.lastChild;    
56         
57         } else {
58             var treecols = document.createElement('treecols');
59             this.node.appendChild(treecols);
60             this.treecols = treecols;
61
62             for (var i = 0; i < this.columns.length; i++) {
63                 var treecol = document.createElement('treecol');
64                 for (var j in this.columns[i]) {
65                     treecol.setAttribute(j,this.columns[i][j]);
66                 }
67                 treecols.appendChild(treecol);
68                 if (this.columns[i].type == 'checkbox') {
69                     treecol.addEventListener(
70                         'click',
71                         function(ev) {
72                             setTimeout(
73                                 function() {
74                                     var toggle = ev.target.getAttribute('toggleAll') || 'on';
75                                     if (toggle == 'off') toggle = 'on'; else toggle = 'off';
76                                     ev.target.setAttribute('toggleAll',toggle);
77                                     obj._toggle_checkbox_column(ev.target,toggle);
78                                 }, 0
79                             );
80                         },
81                         false
82                     );
83                 } else {
84                     treecol.addEventListener(
85                         'click', 
86                         function(ev) {
87                             function do_it() {
88                                 var sortDir = ev.target.getAttribute('sortDir') || 'desc';
89                                 if (sortDir == 'desc') sortDir = 'asc'; else sortDir = 'desc';
90                                 ev.target.setAttribute('sortDir',sortDir);
91                                 obj._sort_tree(ev.target,sortDir);
92                             }
93
94                             if (obj.row_count.total != obj.row_count.fleshed && (obj.row_count.total - obj.row_count.fleshed) > 50) {
95                                 var r = window.confirm(document.getElementById('offlineStrings').getFormattedString('list.row_fetch_warning',[obj.row_count.fleshed,obj.row_count.total]));
96
97                                 if (r) {
98                                     setTimeout( do_it, 0 );
99                                 }
100                             } else {
101                                     setTimeout( do_it, 0 );
102                             }
103                         },
104                         false
105                     );
106                 }
107                 var splitter = document.createElement('splitter');
108                 splitter.setAttribute('class','tree-splitter');
109                 treecols.appendChild(splitter);
110             }
111
112             var treechildren = document.createElement('treechildren');
113             this.node.appendChild(treechildren);
114             this.treechildren = treechildren;
115         }
116         if (typeof params.on_sort == 'function') {
117             this.on_sort = params.on_sort;
118         }
119         if (typeof params.on_checkbox_toggle == 'function') {
120             this.on_checkbox_toggle = params.on_checkbox_toggle;
121         }
122         this.node.addEventListener(
123             'select',
124             function(ev) {
125                 if (typeof params.on_select == 'function') {
126                     params.on_select(ev);
127                 }
128                 var x = document.getElementById(obj.node.id + '_clipfield');
129                 if (x) {
130                     var sel = obj.retrieve_selection();
131                     x.setAttribute('disabled', sel.length == 0);
132                 }
133             },
134             false
135         );
136         if (typeof params.on_click == 'function') {
137             this.node.addEventListener(
138                 'click',
139                 params.on_click,
140                 false
141             );
142         }
143         /*
144         this.node.addEventListener(
145             'mousemove',
146             function(ev) { obj.detect_visible(); },
147             false
148         );
149         */
150         this.node.addEventListener(
151             'keypress',
152             function(ev) { obj.auto_retrieve(); },
153             false
154         );
155         this.node.addEventListener(
156             'click',
157             function(ev) { obj.auto_retrieve(); },
158             false
159         );
160         window.addEventListener(
161             'resize',
162             function(ev) { obj.auto_retrieve(); },
163             false
164         );
165         /* FIXME -- find events on scrollbar to trigger this */
166         obj.detect_visible_polling();    
167         /*
168         var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(this.node)[1] )[1];
169         var slider = document.getAnonymousNodes( scrollbar )[2];
170         alert('scrollbar = ' + scrollbar.nodeName + ' grippy = ' + slider.nodeName);
171         scrollbar.addEventListener('click',function(){alert('sb click');},false);
172         scrollbar.addEventListener('command',function(){alert('sb command');},false);
173         scrollbar.addEventListener('scroll',function(){alert('sb scroll');},false);
174         slider.addEventListener('click',function(){alert('slider click');},false);
175         slider.addEventListener('command',function(){alert('slider command');},false);
176         slider.addEventListener('scroll',function(){alert('slider scroll');},false);
177         */
178         this.node.addEventListener('scroll',function(){ obj.auto_retrieve(); },false);
179
180         this.restores_columns(params);
181     },
182
183     '_init_listbox' : function (params) {
184         if (this.prebuilt) {
185         } else {
186             var listhead = document.createElement('listhead');
187             this.node.appendChild(listhead);
188
189             var listcols = document.createElement('listcols');
190             this.node.appendChild(listcols);
191
192             for (var i = 0; i < this.columns.length; i++) {
193                 var listheader = document.createElement('listheader');
194                 listhead.appendChild(listheader);
195                 var listcol = document.createElement('listcol');
196                 listcols.appendChild(listcol);
197                 for (var j in this.columns[i]) {
198                     listheader.setAttribute(j,this.columns[i][j]);
199                     listcol.setAttribute(j,this.columns[i][j]);
200                 };
201             }
202         }
203     },
204
205     'save_columns' : function (params) {
206         var obj = this;
207         switch (this.node.nodeName) {
208             case 'tree' : this._save_columns_tree(params); break;
209             default: throw('NYI: Need .save_columns() for ' + this.node.nodeName); break;
210         }
211     },
212
213     '_save_columns_tree' : function (params) {
214         var obj = this;
215         try {
216             var id = obj.node.getAttribute('id'); if (!id) {
217                 alert("FIXME: The columns for this list cannot be saved because the list has no id.");
218                 return;
219             }
220             var my_cols = {};
221             var nl = obj.node.getElementsByTagName('treecol');
222             for (var i = 0; i < nl.length; i++) {
223                 var col = nl[i];
224                 var col_id = col.getAttribute('id');
225                 if (!col_id) {
226                     alert('FIXME: A column in this list does not have an id and cannot be saved');
227                     continue;
228                 }
229                 var col_hidden = col.getAttribute('hidden'); 
230                 var col_width = col.getAttribute('width'); 
231                 var col_ordinal = col.getAttribute('ordinal'); 
232                 my_cols[ col_id ] = { 'hidden' : col_hidden, 'width' : col_width, 'ordinal' : col_ordinal };
233             }
234             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
235             JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
236             file.set_object(my_cols);
237             file.close();
238             alert(document.getElementById('offlineStrings').getString('list.columns_saved'));
239         } catch(E) {
240             obj.error.standard_unexpected_error_alert('_save_columns_tree',E);
241         }
242     },
243
244     'restores_columns' : function (params) {
245         var obj = this;
246         switch (this.node.nodeName) {
247             case 'tree' : this._restores_columns_tree(params); break;
248             default: throw('NYI: Need .restores_columns() for ' + this.node.nodeName); break;
249         }
250     },
251
252     '_restores_columns_tree' : function (params) {
253         var obj = this;
254         try {
255             var id = obj.node.getAttribute('id'); if (!id) {
256                 alert("FIXME: The columns for this list cannot be restored because the list has no id.");
257                 return;
258             }
259
260             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
261             JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
262             if (file._file.exists()) {
263                 var my_cols = file.get_object(); file.close();
264                 var nl = obj.node.getElementsByTagName('treecol');
265                 for (var i = 0; i < nl.length; i++) {
266                     var col = nl[i];
267                     var col_id = col.getAttribute('id');
268                     if (!col_id) {
269                         alert('FIXME: A column in this list does not have an id and cannot be saved');
270                         continue;
271                     }
272                     if (typeof my_cols[col_id] != 'undefined') {
273                         col.setAttribute('hidden',my_cols[col_id].hidden); 
274                         col.setAttribute('width',my_cols[col_id].width); 
275                         col.setAttribute('ordinal',my_cols[col_id].ordinal); 
276                     } else {
277                         obj.error.sdump('D_ERROR','WARNING: Column ' + col_id + ' did not have a saved state.');
278                     }
279                 }
280             }
281         } catch(E) {
282             obj.error.standard_unexpected_error_alert('_restore_columns_tree',E);
283         }
284     },
285
286     'clear' : function (params) {
287         var obj = this;
288         switch (this.node.nodeName) {
289             case 'tree' : this._clear_tree(params); break;
290             case 'listbox' : this._clear_listbox(params); break;
291             default: throw('NYI: Need .clear() for ' + this.node.nodeName); break;
292         }
293         this.error.sdump('D_LIST','Clearing list ' + this.node.getAttribute('id') + '\n');
294         this.row_count.total = 0;
295         this.row_count.fleshed = 0;
296         setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
297     },
298
299     '_clear_tree' : function(params) {
300         var obj = this;
301         if (obj.error.sdump_levels.D_LIST_DUMP_ON_CLEAR) {
302             obj.error.sdump('D_LIST_DUMP_ON_CLEAR',obj.dump());
303         }
304         if (obj.error.sdump_levels.D_LIST_DUMP_WITH_KEYS_ON_CLEAR) {
305             obj.error.sdump('D_LIST_DUMP_WITH_KEYS_ON_CLEAR',obj.dump_with_keys());
306         }
307         while (obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
308     },
309
310     '_clear_listbox' : function(params) {
311         var obj = this;
312         var items = [];
313         var nl = this.node.getElementsByTagName('listitem');
314         for (var i = 0; i < nl.length; i++) {
315             items.push( nl[i] );
316         }
317         for (var i = 0; i < items.length; i++) {
318             this.node.removeChild(items[i]);
319         }
320     },
321
322     'append' : function (params) {
323         var rnode;
324         var obj = this;
325         switch (this.node.nodeName) {
326             case 'tree' : rparams = this._append_to_tree(params); break;
327             case 'listbox' : rparams = this._append_to_listbox(params); break;
328             default: throw('NYI: Need .append() for ' + this.node.nodeName); break;
329         }
330         if (rparams && params.attributes) {
331             for (var i in params.attributes) {
332                 rparams.my_node.setAttribute(i,params.attributes[i]);
333             }
334         }
335         this.row_count.total++;
336         if (this.row_count.fleshed == this.row_count.total) {
337             setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
338         }
339         rparams.my_node.setAttribute('unique_row_counter',obj.unique_row_counter);
340         rparams.unique_row_counter = obj.unique_row_counter++;
341         if (typeof params.on_append == 'function') {
342             params.on_append(rparams);
343         }
344         return rparams;
345     },
346     
347     'refresh_row' : function (params) {
348         var rnode;
349         var obj = this;
350         switch (this.node.nodeName) {
351             case 'tree' : rparams = this._refresh_row_in_tree(params); break;
352             default: throw('NYI: Need .refresh_row() for ' + this.node.nodeName); break;
353         }
354         if (rparams && params.attributes) {
355             for (var i in params.attributes) {
356                 rparams.my_node.setAttribute(i,params.attributes[i]);
357             }
358         }
359         this.row_count.fleshed--;
360         return rparams;
361     },
362
363
364     '_append_to_tree' : function (params) {
365
366         var obj = this;
367
368         if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
369
370         var s = ('util.list.append: params = ' + (params) + '\n');
371
372         var treechildren_node = this.treechildren;
373
374         if (params.node && params.node.nodeName == 'treeitem') {
375             params.node.setAttribute('container','true'); /* params.node.setAttribute('open','true'); */
376             if (params.node.lastChild.nodeName == 'treechildren') {
377                 treechildren_node = params.node.lastChild;
378             } else {
379                 treechildren_node = document.createElement('treechildren');
380                 params.node.appendChild(treechildren_node);
381             }
382         }
383
384         var treeitem = document.createElement('treeitem');
385         treeitem.setAttribute('retrieve_id',params.retrieve_id);
386         if (typeof params.to_bottom != 'undefined') {
387             treechildren_node.appendChild( treeitem );
388             if (typeof params.no_auto_select == 'undefined') {
389                 if (!obj.auto_select_pending) {
390                     obj.auto_select_pending = true;
391                     setTimeout(function() {
392                         dump('auto-selecting\n');
393                         var idx = Number(obj.node.view.rowCount)-1;
394                         try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
395                         try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
396                         obj.auto_select_pending = false;
397                         try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
398                     }, 1000);
399                 }
400             }
401         } else {
402             if (treechildren_node.firstChild) {
403                 treechildren_node.insertBefore( treeitem, treechildren_node.firstChild );
404             } else {
405                 treechildren_node.appendChild( treeitem );
406             }
407             if (typeof params.no_auto_select == 'undefined') {
408                 if (!obj.auto_select_pending) {
409                     obj.auto_select_pending = true;
410                     setTimeout(function() {
411                         try { obj.node.view.selection.select(0); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
412                         try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
413                         obj.auto_select_pending = false;
414                         try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(0).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
415                     }, 1000);
416                 }
417             }
418         }
419         var treerow = document.createElement('treerow');
420         treeitem.appendChild( treerow );
421         treerow.setAttribute('retrieve_id',params.retrieve_id);
422         if (params.row_properties) treerow.setAttribute('properties',params.row_properties);
423
424         s += ('tree = ' + this.node + '  treechildren = ' + treechildren_node + '\n');
425         s += ('treeitem = ' + treeitem + '  treerow = ' + treerow + '\n');
426
427         if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
428
429             obj.put_retrieving_label(treerow);
430             treerow.addEventListener(
431                 'flesh',
432                 function() {
433
434                     if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
435
436                     treerow.setAttribute('retrieved','true');
437
438                     //dump('fleshing = ' + params.retrieve_id + '\n');
439
440                     function inc_fleshed() {
441                         if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
442                         treerow.setAttribute('fleshed','true');
443                         obj.row_count.fleshed++;
444                         if (obj.row_count.fleshed >= obj.row_count.total) {
445                             setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
446                         }
447                     }
448
449                     params.row_node = treeitem;
450                     params.on_retrieve = function(p) {
451                         try {
452                             p.row = params.row;
453                             obj._map_row_to_treecell(p,treerow);
454                             inc_fleshed();
455                             var idx = obj.node.contentView.getIndexOfItem( params.row_node );
456                             dump('idx = ' + idx + '\n');
457                             // if current row is selected, send another select event to re-sync data that the client code fetches on selects
458                             if ( obj.node.view.selection.isSelected( idx ) ) {
459                                 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
460                                 util.widgets.dispatch('select',obj.node);
461                             }
462                         } catch(E) {
463                             // Let's not alert on this for now.  Getting contentView has no properties in record buckets under certain conditions
464                             dump('fixme2: ' + E + '\n');
465                         }
466                     }
467
468                     if (typeof params.retrieve_row == 'function') {
469
470                         params.retrieve_row( params );
471
472                     } else if (typeof obj.retrieve_row == 'function') {
473
474                             obj.retrieve_row( params );
475
476                     } else {
477                     
478                             inc_fleshed();
479                     }
480                 },
481                 false
482             );
483             /*
484             setTimeout(
485                 function() {
486                     util.widgets.dispatch('flesh',treerow);
487                 }, 0
488             );
489             */
490         } else {
491             obj.put_retrieving_label(treerow);
492             treerow.addEventListener(
493                 'flesh',
494                 function() {
495                     //dump('fleshing anon\n');
496                     if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
497                     obj._map_row_to_treecell(params,treerow);
498                     treerow.setAttribute('retrieved','true');
499                     treerow.setAttribute('fleshed','true');
500                     obj.row_count.fleshed++;
501                     if (obj.row_count.fleshed >= obj.row_count.total) {
502                         setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
503                     }
504                 },
505                 false
506             );
507             /*
508             setTimeout(
509                 function() {
510                     util.widgets.dispatch('flesh',treerow);
511                 }, 0
512             );
513             */
514         }
515         this.error.sdump('D_LIST',s);
516
517             try {
518
519                 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
520                     // Remove oldest row
521                     //if (typeof params.to_bottom != 'undefined') 
522                     if (typeof params.to_top == 'undefined') {
523                         if (typeof params.on_delete == 'function') { prams.on_delete( treechildren_node.firstChild.getAttribute('unique_row_counter') ); }
524                         treechildren_node.removeChild( treechildren_node.firstChild );
525                     } else {
526                         if (typeof params.on_delete == 'function') { prams.on_delete( treechildren_node.lastChild.getAttribute('unique_row_counter') ); }
527                         treechildren_node.removeChild( treechildren_node.lastChild );
528                     }
529                 }
530             } catch(E) {
531             }
532
533         setTimeout( function() { obj.auto_retrieve(); }, 0 );
534
535         params.my_node = treeitem;
536         return params;
537     },
538
539     '_refresh_row_in_tree' : function (params) {
540
541         var obj = this;
542
543         if (typeof params.row == 'undefined') throw('util.list.refresh_row: Object must contain a row');
544         if (typeof params.my_node == 'undefined') throw('util.list.refresh_row: Object must contain a my_node');
545         if (params.my_node.nodeName != 'treeitem') throw('util.list.refresh_rwo: my_node must be a treeitem');
546
547         var s = ('util.list.refresh_row: params = ' + (params) + '\n');
548
549         var treeitem = params.my_node;
550         treeitem.setAttribute('retrieve_id',params.retrieve_id);
551         if (typeof params.to_bottom != 'undefined') {
552             if (typeof params.no_auto_select == 'undefined') {
553                 if (!obj.auto_select_pending) {
554                     obj.auto_select_pending = true;
555                     setTimeout(function() {
556                         dump('auto-selecting\n');
557                         var idx = Number(obj.node.view.rowCount)-1;
558                         try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
559                         try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
560                         obj.auto_select_pending = false;
561                         try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
562                     }, 1000);
563                 }
564             }
565         }
566         var delete_me = [];
567         for (var i in treeitem.childNodes) if (treeitem.childNodes[i].nodeName == 'treerow') delete_me.push(treeitem.childNodes[i]);
568         for (var i = 0; i < delete_me.length; i++) treeitem.removeChild(delete_me[i]);
569         var treerow = document.createElement('treerow');
570         treeitem.appendChild( treerow );
571         treerow.setAttribute('retrieve_id',params.retrieve_id);
572         if (params.row_properties) treerow.setAttribute('properties',params.row_properties);
573
574         s += ('tree = ' + this.node.nodeName + '\n');
575         s += ('treeitem = ' + treeitem.nodeName + '  treerow = ' + treerow.nodeName + '\n');
576
577         if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
578
579             s += 'found a retrieve_row function\n';
580
581             obj.put_retrieving_label(treerow);
582             treerow.addEventListener(
583                 'flesh',
584                 function() {
585
586                     if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
587
588                     treerow.setAttribute('retrieved','true');
589
590                     //dump('fleshing = ' + params.retrieve_id + '\n');
591
592                     function inc_fleshed() {
593                         if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
594                         treerow.setAttribute('fleshed','true');
595                         obj.row_count.fleshed++;
596                         if (obj.row_count.fleshed >= obj.row_count.total) {
597                             setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
598                         }
599                     }
600
601                     params.row_node = treeitem;
602                     params.on_retrieve = function(p) {
603                         try {
604                             p.row = params.row;
605                             obj._map_row_to_treecell(p,treerow);
606                             inc_fleshed();
607                             var idx = obj.node.contentView.getIndexOfItem( params.row_node );
608                             dump('idx = ' + idx + '\n');
609                             // if current row is selected, send another select event to re-sync data that the client code fetches on selects
610                             if ( obj.node.view.selection.isSelected( idx ) ) {
611                                 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
612                                 util.widgets.dispatch('select',obj.node);
613                             }
614                         } catch(E) {
615                             // Let's not alert on this for now.  Getting contentView has no properties in record buckets under certain conditions
616                             dump('fixme2: ' + E + '\n');
617                         }
618                     }
619
620                     if (typeof params.retrieve_row == 'function') {
621
622                         params.retrieve_row( params );
623
624                     } else if (typeof obj.retrieve_row == 'function') {
625
626                             obj.retrieve_row( params );
627
628                     } else {
629                     
630                             inc_fleshed();
631                     }
632                 },
633                 false
634             );
635             /*
636             setTimeout(
637                 function() {
638                     util.widgets.dispatch('flesh',treerow);
639                 }, 0
640             );
641             */
642         } else {
643
644             s += 'did not find a retrieve_row function\n';
645
646             obj.put_retrieving_label(treerow);
647             treerow.addEventListener(
648                 'flesh',
649                 function() {
650                     //dump('fleshing anon\n');
651                     if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
652                     obj._map_row_to_treecell(params,treerow);
653                     treerow.setAttribute('retrieved','true');
654                     treerow.setAttribute('fleshed','true');
655                     obj.row_count.fleshed++;
656                     if (obj.row_count.fleshed >= obj.row_count.total) {
657                         setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
658                     }
659                 },
660                 false
661             );
662             /*
663             setTimeout(
664                 function() {
665                     util.widgets.dispatch('flesh',treerow);
666                 }, 0
667             );
668             */
669         }
670
671             try {
672
673                 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
674                     // Remove oldest row
675                     //if (typeof params.to_bottom != 'undefined') 
676                     if (typeof params.to_top == 'undefined') {
677                         treechildren_node.removeChild( treechildren_node.firstChild );
678                     } else {
679                         treechildren_node.removeChild( treechildren_node.lastChild );
680                     }
681                 }
682             } catch(E) {
683             }
684
685         setTimeout( function() { obj.auto_retrieve(); }, 0 );
686
687         JSAN.use('util.widgets'); util.widgets.dispatch('select',obj.node);
688
689         this.error.sdump('D_LIST',s);
690
691         return params;
692     },
693
694     'put_retrieving_label' : function(treerow) {
695         var obj = this;
696         try {
697             /*
698             var cols_idx = 0;
699             dump('put_retrieving_label.  columns = ' + js2JSON(obj.columns) + '\n');
700             while( obj.columns[cols_idx] && obj.columns[cols_idx].hidden && obj.columns[cols_idx].hidden == 'true') {
701                 dump('\t' + cols_idx);
702                 var treecell = document.createElement('treecell');
703                 treerow.appendChild(treecell);
704                 cols_idx++;
705             }
706             */
707             for (var i = 0; i < obj.columns.length; i++) {
708             var treecell = document.createElement('treecell'); treecell.setAttribute('label',document.getElementById('offlineStrings').getString('list.row_retrieving'));
709             treerow.appendChild(treecell);
710             }
711             /*
712             dump('\t' + cols_idx + '\n');
713             */
714         } catch(E) {
715             alert(E);
716         }
717     },
718
719     'detect_visible' : function() {
720         var obj = this;
721         try {
722             //dump('detect_visible  obj.node = ' + obj.node + '\n');
723             /* FIXME - this is a hack.. if the implementation of tree changes, this could break */
724             try {
725                 /*var s = ''; var A = document.getAnonymousNodes(obj.node);
726                 for (var i in A) {
727                     var B = A[i];
728                     s += '\t' + (typeof B.nodeName != 'undefined' ? B.nodeName : B ) + '\n'; 
729                     if (typeof B.childNodes != 'undefined') for (var j = 0; j < B.childNodes.length; j++) {
730                         var C = B.childNodes[j];
731                         s += '\t\t' + C.nodeName + '\n';
732                     }
733                 }
734                 obj.error.sdump('D_XULRUNNER','document.getAnonymousNodes(' + obj.node.nodeName + ') = \n' + s + '\n');*/
735                 var scrollbar = document.getAnonymousNodes(obj.node)[2].firstChild;
736                 var curpos = scrollbar.getAttribute('curpos');
737                 var maxpos = scrollbar.getAttribute('maxpos');
738                 //alert('curpos = ' + curpos + ' maxpos = ' + maxpos + ' obj.curpos = ' + obj.curpos + ' obj.maxpos = ' + obj.maxpos + '\n');
739                 if ((curpos != obj.curpos) || (maxpos != obj.maxpos)) {
740                     if ( obj.auto_retrieve() > 0 ) {
741                         obj.curpos = curpos; obj.maxpos = maxpos;
742                     }
743                 }
744             } catch(E) {
745                 obj.error.sdump('D_XULRUNNER', 'List implementation changed? ' + E);
746             }
747         } catch(E) { obj.error.sdump('D_ERROR',E); }
748     },
749
750     'detect_visible_polling' : function() {
751         try {
752             //alert('detect_visible_polling');
753             var obj = this;
754             obj.detect_visible();
755             setTimeout(function() { try { obj.detect_visible_polling(); } catch(E) { alert(E); } },2000);
756         } catch(E) {
757             alert(E);
758         }
759     },
760
761
762     'auto_retrieve' : function(params) {
763         var obj = this;
764         switch (this.node.nodeName) {
765             case 'tree' : obj._auto_retrieve_tree(params); break;
766             default: throw('NYI: Need .auto_retrieve() for ' + obj.node.nodeName); break;
767         }
768     },
769
770     '_auto_retrieve_tree' : function (params) {
771         var obj = this;
772         if (!obj.auto_retrieve_in_progress) {
773             obj.auto_retrieve_in_progress = true;
774             setTimeout(
775                 function() {
776                     try {
777                             //alert('auto_retrieve\n');
778                             var count = 0;
779                             var startpos = obj.node.treeBoxObject.getFirstVisibleRow();
780                             var endpos = obj.node.treeBoxObject.getLastVisibleRow();
781                             if (startpos > endpos) endpos = obj.node.treeBoxObject.getPageLength();
782                             //dump('startpos = ' + startpos + ' endpos = ' + endpos + '\n');
783                             for (var i = startpos; i < endpos + 4; i++) {
784                                 try {
785                                     //dump('trying index ' + i + '\n');
786                                     var item = obj.node.contentView.getItemAtIndex(i).firstChild;
787                                     if (item && item.getAttribute('retrieved') != 'true' ) {
788                                         //dump('\tgot an unfleshed item = ' + item + ' = ' + item.nodeName + '\n');
789                                         util.widgets.dispatch('flesh',item); count++;
790                                     }
791                                 } catch(E) {
792                                     //dump(i + ' : ' + E + '\n');
793                                 }
794                             }
795                             obj.auto_retrieve_in_progress = false;
796                             return count;
797                     } catch(E) { alert(E); }
798                 }, 1
799             );
800         }
801     },
802
803     'exec_on_all_fleshed' : function() {
804         var obj = this;
805         try {
806             if (obj.on_all_fleshed) {
807                 if (typeof obj.on_all_fleshed == 'function') {
808                     dump('exec_on_all_fleshed == function\n');
809                     setTimeout( 
810                         function() { 
811                             try { obj.on_all_fleshed(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',obj.on_all_fleshed); }
812                         }, 0 
813                     );
814                 } else if (typeof obj.on_all_fleshed.length != 'undefined') {
815                     dump('exec_on_all_fleshed == array\n');
816                     setTimeout(
817                         function() {
818                             try {
819                                 dump('exec_on_all_fleshed, processing on_all_fleshed array, length = ' + obj.on_all_fleshed.length + '\n');
820                                 var f = obj.on_all_fleshed.pop();
821                                 if (typeof f == 'function') { 
822                                     try { f(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',f); } 
823                                 }
824                                 if (obj.on_all_fleshed.length > 0) arguments.callee(); 
825                             } catch(E) {
826                                 obj.error.standard_unexpected_error_alert('exec_on_all_fleshed callback error',E);
827                             }
828                         }, 0
829                     ); 
830                 } else {
831                     obj.error.standard_unexpected_error_alert('unexpected on_all_fleshed object: ', obj.on_all_fleshed);
832                 }
833             }
834         } catch(E) {
835             obj.error.standard_unexpected_error_alert('exec_on_all-fleshed error',E);
836         }
837     },
838
839     'full_retrieve' : function(params) {
840         var obj = this;
841         switch (this.node.nodeName) {
842             case 'tree' : obj._full_retrieve_tree(params); break;
843             default: throw('NYI: Need .full_retrieve() for ' + obj.node.nodeName); break;
844         }
845     },
846
847     '_full_retrieve_tree' : function(params) {
848         var obj = this;
849         try {
850             if (obj.row_count.fleshed >= obj.row_count.total) {
851                 dump('Full retrieve... tree seems to be in sync\n' + js2JSON(obj.row_count) + '\n');
852                 obj.exec_on_all_fleshed();
853             } else {
854                 dump('Full retrieve... syncing tree' + js2JSON(obj.row_count) + '\n');
855                 JSAN.use('util.widgets');
856                 var nodes = obj.treechildren.childNodes;
857                 for (var i = 0; i < nodes.length; i++) {
858                     util.widgets.dispatch('flesh',nodes[i].firstChild);
859                 }
860             }
861         } catch(E) {
862             obj.error.standard_unexpected_error_alert('_full_retrieve_tree',E);
863         }
864     },
865
866     '_append_to_listbox' : function (params) {
867
868         var obj = this;
869
870         if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
871
872         var s = ('util.list.append: params = ' + (params) + '\n');
873
874         var listitem = document.createElement('listitem');
875
876         s += ('listbox = ' + this.node + '  listitem = ' + listitem + '\n');
877
878         if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
879
880             setTimeout(
881                 function() {
882                     listitem.setAttribute('retrieve_id',params.retrieve_id);
883                     //FIXME//Make async and fire when row is visible in list
884                     var row;
885
886                     params.row_node = listitem;
887                     params.on_retrieve = function(row) {
888                         params.row = row;
889                         obj._map_row_to_listcell(params,listitem);
890                         obj.node.appendChild( listitem );
891                         util.widgets.dispatch('select',obj.node);
892                     }
893
894                     if (typeof params.retrieve_row == 'function') {
895
896                         row = params.retrieve_row( params );
897
898                     } else {
899
900                         if (typeof obj.retrieve_row == 'function') {
901
902                             row = obj.retrieve_row( params );
903
904                         }
905                     }
906                 }, 0
907             );
908         } else {
909             this._map_row_to_listcell(params,listitem);
910             this.node.appendChild( listitem );
911         }
912
913         this.error.sdump('D_LIST',s);
914         params.my_node = listitem;
915         return params;
916
917     },
918
919     '_map_row_to_treecell' : function(params,treerow) {
920         var obj = this;
921         var s = '';
922         util.widgets.remove_children(treerow);
923
924         if (typeof params.map_row_to_column == 'function' || typeof this.map_row_to_column == 'function') {
925
926             for (var i = 0; i < this.columns.length; i++) {
927                 var treecell = document.createElement('treecell');
928                 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
929                 var label = '';
930                 if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
931                     treecell.setAttribute('label',label);
932                     treerow.appendChild( treecell );
933                     s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
934                     continue;
935                 }
936                 if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
937                     treecell.setAttribute('label',label);
938                     treerow.appendChild( treecell );
939                     s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
940                     continue;
941                 }
942     
943                 if (typeof params.map_row_to_column == 'function')  {
944     
945                     label = params.map_row_to_column(params.row,this.columns[i]);
946     
947                 } else if (typeof this.map_row_to_column == 'function') {
948     
949                     label = this.map_row_to_column(params.row,this.columns[i]);
950     
951                 }
952                 if (this.columns[i].type == 'checkbox') { treecell.setAttribute('value',label); } else { treecell.setAttribute('label',label ? label : ''); }
953                 treerow.appendChild( treecell );
954                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
955             }
956         } else if (typeof params.map_row_to_columns == 'function' || typeof this.map_row_to_columns == 'function') {
957
958             var labels = [];
959
960             if (typeof params.map_row_to_columns == 'function') {
961
962                 labels = params.map_row_to_columns(params.row,this.columns);
963
964             } else if (typeof this.map_row_to_columns == 'function') {
965
966                 labels = this.map_row_to_columns(params.row,this.columns);
967
968             }
969             for (var i = 0; i < labels.length; i++) {
970                 var treecell = document.createElement('treecell');
971                 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
972                 if ( this.columns[i].type == 'checkbox') {
973                     treecell.setAttribute('value', labels[i]);
974                 } else {
975                     treecell.setAttribute('label',typeof labels[i] == 'string' || typeof labels[i] == 'number' ? labels[i] : '');
976                 }
977                 treerow.appendChild( treecell );
978                 s += ('treecell = ' + treecell + ' with label = ' + labels[i] + '\n');
979             }
980
981         } else {
982
983             throw('No row to column mapping function.');
984         }
985         this.error.sdump('D_LIST',s);
986     },
987
988     '_map_row_to_listcell' : function(params,listitem) {
989         var obj = this;
990         var s = '';
991         for (var i = 0; i < this.columns.length; i++) {
992             var value = '';
993             if (typeof params.map_row_to_column == 'function')  {
994
995                 value = params.map_row_to_column(params.row,this.columns[i]);
996
997             } else {
998
999                 if (typeof this.map_row_to_column == 'function') {
1000
1001                     value = this.map_row_to_column(params.row,this.columns[i]);
1002                 }
1003             }
1004             if (typeof value == 'string' || typeof value == 'number') {
1005                 var listcell = document.createElement('listcell');
1006                 listcell.setAttribute('label',value);
1007                 listitem.appendChild(listcell);
1008                 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
1009             } else {
1010                 listitem.appendChild(value);
1011                 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
1012             }
1013         }
1014         this.error.sdump('D_LIST',s);
1015     },
1016
1017     'select_all' : function(params) {
1018         var obj = this;
1019         switch(this.node.nodeName) {
1020             case 'tree' : return this._select_all_from_tree(params); break;
1021             default: throw('NYI: Need ._select_all_from_() for ' + this.node.nodeName); break;
1022         }
1023     },
1024
1025     '_select_all_from_tree' : function(params) {
1026         var obj = this;
1027         this.node.view.selection.selectAll();
1028     },
1029
1030     'retrieve_selection' : function(params) {
1031         var obj = this;
1032         switch(this.node.nodeName) {
1033             case 'tree' : return this._retrieve_selection_from_tree(params); break;
1034             default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
1035         }
1036     },
1037
1038     '_retrieve_selection_from_tree' : function(params) {
1039         var obj = this;
1040         var list = [];
1041         var start = new Object();
1042         var end = new Object();
1043         var numRanges = this.node.view.selection.getRangeCount();
1044         for (var t=0; t<numRanges; t++){
1045             this.node.view.selection.getRangeAt(t,start,end);
1046             for (var v=start.value; v<=end.value; v++){
1047                 var i = this.node.contentView.getItemAtIndex(v);
1048                 list.push( i );
1049             }
1050         }
1051         return list;
1052     },
1053
1054     'dump' : function(params) {
1055         var obj = this;
1056         switch(this.node.nodeName) {
1057             case 'tree' : return this._dump_tree(params); break;
1058             default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
1059         }
1060     },
1061
1062     '_dump_tree' : function(params) {
1063         var obj = this;
1064         var dump = [];
1065         for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1066             var row = [];
1067             var treeitem = this.treechildren.childNodes[i];
1068             var treerow = treeitem.firstChild;
1069             for (var j = 0; j < treerow.childNodes.length; j++) {
1070                 row.push( treerow.childNodes[j].getAttribute('label') );
1071             }
1072             dump.push( row );
1073         }
1074         return dump;
1075     },
1076
1077     'dump_with_keys' : function(params) {
1078         var obj = this;
1079         switch(this.node.nodeName) {
1080             case 'tree' : return this._dump_tree_with_keys(params); break;
1081             default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
1082         }
1083
1084     },
1085
1086     '_dump_tree_with_keys' : function(params) {
1087         var obj = this;
1088         var dump = [];
1089         for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1090             var row = {};
1091             var treeitem = this.treechildren.childNodes[i];
1092             var treerow = treeitem.firstChild;
1093             for (var j = 0; j < treerow.childNodes.length; j++) {
1094                 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
1095             }
1096             dump.push( row );
1097         }
1098         return dump;
1099     },
1100
1101     'dump_csv' : function(params) {
1102         var obj = this;
1103         switch(this.node.nodeName) {
1104             case 'tree' : return this._dump_tree_csv(params); break;
1105             default: throw('NYI: Need .dump_csv() for ' + this.node.nodeName); break;
1106         }
1107
1108     },
1109
1110     '_dump_tree_csv' : function(params) {
1111         var obj = this;
1112         var dump = '';
1113         for (var j = 0; j < obj.columns.length; j++) {
1114             if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1115                 /* skip */
1116             } else {
1117                 if (dump) dump += ',';
1118                 dump += '"' + obj.columns[j].label.replace(/"/g, '""') + '"';
1119             }
1120         }
1121         dump += '\r\n';
1122         for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1123             var row = '';
1124             var treeitem = this.treechildren.childNodes[i];
1125             var treerow = treeitem.firstChild;
1126             for (var j = 0; j < treerow.childNodes.length; j++) {
1127                 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1128                     /* skip */
1129                 } else {
1130                     if (row) row += ',';
1131                     row += '"' + treerow.childNodes[j].getAttribute('label').replace(/"/g, '""') + '"';
1132                 }
1133             }
1134             dump +=  row + '\r\n';
1135         }
1136         return dump;
1137     },
1138
1139     'dump_csv_to_clipboard' : function(params) {
1140         var obj = this;
1141         if (typeof params == 'undefined') params = {};
1142         if (params.no_full_retrieve) {
1143             copy_to_clipboard( obj.dump_csv( params ) );
1144         } else {
1145             obj.wrap_in_full_retrieve( function() { copy_to_clipboard( obj.dump_csv( params ) ); } );
1146         }
1147     },
1148
1149     'dump_csv_to_printer' : function(params) {
1150         var obj = this;
1151         JSAN.use('util.print'); var print = new util.print();
1152         if (typeof params == 'undefined') params = {};
1153         if (params.no_full_retrieve) {
1154             print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1155         } else {
1156             obj.wrap_in_full_retrieve( 
1157                 function() { 
1158                     print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1159                 }
1160             );
1161         }
1162     },
1163
1164     'dump_csv_to_file' : function(params) {
1165         var obj = this;
1166         JSAN.use('util.file'); var f = new util.file();
1167         if (typeof params == 'undefined') params = {};
1168         if (params.no_full_retrieve) {
1169             params.data = obj.dump_csv( params );
1170             params.not_json = true;
1171             if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1172             f.export_file( params );
1173         } else {
1174             obj.wrap_in_full_retrieve( 
1175                 function() { 
1176                     params.data = obj.dump_csv( params );
1177                     params.not_json = true;
1178                     if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1179                     f.export_file( params );
1180                 }
1181             );
1182         }
1183     },
1184
1185     'print' : function(params) {
1186         if (!params) params = {};
1187         switch(this.node.nodeName) {
1188             case 'tree' : return this._print_tree(params); break;
1189             default: throw('NYI: Need ._print() for ' + this.node.nodeName); break;
1190         }
1191     },
1192
1193     '_print_tree' : function(params) {
1194         var obj = this;
1195         try {
1196             JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
1197             if (!params.staff && data.list.au && data.list.au[0]) {
1198                 params.staff = data.list.au[0];
1199             }
1200             if (!params.lib && data.list.au && data.list.au[0] && data.list.au[0].ws_ou() && data.hash.aou && data.hash.aou[ data.list.au[0].ws_ou() ]) {
1201                 params.lib = data.hash.aou[ data.list.au[0].ws_ou() ];
1202                 params.lib.children(null);
1203             }
1204             if (params.template && data.print_list_templates[ params.template ]) {
1205                 var template = data.print_list_templates[ params.template ];
1206                 for (var i in template) params[i] = template[i];
1207             }
1208             obj.wrap_in_full_retrieve(
1209                 function() {
1210                     try {
1211                         if (!params.list) params.list = obj.dump_with_keys();
1212                         JSAN.use('util.print'); var print = new util.print();
1213                         print.tree_list( params );
1214                         if (typeof params.callback == 'function') params.callback();
1215                     } catch(E) {
1216                         obj.error.standard_unexpected_error_alert('inner _print_tree',E);
1217                     }
1218                 }
1219             );
1220             
1221         } catch(E) {
1222             obj.error.standard_unexpected_error_alert('_print_tree',E);
1223         }
1224     },
1225
1226     'dump_selected_with_keys' : function(params) {
1227         var obj = this;
1228         switch(this.node.nodeName) {
1229             case 'tree' : return this._dump_tree_selection_with_keys(params); break;
1230             default: throw('NYI: Need .dump_selection_with_keys() for ' + this.node.nodeName); break;
1231         }
1232
1233     },
1234
1235     '_dump_tree_selection_with_keys' : function(params) {
1236         var obj = this;
1237         var dump = [];
1238         var list = obj._retrieve_selection_from_tree();
1239         for (var i = 0; i < list.length; i++) {
1240             var row = {};
1241             var treeitem = list[i];
1242             var treerow = treeitem.firstChild;
1243             for (var j = 0; j < treerow.childNodes.length; j++) {
1244                 var value = treerow.childNodes[j].getAttribute('label');
1245                 if (params.skip_hidden_columns) if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') continue;
1246                 var id = obj.columns[j].id; if (params.labels_instead_of_ids) id = obj.columns[j].label;
1247                 row[ id ] = value;
1248             }
1249             dump.push( row );
1250         }
1251         return dump;
1252     },
1253
1254     'clipboard' : function(params) {
1255         try {
1256             var obj = this;
1257             var dump = obj.dump_selected_with_keys({'skip_hidden_columns':true,'labels_instead_of_ids':true});
1258             JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
1259             data.list_clipboard = dump; data.stash('list_clipboard');
1260             JSAN.use('util.window'); var win = new util.window();
1261             win.open(urls.XUL_LIST_CLIPBOARD,'list_clipboard','chrome,resizable,modal');
1262             window.focus(); // sometimes the main window will lower after a clipboard action
1263         } catch(E) {
1264             this.error.standard_unexpected_error_alert('clipboard',E);
1265         }
1266     },
1267
1268     'dump_retrieve_ids' : function(params) {
1269         var obj = this;
1270         switch(this.node.nodeName) {
1271             case 'tree' : return this._dump_retrieve_ids_tree(params); break;
1272             default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
1273         }
1274     },
1275
1276     '_dump_retrieve_ids_tree' : function(params) {
1277         var obj = this;
1278         var dump = [];
1279         for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1280             var treeitem = this.treechildren.childNodes[i];
1281             dump.push( treeitem.getAttribute('retrieve_id') );
1282         }
1283         return dump;
1284     },
1285
1286     'wrap_in_full_retrieve' : function(f) {
1287         var obj = this;
1288         if (typeof obj.on_all_fleshed == 'function') { // legacy
1289             obj.on_all_fleshed = [ obj.on_all_fleshed ];
1290         }
1291         if (! obj.on_all_fleshed) obj.on_all_fleshed = [];
1292         obj.on_all_fleshed.push(f);
1293         obj.full_retrieve();
1294     },
1295
1296     '_sort_tree' : function(col,sortDir) {
1297         var obj = this;
1298         try {
1299             if (obj.node.getAttribute('no_sort')) {
1300                 return;
1301             }
1302             var col_pos;
1303             for (var i = 0; i < obj.columns.length; i++) { 
1304                 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i); 
1305             }
1306             obj.wrap_in_full_retrieve(
1307                 function() {
1308                     try {
1309                         JSAN.use('util.money');
1310                         var rows = [];
1311                         var treeitems = obj.treechildren.childNodes;
1312                         for (var i = 0; i < treeitems.length; i++) {
1313                             var treeitem = treeitems[i];
1314                             var treerow = treeitem.firstChild;
1315                             var treecell = treerow.childNodes[ col_pos ];
1316                             value = ( { 'value' : treecell ? treecell.getAttribute('label') : '', 'node' : treeitem } );
1317                             rows.push( value );
1318                         }
1319                         rows = rows.sort( function(a,b) { 
1320                             a = a.value; b = b.value; 
1321                             if (col.getAttribute('sort_type')) {
1322                                 switch(col.getAttribute('sort_type')) {
1323                                     case 'number' :
1324                                         a = Number(a); b = Number(b);
1325                                     break;
1326                                     case 'money' :
1327                                         a = util.money.dollars_float_to_cents_integer(a);
1328                                         b = util.money.dollars_float_to_cents_integer(b);
1329                                     break;
1330                                     case 'title' : /* special case for "a" and "the".  doesn't use marc 245 indicator */
1331                                         a = String( a ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1332                                         b = String( b ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1333                                     break;
1334                                     default:
1335                                         a = String( a ).toUpperCase();
1336                                         b = String( b ).toUpperCase();
1337                                     break;
1338                                 }
1339                             } else {
1340                                 if (typeof a == 'string' || typeof b == 'string') {
1341                                     a = String( a ).toUpperCase();
1342                                     b = String( b ).toUpperCase();
1343                                 }
1344                             }
1345                             if (a < b) return -1; 
1346                             if (a > b) return 1; 
1347                             return 0; 
1348                         } );
1349                         if (sortDir == 'asc') rows = rows.reverse();
1350                         while(obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
1351                         for (var i = 0; i < rows.length; i++) {
1352                             obj.treechildren.appendChild( rows[i].node );
1353                         }
1354                         if (typeof obj.on_sort == 'function') obj.on_sort();
1355                     } catch(E) {
1356                         obj.error.standard_unexpected_error_alert('sorting',E); 
1357                     }
1358                 }
1359             );
1360         } catch(E) {
1361             obj.error.standard_unexpected_error_alert('pre sorting', E);
1362         }
1363     },
1364
1365     '_toggle_checkbox_column' : function(col,toggle) {
1366         var obj = this;
1367         try {
1368             if (obj.node.getAttribute('no_toggle')) {
1369                 return;
1370             }
1371             var col_pos;
1372             for (var i = 0; i < obj.columns.length; i++) { 
1373                 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i); 
1374             }
1375             var treeitems = obj.treechildren.childNodes;
1376             for (var i = 0; i < treeitems.length; i++) {
1377                 var treeitem = treeitems[i];
1378                 var treerow = treeitem.firstChild;
1379                 var treecell = treerow.childNodes[ col_pos ];
1380                 treecell.setAttribute('value',(toggle == 'on'));
1381             }
1382             if (typeof obj.on_checkbox_toggle == 'function') obj.on_checkbox_toggle(toggle);
1383         } catch(E) {
1384             obj.error.standard_unexpected_error_alert('pre toggle', E);
1385         }
1386     },
1387
1388     'render_list_actions' : function(params) {
1389         var obj = this;
1390         switch(this.node.nodeName) {
1391             case 'tree' : return this._render_list_actions_for_tree(params); break;
1392             default: throw('NYI: Need ._render_list_actions() for ' + this.node.nodeName); break;
1393         }
1394     },
1395
1396     '_render_list_actions_for_tree' : function(params) {
1397         var obj = this;
1398         try {
1399             var btn = document.createElement('button');
1400             btn.setAttribute('id',obj.node.id + '_list_actions');
1401             btn.setAttribute('type','menu');
1402             btn.setAttribute('allowevents','true');
1403             //btn.setAttribute('oncommand','this.firstChild.showPopup();');
1404             btn.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.menu.label'));
1405             btn.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.menu.accesskey'));
1406             var mp = document.createElement('menupopup');
1407             btn.appendChild(mp);
1408             var mi = document.createElement('menuitem');
1409             mi.setAttribute('id',obj.node.id + '_clipfield');
1410             mi.setAttribute('disabled','true');
1411             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.label'));
1412             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.accesskey'));
1413             mp.appendChild(mi);
1414             mi = document.createElement('menuitem');
1415             mi.setAttribute('id',obj.node.id + '_csv_to_clipboard');
1416             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.label'));
1417             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.accesskey'));
1418             mp.appendChild(mi);
1419             mi = document.createElement('menuitem');
1420             mi.setAttribute('id',obj.node.id + '_csv_to_printer');
1421             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.label'));
1422             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.accesskey'));
1423             mp.appendChild(mi);
1424             mi = document.createElement('menuitem');
1425             mi.setAttribute('id',obj.node.id + '_csv_to_file');
1426             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.label'));
1427             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.accesskey'));
1428             mp.appendChild(mi);
1429             mi = document.createElement('menuitem');
1430             mi.setAttribute('id',obj.node.id + '_save_columns');
1431             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.label'));
1432             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.accesskey'));
1433             mp.appendChild(mi);
1434             return btn;
1435         } catch(E) {
1436             obj.error.standard_unexpected_error_alert('rendering list actions',E);
1437         }
1438     },
1439
1440     'set_list_actions' : function(params) {
1441         var obj = this;
1442         switch(this.node.nodeName) {
1443             case 'tree' : return this._set_list_actions_for_tree(params); break;
1444             default: throw('NYI: Need ._set_list_actions() for ' + this.node.nodeName); break;
1445         }
1446     },
1447
1448     '_set_list_actions_for_tree' : function(params) {
1449         // This should be called after the button element from render_list_actions has been appended to the DOM
1450         var obj = this;
1451         try {
1452             var x = document.getElementById(obj.node.id + '_clipfield');
1453             if (x) {
1454                 x.addEventListener(
1455                     'command',
1456                     function() {
1457                         obj.clipboard(params);
1458                         if (params && typeof params.on_complete == 'function') {
1459                             params.on_complete(params);
1460                         }
1461                     },
1462                     false
1463                 );
1464             }
1465             x = document.getElementById(obj.node.id + '_csv_to_clipboard');
1466             if (x) {
1467                 x.addEventListener(
1468                     'command',
1469                     function() {
1470                         obj.dump_csv_to_clipboard(params);
1471                         if (params && typeof params.on_complete == 'function') {
1472                             params.on_complete(params);
1473                         }
1474                     },
1475                     false
1476                 );
1477             }
1478             x = document.getElementById(obj.node.id + '_csv_to_printer');
1479             if (x) {
1480                 x.addEventListener(
1481                     'command',
1482                     function() {
1483                         obj.dump_csv_to_printer(params);
1484                         if (params && typeof params.on_complete == 'function') {
1485                             params.on_complete(params);
1486                         }
1487                     },
1488                     false
1489                 );
1490             }
1491             x = document.getElementById(obj.node.id + '_csv_to_file');
1492             if (x) {
1493                 x.addEventListener(
1494                     'command',
1495                     function() {
1496                         obj.dump_csv_to_file(params);
1497                         if (params && typeof params.on_complete == 'function') {
1498                             params.on_complete(params);
1499                         }
1500                     },
1501                     false
1502                 );
1503             }
1504             x = document.getElementById(obj.node.id + '_save_columns');
1505             if (x) {
1506                 x.addEventListener(
1507                     'command',
1508                     function() {
1509                         obj.save_columns(params);
1510                         if (params && typeof params.on_complete == 'function') {
1511                             params.on_complete(params);
1512                         }
1513                     },
1514                     false
1515                 );
1516             }
1517
1518         } catch(E) {
1519             obj.error.standard_unexpected_error_alert('setting list actions',E);
1520         }
1521     }
1522
1523 }
1524 dump('exiting util.list.js\n');