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