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