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