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