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