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