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