]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/list.js
xul list secondary sorting
[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     this.sub_sorts = [];
13
14     if (!this.node) throw('Could not find element ' + id);
15     switch(this.node.nodeName) {
16         case 'listbox' : 
17         case 'tree' : break;
18         case 'richlistbox' :
19             throw(this.node.nodeName + ' not yet supported'); break;
20         default: throw(this.node.nodeName + ' not supported'); break;
21     }
22
23     JSAN.use('util.error'); this.error = new util.error();
24
25     JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.stash_retrieve();
26
27     JSAN.use('util.functional');
28
29     return this;
30 };
31
32 util.list.prototype = {
33
34     'init' : function (params) {
35
36         var obj = this;
37         obj.scratch_data = {};
38
39         // If set, save and restore columns as if the tree/list id was the value of columns_saved_under
40         obj.columns_saved_under = params.columns_saved_under;
41
42         JSAN.use('util.widgets');
43
44         obj.printer_context = params.printer_context;
45
46         if (typeof params.map_row_to_column == 'function') obj.map_row_to_column = params.map_row_to_column;
47         if (typeof params.map_row_to_columns == 'function') {
48             obj.map_row_to_columns = params.map_row_to_columns;
49         } else {
50             obj.map_row_to_columns = obj.std_map_row_to_columns();
51         }
52         if (typeof params.retrieve_row == 'function') obj.retrieve_row = params.retrieve_row;
53
54         obj.prebuilt = false;
55         if (typeof params.prebuilt != 'undefined') obj.prebuilt = params.prebuilt;
56
57         if (typeof params.columns == 'undefined') throw('util.list.init: No columns');
58         obj.columns = [
59             {
60                 'id' : 'lineno',
61                 'label' : document.getElementById('offlineStrings').getString('list.line_number'),
62                 'flex' : '0',
63                 'no_sort' : 'true',
64                 'properties' : 'ordinal', // column properties for css styling
65                 'hidden' : 'false',
66                 'editable' : false,
67                 'render' : function(my,scratch) {
68                     // special code will handle this based on the attribute we set
69                     // here.  All cells for this column need to be updated whenever
70                     // a list adds, removes, or sorts rows
71                     return '_';
72                 }
73             }
74         ];
75         for (var i = 0; i < params.columns.length; i++) {
76             if (typeof params.columns[i] == 'object') {
77                 obj.columns.push( params.columns[i] );
78             } else {
79                 var cols = obj.fm_columns( params.columns[i] );
80                 for (var j = 0; j < cols.length; j++) {
81                     obj.columns.push( cols[j] );
82                 }
83             }
84         }
85
86         switch(obj.node.nodeName) {
87             case 'tree' : obj._init_tree(params); break;
88             case 'listbox' : obj._init_listbox(params); break;
89             default: throw('NYI: Need ._init() for ' + obj.node.nodeName); break;
90         }
91     },
92
93     '_init_tree' : function (params) {
94         var obj = this;
95         if (this.prebuilt) {
96         
97             this.treechildren = this.node.lastChild;    
98         
99         } else {
100             var treecols = document.createElement('treecols');
101             this.node.appendChild(treecols);
102             this.treecols = treecols;
103
104             var check_for_id_collisions = {};
105             for (var i = 0; i < this.columns.length; i++) {
106                 var treecol = document.createElement('treecol');
107                 for (var j in this.columns[i]) {
108                     var value = this.columns[i][j];
109                     if (j=='id') {
110                         if (typeof check_for_id_collisions[value] == 'undefined') {
111                             check_for_id_collisions[value] = true;
112                         } else {
113                             // Column id's are important for sorting and saving list configuration.  Collisions started happening because
114                             // we were using field names as id's, and then later combining column definitions for multiple objects that
115                             // shared field names.  The downside to this sort of automatic collision prevention is that these generated
116                             // id's can change as we add and remove columns, possibly breaking saved list configurations.
117                             dump('Column collision with id = ' + value + ', renaming to ');
118                             value = value + '_collision_' + i;
119                             dump(value + '\n');
120                         }
121                     }
122                     treecol.setAttribute(j,value);
123                 }
124                 treecols.appendChild(treecol);
125                 if (this.columns[i].type == 'checkbox') {
126                     treecol.addEventListener(
127                         'click',
128                         function(ev) {
129                             setTimeout(
130                                 function() {
131                                     var toggle = ev.target.getAttribute('toggleAll') || 'on';
132                                     if (toggle == 'off') toggle = 'on'; else toggle = 'off';
133                                     ev.target.setAttribute('toggleAll',toggle);
134                                     obj._toggle_checkbox_column(ev.target,toggle);
135                                 }, 0
136                             );
137                         },
138                         false
139                     );
140                 } else {
141                     treecol.addEventListener(
142                         'click', 
143                         function(ev) {
144                             if (ev.target.getAttribute('no_sort')) {
145                                 return;
146                             }
147
148                             function do_it() {
149                                 if (ev.ctrlKey) { // sub sort
150                                     var sortDir = 'asc';
151                                     if (ev.shiftKey) {
152                                         sortDir = 'desc';
153                                     }
154                                     obj.sub_sorts.push({
155                                         'target' : ev.target,
156                                         'sortDir' : sortDir
157                                     });
158                                 } else { // first sort
159                                     var sortDir = ev.target.getAttribute('sortDir') || 'desc';
160                                     if (sortDir == 'desc') sortDir = 'asc'; else sortDir = 'desc';
161                                     if (ev.shiftKey) {
162                                         sortDir = 'desc';
163                                     }
164                                     ev.target.setAttribute('sortDir',sortDir);
165                                     obj.first_sort = {
166                                         'target' : ev.target,
167                                         'sortDir' : sortDir
168                                     };
169                                     obj.sub_sorts = [];
170                                 }
171                                 if (obj.first_sort) {
172                                     obj._sort_tree();
173                                 }
174                             }
175
176                             if (obj.row_count.total != obj.row_count.fleshed && (obj.row_count.total - obj.row_count.fleshed) > 50) {
177                                 var r = window.confirm(document.getElementById('offlineStrings').getFormattedString('list.row_fetch_warning',[obj.row_count.fleshed,obj.row_count.total]));
178
179                                 if (r) {
180                                     setTimeout( do_it, 0 );
181                                 }
182                             } else {
183                                     setTimeout( do_it, 0 );
184                             }
185                         },
186                         false
187                     );
188                 }
189                 var splitter = document.createElement('splitter');
190                 splitter.setAttribute('class','tree-splitter');
191                 treecols.appendChild(splitter);
192             }
193
194             var treechildren = document.createElement('treechildren');
195             this.node.appendChild(treechildren);
196             this.treechildren = treechildren;
197         }
198         if (typeof params.on_sort == 'function') {
199             this.on_sort = params.on_sort;
200         }
201         if (typeof params.on_checkbox_toggle == 'function') {
202             this.on_checkbox_toggle = params.on_checkbox_toggle;
203         }
204         this.node.addEventListener(
205             'select',
206             function(ev) {
207                 if (typeof params.on_select == 'function') {
208                     params.on_select(ev);
209                 }
210                 var x = document.getElementById(obj.node.id + '_clipfield');
211                 if (x) {
212                     var sel = obj.retrieve_selection();
213                     x.setAttribute('disabled', sel.length == 0);
214                 }
215             },
216             false
217         );
218         if (typeof params.on_click == 'function') {
219             this.node.addEventListener(
220                 'click',
221                 params.on_click,
222                 false
223             );
224         }
225         if (typeof params.on_dblclick == 'function') {
226             this.node.addEventListener(
227                 'dblclick',
228                 params.on_dblclick,
229                 false
230             );
231         }
232
233         /*
234         this.node.addEventListener(
235             'mousemove',
236             function(ev) { obj.detect_visible(); },
237             false
238         );
239         */
240         this.node.addEventListener(
241             'keypress',
242             function(ev) { obj.auto_retrieve(); },
243             false
244         );
245         this.node.addEventListener(
246             'click',
247             function(ev) { obj.auto_retrieve(); },
248             false
249         );
250         window.addEventListener(
251             'resize',
252             function(ev) { obj.auto_retrieve(); },
253             false
254         );
255         /* FIXME -- find events on scrollbar to trigger this */
256         obj.detect_visible_polling();    
257         /*
258         var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(this.node)[1] )[1];
259         var slider = document.getAnonymousNodes( scrollbar )[2];
260         alert('scrollbar = ' + scrollbar.nodeName + ' grippy = ' + slider.nodeName);
261         scrollbar.addEventListener('click',function(){alert('sb click');},false);
262         scrollbar.addEventListener('command',function(){alert('sb command');},false);
263         scrollbar.addEventListener('scroll',function(){alert('sb scroll');},false);
264         slider.addEventListener('click',function(){alert('slider click');},false);
265         slider.addEventListener('command',function(){alert('slider command');},false);
266         slider.addEventListener('scroll',function(){alert('slider scroll');},false);
267         */
268         this.node.addEventListener('scroll',function(){ obj.auto_retrieve(); },false);
269
270         this.restores_columns(params);
271     },
272
273     '_init_listbox' : function (params) {
274         if (this.prebuilt) {
275         } else {
276             var listhead = document.createElement('listhead');
277             this.node.appendChild(listhead);
278
279             var listcols = document.createElement('listcols');
280             this.node.appendChild(listcols);
281
282             for (var i = 0; i < this.columns.length; i++) {
283                 var listheader = document.createElement('listheader');
284                 listhead.appendChild(listheader);
285                 var listcol = document.createElement('listcol');
286                 listcols.appendChild(listcol);
287                 for (var j in this.columns[i]) {
288                     listheader.setAttribute(j,this.columns[i][j]);
289                     listcol.setAttribute(j,this.columns[i][j]);
290                 };
291             }
292         }
293     },
294
295     'save_columns' : function (params) {
296         var obj = this;
297         if (obj.data.hash.aous['gui.disable_local_save_columns']) {
298             alert(document.getElementById('offlineStrings').getString('list.column_save_disabled'));
299         } else {
300             switch (this.node.nodeName) {
301                 case 'tree' : this._save_columns_tree(params); break;
302                 default: throw('NYI: Need .save_columns() for ' + this.node.nodeName); break;
303             }
304         }
305     },
306
307     '_save_columns_tree' : function (params) {
308         var obj = this;
309         try {
310             var id = obj.node.getAttribute('id');
311             if (obj.columns_saved_under) { id = obj.columns_saved_under; }
312             if (!id) {
313                 alert("FIXME: The columns for this list cannot be saved because the list has no id.");
314                 return;
315             }
316             var my_cols = {};
317             var nl = obj.node.getElementsByTagName('treecol');
318             for (var i = 0; i < nl.length; i++) {
319                 var col = nl[i];
320                 var col_id = col.getAttribute('id');
321                 if (!col_id) {
322                     alert('FIXME: A column in this list does not have an id and cannot be saved');
323                     continue;
324                 }
325                 var col_hidden = col.getAttribute('hidden'); 
326                 var col_width = col.getAttribute('width'); 
327                 var col_ordinal = col.getAttribute('ordinal'); 
328                 my_cols[ col_id ] = { 'hidden' : col_hidden, 'width' : col_width, 'ordinal' : col_ordinal };
329             }
330             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
331             JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
332             file.set_object(my_cols);
333             file.close();
334             alert(document.getElementById('offlineStrings').getString('list.columns_saved'));
335         } catch(E) {
336             obj.error.standard_unexpected_error_alert('_save_columns_tree',E);
337         }
338     },
339
340     'restores_columns' : function (params) {
341         var obj = this;
342         switch (this.node.nodeName) {
343             case 'tree' : this._restores_columns_tree(params); break;
344             default: throw('NYI: Need .restores_columns() for ' + this.node.nodeName); break;
345         }
346     },
347
348     '_restores_columns_tree' : function (params) {
349         var obj = this;
350         try {
351             var id = obj.node.getAttribute('id');
352             if (obj.columns_saved_under) { id = obj.columns_saved_under; }
353             if (!id) {
354                 alert("FIXME: The columns for this list cannot be restored because the list has no id.");
355                 return;
356             }
357
358             var my_cols;
359             if (! obj.data.hash.aous) { obj.data.hash.aous = {}; }
360             if (! obj.data.hash.aous['gui.disable_local_save_columns']) {
361                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
362                 JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
363                 if (file._file.exists()) {
364                     my_cols = file.get_object(); file.close();
365                 }
366             }
367             /* local file will trump remote file if allowed, so save ourselves an http request if this is the case */
368             if (obj.data.hash.aous['url.remote_column_settings'] && ! my_cols ) {
369                 try {
370                     var x = new XMLHttpRequest();
371                     var url = obj.data.hash.aous['url.remote_column_settings'] + '/tree_columns_for_' + window.escape(id);
372                     x.open("GET", url, false);
373                     x.send(null);
374                     if (x.status == 200) {
375                         my_cols = JSON2js( x.responseText );
376                     }
377                 } catch(E) {
378                     // This can happen in the offline interface if you logged in previously and url.remote_column_settings is set.
379                     // 1) You may be really "offline" now
380                     // 2) the URL may just be a path component without a hostname (ie "/xul/column_settings/"), which won't work
381                     // when appended to chrome://open_ils_staff_client/
382                     dump('Error retrieving column settings from ' + url + ': ' + E + '\n');
383                 }
384             }
385
386             if (my_cols) {
387                 var nl = obj.node.getElementsByTagName('treecol');
388                 for (var i = 0; i < nl.length; i++) {
389                     var col = nl[i];
390                     var col_id = col.getAttribute('id');
391                     if (!col_id) {
392                         alert('FIXME: A column in this list does not have an id and cannot be saved');
393                         continue;
394                     }
395                     if (typeof my_cols[col_id] != 'undefined') {
396                         col.setAttribute('hidden',my_cols[col_id].hidden); 
397                         col.setAttribute('width',my_cols[col_id].width); 
398                         col.setAttribute('ordinal',my_cols[col_id].ordinal); 
399                     } else {
400                         obj.error.sdump('D_ERROR','WARNING: Column ' + col_id + ' did not have a saved state.');
401                     }
402                 }
403             }
404         } catch(E) {
405             obj.error.standard_unexpected_error_alert('_restore_columns_tree',E);
406         }
407     },
408
409     'clear' : function (params) {
410         var obj = this;
411         switch (this.node.nodeName) {
412             case 'tree' : this._clear_tree(params); break;
413             case 'listbox' : this._clear_listbox(params); break;
414             default: throw('NYI: Need .clear() for ' + this.node.nodeName); break;
415         }
416         this.error.sdump('D_LIST','Clearing list ' + this.node.getAttribute('id') + '\n');
417         this.row_count.total = 0;
418         this.row_count.fleshed = 0;
419         setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
420     },
421
422     '_clear_tree' : function(params) {
423         var obj = this;
424         if (obj.error.sdump_levels.D_LIST_DUMP_ON_CLEAR) {
425             obj.error.sdump('D_LIST_DUMP_ON_CLEAR',obj.dump());
426         }
427         if (obj.error.sdump_levels.D_LIST_DUMP_WITH_KEYS_ON_CLEAR) {
428             obj.error.sdump('D_LIST_DUMP_WITH_KEYS_ON_CLEAR',obj.dump_with_keys());
429         }
430         while (obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
431     },
432
433     '_clear_listbox' : function(params) {
434         var obj = this;
435         var items = [];
436         var nl = this.node.getElementsByTagName('listitem');
437         for (var i = 0; i < nl.length; i++) {
438             items.push( nl[i] );
439         }
440         for (var i = 0; i < items.length; i++) {
441             this.node.removeChild(items[i]);
442         }
443     },
444
445     'append' : function (params) {
446         var rnode;
447         var obj = this;
448         switch (this.node.nodeName) {
449             case 'tree' : rparams = this._append_to_tree(params); break;
450             case 'listbox' : rparams = this._append_to_listbox(params); break;
451             default: throw('NYI: Need .append() for ' + this.node.nodeName); break;
452         }
453         if (rparams && params.attributes) {
454             for (var i in params.attributes) {
455                 rparams.treeitem_node.setAttribute(i,params.attributes[i]);
456             }
457         }
458         this.row_count.total++;
459         if (this.row_count.fleshed == this.row_count.total) {
460             setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
461         }
462         rparams.treeitem_node.setAttribute('unique_row_counter',obj.unique_row_counter);
463         rparams.unique_row_counter = obj.unique_row_counter++;
464         if (typeof params.on_append == 'function') {
465             params.on_append(rparams);
466         }
467         return rparams;
468     },
469     
470     'refresh_row' : function (params) {
471         var rnode;
472         var obj = this;
473         switch (this.node.nodeName) {
474             case 'tree' : rparams = this._refresh_row_in_tree(params); break;
475             default: throw('NYI: Need .refresh_row() for ' + this.node.nodeName); break;
476         }
477         if (rparams && params.attributes) {
478             for (var i in params.attributes) {
479                 rparams.treeitem_node.setAttribute(i,params.attributes[i]);
480             }
481         }
482         this.row_count.fleshed--;
483         return rparams;
484     },
485
486
487     '_append_to_tree' : function (params) {
488
489         var obj = this;
490
491         if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
492
493         var s = ('util.list.append: params = ' + (params) + '\n');
494
495         var treechildren_node = this.treechildren;
496
497         if (params.node && params.node.nodeName == 'treeitem') {
498             params.node.setAttribute('container','true'); /* params.node.setAttribute('open','true'); */
499             if (params.node.lastChild.nodeName == 'treechildren') {
500                 treechildren_node = params.node.lastChild;
501             } else {
502                 treechildren_node = document.createElement('treechildren');
503                 params.node.appendChild(treechildren_node);
504             }
505         }
506
507         var treeitem = document.createElement('treeitem');
508         treeitem.setAttribute('retrieve_id',params.retrieve_id);
509         if (typeof params.to_bottom != 'undefined') {
510             treechildren_node.appendChild( treeitem );
511             if (typeof params.no_auto_select == 'undefined') {
512                 if (!obj.auto_select_pending) {
513                     obj.auto_select_pending = true;
514                     setTimeout(function() {
515                         dump('auto-selecting\n');
516                         var idx = Number(obj.node.view.rowCount)-1;
517                         try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
518                         try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
519                         obj.auto_select_pending = false;
520                         try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
521                     }, 1000);
522                 }
523             }
524         } else {
525             if (treechildren_node.firstChild) {
526                 treechildren_node.insertBefore( treeitem, treechildren_node.firstChild );
527             } else {
528                 treechildren_node.appendChild( treeitem );
529             }
530             if (typeof params.no_auto_select == 'undefined') {
531                 if (!obj.auto_select_pending) {
532                     obj.auto_select_pending = true;
533                     setTimeout(function() {
534                         try { obj.node.view.selection.select(0); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
535                         try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
536                         obj.auto_select_pending = false;
537                         try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(0).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
538                     }, 1000);
539                 }
540             }
541         }
542         var treerow = document.createElement('treerow');
543         treeitem.appendChild( treerow );
544         treerow.setAttribute('retrieve_id',params.retrieve_id);
545         if (params.row_properties) treerow.setAttribute('properties',params.row_properties);
546
547         s += ('tree = ' + this.node + '  treechildren = ' + treechildren_node + '\n');
548         s += ('treeitem = ' + treeitem + '  treerow = ' + treerow + '\n');
549
550         obj.put_retrieving_label(treerow);
551
552         if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
553             treerow.addEventListener(
554                 'flesh',
555                 function() {
556
557                     if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
558
559                     treerow.setAttribute('retrieved','true');
560
561                     //dump('fleshing = ' + params.retrieve_id + '\n');
562
563                     function inc_fleshed() {
564                         if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
565                         treerow.setAttribute('fleshed','true');
566                         obj.row_count.fleshed++;
567                         if (obj.row_count.fleshed >= obj.row_count.total) {
568                             setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
569                         }
570                     }
571
572                     params.treeitem_node = treeitem;
573                     params.on_retrieve = function(p) {
574                         try {
575                             p.row = params.row;
576                             obj._map_row_to_treecell(p,treerow);
577                             inc_fleshed();
578                             var idx = obj.node.contentView.getIndexOfItem( params.treeitem_node );
579                             dump('idx = ' + idx + '\n');
580                             // if current row is selected, send another select event to re-sync data that the client code fetches on selects
581                             if ( obj.node.view.selection.isSelected( idx ) ) {
582                                 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
583                                 util.widgets.dispatch('select',obj.node);
584                             }
585                         } catch(E) {
586                             // Let's not alert on this for now.  Getting contentView has no properties in record buckets under certain conditions
587                             dump('fixme2: ' + E + '\n');
588                         }
589                     }
590
591                     if (typeof params.retrieve_row == 'function') {
592
593                         params.retrieve_row( params );
594
595                     } else if (typeof obj.retrieve_row == 'function') {
596
597                             obj.retrieve_row( params );
598
599                     } else {
600                     
601                             inc_fleshed();
602                     }
603                     obj.refresh_ordinals();
604                 },
605                 false
606             );
607             if (typeof params.flesh_immediately != 'undefined') {
608                 if (params.flesh_immediately) {
609                     setTimeout(
610                         function() {
611                             util.widgets.dispatch('flesh',treerow);
612                         }, 0
613                     );
614                 }
615             }
616         } else {
617             treerow.addEventListener(
618                 'flesh',
619                 function() {
620                     //dump('fleshing anon\n');
621                     if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
622                     obj._map_row_to_treecell(params,treerow);
623                     treerow.setAttribute('retrieved','true');
624                     treerow.setAttribute('fleshed','true');
625                     obj.row_count.fleshed++;
626                     if (obj.row_count.fleshed >= obj.row_count.total) {
627                         setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
628                     }
629                     obj.refresh_ordinals();
630                 },
631                 false
632             );
633             if (typeof params.flesh_immediately != 'undefined') {
634                 if (params.flesh_immediately) {
635                     setTimeout(
636                         function() {
637                             util.widgets.dispatch('flesh',treerow);
638                         }, 0
639                     );
640                 }
641             }
642         }
643         this.error.sdump('D_LIST',s);
644
645             try {
646
647                 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
648                     // Remove oldest row
649                     //if (typeof params.to_bottom != 'undefined') 
650                     if (typeof params.to_top == 'undefined') {
651                         if (typeof params.on_delete == 'function') { params.on_delete( treechildren_node.firstChild.getAttribute('unique_row_counter') ); }
652                         treechildren_node.removeChild( treechildren_node.firstChild );
653                     } else {
654                         if (typeof params.on_delete == 'function') { params.on_delete( treechildren_node.lastChild.getAttribute('unique_row_counter') ); }
655                         treechildren_node.removeChild( treechildren_node.lastChild );
656                     }
657                 }
658             } catch(E) {
659             }
660
661         setTimeout( function() { obj.auto_retrieve(); obj.refresh_ordinals(); }, 0 );
662
663         params.treeitem_node = treeitem;
664         return params;
665     },
666
667     '_refresh_row_in_tree' : function (params) {
668
669         var obj = this;
670
671         if (typeof params.row == 'undefined') throw('util.list.refresh_row: Object must contain a row');
672         if (typeof params.treeitem_node == 'undefined') throw('util.list.refresh_row: Object must contain a treeitem_node');
673         if (params.treeitem_node.nodeName != 'treeitem') throw('util.list.refresh_rwo: treeitem_node must be a treeitem');
674
675         var s = ('util.list.refresh_row: params = ' + (params) + '\n');
676
677         var treeitem = params.treeitem_node;
678         treeitem.setAttribute('retrieve_id',params.retrieve_id);
679         if (typeof params.to_bottom != 'undefined') {
680             if (typeof params.no_auto_select == 'undefined') {
681                 if (!obj.auto_select_pending) {
682                     obj.auto_select_pending = true;
683                     setTimeout(function() {
684                         dump('auto-selecting\n');
685                         var idx = Number(obj.node.view.rowCount)-1;
686                         try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
687                         try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
688                         obj.auto_select_pending = false;
689                         try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
690                     }, 1000);
691                 }
692             }
693         }
694         //var delete_me = [];
695         //for (var i in treeitem.childNodes) if (treeitem.childNodes[i].nodeName == 'treerow') delete_me.push(treeitem.childNodes[i]);
696         //for (var i = 0; i < delete_me.length; i++) treeitem.removeChild(delete_me[i]);
697         var prev_treerow = treeitem.firstChild; /* FIXME: worry about hierarchal lists like copy_browser? */
698         var treerow = document.createElement('treerow');
699         while (prev_treerow.firstChild) {
700             treerow.appendChild( prev_treerow.removeChild( prev_treerow.firstChild ) );
701         }
702         treeitem.replaceChild( treerow, prev_treerow );
703         treerow.setAttribute('retrieve_id',params.retrieve_id);
704         if (params.row_properties) treerow.setAttribute('properties',params.row_properties);
705
706         s += ('tree = ' + this.node.nodeName + '\n');
707         s += ('treeitem = ' + treeitem.nodeName + '  treerow = ' + treerow.nodeName + '\n');
708
709         obj.put_retrieving_label(treerow);
710
711         if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
712
713             s += 'found a retrieve_row function\n';
714
715             treerow.addEventListener(
716                 'flesh',
717                 function() {
718
719                     if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
720
721                     treerow.setAttribute('retrieved','true');
722
723                     //dump('fleshing = ' + params.retrieve_id + '\n');
724
725                     function inc_fleshed() {
726                         if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
727                         treerow.setAttribute('fleshed','true');
728                         obj.row_count.fleshed++;
729                         if (obj.row_count.fleshed >= obj.row_count.total) {
730                             setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
731                         }
732                     }
733
734                     params.treeitem_node = treeitem;
735                     params.on_retrieve = function(p) {
736                         try {
737                             p.row = params.row;
738                             obj._map_row_to_treecell(p,treerow);
739                             inc_fleshed();
740                             var idx = obj.node.contentView.getIndexOfItem( params.treeitem_node );
741                             dump('idx = ' + idx + '\n');
742                             // if current row is selected, send another select event to re-sync data that the client code fetches on selects
743                             if ( obj.node.view.selection.isSelected( idx ) ) {
744                                 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
745                                 util.widgets.dispatch('select',obj.node);
746                             }
747                         } catch(E) {
748                             // Let's not alert on this for now.  Getting contentView has no properties in record buckets under certain conditions
749                             dump('fixme2: ' + E + '\n');
750                         }
751                     }
752
753                     if (typeof params.retrieve_row == 'function') {
754
755                         params.retrieve_row( params );
756
757                     } else if (typeof obj.retrieve_row == 'function') {
758
759                             obj.retrieve_row( params );
760
761                     } else {
762                     
763                             inc_fleshed();
764                     }
765                     obj.refresh_ordinals();
766                 },
767                 false
768             );
769             if (typeof params.flesh_immediately != 'undefined') {
770                 if (params.flesh_immediately) {
771                     setTimeout(
772                         function() {
773                             util.widgets.dispatch('flesh',treerow);
774                         }, 0
775                     );
776                 }
777             }
778
779         } else {
780
781             s += 'did not find a retrieve_row function\n';
782
783             treerow.addEventListener(
784                 'flesh',
785                 function() {
786                     //dump('fleshing anon\n');
787                     if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
788                     obj._map_row_to_treecell(params,treerow);
789                     treerow.setAttribute('retrieved','true');
790                     treerow.setAttribute('fleshed','true');
791                     obj.row_count.fleshed++;
792                     if (obj.row_count.fleshed >= obj.row_count.total) {
793                         setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
794                     }
795                     obj.refresh_ordinals();
796                 },
797                 false
798             );
799             if (typeof params.flesh_immediately != 'undefined') {
800                 if (params.flesh_immediately) {
801                     setTimeout(
802                         function() {
803                             util.widgets.dispatch('flesh',treerow);
804                         }, 0
805                     );
806                 }
807             }
808
809         }
810
811             try {
812
813                 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
814                     // Remove oldest row
815                     //if (typeof params.to_bottom != 'undefined') 
816                     if (typeof params.to_top == 'undefined') {
817                         treechildren_node.removeChild( treechildren_node.firstChild );
818                     } else {
819                         treechildren_node.removeChild( treechildren_node.lastChild );
820                     }
821                 }
822             } catch(E) {
823             }
824
825         setTimeout( function() { obj.auto_retrieve(); obj.refresh_ordinals(); }, 0 );
826
827         JSAN.use('util.widgets'); util.widgets.dispatch('select',obj.node);
828
829         this.error.sdump('D_LIST',s);
830
831         return params;
832     },
833
834     'refresh_ordinals' : function() {
835         var obj = this;
836         try {
837             if (obj.refresh_ordinals_timeout_id) { return; }
838
839             function _refresh_ordinals(clear) {
840                 var nl = obj.node.getElementsByAttribute('label','_');
841                 for (var i = 0; i < nl.length; i++) {
842                     nl[i].setAttribute(
843                         'ord_col',
844                         'true'
845                     );
846                     nl[i].setAttribute( // treecell properties for css styling
847                         'properties',
848                         'ordinal'
849                     );
850                 }
851                 nl = obj.node.getElementsByAttribute('ord_col','true');
852                 for (var i = 0; i < nl.length; i++) {
853                     nl[i].setAttribute(
854                         'label',
855                         // we could just use 'i' here if we trust the order of elements
856                         1 + obj.node.contentView.getIndexOfItem(nl[i].parentNode.parentNode) // treeitem
857                     );
858                 }
859                 if (clear) { obj.refresh_ordinals_timeout_id = null; }
860             }
861
862             // spamming this to cover race conditions
863             setTimeout(_refresh_ordinals, 500); // for speedy looking UI updates
864             setTimeout(_refresh_ordinals, 2000); // for most uses
865             obj.refresh_ordinals_timeout_id = setTimeout(
866                 function() {
867                     _refresh_ordinals(true);
868                 },
869                 4000 // just in case, say with a slow rendering list
870             );
871
872         } catch(E) {
873             alert('Error in list.js, refresh_ordinals(): ' + E);
874         }
875     },
876
877     'put_retrieving_label' : function(treerow) {
878         var obj = this;
879         try {
880             for (var i = 0; i < obj.columns.length; i++) {
881                 var treecell;
882                 if (typeof treerow.childNodes[i] == 'undefined') {
883                     treecell = document.createElement('treecell');
884                     treerow.appendChild(treecell);
885                 } else {
886                     treecell = treerow.childNodes[i];
887                 }
888                 treecell.setAttribute('label',document.getElementById('offlineStrings').getString('list.row_retrieving'));
889             }
890         } catch(E) {
891             alert('Error in list.js, put_retrieving_label(): ' + E);
892         }
893     },
894
895     'detect_visible' : function() {
896         var obj = this;
897         try {
898             //dump('detect_visible  obj.node = ' + obj.node + '\n');
899             /* FIXME - this is a hack.. if the implementation of tree changes, this could break */
900             try {
901                 /*var s = ''; var A = document.getAnonymousNodes(obj.node);
902                 for (var i in A) {
903                     var B = A[i];
904                     s += '\t' + (typeof B.nodeName != 'undefined' ? B.nodeName : B ) + '\n'; 
905                     if (typeof B.childNodes != 'undefined') for (var j = 0; j < B.childNodes.length; j++) {
906                         var C = B.childNodes[j];
907                         s += '\t\t' + C.nodeName + '\n';
908                     }
909                 }
910                 obj.error.sdump('D_XULRUNNER','document.getAnonymousNodes(' + obj.node.nodeName + ') = \n' + s + '\n');*/
911                 var scrollbar = document.getAnonymousNodes(obj.node)[2].firstChild;
912                 var curpos = scrollbar.getAttribute('curpos');
913                 var maxpos = scrollbar.getAttribute('maxpos');
914                 //alert('curpos = ' + curpos + ' maxpos = ' + maxpos + ' obj.curpos = ' + obj.curpos + ' obj.maxpos = ' + obj.maxpos + '\n');
915                 if ((curpos != obj.curpos) || (maxpos != obj.maxpos)) {
916                     if ( obj.auto_retrieve() > 0 ) {
917                         obj.curpos = curpos; obj.maxpos = maxpos;
918                     }
919                 }
920             } catch(E) {
921                 obj.error.sdump('D_XULRUNNER', 'List implementation changed? ' + E);
922             }
923         } catch(E) { obj.error.sdump('D_ERROR',E); }
924     },
925
926     'detect_visible_polling' : function() {
927         try {
928             //alert('detect_visible_polling');
929             var obj = this;
930             obj.detect_visible();
931             setTimeout(function() { try { obj.detect_visible_polling(); } catch(E) { alert(E); } },2000);
932         } catch(E) {
933             alert(E);
934         }
935     },
936
937
938     'auto_retrieve' : function(params) {
939         var obj = this;
940         switch (this.node.nodeName) {
941             case 'tree' : obj._auto_retrieve_tree(params); break;
942             default: throw('NYI: Need .auto_retrieve() for ' + obj.node.nodeName); break;
943         }
944     },
945
946     '_auto_retrieve_tree' : function (params) {
947         var obj = this;
948         if (!obj.auto_retrieve_in_progress) {
949             obj.auto_retrieve_in_progress = true;
950             setTimeout(
951                 function() {
952                     try {
953                             //alert('auto_retrieve\n');
954                             var count = 0;
955                             var startpos = obj.node.treeBoxObject.getFirstVisibleRow();
956                             var endpos = obj.node.treeBoxObject.getLastVisibleRow();
957                             if (startpos > endpos) endpos = obj.node.treeBoxObject.getPageLength();
958                             //dump('startpos = ' + startpos + ' endpos = ' + endpos + '\n');
959                             for (var i = startpos; i < endpos + 4; i++) {
960                                 try {
961                                     //dump('trying index ' + i + '\n');
962                                     var item = obj.node.contentView.getItemAtIndex(i).firstChild;
963                                     if (item && item.getAttribute('retrieved') != 'true' ) {
964                                         //dump('\tgot an unfleshed item = ' + item + ' = ' + item.nodeName + '\n');
965                                         util.widgets.dispatch('flesh',item); count++;
966                                     }
967                                 } catch(E) {
968                                     //dump(i + ' : ' + E + '\n');
969                                 }
970                             }
971                             obj.auto_retrieve_in_progress = false;
972                             return count;
973                     } catch(E) { alert(E); }
974                 }, 1
975             );
976         }
977     },
978
979     'exec_on_all_fleshed' : function() {
980         var obj = this;
981         try {
982             if (obj.on_all_fleshed) {
983                 if (typeof obj.on_all_fleshed == 'function') {
984                     dump('exec_on_all_fleshed == function\n');
985                     setTimeout( 
986                         function() { 
987                             try { obj.on_all_fleshed(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',obj.on_all_fleshed); }
988                         }, 0 
989                     );
990                 } else if (typeof obj.on_all_fleshed.length != 'undefined') {
991                     dump('exec_on_all_fleshed == array\n');
992                     setTimeout(
993                         function() {
994                             try {
995                                 dump('exec_on_all_fleshed, processing on_all_fleshed array, length = ' + obj.on_all_fleshed.length + '\n');
996                                 var f = obj.on_all_fleshed.pop();
997                                 if (typeof f == 'function') { 
998                                     try { f(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',f); } 
999                                 }
1000                                 if (obj.on_all_fleshed.length > 0) arguments.callee(); 
1001                             } catch(E) {
1002                                 obj.error.standard_unexpected_error_alert('exec_on_all_fleshed callback error',E);
1003                             }
1004                         }, 0
1005                     ); 
1006                 } else {
1007                     obj.error.standard_unexpected_error_alert('unexpected on_all_fleshed object: ', obj.on_all_fleshed);
1008                 }
1009             }
1010         } catch(E) {
1011             obj.error.standard_unexpected_error_alert('exec_on_all-fleshed error',E);
1012         }
1013     },
1014
1015     'full_retrieve' : function(params) {
1016         var obj = this;
1017         switch (this.node.nodeName) {
1018             case 'tree' : obj._full_retrieve_tree(params); break;
1019             default: throw('NYI: Need .full_retrieve() for ' + obj.node.nodeName); break;
1020         }
1021         obj.refresh_ordinals();
1022     },
1023
1024     '_full_retrieve_tree' : function(params) {
1025         var obj = this;
1026         try {
1027             if (obj.row_count.fleshed >= obj.row_count.total) {
1028                 dump('Full retrieve... tree seems to be in sync\n' + js2JSON(obj.row_count) + '\n');
1029                 obj.exec_on_all_fleshed();
1030             } else {
1031                 dump('Full retrieve... syncing tree' + js2JSON(obj.row_count) + '\n');
1032                 JSAN.use('util.widgets');
1033                 var nodes = obj.treechildren.childNodes;
1034                 for (var i = 0; i < nodes.length; i++) {
1035                     util.widgets.dispatch('flesh',nodes[i].firstChild);
1036                 }
1037             }
1038         } catch(E) {
1039             obj.error.standard_unexpected_error_alert('_full_retrieve_tree',E);
1040         }
1041     },
1042
1043     '_append_to_listbox' : function (params) {
1044
1045         var obj = this;
1046
1047         if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
1048
1049         var s = ('util.list.append: params = ' + (params) + '\n');
1050
1051         var listitem = document.createElement('listitem');
1052
1053         s += ('listbox = ' + this.node + '  listitem = ' + listitem + '\n');
1054
1055         if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
1056
1057             setTimeout(
1058                 function() {
1059                     listitem.setAttribute('retrieve_id',params.retrieve_id);
1060                     //FIXME//Make async and fire when row is visible in list
1061                     var row;
1062
1063                     params.treeitem_node = listitem;
1064                     params.on_retrieve = function(row) {
1065                         params.row = row;
1066                         obj._map_row_to_listcell(params,listitem);
1067                         obj.node.appendChild( listitem );
1068                         util.widgets.dispatch('select',obj.node);
1069                     }
1070
1071                     if (typeof params.retrieve_row == 'function') {
1072
1073                         row = params.retrieve_row( params );
1074
1075                     } else {
1076
1077                         if (typeof obj.retrieve_row == 'function') {
1078
1079                             row = obj.retrieve_row( params );
1080
1081                         }
1082                     }
1083                 }, 0
1084             );
1085         } else {
1086             this._map_row_to_listcell(params,listitem);
1087             this.node.appendChild( listitem );
1088         }
1089
1090         this.error.sdump('D_LIST',s);
1091         params.treeitem_node = listitem;
1092         return params;
1093
1094     },
1095
1096     '_map_row_to_treecell' : function(params,treerow) {
1097         var obj = this;
1098         var s = '';
1099
1100         if (typeof params.map_row_to_column == 'function' || typeof this.map_row_to_column == 'function') {
1101
1102             for (var i = 0; i < this.columns.length; i++) {
1103                 var treecell;
1104                 if (typeof treerow.childNodes[i] == 'undefined') {
1105                     treecell = document.createElement('treecell');
1106                     treerow.appendChild( treecell );
1107                 } else {
1108                     treecell = treerow.childNodes[i];
1109                 }
1110                 
1111                 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
1112                 var label = '';
1113                 var sort_value = '';
1114
1115                 // What skip columns is doing is rendering the treecells as blank/empty
1116                 if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
1117                     treecell.setAttribute('label',label);
1118                     s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
1119                     continue;
1120                 }
1121                 if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
1122                     treecell.setAttribute('label',label);
1123                     s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
1124                     continue;
1125                 }
1126     
1127                 if (typeof params.map_row_to_column == 'function')  {
1128     
1129                     label = params.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1130     
1131                 } else if (typeof this.map_row_to_column == 'function') {
1132     
1133                     label = this.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1134     
1135                 }
1136                 if (this.columns[i].type == 'checkbox') { treecell.setAttribute('value',label); } else { treecell.setAttribute('label',label ? label : ''); }
1137                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
1138             }
1139         } else if (typeof params.map_row_to_columns == 'function' || typeof this.map_row_to_columns == 'function') {
1140
1141             var labels = [];
1142             var sort_values = [];
1143
1144             if (typeof params.map_row_to_columns == 'function') {
1145
1146                 var values = params.map_row_to_columns(params.row,this.columns,this.scratch_data);
1147                 if (typeof values.values == 'undefined') {
1148                     labels = values;
1149                 } else {
1150                     labels = values.values;
1151                     sort_values = values.sort_values;
1152                 }
1153
1154             } else if (typeof this.map_row_to_columns == 'function') {
1155
1156                 var values = this.map_row_to_columns(params.row,this.columns,this.scratch_data);
1157                 if (typeof values.values == 'undefined') {
1158                     labels = values;
1159                 } else {
1160                     labels = values.values;
1161                     sort_values = values.sort_values;
1162                 }
1163             }
1164             for (var i = 0; i < labels.length; i++) {
1165                 var treecell;
1166                 if (typeof treerow.childNodes[i] == 'undefined') {
1167                     treecell = document.createElement('treecell');
1168                     treerow.appendChild(treecell);
1169                 } else {
1170                     treecell = treerow.childNodes[i];
1171                 }
1172                 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
1173                 if ( this.columns[i].type == 'checkbox') {
1174                     treecell.setAttribute('value', labels[i]);
1175                 } else {
1176                     treecell.setAttribute('label',typeof labels[i] == 'string' || typeof labels[i] == 'number' ? labels[i] : '');
1177                 }
1178                 if (sort_values[i]) {
1179                     treecell.setAttribute('sort_value',js2JSON(sort_values[i]));
1180                 }
1181                 s += ('treecell = ' + treecell + ' with label = ' + labels[i] + '\n');
1182             }
1183
1184         } else {
1185
1186             throw('No row to column mapping function.');
1187         }
1188         this.error.sdump('D_LIST',s);
1189     },
1190
1191     '_map_row_to_listcell' : function(params,listitem) {
1192         var obj = this;
1193         var s = '';
1194         for (var i = 0; i < this.columns.length; i++) {
1195             var value = '';
1196             if (typeof params.map_row_to_column == 'function')  {
1197
1198                 value = params.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1199
1200             } else {
1201
1202                 if (typeof this.map_row_to_column == 'function') {
1203
1204                     value = this.map_row_to_column(params.row,this.columns[i],this.scratch_data);
1205                 }
1206             }
1207             if (typeof value == 'string' || typeof value == 'number') {
1208                 var listcell = document.createElement('listcell');
1209                 listcell.setAttribute('label',value);
1210                 listitem.appendChild(listcell);
1211                 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
1212             } else {
1213                 listitem.appendChild(value);
1214                 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
1215             }
1216         }
1217         this.error.sdump('D_LIST',s);
1218     },
1219
1220     'select_all' : function(params) {
1221         var obj = this;
1222         switch(this.node.nodeName) {
1223             case 'tree' : return this._select_all_from_tree(params); break;
1224             default: throw('NYI: Need ._select_all_from_() for ' + this.node.nodeName); break;
1225         }
1226     },
1227
1228     '_select_all_from_tree' : function(params) {
1229         var obj = this;
1230         this.node.view.selection.selectAll();
1231     },
1232
1233     'retrieve_selection' : function(params) {
1234         var obj = this;
1235         switch(this.node.nodeName) {
1236             case 'tree' : return this._retrieve_selection_from_tree(params); break;
1237             default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
1238         }
1239     },
1240
1241     '_retrieve_selection_from_tree' : function(params) {
1242         var obj = this;
1243         var list = [];
1244         var start = new Object();
1245         var end = new Object();
1246         var numRanges = this.node.view.selection.getRangeCount();
1247         for (var t=0; t<numRanges; t++){
1248             this.node.view.selection.getRangeAt(t,start,end);
1249             for (var v=start.value; v<=end.value; v++){
1250                 var i = this.node.contentView.getItemAtIndex(v);
1251                 list.push( i );
1252             }
1253         }
1254         return list;
1255     },
1256
1257     'dump' : function(params) {
1258         var obj = this;
1259         switch(this.node.nodeName) {
1260             case 'tree' : return this._dump_tree(params); break;
1261             default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
1262         }
1263     },
1264
1265     '_dump_tree' : function(params) {
1266         var obj = this;
1267         var dump = [];
1268         for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1269             var row = [];
1270             var treeitem = this.treechildren.childNodes[i];
1271             var treerow = treeitem.firstChild;
1272             for (var j = 0; j < treerow.childNodes.length; j++) {
1273                 row.push( treerow.childNodes[j].getAttribute('label') );
1274             }
1275             dump.push( row );
1276         }
1277         return dump;
1278     },
1279
1280     'dump_with_keys' : function(params) {
1281         var obj = this;
1282         switch(this.node.nodeName) {
1283             case 'tree' : return this._dump_tree_with_keys(params); break;
1284             default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
1285         }
1286
1287     },
1288
1289     '_dump_tree_with_keys' : function(params) {
1290         var obj = this;
1291         var dump = [];
1292
1293         function process_tree(treechildren) {
1294             for (var i = 0; i < treechildren.childNodes.length; i++) {
1295                 var row = {};
1296                 var treeitem = treechildren.childNodes[i];
1297                 var treerow = treeitem.firstChild;
1298                 for (var j = 0; j < treerow.childNodes.length; j++) {
1299                     if (typeof obj.columns[j] == 'undefined') {
1300                         dump('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n');
1301                         dump('_dump_tree_with_keys @ ' + location.href + '\n');
1302                         dump('\ttreerow.childNodes.length='+treerow.childNodes.length+' j='+j+' obj.columns.length='+obj.columns.length+'\n');
1303                         debugger;
1304                     } else {
1305                         row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
1306                         var sort = treerow.childNodes[j].getAttribute('sort_value');
1307                         if(sort) {
1308                             row[ obj.columns[j].id + '_sort_value' ] = sort;
1309                         }
1310                     }
1311                 }
1312                 dump.push( row );
1313                 if (treeitem.childNodes.length > 1) {
1314                     process_tree(treeitem.lastChild);
1315                 }
1316             }
1317         }
1318
1319         process_tree(this.treechildren);
1320
1321         return dump;
1322     },
1323
1324     'dump_csv' : function(params) {
1325         var obj = this;
1326         switch(this.node.nodeName) {
1327             case 'tree' : return this._dump_tree_csv(params); break;
1328             default: throw('NYI: Need .dump_csv() for ' + this.node.nodeName); break;
1329         }
1330
1331     },
1332
1333     '_dump_tree_csv' : function(params) {
1334         var obj = this;
1335         var _dump = '';
1336         var ord_cols = [];
1337         for (var j = 0; j < obj.columns.length; j++) {
1338             if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1339                 /* skip */
1340             } else {
1341                 ord_cols.push( [ obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('ordinal'), j ] );
1342             }
1343         }
1344         ord_cols.sort( function(a,b) { 
1345             if ( Number( a[0] ) < Number( b[0] ) ) return -1; 
1346             if ( Number( a[0] ) > Number( b[0] ) ) return 1; 
1347             return 0;
1348         } );
1349         for (var j = 0; j < ord_cols.length; j++) {
1350             if (_dump) _dump += ',';
1351             _dump += '"' + obj.columns[ ord_cols[j][1] ].label.replace(/"/g, '""') + '"';
1352         }
1353         _dump += '\r\n';
1354
1355         function process_tree(treechildren) {
1356             for (var i = 0; i < treechildren.childNodes.length; i++) {
1357                 var row = '';
1358                 var treeitem = treechildren.childNodes[i];
1359                 var treerow = treeitem.firstChild;
1360                 for (var j = 0; j < ord_cols.length; j++) {
1361                     if (row) row += ',';
1362                     row += '"' + treerow.childNodes[ ord_cols[j][1] ].getAttribute('label').replace(/"/g, '""') + '"';
1363                 }
1364                 _dump +=  row + '\r\n';
1365                 if (treeitem.childNodes.length > 1) {
1366                     process_tree(treeitem.lastChild);
1367                 }
1368             }
1369         }
1370
1371         process_tree(this.treechildren);
1372
1373         return _dump;
1374     },
1375
1376     'dump_extended_format' : function(params) {
1377         var obj = this;
1378         switch(this.node.nodeName) {
1379             case 'tree' : return this._dump_tree_extended_format(params); break;
1380             default: throw('NYI: Need .dump_extended_format() for ' + this.node.nodeName); break;
1381         }
1382
1383     },
1384
1385     '_dump_tree_extended_format' : function(params) {
1386         var obj = this;
1387         var _dump = '';
1388         var ord_cols = [];
1389         for (var j = 0; j < obj.columns.length; j++) {
1390             if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1391                 /* skip */
1392             } else {
1393                 ord_cols.push( [ obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('ordinal'), j ] );
1394             }
1395         }
1396         ord_cols.sort( function(a,b) { 
1397             if ( Number( a[0] ) < Number( b[0] ) ) return -1; 
1398             if ( Number( a[0] ) > Number( b[0] ) ) return 1; 
1399             return 0;
1400         } );
1401
1402         function process_tree(treechildren) {
1403             for (var i = 0; i < treechildren.childNodes.length; i++) {
1404                 var row = document.getElementById('offlineStrings').getString('list.dump_extended_format.record_separator') + '\r\n';
1405                 var treeitem = treechildren.childNodes[i];
1406                 var treerow = treeitem.firstChild;
1407                 for (var j = 0; j < ord_cols.length; j++) {
1408                     row += obj.columns[ ord_cols[j][1] ].label + ': ' + treerow.childNodes[ ord_cols[j][1] ].getAttribute('label') + '\r\n';
1409                 }
1410                 _dump +=  row + '\r\n';
1411                 if (treeitem.childNodes.length > 1) {
1412                     process_tree(treeitem.lastChild);
1413                 }
1414             }
1415         }
1416
1417         process_tree(this.treechildren);
1418
1419         return _dump;
1420     },
1421
1422     'dump_csv_to_clipboard' : function(params) {
1423         var obj = this;
1424         if (typeof params == 'undefined') params = {};
1425         if (params.no_full_retrieve) {
1426             copy_to_clipboard( obj.dump_csv( params ) );
1427         } else {
1428             obj.wrap_in_full_retrieve( function() { copy_to_clipboard( obj.dump_csv( params ) ); } );
1429         }
1430     },
1431
1432     'dump_csv_to_printer' : function(params) {
1433         var obj = this;
1434         if (typeof params == 'undefined') params = {};
1435         JSAN.use('util.print'); var print = new util.print(params.printer_context || obj.printer_context);
1436         if (params.no_full_retrieve) {
1437             print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1438         } else {
1439             obj.wrap_in_full_retrieve( 
1440                 function() { 
1441                     print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1442                 }
1443             );
1444         }
1445     },
1446
1447     'dump_extended_format_to_printer' : function(params) {
1448         var obj = this;
1449         if (typeof params == 'undefined') params = {};
1450         JSAN.use('util.print'); var print = new util.print(params.printer_context || obj.printer_context);
1451         if (params.no_full_retrieve) {
1452             print.simple( obj.dump_extended_format( params ), {'content_type':'text/plain'} );
1453         } else {
1454             obj.wrap_in_full_retrieve( 
1455                 function() { 
1456                     print.simple( obj.dump_extended_format( params ), {'content_type':'text/plain'} );
1457                 }
1458             );
1459         }
1460     },
1461
1462     'dump_csv_to_file' : function(params) {
1463         var obj = this;
1464         JSAN.use('util.file'); var f = new util.file();
1465         if (typeof params == 'undefined') params = {};
1466         if (params.no_full_retrieve) {
1467             params.data = obj.dump_csv( params );
1468             params.not_json = true;
1469             if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1470             f.export_file( params );
1471         } else {
1472             obj.wrap_in_full_retrieve( 
1473                 function() { 
1474                     params.data = obj.dump_csv( params );
1475                     params.not_json = true;
1476                     if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1477                     f.export_file( params );
1478                 }
1479             );
1480         }
1481     },
1482
1483     'print' : function(params) {
1484         if (!params) params = {};
1485         switch(this.node.nodeName) {
1486             case 'tree' : return this._print_tree(params); break;
1487             default: throw('NYI: Need ._print() for ' + this.node.nodeName); break;
1488         }
1489     },
1490
1491     '_print_tree' : function(params) {
1492         var obj = this;
1493         try {
1494             var data = obj.data; data.stash_retrieve();
1495             if (!params.staff && data.list.au && data.list.au[0]) {
1496                 params.staff = data.list.au[0];
1497             }
1498             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() ]) {
1499                 params.lib = data.hash.aou[ data.list.au[0].ws_ou() ];
1500                 params.lib.children(null);
1501             }
1502             if (params.template && data.print_list_templates[ params.template ]) {
1503                 var template = data.print_list_templates[ params.template ];
1504                 if (template.inherit) {
1505                     template = data.print_list_templates[ template.inherit ];
1506                     // if someone wants to implement recursion later, feel free
1507                 }
1508                 for (var i in template) params[i] = template[i];
1509             }
1510             obj.wrap_in_full_retrieve(
1511                 function() {
1512                     try {
1513                         if (!params.list) params.list = obj.dump_with_keys();
1514                         JSAN.use('util.print'); var print = new util.print(params.printer_context || obj.printer_context);
1515                         print.tree_list( params );
1516                         if (typeof params.callback == 'function') params.callback();
1517                     } catch(E) {
1518                         obj.error.standard_unexpected_error_alert('inner _print_tree',E);
1519                     }
1520                 }
1521             );
1522             
1523         } catch(E) {
1524             obj.error.standard_unexpected_error_alert('_print_tree',E);
1525         }
1526     },
1527
1528     'dump_selected_with_keys' : function(params) {
1529         var obj = this;
1530         switch(this.node.nodeName) {
1531             case 'tree' : return this._dump_tree_selection_with_keys(params); break;
1532             default: throw('NYI: Need .dump_selection_with_keys() for ' + this.node.nodeName); break;
1533         }
1534
1535     },
1536
1537     '_dump_tree_selection_with_keys' : function(params) {
1538         var obj = this;
1539         var dump = [];
1540         var list = obj._retrieve_selection_from_tree();
1541         for (var i = 0; i < list.length; i++) {
1542             var row = {};
1543             var treeitem = list[i];
1544             var treerow = treeitem.firstChild;
1545             for (var j = 0; j < treerow.childNodes.length; j++) {
1546                 var value = treerow.childNodes[j].getAttribute('label');
1547                 if (params.skip_hidden_columns) if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') continue;
1548                 if (typeof obj.columns[j] == 'undefined') {
1549                     dump('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n');
1550                     dump('_dump_tree_selection_with_keys @ ' + location.href + '\n');
1551                     dump('\ttreerow.childNodes.length='+treerow.childNodes.length+' j='+j+' obj.columns.length='+obj.columns.length+'\n');
1552                     debugger;
1553                 } else {
1554                     var id = obj.columns[j].id; if (params.labels_instead_of_ids) id = obj.columns[j].label;
1555                     row[ id ] = value;
1556                 }
1557             }
1558             dump.push( row );
1559         }
1560         return dump;
1561     },
1562
1563     'clipboard' : function(params) {
1564         try {
1565             var obj = this;
1566             var dump = obj.dump_selected_with_keys({'skip_hidden_columns':true,'labels_instead_of_ids':true});
1567             obj.data.stash_retrieve();
1568             obj.data.list_clipboard = dump; obj.data.stash('list_clipboard');
1569             JSAN.use('util.window'); var win = new util.window();
1570             win.open(urls.XUL_LIST_CLIPBOARD,'list_clipboard','chrome,resizable,modal');
1571             window.focus(); // sometimes the main window will lower after a clipboard action
1572         } catch(E) {
1573             this.error.standard_unexpected_error_alert('clipboard',E);
1574         }
1575     },
1576
1577     'dump_retrieve_ids' : function(params) {
1578         var obj = this;
1579         switch(this.node.nodeName) {
1580             case 'tree' : return this._dump_retrieve_ids_tree(params); break;
1581             default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
1582         }
1583     },
1584
1585     '_dump_retrieve_ids_tree' : function(params) {
1586         var obj = this;
1587         var dump = [];
1588         for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1589             var treeitem = this.treechildren.childNodes[i];
1590             dump.push( treeitem.getAttribute('retrieve_id') );
1591         }
1592         return dump;
1593     },
1594
1595     'wrap_in_full_retrieve' : function(f) {
1596         var obj = this;
1597         if (typeof obj.on_all_fleshed == 'function') { // legacy
1598             obj.on_all_fleshed = [ obj.on_all_fleshed ];
1599         }
1600         if (! obj.on_all_fleshed) obj.on_all_fleshed = [];
1601         obj.on_all_fleshed.push(f);
1602         obj.full_retrieve();
1603     },
1604
1605     '_sort_tree' : function() {
1606         var obj = this;
1607         try {
1608             if (obj.node.getAttribute('no_sort')) {
1609                 return;
1610             }
1611
1612             var sorts = [ obj.first_sort ].concat( obj.sub_sorts );
1613             var columns = util.functional.map_list(
1614                 sorts,
1615                 function(e,idx) {
1616                     return e.target;
1617                 }
1618             );
1619             var column_positions = [];
1620             for (var i = 0; i < columns.length; i++) {
1621                 for (var j = 0; j < obj.columns.length; j++) {
1622                     if (obj.columns[j].id == columns[i].id) {
1623                         column_positions.push( function(a){return a;}(j) );
1624                     }
1625                 }
1626             }
1627             obj.wrap_in_full_retrieve(
1628                 function() {
1629                     try {
1630                         JSAN.use('util.money');
1631                         var rows = [];
1632                         var treeitems = obj.treechildren.childNodes;
1633                         for (var i = 0; i < treeitems.length; i++) {
1634                             var treeitem = treeitems[i];
1635                             var treerow = treeitem.firstChild;
1636
1637                             function get_value(treecell) {
1638                                 value = ( {
1639                                     'value' : treecell
1640                                         ? treecell.getAttribute('label')
1641                                         : '',
1642                                     'sort_value' : treecell ? treecell.hasAttribute('sort_value')
1643                                         ? JSON2js(
1644                                             treecell.getAttribute('sort_value'))
1645                                         : '' : ''
1646                                 } );
1647                                 return value;
1648                             }
1649
1650                             var values = [];
1651                             for (var j = 0; j < column_positions.length; j++) {
1652                                 var treecell = treerow.childNodes[ column_positions[j] ];
1653                                 values.push({
1654                                     'position' : column_positions[j],
1655                                     'value' : get_value(treecell)
1656                                 });
1657                             }
1658
1659                             rows.push({
1660                                 'values' : values,
1661                                 'node' : treeitem
1662                             });
1663                         }
1664                         rows = rows.sort( function(A,B) {
1665                             function normalize(a,b,p) {
1666                                 if (a.sort_value) {
1667                                     a = a.sort_value;
1668                                     b = b.sort_value;
1669                                 } else {
1670                                     a = a.value;
1671                                     b = b.value;
1672                                     if (obj.columns[p].getAttribute('sort_type')) {
1673                                         switch(obj.columns[p].getAttribute('sort_type')) {
1674                                             case 'date' :
1675                                                 JSAN.use('util.date'); // to pull in dojo.date.locale
1676                                                 a = dojo.date.locale.parse(a,{});
1677                                                 b = dojo.date.locale.parse(b,{});
1678                                             break;
1679                                             case 'number' :
1680                                                 a = Number(a); b = Number(b);
1681                                             break;
1682                                             case 'money' :
1683                                                 a = util.money.dollars_float_to_cents_integer(a);
1684                                                 b = util.money.dollars_float_to_cents_integer(b);
1685                                             break;
1686                                             case 'title' : /* special case for "a" and "the".  doesn't use marc 245 indicator */
1687                                                 a = String( a ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1688                                                 b = String( b ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1689                                             break;
1690                                             default:
1691                                                 a = String( a ).toUpperCase();
1692                                                 b = String( b ).toUpperCase();
1693                                             break;
1694                                         }
1695                                     } else {
1696                                         if (typeof a == 'string' || typeof b == 'string') {
1697                                             a = String( a ).toUpperCase();
1698                                             b = String( b ).toUpperCase();
1699                                         }
1700                                     }
1701                                 }
1702                                 return [ a, b ];
1703                             }
1704
1705                             for (var i = 0; i < sorts.length; i++) {
1706                                 var values;
1707                                 if (sorts[i].sortDir == 'asc') {
1708                                     values = normalize(
1709                                         B['values'][i]['value'],
1710                                         A['values'][i]['value'],
1711                                         A['values'][i]['position']
1712                                     );
1713                                 } else {
1714                                     values = normalize(
1715                                         A['values'][i]['value'],
1716                                         B['values'][i]['value'],
1717                                         A['values'][i]['position']
1718                                     );
1719                                 }
1720                                 if (values[0] < values[1] ) {
1721                                     return -1;
1722                                 }
1723                                 if (values[0] > values[1] ) {
1724                                     return 1;
1725                                 }
1726                             }
1727                             return 0; 
1728                         } );
1729                         while(obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
1730                         for (var i = 0; i < rows.length; i++) {
1731                             obj.treechildren.appendChild( rows[i].node );
1732                         }
1733                         if (typeof obj.on_sort == 'function') obj.on_sort();
1734                     } catch(E) {
1735                         obj.error.standard_unexpected_error_alert('sorting',E); 
1736                     }
1737                     obj.refresh_ordinals();
1738                 }
1739             );
1740         } catch(E) {
1741             obj.error.standard_unexpected_error_alert('pre sorting', E);
1742         }
1743     },
1744
1745     '_toggle_checkbox_column' : function(col,toggle) {
1746         var obj = this;
1747         try {
1748             if (obj.node.getAttribute('no_toggle')) {
1749                 return;
1750             }
1751             var col_pos;
1752             for (var i = 0; i < obj.columns.length; i++) { 
1753                 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i); 
1754             }
1755             var treeitems = obj.treechildren.childNodes;
1756             for (var i = 0; i < treeitems.length; i++) {
1757                 var treeitem = treeitems[i];
1758                 var treerow = treeitem.firstChild;
1759                 var treecell = treerow.childNodes[ col_pos ];
1760                 treecell.setAttribute('value',(toggle == 'on'));
1761             }
1762             if (typeof obj.on_checkbox_toggle == 'function') obj.on_checkbox_toggle(toggle);
1763         } catch(E) {
1764             obj.error.standard_unexpected_error_alert('pre toggle', E);
1765         }
1766     },
1767
1768     'render_list_actions' : function(params) {
1769         var obj = this;
1770         switch(this.node.nodeName) {
1771             case 'tree' : return this._render_list_actions_for_tree(params); break;
1772             default: throw('NYI: Need ._render_list_actions() for ' + this.node.nodeName); break;
1773         }
1774     },
1775
1776     '_render_list_actions_for_tree' : function(params) {
1777         var obj = this;
1778         try {
1779             var btn = document.createElement('button');
1780             btn.setAttribute('id',obj.node.id + '_list_actions');
1781             btn.setAttribute('type','menu');
1782             btn.setAttribute('allowevents','true');
1783             //btn.setAttribute('oncommand','this.firstChild.showPopup();');
1784             btn.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.menu.label'));
1785             btn.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.menu.accesskey'));
1786             var mp = document.createElement('menupopup');
1787             btn.appendChild(mp);
1788             var mi = document.createElement('menuitem');
1789             mi.setAttribute('id',obj.node.id + '_clipfield');
1790             mi.setAttribute('disabled','true');
1791             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.label'));
1792             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.accesskey'));
1793             mp.appendChild(mi);
1794             mi = document.createElement('menuitem');
1795             mi.setAttribute('id',obj.node.id + '_csv_to_clipboard');
1796             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.label'));
1797             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.accesskey'));
1798             mp.appendChild(mi);
1799             mi = document.createElement('menuitem');
1800             mi.setAttribute('id',obj.node.id + '_csv_to_printer');
1801             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.label'));
1802             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.accesskey'));
1803             mp.appendChild(mi);
1804             mi = document.createElement('menuitem');
1805             mi.setAttribute('id',obj.node.id + '_extended_to_printer');
1806             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.extended_to_printer.label'));
1807             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.extended_to_printer.accesskey'));
1808             mp.appendChild(mi);
1809             mi = document.createElement('menuitem');
1810             mi.setAttribute('id',obj.node.id + '_csv_to_file');
1811             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.label'));
1812             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.accesskey'));
1813             mp.appendChild(mi);
1814             mi = document.createElement('menuitem');
1815             mi.setAttribute('id',obj.node.id + '_save_columns');
1816             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.label'));
1817             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.accesskey'));
1818             if (obj.data.hash.aous['gui.disable_local_save_columns']) {
1819                 mi.setAttribute('disabled','true');
1820             }
1821             mp.appendChild(mi);
1822             return btn;
1823         } catch(E) {
1824             obj.error.standard_unexpected_error_alert('rendering list actions',E);
1825         }
1826     },
1827
1828     'set_list_actions' : function(params) {
1829         var obj = this;
1830         switch(this.node.nodeName) {
1831             case 'tree' : return this._set_list_actions_for_tree(params); break;
1832             default: throw('NYI: Need ._set_list_actions() for ' + this.node.nodeName); break;
1833         }
1834     },
1835
1836     '_set_list_actions_for_tree' : function(params) {
1837         // This should be called after the button element from render_list_actions has been appended to the DOM
1838         var obj = this;
1839         try {
1840             var x = document.getElementById(obj.node.id + '_clipfield');
1841             if (x) {
1842                 x.addEventListener(
1843                     'command',
1844                     function() {
1845                         obj.clipboard(params);
1846                         if (params && typeof params.on_complete == 'function') {
1847                             params.on_complete(params);
1848                         }
1849                     },
1850                     false
1851                 );
1852             }
1853             x = document.getElementById(obj.node.id + '_csv_to_clipboard');
1854             if (x) {
1855                 x.addEventListener(
1856                     'command',
1857                     function() {
1858                         obj.dump_csv_to_clipboard(params);
1859                         if (params && typeof params.on_complete == 'function') {
1860                             params.on_complete(params);
1861                         }
1862                     },
1863                     false
1864                 );
1865             }
1866             x = document.getElementById(obj.node.id + '_csv_to_printer');
1867             if (x) {
1868                 x.addEventListener(
1869                     'command',
1870                     function() {
1871                         obj.dump_csv_to_printer(params);
1872                         if (params && typeof params.on_complete == 'function') {
1873                             params.on_complete(params);
1874                         }
1875                     },
1876                     false
1877                 );
1878             }
1879             x = document.getElementById(obj.node.id + '_extended_to_printer');
1880             if (x) {
1881                 x.addEventListener(
1882                     'command',
1883                     function() {
1884                         obj.dump_extended_format_to_printer(params);
1885                         if (params && typeof params.on_complete == 'function') {
1886                             params.on_complete(params);
1887                         }
1888                     },
1889                     false
1890                 );
1891             }
1892
1893             x = document.getElementById(obj.node.id + '_csv_to_file');
1894             if (x) {
1895                 x.addEventListener(
1896                     'command',
1897                     function() {
1898                         obj.dump_csv_to_file(params);
1899                         if (params && typeof params.on_complete == 'function') {
1900                             params.on_complete(params);
1901                         }
1902                     },
1903                     false
1904                 );
1905             }
1906             x = document.getElementById(obj.node.id + '_save_columns');
1907             if (x) {
1908                 x.addEventListener(
1909                     'command',
1910                     function() {
1911                         obj.save_columns(params);
1912                         if (params && typeof params.on_complete == 'function') {
1913                             params.on_complete(params);
1914                         }
1915                     },
1916                     false
1917                 );
1918             }
1919
1920         } catch(E) {
1921             obj.error.standard_unexpected_error_alert('setting list actions',E);
1922         }
1923     },
1924
1925     // Takes fieldmapper class name and attempts to spit out column definitions suitable for .init
1926     'fm_columns' : function(hint,column_extras,prefix) {
1927         var obj = this;
1928         var columns = [];
1929         if (!prefix) { prefix = ''; }
1930         try {
1931             // requires the dojo library fieldmapper.autoIDL
1932             if (typeof fieldmapper == 'undefined') { throw 'fieldmapper undefined'; }
1933             if (typeof fieldmapper.IDL == 'undefined') { throw 'fieldmapper.IDL undefined'; }
1934             if (typeof fieldmapper.IDL.fmclasses == 'undefined') { throw 'fieldmapper.IDL.fmclasses undefined'; }
1935             if (typeof fieldmapper.IDL.fmclasses[hint] == 'undefined') { throw 'fieldmapper.IDL.fmclasses.' + hint + ' undefined'; }
1936             var my_class = fieldmapper.IDL.fmclasses[hint]; 
1937             var data = obj.data; data.stash_retrieve();
1938
1939             function col_def(my_field) {
1940                 var col_id = prefix + hint + '_' + my_field.name;
1941                 var dataobj = hint;
1942                 var datafield = my_field.name;
1943                 var fleshed_display_field;
1944                 if (column_extras) {
1945                     if (column_extras['*']) {
1946                         if (column_extras['*']['dataobj']) {
1947                             dataobj = column_extras['*']['dataobj'];
1948                         }
1949                     }
1950                     if (column_extras[col_id]) {
1951                         if (column_extras[col_id]['dataobj']) {
1952                             dataobj = column_extras[col_id]['dataobj'];
1953                         }
1954                         if (column_extras[col_id]['datafield']) {
1955                             datafield = column_extras[col_id]['datafield'];
1956                         }
1957                         if (column_extras[col_id]['fleshed_display_field']) {
1958                             fleshed_display_field = column_extras[col_id]['fleshed_display_field'];
1959                         }
1960                     }
1961                 }
1962                 var def = {
1963                     'id' : col_id,
1964                     'label' : my_field.label || my_field.name,
1965                     'sort_type' : [ 'int', 'float', 'id', 'number' ].indexOf(my_field.datatype) > -1 ? 'number' : 
1966                         ( my_field.datatype == 'money' ? 'money' : 
1967                         ( my_field.datatype == 'timestamp' ? 'date' : 'default')),
1968                     'hidden' : my_field.virtual || my_field.datatype == 'link',
1969                     'flex' : 1
1970                 };                    
1971                 // my_field.datatype => bool float id int interval link money number org_unit text timestamp
1972                 if (my_field.datatype == 'link') {
1973                     def.render = function(my) { 
1974                         // is the object fleshed?
1975                         return my[dataobj][datafield]() && typeof my[dataobj][datafield]() == 'object'
1976                             // yes, show the display field
1977                             ? my[dataobj][datafield]()[fleshed_display_field||my_field.key]()
1978                             // no, do we have its class in data.hash?
1979                             : ( typeof data.hash[ my[dataobj].Structure.field_map[datafield].class ] != 'undefined'
1980                                 // yes, do we have this particular object cached?
1981                                 ? ( data.hash[ my[dataobj].Structure.field_map[datafield].class ][ my[dataobj][datafield]() ]
1982                                     // yes, show the display field
1983                                     ? data.hash[ my[dataobj].Structure.field_map[datafield].class ][ my[dataobj][datafield]() ][
1984                                         fleshed_display_field||my_field.key
1985                                     ]()
1986                                     // no, just show the raw value
1987                                     : my[dataobj][datafield]()
1988                                 )
1989                                 // no, just show the raw value
1990                                 : my[dataobj][datafield]()
1991                             ); 
1992                     }
1993                 } else {
1994                     def.render = function(my) { return my[dataobj][datafield](); }
1995                 }
1996                 if (my_field.datatype == 'timestamp') {
1997                     JSAN.use('util.date');
1998                     def.render = function(my) {
1999                         return util.date.formatted_date( my[dataobj][datafield](), '%{localized}' );
2000                     }
2001                     def.sort_value = function(my) {
2002                         return util.date.db_date2Date( my[dataobj][datafield]() ).getTime();
2003                     }
2004                 }
2005                 if (my_field.datatype == 'org_unit') {
2006                     def.render = function(my) {
2007                         return typeof my[dataobj][datafield]() == 'object' ? my[dataobj][datafield]().shortname() : data.hash.aou[ my[dataobj][datafield]() ].shortname();
2008                     }
2009                 }
2010                 if (my_field.datatype == 'money') {
2011                     JSAN.use('util.money');
2012                     def.render = function(my) {
2013                         return util.money.sanitize( my[dataobj][datafield]() );
2014                     }
2015                     def.sort_value = function(my) {
2016                         return util.money.dollars_float_to_cents_integer( my[dataobj][datafield]() );
2017                     }
2018                 }
2019                 if (column_extras) {
2020                     if (column_extras['*']) {
2021                         for (var attr in column_extras['*']) {
2022                             def[attr] = column_extras['*'][attr];
2023                         }
2024                         if (column_extras['*']['expanded_label']) {
2025                             def.label = my_class.label + ': ' + def.label;
2026                         }
2027                         if (column_extras['*']['label_prefix']) {
2028                             def.label = column_extras['*']['label_prefix'] + def.label;
2029                         }
2030                         if (column_extras['*']['remove_virtual']) {
2031                             if (my_field.virtual) {
2032                                 def.remove_me = true;
2033                             }
2034                         }
2035                     }
2036                     if (column_extras[col_id]) {
2037                         for (var attr in column_extras[col_id]) {
2038                             def[attr] = column_extras[col_id][attr];
2039                         }
2040                         if (column_extras[col_id]['keep_me']) {
2041                             def.remove_me = false;
2042                         }
2043                         if (column_extras[col_id]['label_prefix']) {
2044                             def.label = column_extras[col_id]['label_prefix'] + def.label;
2045                         }
2046                     }
2047                 }
2048                 if (def.remove_me) {
2049                     dump('Skipping ' + def.label + '\n');
2050                     return null;
2051                 } else {
2052                     dump('Defining ' + def.label + '\n');
2053                     return def;
2054                 }
2055             }
2056  
2057             for (var i = 0; i < my_class.fields.length; i++) {
2058                 var my_field = my_class.fields[i];
2059                 var def = col_def(my_field);
2060                 if (def) {
2061                     columns.push( def );
2062                 }
2063             }
2064
2065         } catch(E) {
2066             obj.error.standard_unexpected_error_alert('fm_columns()',E);
2067         }
2068         return columns;
2069     },
2070     // Default for the map_row_to_columns function for .init
2071     'std_map_row_to_columns' : function(error_value) {
2072         return function(row,cols,scratch) {
2073             // row contains { 'my' : { 'acp' : {}, 'circ' : {}, 'mvr' : {} } }
2074             // cols contains all of the objects listed above in columns
2075             // scratch is a temporary space shared by all cells/rows (or just per row if not explicitly passed in)
2076             if (!scratch) { scratch = {}; }
2077
2078             var obj = {};
2079             JSAN.use('util.error'); obj.error = new util.error();
2080             JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
2081             JSAN.use('util.network'); obj.network = new util.network();
2082             JSAN.use('util.money');
2083
2084             var my = row.my;
2085             var values = [];
2086             var sort_values = [];
2087             var cmd = '';
2088             try {
2089                 for (var i = 0; i < cols.length; i++) {
2090                     switch (typeof cols[i].render) {
2091                         case 'function': try { values[i] = cols[i].render(my,scratch); } catch(E) { values[i] = error_value; obj.error.sdump('D_COLUMN_RENDER_ERROR',E); } break;
2092                         case 'string' : cmd += 'try { ' + cols[i].render + '; values['+i+'] = v; } catch(E) { values['+i+'] = error_value; }'; break;
2093                         default: cmd += 'values['+i+'] = "??? '+(typeof cols[i].render)+'"; ';
2094                     }
2095                     switch (typeof cols[i].sort_value) {
2096                         case 'function':
2097                             try {
2098                                 sort_values[i] = cols[i].sort_value(my,scratch);
2099                             } catch(E) {
2100                                 sort_values[i] = error_value;
2101                                 obj.error.sdump('D_COLUMN_RENDER_ERROR',E);
2102                             }
2103                         break;
2104                         case 'string' :
2105                             cmd += 'try { '
2106                                 + cols[i].sort_value
2107                                 + '; values['
2108                                 + i
2109                                 +'] = v; } catch(E) { sort_values['
2110                                 + i
2111                                 + '] = error_value; }';
2112                         break;
2113                         default:
2114                             cmd += 'sort_values['+i+'] = values[' + i + '];';
2115                     }
2116                 }
2117                 if (cmd) eval( cmd );
2118             } catch(E) {
2119                 obj.error.sdump('D_WARN','map_row_to_column: ' + E);
2120                 if (error_value) { value = error_value; } else { value = '   ' };
2121             }
2122             return { 'values' : values, 'sort_values' : sort_values };
2123         }
2124     }
2125 }
2126 dump('exiting util.list.js\n');