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