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