]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/list.js
backporting from staff-client-experiment: refactor standard list actions (the ones...
[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         if (!this.node) throw('Could not find element ' + id);
11         switch(this.node.nodeName) {
12                 case 'listbox' : 
13                 case 'tree' : break;
14                 case 'richlistbox' :
15                         throw(this.node.nodeName + ' not yet supported'); break;
16                 default: throw(this.node.nodeName + ' not supported'); break;
17         }
18
19         JSAN.use('util.error'); this.error = new util.error();
20
21         return this;
22 };
23
24 util.list.prototype = {
25
26         'init' : function (params) {
27
28                 var obj = this;
29
30                 JSAN.use('util.widgets');
31
32                 if (typeof params.map_row_to_column == 'function') obj.map_row_to_column = params.map_row_to_column;
33                 if (typeof params.map_row_to_columns == 'function') obj.map_row_to_columns = params.map_row_to_columns;
34                 if (typeof params.retrieve_row == 'function') obj.retrieve_row = params.retrieve_row;
35
36                 obj.prebuilt = false;
37                 if (typeof params.prebuilt != 'undefined') obj.prebuilt = params.prebuilt;
38
39                 if (typeof params.columns == 'undefined') throw('util.list.init: No columns');
40                 obj.columns = params.columns;
41
42                 switch(obj.node.nodeName) {
43                         case 'tree' : obj._init_tree(params); break;
44                         case 'listbox' : obj._init_listbox(params); break;
45                         default: throw('NYI: Need ._init() for ' + obj.node.nodeName); break;
46                 }
47         },
48
49         '_init_tree' : function (params) {
50                 var obj = this;
51                 if (this.prebuilt) {
52                 
53                         this.treechildren = this.node.lastChild;        
54                 
55                 } else {
56                         var treecols = document.createElement('treecols');
57                         this.node.appendChild(treecols);
58                         this.treecols = treecols;
59
60                         for (var i = 0; i < this.columns.length; i++) {
61                                 var treecol = document.createElement('treecol');
62                                 for (var j in this.columns[i]) {
63                                         treecol.setAttribute(j,this.columns[i][j]);
64                                 }
65                                 treecols.appendChild(treecol);
66                                 treecol.addEventListener(
67                                         'click', 
68                                         function(ev) {
69                                                 function do_it() {
70                                                         var sortDir = ev.target.getAttribute('sortDir') || 'desc';
71                                                         if (sortDir == 'desc') sortDir = 'asc'; else sortDir = 'desc';
72                                                         ev.target.setAttribute('sortDir',sortDir);
73                                                         obj._sort_tree(ev.target,sortDir);
74                                                 }
75
76                                                 if (obj.row_count.total != obj.row_count.fleshed && (obj.row_count.total - obj.row_count.fleshed) > 50) {
77                                                         var r = window.confirm(document.getElementById('offlineStrings').getFormattedString('list.row_fetch_warning',[obj.row_count.fleshed,obj.row_count.total]));
78
79                                                         if (r) {
80                                                                 setTimeout( do_it, 0 );
81                                                         }
82                                                 } else {
83                                                                 setTimeout( do_it, 0 );
84                                                 }
85                                         },
86                                         false
87                                 );
88                                 var splitter = document.createElement('splitter');
89                                 splitter.setAttribute('class','tree-splitter');
90                                 treecols.appendChild(splitter);
91                         }
92
93                         var treechildren = document.createElement('treechildren');
94                         this.node.appendChild(treechildren);
95                         this.treechildren = treechildren;
96                 }
97         this.node.addEventListener(
98             'select',
99             function(ev) {
100                 if (typeof params.on_select == 'function') {
101                     params.on_select(ev);
102                 }
103                 var x = document.getElementById(obj.node.id + '_clipfield');
104                 if (x) {
105                     var sel = obj.retrieve_selection();
106                     x.setAttribute('disabled', sel.length == 0);
107                 }
108             },
109             false
110         );
111                 if (typeof params.on_click == 'function') {
112                         this.node.addEventListener(
113                                 'click',
114                                 params.on_click,
115                                 false
116                         );
117                 }
118                 /*
119                 this.node.addEventListener(
120                         'mousemove',
121                         function(ev) { obj.detect_visible(); },
122                         false
123                 );
124                 */
125                 this.node.addEventListener(
126                         'keypress',
127                         function(ev) { obj.auto_retrieve(); },
128                         false
129                 );
130                 this.node.addEventListener(
131                         'click',
132                         function(ev) { obj.auto_retrieve(); },
133                         false
134                 );
135                 window.addEventListener(
136                         'resize',
137                         function(ev) { obj.auto_retrieve(); },
138                         false
139                 );
140                 /* FIXME -- find events on scrollbar to trigger this */
141                 obj.detect_visible_polling();   
142                 /*
143                 var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(this.node)[1] )[1];
144                 var slider = document.getAnonymousNodes( scrollbar )[2];
145                 alert('scrollbar = ' + scrollbar.nodeName + ' grippy = ' + slider.nodeName);
146                 scrollbar.addEventListener('click',function(){alert('sb click');},false);
147                 scrollbar.addEventListener('command',function(){alert('sb command');},false);
148                 scrollbar.addEventListener('scroll',function(){alert('sb scroll');},false);
149                 slider.addEventListener('click',function(){alert('slider click');},false);
150                 slider.addEventListener('command',function(){alert('slider command');},false);
151                 slider.addEventListener('scroll',function(){alert('slider scroll');},false);
152                 */
153                 this.node.addEventListener('scroll',function(){ obj.auto_retrieve(); },false);
154
155                 this.restores_columns(params);
156         },
157
158         '_init_listbox' : function (params) {
159                 if (this.prebuilt) {
160                 } else {
161                         var listhead = document.createElement('listhead');
162                         this.node.appendChild(listhead);
163
164                         var listcols = document.createElement('listcols');
165                         this.node.appendChild(listcols);
166
167                         for (var i = 0; i < this.columns.length; i++) {
168                                 var listheader = document.createElement('listheader');
169                                 listhead.appendChild(listheader);
170                                 var listcol = document.createElement('listcol');
171                                 listcols.appendChild(listcol);
172                                 for (var j in this.columns[i]) {
173                                         listheader.setAttribute(j,this.columns[i][j]);
174                                         listcol.setAttribute(j,this.columns[i][j]);
175                                 };
176                         }
177                 }
178         },
179
180         'save_columns' : function (params) {
181                 var obj = this;
182                 switch (this.node.nodeName) {
183                         case 'tree' : this._save_columns_tree(params); break;
184                         default: throw('NYI: Need .save_columns() for ' + this.node.nodeName); break;
185                 }
186         },
187
188         '_save_columns_tree' : function (params) {
189                 var obj = this;
190                 try {
191                         var id = obj.node.getAttribute('id'); if (!id) {
192                                 alert("FIXME: The columns for this list cannot be saved because the list has no id.");
193                                 return;
194                         }
195                         var my_cols = {};
196                         var nl = obj.node.getElementsByTagName('treecol');
197                         for (var i = 0; i < nl.length; i++) {
198                                 var col = nl[i];
199                                 var col_id = col.getAttribute('id');
200                                 if (!col_id) {
201                                         alert('FIXME: A column in this list does not have an id and cannot be saved');
202                                         continue;
203                                 }
204                                 var col_hidden = col.getAttribute('hidden'); 
205                                 var col_width = col.getAttribute('width'); 
206                                 var col_ordinal = col.getAttribute('ordinal'); 
207                                 my_cols[ col_id ] = { 'hidden' : col_hidden, 'width' : col_width, 'ordinal' : col_ordinal };
208                         }
209                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
210                         JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
211                         file.set_object(my_cols);
212                         file.close();
213                         alert(document.getElementById('offlineStrings').getString('list.columns_saved'));
214                 } catch(E) {
215                         obj.error.standard_unexpected_error_alert('_save_columns_tree',E);
216                 }
217         },
218
219         'restores_columns' : function (params) {
220                 var obj = this;
221                 switch (this.node.nodeName) {
222                         case 'tree' : this._restores_columns_tree(params); break;
223                         default: throw('NYI: Need .restores_columns() for ' + this.node.nodeName); break;
224                 }
225         },
226
227         '_restores_columns_tree' : function (params) {
228                 var obj = this;
229                 try {
230                         var id = obj.node.getAttribute('id'); if (!id) {
231                                 alert("FIXME: The columns for this list cannot be restored because the list has no id.");
232                                 return;
233                         }
234
235                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
236                         JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
237                         if (file._file.exists()) {
238                                 var my_cols = file.get_object(); file.close();
239                                 var nl = obj.node.getElementsByTagName('treecol');
240                                 for (var i = 0; i < nl.length; i++) {
241                                         var col = nl[i];
242                                         var col_id = col.getAttribute('id');
243                                         if (!col_id) {
244                                                 alert('FIXME: A column in this list does not have an id and cannot be saved');
245                                                 continue;
246                                         }
247                                         if (typeof my_cols[col_id] != 'undefined') {
248                                                 col.setAttribute('hidden',my_cols[col_id].hidden); 
249                                                 col.setAttribute('width',my_cols[col_id].width); 
250                                                 col.setAttribute('ordinal',my_cols[col_id].ordinal); 
251                                         } else {
252                                                 obj.error.sdump('D_ERROR','WARNING: Column ' + col_id + ' did not have a saved state.');
253                                         }
254                                 }
255                         }
256                 } catch(E) {
257                         obj.error.standard_unexpected_error_alert('_restore_columns_tree',E);
258                 }
259         },
260
261         'clear' : function (params) {
262                 var obj = this;
263                 switch (this.node.nodeName) {
264                         case 'tree' : this._clear_tree(params); break;
265                         case 'listbox' : this._clear_listbox(params); break;
266                         default: throw('NYI: Need .clear() for ' + this.node.nodeName); break;
267                 }
268                 this.error.sdump('D_LIST','Clearing list ' + this.node.getAttribute('id') + '\n');
269                 this.row_count.total = 0;
270                 this.row_count.fleshed = 0;
271                 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
272         },
273
274         '_clear_tree' : function(params) {
275                 var obj = this;
276                 if (obj.error.sdump_levels.D_LIST_DUMP_ON_CLEAR) {
277                         obj.error.sdump('D_LIST_DUMP_ON_CLEAR',obj.dump());
278                 }
279                 if (obj.error.sdump_levels.D_LIST_DUMP_WITH_KEYS_ON_CLEAR) {
280                         obj.error.sdump('D_LIST_DUMP_WITH_KEYS_ON_CLEAR',obj.dump_with_keys());
281                 }
282                 while (obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
283         },
284
285         '_clear_listbox' : function(params) {
286                 var obj = this;
287                 var items = [];
288                 var nl = this.node.getElementsByTagName('listitem');
289                 for (var i = 0; i < nl.length; i++) {
290                         items.push( nl[i] );
291                 }
292                 for (var i = 0; i < items.length; i++) {
293                         this.node.removeChild(items[i]);
294                 }
295         },
296
297         'append' : function (params) {
298                 var rnode;
299                 var obj = this;
300                 switch (this.node.nodeName) {
301                         case 'tree' : rparams = this._append_to_tree(params); break;
302                         case 'listbox' : rparams = this._append_to_listbox(params); break;
303                         default: throw('NYI: Need .append() for ' + this.node.nodeName); break;
304                 }
305                 if (rparams && params.attributes) {
306                         for (var i in params.attributes) {
307                                 rparams.my_node.setAttribute(i,params.attributes[i]);
308                         }
309                 }
310                 this.row_count.total++;
311                 if (this.row_count.fleshed == this.row_count.total) {
312                         setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
313                 }
314                 return rparams;
315         },
316         
317         'refresh_row' : function (params) {
318                 var rnode;
319                 var obj = this;
320                 switch (this.node.nodeName) {
321                         case 'tree' : rparams = this._refresh_row_in_tree(params); break;
322                         default: throw('NYI: Need .refresh_row() for ' + this.node.nodeName); break;
323                 }
324                 if (rparams && params.attributes) {
325                         for (var i in params.attributes) {
326                                 rparams.my_node.setAttribute(i,params.attributes[i]);
327                         }
328                 }
329         this.row_count.fleshed--;
330                 return rparams;
331         },
332
333
334         '_append_to_tree' : function (params) {
335
336                 var obj = this;
337
338                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
339
340                 var s = ('util.list.append: params = ' + (params) + '\n');
341
342                 var treechildren_node = this.treechildren;
343
344                 if (params.node && params.node.nodeName == 'treeitem') {
345                         params.node.setAttribute('container','true'); /* params.node.setAttribute('open','true'); */
346                         if (params.node.lastChild.nodeName == 'treechildren') {
347                                 treechildren_node = params.node.lastChild;
348                         } else {
349                                 treechildren_node = document.createElement('treechildren');
350                                 params.node.appendChild(treechildren_node);
351                         }
352                 }
353
354                 var treeitem = document.createElement('treeitem');
355                 treeitem.setAttribute('retrieve_id',params.retrieve_id);
356                 if (typeof params.to_bottom != 'undefined') {
357                         treechildren_node.appendChild( treeitem );
358                         if (typeof params.no_auto_select == 'undefined') {
359                                 if (!obj.auto_select_pending) {
360                                         obj.auto_select_pending = true;
361                                         setTimeout(function() {
362                                                 dump('auto-selecting\n');
363                                                 var idx = Number(obj.node.view.rowCount)-1;
364                                                 try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
365                                                 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
366                                                 obj.auto_select_pending = false;
367                                                 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
368                                         }, 1000);
369                                 }
370                         }
371                 } else {
372                         if (treechildren_node.firstChild) {
373                                 treechildren_node.insertBefore( treeitem, treechildren_node.firstChild );
374                         } else {
375                                 treechildren_node.appendChild( treeitem );
376                         }
377                         if (typeof params.no_auto_select == 'undefined') {
378                                 if (!obj.auto_select_pending) {
379                                         obj.auto_select_pending = true;
380                                         setTimeout(function() {
381                                                 try { obj.node.view.selection.select(0); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
382                                                 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
383                                                 obj.auto_select_pending = false;
384                                                 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(0).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
385                                         }, 1000);
386                                 }
387                         }
388                 }
389                 var treerow = document.createElement('treerow');
390                 treeitem.appendChild( treerow );
391                 treerow.setAttribute('retrieve_id',params.retrieve_id);
392
393                 s += ('tree = ' + this.node + '  treechildren = ' + treechildren_node + '\n');
394                 s += ('treeitem = ' + treeitem + '  treerow = ' + treerow + '\n');
395
396                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
397
398                         obj.put_retrieving_label(treerow);
399                         treerow.addEventListener(
400                                 'flesh',
401                                 function() {
402
403                                         if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
404
405                                         treerow.setAttribute('retrieved','true');
406
407                                         //dump('fleshing = ' + params.retrieve_id + '\n');
408
409                                         function inc_fleshed() {
410                                                 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
411                                                 treerow.setAttribute('fleshed','true');
412                                                 obj.row_count.fleshed++;
413                                                 if (obj.row_count.fleshed >= obj.row_count.total) {
414                                                         setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
415                                                 }
416                                         }
417
418                                         params.row_node = treeitem;
419                                         params.on_retrieve = function(p) {
420                                                 try {
421                                                         p.row = params.row;
422                                                         obj._map_row_to_treecell(p,treerow);
423                                                         inc_fleshed();
424                                                         var idx = obj.node.contentView.getIndexOfItem( params.row_node );
425                                                         dump('idx = ' + idx + '\n');
426                                                         // if current row is selected, send another select event to re-sync data that the client code fetches on selects
427                                                         if ( obj.node.view.selection.isSelected( idx ) ) {
428                                                                 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
429                                                                 util.widgets.dispatch('select',obj.node);
430                                                         }
431                                                 } catch(E) {
432                             // Let's not alert on this for now.  Getting contentView has no properties in record buckets under certain conditions
433                                                         dump('fixme2: ' + E + '\n');
434                                                 }
435                                         }
436
437                                         if (typeof params.retrieve_row == 'function') {
438
439                                                 params.retrieve_row( params );
440
441                                         } else if (typeof obj.retrieve_row == 'function') {
442
443                                                         obj.retrieve_row( params );
444
445                                         } else {
446                                         
447                                                         inc_fleshed();
448                                         }
449                                 },
450                                 false
451                         );
452                         /*
453                         setTimeout(
454                                 function() {
455                                         util.widgets.dispatch('flesh',treerow);
456                                 }, 0
457                         );
458                         */
459                 } else {
460                         obj.put_retrieving_label(treerow);
461                         treerow.addEventListener(
462                                 'flesh',
463                                 function() {
464                                         //dump('fleshing anon\n');
465                                         if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
466                                         obj._map_row_to_treecell(params,treerow);
467                                         treerow.setAttribute('retrieved','true');
468                                         treerow.setAttribute('fleshed','true');
469                                         obj.row_count.fleshed++;
470                                         if (obj.row_count.fleshed >= obj.row_count.total) {
471                                                 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
472                                         }
473                                 },
474                                 false
475                         );
476                         /*
477                         setTimeout(
478                                 function() {
479                                         util.widgets.dispatch('flesh',treerow);
480                                 }, 0
481                         );
482                         */
483                 }
484                 this.error.sdump('D_LIST',s);
485
486                         try {
487
488                                 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
489                                         // Remove oldest row
490                                         //if (typeof params.to_bottom != 'undefined') 
491                                         if (typeof params.to_top == 'undefined') {
492                                                 treechildren_node.removeChild( treechildren_node.firstChild );
493                                         } else {
494                                                 treechildren_node.removeChild( treechildren_node.lastChild );
495                                         }
496                                 }
497                         } catch(E) {
498                         }
499
500                 setTimeout( function() { obj.auto_retrieve(); }, 0 );
501
502                 params.my_node = treeitem;
503                 return params;
504         },
505
506         '_refresh_row_in_tree' : function (params) {
507
508                 var obj = this;
509
510                 if (typeof params.row == 'undefined') throw('util.list.refresh_row: Object must contain a row');
511                 if (typeof params.my_node == 'undefined') throw('util.list.refresh_row: Object must contain a my_node');
512                 if (params.my_node.nodeName != 'treeitem') throw('util.list.refresh_rwo: my_node must be a treeitem');
513
514                 var s = ('util.list.refresh_row: params = ' + (params) + '\n');
515
516                 var treeitem = params.my_node;
517                 treeitem.setAttribute('retrieve_id',params.retrieve_id);
518                 if (typeof params.to_bottom != 'undefined') {
519                         if (typeof params.no_auto_select == 'undefined') {
520                                 if (!obj.auto_select_pending) {
521                                         obj.auto_select_pending = true;
522                                         setTimeout(function() {
523                                                 dump('auto-selecting\n');
524                                                 var idx = Number(obj.node.view.rowCount)-1;
525                                                 try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
526                                                 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
527                                                 obj.auto_select_pending = false;
528                                                 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
529                                         }, 1000);
530                                 }
531                         }
532                 }
533                 var delete_me = [];
534                 for (var i in treeitem.childNodes) if (treeitem.childNodes[i].nodeName == 'treerow') delete_me.push(treeitem.childNodes[i]);
535                 for (var i = 0; i < delete_me.length; i++) treeitem.removeChild(delete_me[i]);
536                 var treerow = document.createElement('treerow');
537                 treeitem.appendChild( treerow );
538                 treerow.setAttribute('retrieve_id',params.retrieve_id);
539
540                 s += ('tree = ' + this.node + '\n');
541                 s += ('treeitem = ' + treeitem + '  treerow = ' + treerow + '\n');
542
543                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
544
545                         obj.put_retrieving_label(treerow);
546                         treerow.addEventListener(
547                                 'flesh',
548                                 function() {
549
550                                         if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
551
552                                         treerow.setAttribute('retrieved','true');
553
554                                         //dump('fleshing = ' + params.retrieve_id + '\n');
555
556                                         function inc_fleshed() {
557                                                 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
558                                                 treerow.setAttribute('fleshed','true');
559                                                 obj.row_count.fleshed++;
560                                                 if (obj.row_count.fleshed >= obj.row_count.total) {
561                                                         setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
562                                                 }
563                                         }
564
565                                         params.row_node = treeitem;
566                                         params.on_retrieve = function(p) {
567                                                 try {
568                                                         p.row = params.row;
569                                                         obj._map_row_to_treecell(p,treerow);
570                                                         inc_fleshed();
571                                                         var idx = obj.node.contentView.getIndexOfItem( params.row_node );
572                                                         dump('idx = ' + idx + '\n');
573                                                         // if current row is selected, send another select event to re-sync data that the client code fetches on selects
574                                                         if ( obj.node.view.selection.isSelected( idx ) ) {
575                                                                 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
576                                                                 util.widgets.dispatch('select',obj.node);
577                                                         }
578                                                 } catch(E) {
579                             // Let's not alert on this for now.  Getting contentView has no properties in record buckets under certain conditions
580                                                         dump('fixme2: ' + E + '\n');
581                                                 }
582                                         }
583
584                                         if (typeof params.retrieve_row == 'function') {
585
586                                                 params.retrieve_row( params );
587
588                                         } else if (typeof obj.retrieve_row == 'function') {
589
590                                                         obj.retrieve_row( params );
591
592                                         } else {
593                                         
594                                                         inc_fleshed();
595                                         }
596                                 },
597                                 false
598                         );
599                         /*
600                         setTimeout(
601                                 function() {
602                                         util.widgets.dispatch('flesh',treerow);
603                                 }, 0
604                         );
605                         */
606                 } else {
607                         obj.put_retrieving_label(treerow);
608                         treerow.addEventListener(
609                                 'flesh',
610                                 function() {
611                                         //dump('fleshing anon\n');
612                                         if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
613                                         obj._map_row_to_treecell(params,treerow);
614                                         treerow.setAttribute('retrieved','true');
615                                         treerow.setAttribute('fleshed','true');
616                                         obj.row_count.fleshed++;
617                                         if (obj.row_count.fleshed >= obj.row_count.total) {
618                                                 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
619                                         }
620                                 },
621                                 false
622                         );
623                         /*
624                         setTimeout(
625                                 function() {
626                                         util.widgets.dispatch('flesh',treerow);
627                                 }, 0
628                         );
629                         */
630                 }
631                 this.error.sdump('D_LIST',s);
632
633                         try {
634
635                                 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
636                                         // Remove oldest row
637                                         //if (typeof params.to_bottom != 'undefined') 
638                                         if (typeof params.to_top == 'undefined') {
639                                                 treechildren_node.removeChild( treechildren_node.firstChild );
640                                         } else {
641                                                 treechildren_node.removeChild( treechildren_node.lastChild );
642                                         }
643                                 }
644                         } catch(E) {
645                         }
646
647                 setTimeout( function() { obj.auto_retrieve(); }, 0 );
648
649         JSAN.use('util.widgets'); util.widgets.dispatch('select',obj.node);
650
651                 return params;
652         },
653
654         'put_retrieving_label' : function(treerow) {
655                 var obj = this;
656                 try {
657                         /*
658                         var cols_idx = 0;
659                         dump('put_retrieving_label.  columns = ' + js2JSON(obj.columns) + '\n');
660                         while( obj.columns[cols_idx] && obj.columns[cols_idx].hidden && obj.columns[cols_idx].hidden == 'true') {
661                                 dump('\t' + cols_idx);
662                                 var treecell = document.createElement('treecell');
663                                 treerow.appendChild(treecell);
664                                 cols_idx++;
665                         }
666                         */
667                         for (var i = 0; i < obj.columns.length; i++) {
668                         var treecell = document.createElement('treecell'); treecell.setAttribute('label',document.getElementById('offlineStrings').getString('list.row_retrieving'));
669                         treerow.appendChild(treecell);
670                         }
671                         /*
672                         dump('\t' + cols_idx + '\n');
673                         */
674                 } catch(E) {
675                         alert(E);
676                 }
677         },
678
679         'detect_visible' : function() {
680                 var obj = this;
681                 try {
682                         //dump('detect_visible  obj.node = ' + obj.node + '\n');
683                         /* FIXME - this is a hack.. if the implementation of tree changes, this could break */
684                         try {
685                                 var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(obj.node)[1] )[1];
686                                 var curpos = scrollbar.getAttribute('curpos');
687                                 var maxpos = scrollbar.getAttribute('maxpos');
688                                 //alert('curpos = ' + curpos + ' maxpos = ' + maxpos + ' obj.curpos = ' + obj.curpos + ' obj.maxpos = ' + obj.maxpos + '\n');
689                                 if ((curpos != obj.curpos) || (maxpos != obj.maxpos)) {
690                                         if ( obj.auto_retrieve() > 0 ) {
691                                                 obj.curpos = curpos; obj.maxpos = maxpos;
692                                         }
693                                 }
694                         } catch(E) {
695                                 obj.error.sdump('D_XULRUNNER', 'List implementation changed? ' + E);
696                         }
697                 } catch(E) { obj.error.sdump('D_ERROR',E); }
698         },
699
700         'detect_visible_polling' : function() {
701                 try {
702                         //alert('detect_visible_polling');
703                         var obj = this;
704                         obj.detect_visible();
705                         setTimeout(function() { try { obj.detect_visible_polling(); } catch(E) { alert(E); } },2000);
706                 } catch(E) {
707                         alert(E);
708                 }
709         },
710
711
712         'auto_retrieve' : function(params) {
713                 var obj = this;
714                 switch (this.node.nodeName) {
715                         case 'tree' : obj._auto_retrieve_tree(params); break;
716                         default: throw('NYI: Need .auto_retrieve() for ' + obj.node.nodeName); break;
717                 }
718         },
719
720         '_auto_retrieve_tree' : function (params) {
721                 var obj = this;
722                 if (!obj.auto_retrieve_in_progress) {
723                         obj.auto_retrieve_in_progress = true;
724                         setTimeout(
725                                 function() {
726                                         try {
727                                                         //alert('auto_retrieve\n');
728                                                         var count = 0;
729                                                         var startpos = obj.node.treeBoxObject.getFirstVisibleRow();
730                                                         var endpos = obj.node.treeBoxObject.getLastVisibleRow();
731                                                         if (startpos > endpos) endpos = obj.node.treeBoxObject.getPageLength();
732                                                         //dump('startpos = ' + startpos + ' endpos = ' + endpos + '\n');
733                                                         for (var i = startpos; i < endpos + 4; i++) {
734                                                                 try {
735                                                                         //dump('trying index ' + i + '\n');
736                                                                         var item = obj.node.contentView.getItemAtIndex(i).firstChild;
737                                                                         if (item && item.getAttribute('retrieved') != 'true' ) {
738                                                                                 //dump('\tgot an unfleshed item = ' + item + ' = ' + item.nodeName + '\n');
739                                                                                 util.widgets.dispatch('flesh',item); count++;
740                                                                         }
741                                                                 } catch(E) {
742                                                                         //dump(i + ' : ' + E + '\n');
743                                                                 }
744                                                         }
745                                                         obj.auto_retrieve_in_progress = false;
746                                                         return count;
747                                         } catch(E) { alert(E); }
748                                 }, 1
749                         );
750                 }
751         },
752
753     'exec_on_all_fleshed' : function() {
754         var obj = this;
755         try {
756             if (obj.on_all_fleshed) {
757                                 if (typeof obj.on_all_fleshed == 'function') {
758                     dump('exec_on_all_fleshed == function\n');
759                                         setTimeout( 
760                         function() { 
761                             try { obj.on_all_fleshed(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',obj.on_all_fleshed); }
762                         }, 0 
763                     );
764                                 } else if (typeof obj.on_all_fleshed.length != 'undefined') {
765                     dump('exec_on_all_fleshed == array\n');
766                     setTimeout(
767                         function() {
768                             try {
769                                 dump('exec_on_all_fleshed, processing on_all_fleshed array, length = ' + obj.on_all_fleshed.length + '\n');
770                                 var f = obj.on_all_fleshed.pop();
771                                 if (typeof f == 'function') { 
772                                     try { f(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',f); } 
773                                 }
774                                 if (obj.on_all_fleshed.length > 0) arguments.callee(); 
775                             } catch(E) {
776                                 obj.error.standard_unexpected_error_alert('exec_on_all_fleshed callback error',E);
777                             }
778                         }, 0
779                     ); 
780                                 } else {
781                     obj.error.standard_unexpected_error_alert('unexpected on_all_fleshed object: ', obj.on_all_fleshed);
782                 }
783             }
784         } catch(E) {
785             obj.error.standard_unexpected_error_alert('exec_on_all-fleshed error',E);
786         }
787     },
788
789         'full_retrieve' : function(params) {
790                 var obj = this;
791                 switch (this.node.nodeName) {
792                         case 'tree' : obj._full_retrieve_tree(params); break;
793                         default: throw('NYI: Need .full_retrieve() for ' + obj.node.nodeName); break;
794                 }
795         },
796
797         '_full_retrieve_tree' : function(params) {
798                 var obj = this;
799                 try {
800                         if (obj.row_count.fleshed >= obj.row_count.total) {
801                                 dump('Full retrieve... tree seems to be in sync\n' + js2JSON(obj.row_count) + '\n');
802                 obj.exec_on_all_fleshed();
803                         } else {
804                                 dump('Full retrieve... syncing tree' + js2JSON(obj.row_count) + '\n');
805                                 JSAN.use('util.widgets');
806                                 var nodes = obj.treechildren.childNodes;
807                                 for (var i = 0; i < nodes.length; i++) {
808                                         util.widgets.dispatch('flesh',nodes[i].firstChild);
809                                 }
810                         }
811                 } catch(E) {
812                         obj.error.standard_unexpected_error_alert('_full_retrieve_tree',E);
813                 }
814         },
815
816         '_append_to_listbox' : function (params) {
817
818                 var obj = this;
819
820                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
821
822                 var s = ('util.list.append: params = ' + (params) + '\n');
823
824                 var listitem = document.createElement('listitem');
825
826                 s += ('listbox = ' + this.node + '  listitem = ' + listitem + '\n');
827
828                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
829
830                         setTimeout(
831                                 function() {
832                                         listitem.setAttribute('retrieve_id',params.retrieve_id);
833                                         //FIXME//Make async and fire when row is visible in list
834                                         var row;
835
836                                         params.row_node = listitem;
837                                         params.on_retrieve = function(row) {
838                                                 params.row = row;
839                                                 obj._map_row_to_listcell(params,listitem);
840                                                 obj.node.appendChild( listitem );
841                                                 util.widgets.dispatch('select',obj.node);
842                                         }
843
844                                         if (typeof params.retrieve_row == 'function') {
845
846                                                 row = params.retrieve_row( params );
847
848                                         } else {
849
850                                                 if (typeof obj.retrieve_row == 'function') {
851
852                                                         row = obj.retrieve_row( params );
853
854                                                 }
855                                         }
856                                 }, 0
857                         );
858                 } else {
859                         this._map_row_to_listcell(params,listitem);
860                         this.node.appendChild( listitem );
861                 }
862
863                 this.error.sdump('D_LIST',s);
864                 params.my_node = listitem;
865                 return params;
866
867         },
868
869         '_map_row_to_treecell' : function(params,treerow) {
870                 var obj = this;
871                 var s = '';
872                 util.widgets.remove_children(treerow);
873
874                 if (typeof params.map_row_to_column == 'function' || typeof this.map_row_to_column == 'function') {
875
876                         for (var i = 0; i < this.columns.length; i++) {
877                                 var treecell = document.createElement('treecell');
878                                 var label = '';
879                                 if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
880                                         treecell.setAttribute('label',label);
881                                         treerow.appendChild( treecell );
882                                         s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
883                                         continue;
884                                 }
885                                 if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
886                                         treecell.setAttribute('label',label);
887                                         treerow.appendChild( treecell );
888                                         s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
889                                         continue;
890                                 }
891         
892                                 if (typeof params.map_row_to_column == 'function')  {
893         
894                                         label = params.map_row_to_column(params.row,this.columns[i]);
895         
896                                 } else if (typeof this.map_row_to_column == 'function') {
897         
898                                         label = this.map_row_to_column(params.row,this.columns[i]);
899         
900                                 }
901                                 treecell.setAttribute('label',label ? label : '');
902                                 treerow.appendChild( treecell );
903                                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
904                         }
905                 } else if (typeof params.map_row_to_columns == 'function' || typeof this.map_row_to_columns == 'function') {
906
907                         var labels = [];
908
909                         if (typeof params.map_row_to_columns == 'function') {
910
911                                 labels = params.map_row_to_columns(params.row,this.columns);
912
913                         } else if (typeof this.map_row_to_columns == 'function') {
914
915                                 labels = this.map_row_to_columns(params.row,this.columns);
916
917                         }
918                         for (var i = 0; i < labels.length; i++) {
919                                 var treecell = document.createElement('treecell');
920                                 treecell.setAttribute('label',typeof labels[i] == 'string' || typeof labels[i] == 'number' ? labels[i] : '');
921                                 treerow.appendChild( treecell );
922                                 s += ('treecell = ' + treecell + ' with label = ' + labels[i] + '\n');
923                         }
924
925                 } else {
926
927                         throw('No row to column mapping function.');
928                 }
929                 this.error.sdump('D_LIST',s);
930         },
931
932         '_map_row_to_listcell' : function(params,listitem) {
933                 var obj = this;
934                 var s = '';
935                 for (var i = 0; i < this.columns.length; i++) {
936                         var value = '';
937                         if (typeof params.map_row_to_column == 'function')  {
938
939                                 value = params.map_row_to_column(params.row,this.columns[i]);
940
941                         } else {
942
943                                 if (typeof this.map_row_to_column == 'function') {
944
945                                         value = this.map_row_to_column(params.row,this.columns[i]);
946                                 }
947                         }
948                         if (typeof value == 'string' || typeof value == 'number') {
949                                 var listcell = document.createElement('listcell');
950                                 listcell.setAttribute('label',value);
951                                 listitem.appendChild(listcell);
952                                 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
953                         } else {
954                                 listitem.appendChild(value);
955                                 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
956                         }
957                 }
958                 this.error.sdump('D_LIST',s);
959         },
960
961         'select_all' : function(params) {
962                 var obj = this;
963                 switch(this.node.nodeName) {
964                         case 'tree' : return this._select_all_from_tree(params); break;
965                         default: throw('NYI: Need ._select_all_from_() for ' + this.node.nodeName); break;
966                 }
967         },
968
969         '_select_all_from_tree' : function(params) {
970                 var obj = this;
971                 this.node.view.selection.selectAll();
972         },
973
974         'retrieve_selection' : function(params) {
975                 var obj = this;
976                 switch(this.node.nodeName) {
977                         case 'tree' : return this._retrieve_selection_from_tree(params); break;
978                         default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
979                 }
980         },
981
982         '_retrieve_selection_from_tree' : function(params) {
983                 var obj = this;
984                 var list = [];
985                 var start = new Object();
986                 var end = new Object();
987                 var numRanges = this.node.view.selection.getRangeCount();
988                 for (var t=0; t<numRanges; t++){
989                         this.node.view.selection.getRangeAt(t,start,end);
990                         for (var v=start.value; v<=end.value; v++){
991                                 var i = this.node.contentView.getItemAtIndex(v);
992                                 list.push( i );
993                         }
994                 }
995                 return list;
996         },
997
998         'dump' : function(params) {
999                 var obj = this;
1000                 switch(this.node.nodeName) {
1001                         case 'tree' : return this._dump_tree(params); break;
1002                         default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
1003                 }
1004         },
1005
1006         '_dump_tree' : function(params) {
1007                 var obj = this;
1008                 var dump = [];
1009                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1010                         var row = [];
1011                         var treeitem = this.treechildren.childNodes[i];
1012                         var treerow = treeitem.firstChild;
1013                         for (var j = 0; j < treerow.childNodes.length; j++) {
1014                                 row.push( treerow.childNodes[j].getAttribute('label') );
1015                         }
1016                         dump.push( row );
1017                 }
1018                 return dump;
1019         },
1020
1021         'dump_with_keys' : function(params) {
1022                 var obj = this;
1023                 switch(this.node.nodeName) {
1024                         case 'tree' : return this._dump_tree_with_keys(params); break;
1025                         default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
1026                 }
1027
1028         },
1029
1030         '_dump_tree_with_keys' : function(params) {
1031                 var obj = this;
1032                 var dump = [];
1033                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1034                         var row = {};
1035                         var treeitem = this.treechildren.childNodes[i];
1036                         var treerow = treeitem.firstChild;
1037                         for (var j = 0; j < treerow.childNodes.length; j++) {
1038                                 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
1039                         }
1040                         dump.push( row );
1041                 }
1042                 return dump;
1043         },
1044
1045         'dump_csv' : function(params) {
1046                 var obj = this;
1047                 switch(this.node.nodeName) {
1048                         case 'tree' : return this._dump_tree_csv(params); break;
1049                         default: throw('NYI: Need .dump_csv() for ' + this.node.nodeName); break;
1050                 }
1051
1052         },
1053
1054         '_dump_tree_csv' : function(params) {
1055                 var obj = this;
1056                 var dump = '';
1057                 for (var j = 0; j < obj.columns.length; j++) {
1058                         if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1059                                 /* skip */
1060                         } else {
1061                                 if (dump) dump += ',';
1062                                 dump += '"' + obj.columns[j].label.replace(/"/g, '""') + '"';
1063                         }
1064                 }
1065                 dump += '\r\n';
1066                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1067                         var row = '';
1068                         var treeitem = this.treechildren.childNodes[i];
1069                         var treerow = treeitem.firstChild;
1070                         for (var j = 0; j < treerow.childNodes.length; j++) {
1071                                 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1072                                         /* skip */
1073                                 } else {
1074                                         if (row) row += ',';
1075                                         row += '"' + treerow.childNodes[j].getAttribute('label').replace(/"/g, '""') + '"';
1076                                 }
1077                         }
1078                         dump +=  row + '\r\n';
1079                 }
1080                 return dump;
1081         },
1082
1083     'dump_csv_to_clipboard' : function(params) {
1084         var obj = this;
1085         if (typeof params == 'undefined') params = {};
1086         if (params.no_full_retrieve) {
1087             copy_to_clipboard( obj.dump_csv( params ) );
1088         } else {
1089             obj.wrap_in_full_retrieve( function() { copy_to_clipboard( obj.dump_csv( params ) ); } );
1090         }
1091     },
1092
1093     'dump_csv_to_printer' : function(params) {
1094         var obj = this;
1095         JSAN.use('util.print'); var print = new util.print();
1096         if (typeof params == 'undefined') params = {};
1097         if (params.no_full_retrieve) {
1098             print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1099         } else {
1100             obj.wrap_in_full_retrieve( 
1101                 function() { 
1102                     print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1103                 }
1104             );
1105         }
1106     },
1107
1108     'dump_csv_to_file' : function(params) {
1109         var obj = this;
1110         JSAN.use('util.file'); var f = new util.file();
1111         if (typeof params == 'undefined') params = {};
1112         if (params.no_full_retrieve) {
1113             params.data = obj.dump_csv( params );
1114             params.not_json = true;
1115             if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1116             f.export_file( params );
1117         } else {
1118             obj.wrap_in_full_retrieve( 
1119                 function() { 
1120                     params.data = obj.dump_csv( params );
1121                     params.not_json = true;
1122                     if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1123                     f.export_file( params );
1124                 }
1125             );
1126         }
1127     },
1128
1129     'print' : function(params) {
1130         if (!params) params = {};
1131                 switch(this.node.nodeName) {
1132                         case 'tree' : return this._print_tree(params); break;
1133                         default: throw('NYI: Need ._print() for ' + this.node.nodeName); break;
1134                 }
1135     },
1136
1137     '_print_tree' : function(params) {
1138         var obj = this;
1139         try {
1140                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
1141             if (!params.staff && data.list.au && data.list.au[0]) {
1142                 params.staff = data.list.au[0];
1143             }
1144             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() ]) {
1145                 params.lib = data.hash.aou[ data.list.au[0].ws_ou() ];
1146                 params.lib.children(null);
1147             }
1148             if (params.template && data.print_list_templates[ params.template ]) {
1149                 var template = data.print_list_templates[ params.template ];
1150                 for (var i in template) params[i] = template[i];
1151             }
1152             obj.wrap_in_full_retrieve(
1153                 function() {
1154                     try {
1155                         if (!params.list) params.list = obj.dump_with_keys();
1156                         JSAN.use('util.print'); var print = new util.print();
1157                         print.tree_list( params );
1158                         if (typeof params.callback == 'function') params.callback();
1159                     } catch(E) {
1160                                     obj.error.standard_unexpected_error_alert('inner _print_tree',E);
1161                     }
1162                 }
1163             );
1164             
1165         } catch(E) {
1166                         obj.error.standard_unexpected_error_alert('_print_tree',E);
1167         }
1168     },
1169
1170         'dump_selected_with_keys' : function(params) {
1171                 var obj = this;
1172                 switch(this.node.nodeName) {
1173                         case 'tree' : return this._dump_tree_selection_with_keys(params); break;
1174                         default: throw('NYI: Need .dump_selection_with_keys() for ' + this.node.nodeName); break;
1175                 }
1176
1177         },
1178
1179         '_dump_tree_selection_with_keys' : function(params) {
1180                 var obj = this;
1181                 var dump = [];
1182                 var list = obj._retrieve_selection_from_tree();
1183                 for (var i = 0; i < list.length; i++) {
1184                         var row = {};
1185                         var treeitem = list[i];
1186                         var treerow = treeitem.firstChild;
1187                         for (var j = 0; j < treerow.childNodes.length; j++) {
1188                                 var value = treerow.childNodes[j].getAttribute('label');
1189                                 if (params.skip_hidden_columns) if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') continue;
1190                                 var id = obj.columns[j].id; if (params.labels_instead_of_ids) id = obj.columns[j].label;
1191                                 row[ id ] = value;
1192                         }
1193                         dump.push( row );
1194                 }
1195                 return dump;
1196         },
1197
1198         'clipboard' : function(params) {
1199                 try {
1200                         var obj = this;
1201                         var dump = obj.dump_selected_with_keys({'skip_hidden_columns':true,'labels_instead_of_ids':true});
1202                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
1203                         data.list_clipboard = dump; data.stash('list_clipboard');
1204                         JSAN.use('util.window'); var win = new util.window();
1205                         win.open(urls.XUL_LIST_CLIPBOARD,'list_clipboard','chrome,resizable,modal');
1206             window.focus(); // sometimes the main window will lower after a clipboard action
1207                 } catch(E) {
1208                         this.error.standard_unexpected_error_alert('clipboard',E);
1209                 }
1210         },
1211
1212         'dump_retrieve_ids' : function(params) {
1213                 var obj = this;
1214                 switch(this.node.nodeName) {
1215                         case 'tree' : return this._dump_retrieve_ids_tree(params); break;
1216                         default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
1217                 }
1218         },
1219
1220         '_dump_retrieve_ids_tree' : function(params) {
1221                 var obj = this;
1222                 var dump = [];
1223                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1224                         var treeitem = this.treechildren.childNodes[i];
1225                         dump.push( treeitem.getAttribute('retrieve_id') );
1226                 }
1227                 return dump;
1228         },
1229
1230     'wrap_in_full_retrieve' : function(f) {
1231         var obj = this;
1232                 if (typeof obj.on_all_fleshed == 'function') { // legacy
1233             obj.on_all_fleshed = [ obj.on_all_fleshed ];
1234                 }
1235         if (! obj.on_all_fleshed) obj.on_all_fleshed = [];
1236         obj.on_all_fleshed.push(f);
1237         obj.full_retrieve();
1238     },
1239
1240         '_sort_tree' : function(col,sortDir) {
1241                 var obj = this;
1242                 try {
1243                         if (obj.node.getAttribute('no_sort')) {
1244                                 return;
1245                         }
1246                         var col_pos;
1247                         for (var i = 0; i < obj.columns.length; i++) { 
1248                                 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i); 
1249                         }
1250             obj.wrap_in_full_retrieve(
1251                 function() {
1252                                         try {
1253                                                 JSAN.use('util.money');
1254                                                 var rows = [];
1255                                                 var treeitems = obj.treechildren.childNodes;
1256                                                 for (var i = 0; i < treeitems.length; i++) {
1257                                                         var treeitem = treeitems[i];
1258                                                         var treerow = treeitem.firstChild;
1259                                                         var treecell = treerow.childNodes[ col_pos ];
1260                                                         value = ( { 'value' : treecell ? treecell.getAttribute('label') : '', 'node' : treeitem } );
1261                                                         rows.push( value );
1262                                                 }
1263                                                 rows = rows.sort( function(a,b) { 
1264                                                         a = a.value; b = b.value; 
1265                                                         if (col.getAttribute('sort_type')) {
1266                                                                 switch(col.getAttribute('sort_type')) {
1267                                                                         case 'number' :
1268                                                                                 a = Number(a); b = Number(b);
1269                                                                         break;
1270                                                                         case 'money' :
1271                                                                                 a = util.money.dollars_float_to_cents_integer(a);
1272                                                                                 b = util.money.dollars_float_to_cents_integer(b);
1273                                                                         break;
1274                                                                         case 'title' : /* special case for "a" and "the".  doesn't use marc 245 indicator */
1275                                                                                 a = String( a ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1276                                                                                 b = String( b ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1277                                                                         break;
1278                                                                         default:
1279                                                                                 a = String( a ).toUpperCase();
1280                                                                                 b = String( b ).toUpperCase();
1281                                                                         break;
1282                                                                 }
1283                                                         } else {
1284                                                                 if (typeof a == 'string' || typeof b == 'string') {
1285                                                                         a = String( a ).toUpperCase();
1286                                                                         b = String( b ).toUpperCase();
1287                                                                 }
1288                                                         }
1289                                                         if (a < b) return -1; 
1290                                                         if (a > b) return 1; 
1291                                                         return 0; 
1292                                                 } );
1293                                                 if (sortDir == 'asc') rows = rows.reverse();
1294                                                 while(obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
1295                                                 for (var i = 0; i < rows.length; i++) {
1296                                                         obj.treechildren.appendChild( rows[i].node );
1297                                                 }
1298                                         } catch(E) {
1299                                                 obj.error.standard_unexpected_error_alert('sorting',E); 
1300                                         }
1301                                 }
1302             );
1303                 } catch(E) {
1304                         obj.error.standard_unexpected_error_alert('pre sorting', E);
1305                 }
1306         },
1307
1308         'render_list_actions' : function(params) {
1309                 var obj = this;
1310                 switch(this.node.nodeName) {
1311                         case 'tree' : return this._render_list_actions_for_tree(params); break;
1312                         default: throw('NYI: Need ._render_list_actions() for ' + this.node.nodeName); break;
1313                 }
1314         },
1315
1316     '_render_list_actions_for_tree' : function(params) {
1317         var obj = this;
1318         try {
1319             var btn = document.createElement('button');
1320             btn.setAttribute('id',obj.node.id + '_list_actions');
1321             btn.setAttribute('type','menu');
1322             btn.setAttribute('allowevents','true');
1323             //btn.setAttribute('oncommand','this.firstChild.showPopup();');
1324             btn.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.menu.label'));
1325             btn.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.menu.accesskey'));
1326             var mp = document.createElement('menupopup');
1327             btn.appendChild(mp);
1328             var mi = document.createElement('menuitem');
1329             mi.setAttribute('id',obj.node.id + '_clipfield');
1330             mi.setAttribute('disabled','true');
1331             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.label'));
1332             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.accesskey'));
1333             mp.appendChild(mi);
1334             mi = document.createElement('menuitem');
1335             mi.setAttribute('id',obj.node.id + '_csv_to_clipboard');
1336             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.label'));
1337             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.accesskey'));
1338             mp.appendChild(mi);
1339             mi = document.createElement('menuitem');
1340             mi.setAttribute('id',obj.node.id + '_csv_to_printer');
1341             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.label'));
1342             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.accesskey'));
1343             mp.appendChild(mi);
1344             mi = document.createElement('menuitem');
1345             mi.setAttribute('id',obj.node.id + '_csv_to_file');
1346             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.label'));
1347             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.accesskey'));
1348             mp.appendChild(mi);
1349             mi = document.createElement('menuitem');
1350             mi.setAttribute('id',obj.node.id + '_save_columns');
1351             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.label'));
1352             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.accesskey'));
1353             mp.appendChild(mi);
1354             return btn;
1355         } catch(E) {
1356             obj.error.standard_unexpected_error_alert('rendering list actions',E);
1357         }
1358     },
1359
1360         'set_list_actions' : function(params) {
1361                 var obj = this;
1362                 switch(this.node.nodeName) {
1363                         case 'tree' : return this._set_list_actions_for_tree(params); break;
1364                         default: throw('NYI: Need ._set_list_actions() for ' + this.node.nodeName); break;
1365                 }
1366         },
1367
1368     '_set_list_actions_for_tree' : function(params) {
1369         // This should be called after the button element from render_list_actions has been appended to the DOM
1370         var obj = this;
1371         try {
1372             var x = document.getElementById(obj.node.id + '_clipfield');
1373             if (x) {
1374                 x.addEventListener(
1375                     'command',
1376                     function() {
1377                         obj.clipboard(params);
1378                         if (params && typeof params.on_complete == 'function') {
1379                             params.on_complete(params);
1380                         }
1381                     },
1382                     false
1383                 );
1384             }
1385             x = document.getElementById(obj.node.id + '_csv_to_clipboard');
1386             if (x) {
1387                 x.addEventListener(
1388                     'command',
1389                     function() {
1390                         obj.dump_csv_to_clipboard(params);
1391                         if (params && typeof params.on_complete == 'function') {
1392                             params.on_complete(params);
1393                         }
1394                     },
1395                     false
1396                 );
1397             }
1398             x = document.getElementById(obj.node.id + '_csv_to_printer');
1399             if (x) {
1400                 x.addEventListener(
1401                     'command',
1402                     function() {
1403                         obj.dump_csv_to_printer(params);
1404                         if (params && typeof params.on_complete == 'function') {
1405                             params.on_complete(params);
1406                         }
1407                     },
1408                     false
1409                 );
1410             }
1411             x = document.getElementById(obj.node.id + '_csv_to_file');
1412             if (x) {
1413                 x.addEventListener(
1414                     'command',
1415                     function() {
1416                         obj.dump_csv_to_file(params);
1417                         if (params && typeof params.on_complete == 'function') {
1418                             params.on_complete(params);
1419                         }
1420                     },
1421                     false
1422                 );
1423             }
1424             x = document.getElementById(obj.node.id + '_save_columns');
1425             if (x) {
1426                 x.addEventListener(
1427                     'command',
1428                     function() {
1429                         obj.save_columns(params);
1430                         if (params && typeof params.on_complete == 'function') {
1431                             params.on_complete(params);
1432                         }
1433                     },
1434                     false
1435                 );
1436             }
1437
1438         } catch(E) {
1439             obj.error.standard_unexpected_error_alert('setting list actions',E);
1440         }
1441     }
1442
1443 }
1444 dump('exiting util.list.js\n');