]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/list.js
Support editable trees, which is needed if we want a usable checkbox column in a...
[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         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                 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
879                                 var label = '';
880                                 if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
881                                         treecell.setAttribute('label',label);
882                                         treerow.appendChild( treecell );
883                                         s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
884                                         continue;
885                                 }
886                                 if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
887                                         treecell.setAttribute('label',label);
888                                         treerow.appendChild( treecell );
889                                         s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
890                                         continue;
891                                 }
892         
893                                 if (typeof params.map_row_to_column == 'function')  {
894         
895                                         label = params.map_row_to_column(params.row,this.columns[i]);
896         
897                                 } else if (typeof this.map_row_to_column == 'function') {
898         
899                                         label = this.map_row_to_column(params.row,this.columns[i]);
900         
901                                 }
902                                 if (this.columns[i].type == 'checkbox') { treecell.setAttribute('value',label); } else { treecell.setAttribute('label',label ? label : ''); }
903                                 treerow.appendChild( treecell );
904                                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
905                         }
906                 } else if (typeof params.map_row_to_columns == 'function' || typeof this.map_row_to_columns == 'function') {
907
908                         var labels = [];
909
910                         if (typeof params.map_row_to_columns == 'function') {
911
912                                 labels = params.map_row_to_columns(params.row,this.columns);
913
914                         } else if (typeof this.map_row_to_columns == 'function') {
915
916                                 labels = this.map_row_to_columns(params.row,this.columns);
917
918                         }
919                         for (var i = 0; i < labels.length; i++) {
920                                 var treecell = document.createElement('treecell');
921                 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
922                 if ( this.columns[i].type == 'checkbox') {
923                     treecell.setAttribute('value', labels[i]);
924                 } else {
925                                     treecell.setAttribute('label',typeof labels[i] == 'string' || typeof labels[i] == 'number' ? labels[i] : '');
926                 }
927                                 treerow.appendChild( treecell );
928                                 s += ('treecell = ' + treecell + ' with label = ' + labels[i] + '\n');
929                         }
930
931                 } else {
932
933                         throw('No row to column mapping function.');
934                 }
935                 this.error.sdump('D_LIST',s);
936         },
937
938         '_map_row_to_listcell' : function(params,listitem) {
939                 var obj = this;
940                 var s = '';
941                 for (var i = 0; i < this.columns.length; i++) {
942                         var value = '';
943                         if (typeof params.map_row_to_column == 'function')  {
944
945                                 value = params.map_row_to_column(params.row,this.columns[i]);
946
947                         } else {
948
949                                 if (typeof this.map_row_to_column == 'function') {
950
951                                         value = this.map_row_to_column(params.row,this.columns[i]);
952                                 }
953                         }
954                         if (typeof value == 'string' || typeof value == 'number') {
955                                 var listcell = document.createElement('listcell');
956                                 listcell.setAttribute('label',value);
957                                 listitem.appendChild(listcell);
958                                 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
959                         } else {
960                                 listitem.appendChild(value);
961                                 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
962                         }
963                 }
964                 this.error.sdump('D_LIST',s);
965         },
966
967         'select_all' : function(params) {
968                 var obj = this;
969                 switch(this.node.nodeName) {
970                         case 'tree' : return this._select_all_from_tree(params); break;
971                         default: throw('NYI: Need ._select_all_from_() for ' + this.node.nodeName); break;
972                 }
973         },
974
975         '_select_all_from_tree' : function(params) {
976                 var obj = this;
977                 this.node.view.selection.selectAll();
978         },
979
980         'retrieve_selection' : function(params) {
981                 var obj = this;
982                 switch(this.node.nodeName) {
983                         case 'tree' : return this._retrieve_selection_from_tree(params); break;
984                         default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
985                 }
986         },
987
988         '_retrieve_selection_from_tree' : function(params) {
989                 var obj = this;
990                 var list = [];
991                 var start = new Object();
992                 var end = new Object();
993                 var numRanges = this.node.view.selection.getRangeCount();
994                 for (var t=0; t<numRanges; t++){
995                         this.node.view.selection.getRangeAt(t,start,end);
996                         for (var v=start.value; v<=end.value; v++){
997                                 var i = this.node.contentView.getItemAtIndex(v);
998                                 list.push( i );
999                         }
1000                 }
1001                 return list;
1002         },
1003
1004         'dump' : function(params) {
1005                 var obj = this;
1006                 switch(this.node.nodeName) {
1007                         case 'tree' : return this._dump_tree(params); break;
1008                         default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
1009                 }
1010         },
1011
1012         '_dump_tree' : function(params) {
1013                 var obj = this;
1014                 var dump = [];
1015                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1016                         var row = [];
1017                         var treeitem = this.treechildren.childNodes[i];
1018                         var treerow = treeitem.firstChild;
1019                         for (var j = 0; j < treerow.childNodes.length; j++) {
1020                                 row.push( treerow.childNodes[j].getAttribute('label') );
1021                         }
1022                         dump.push( row );
1023                 }
1024                 return dump;
1025         },
1026
1027         'dump_with_keys' : function(params) {
1028                 var obj = this;
1029                 switch(this.node.nodeName) {
1030                         case 'tree' : return this._dump_tree_with_keys(params); break;
1031                         default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
1032                 }
1033
1034         },
1035
1036         '_dump_tree_with_keys' : function(params) {
1037                 var obj = this;
1038                 var dump = [];
1039                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1040                         var row = {};
1041                         var treeitem = this.treechildren.childNodes[i];
1042                         var treerow = treeitem.firstChild;
1043                         for (var j = 0; j < treerow.childNodes.length; j++) {
1044                                 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
1045                         }
1046                         dump.push( row );
1047                 }
1048                 return dump;
1049         },
1050
1051         'dump_csv' : function(params) {
1052                 var obj = this;
1053                 switch(this.node.nodeName) {
1054                         case 'tree' : return this._dump_tree_csv(params); break;
1055                         default: throw('NYI: Need .dump_csv() for ' + this.node.nodeName); break;
1056                 }
1057
1058         },
1059
1060         '_dump_tree_csv' : function(params) {
1061                 var obj = this;
1062                 var dump = '';
1063                 for (var j = 0; j < obj.columns.length; j++) {
1064                         if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1065                                 /* skip */
1066                         } else {
1067                                 if (dump) dump += ',';
1068                                 dump += '"' + obj.columns[j].label.replace(/"/g, '""') + '"';
1069                         }
1070                 }
1071                 dump += '\r\n';
1072                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1073                         var row = '';
1074                         var treeitem = this.treechildren.childNodes[i];
1075                         var treerow = treeitem.firstChild;
1076                         for (var j = 0; j < treerow.childNodes.length; j++) {
1077                                 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1078                                         /* skip */
1079                                 } else {
1080                                         if (row) row += ',';
1081                                         row += '"' + treerow.childNodes[j].getAttribute('label').replace(/"/g, '""') + '"';
1082                                 }
1083                         }
1084                         dump +=  row + '\r\n';
1085                 }
1086                 return dump;
1087         },
1088
1089     'dump_csv_to_clipboard' : function(params) {
1090         var obj = this;
1091         if (typeof params == 'undefined') params = {};
1092         if (params.no_full_retrieve) {
1093             copy_to_clipboard( obj.dump_csv( params ) );
1094         } else {
1095             obj.wrap_in_full_retrieve( function() { copy_to_clipboard( obj.dump_csv( params ) ); } );
1096         }
1097     },
1098
1099     'dump_csv_to_printer' : function(params) {
1100         var obj = this;
1101         JSAN.use('util.print'); var print = new util.print();
1102         if (typeof params == 'undefined') params = {};
1103         if (params.no_full_retrieve) {
1104             print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1105         } else {
1106             obj.wrap_in_full_retrieve( 
1107                 function() { 
1108                     print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1109                 }
1110             );
1111         }
1112     },
1113
1114     'dump_csv_to_file' : function(params) {
1115         var obj = this;
1116         JSAN.use('util.file'); var f = new util.file();
1117         if (typeof params == 'undefined') params = {};
1118         if (params.no_full_retrieve) {
1119             params.data = obj.dump_csv( params );
1120             params.not_json = true;
1121             if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1122             f.export_file( params );
1123         } else {
1124             obj.wrap_in_full_retrieve( 
1125                 function() { 
1126                     params.data = obj.dump_csv( params );
1127                     params.not_json = true;
1128                     if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1129                     f.export_file( params );
1130                 }
1131             );
1132         }
1133     },
1134
1135     'print' : function(params) {
1136         if (!params) params = {};
1137                 switch(this.node.nodeName) {
1138                         case 'tree' : return this._print_tree(params); break;
1139                         default: throw('NYI: Need ._print() for ' + this.node.nodeName); break;
1140                 }
1141     },
1142
1143     '_print_tree' : function(params) {
1144         var obj = this;
1145         try {
1146                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
1147             if (!params.staff && data.list.au && data.list.au[0]) {
1148                 params.staff = data.list.au[0];
1149             }
1150             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() ]) {
1151                 params.lib = data.hash.aou[ data.list.au[0].ws_ou() ];
1152                 params.lib.children(null);
1153             }
1154             if (params.template && data.print_list_templates[ params.template ]) {
1155                 var template = data.print_list_templates[ params.template ];
1156                 for (var i in template) params[i] = template[i];
1157             }
1158             obj.wrap_in_full_retrieve(
1159                 function() {
1160                     try {
1161                         if (!params.list) params.list = obj.dump_with_keys();
1162                         JSAN.use('util.print'); var print = new util.print();
1163                         print.tree_list( params );
1164                         if (typeof params.callback == 'function') params.callback();
1165                     } catch(E) {
1166                                     obj.error.standard_unexpected_error_alert('inner _print_tree',E);
1167                     }
1168                 }
1169             );
1170             
1171         } catch(E) {
1172                         obj.error.standard_unexpected_error_alert('_print_tree',E);
1173         }
1174     },
1175
1176         'dump_selected_with_keys' : function(params) {
1177                 var obj = this;
1178                 switch(this.node.nodeName) {
1179                         case 'tree' : return this._dump_tree_selection_with_keys(params); break;
1180                         default: throw('NYI: Need .dump_selection_with_keys() for ' + this.node.nodeName); break;
1181                 }
1182
1183         },
1184
1185         '_dump_tree_selection_with_keys' : function(params) {
1186                 var obj = this;
1187                 var dump = [];
1188                 var list = obj._retrieve_selection_from_tree();
1189                 for (var i = 0; i < list.length; i++) {
1190                         var row = {};
1191                         var treeitem = list[i];
1192                         var treerow = treeitem.firstChild;
1193                         for (var j = 0; j < treerow.childNodes.length; j++) {
1194                                 var value = treerow.childNodes[j].getAttribute('label');
1195                                 if (params.skip_hidden_columns) if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') continue;
1196                                 var id = obj.columns[j].id; if (params.labels_instead_of_ids) id = obj.columns[j].label;
1197                                 row[ id ] = value;
1198                         }
1199                         dump.push( row );
1200                 }
1201                 return dump;
1202         },
1203
1204         'clipboard' : function(params) {
1205                 try {
1206                         var obj = this;
1207                         var dump = obj.dump_selected_with_keys({'skip_hidden_columns':true,'labels_instead_of_ids':true});
1208                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
1209                         data.list_clipboard = dump; data.stash('list_clipboard');
1210                         JSAN.use('util.window'); var win = new util.window();
1211                         win.open(urls.XUL_LIST_CLIPBOARD,'list_clipboard','chrome,resizable,modal');
1212             window.focus(); // sometimes the main window will lower after a clipboard action
1213                 } catch(E) {
1214                         this.error.standard_unexpected_error_alert('clipboard',E);
1215                 }
1216         },
1217
1218         'dump_retrieve_ids' : function(params) {
1219                 var obj = this;
1220                 switch(this.node.nodeName) {
1221                         case 'tree' : return this._dump_retrieve_ids_tree(params); break;
1222                         default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
1223                 }
1224         },
1225
1226         '_dump_retrieve_ids_tree' : function(params) {
1227                 var obj = this;
1228                 var dump = [];
1229                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1230                         var treeitem = this.treechildren.childNodes[i];
1231                         dump.push( treeitem.getAttribute('retrieve_id') );
1232                 }
1233                 return dump;
1234         },
1235
1236     'wrap_in_full_retrieve' : function(f) {
1237         var obj = this;
1238                 if (typeof obj.on_all_fleshed == 'function') { // legacy
1239             obj.on_all_fleshed = [ obj.on_all_fleshed ];
1240                 }
1241         if (! obj.on_all_fleshed) obj.on_all_fleshed = [];
1242         obj.on_all_fleshed.push(f);
1243         obj.full_retrieve();
1244     },
1245
1246         '_sort_tree' : function(col,sortDir) {
1247                 var obj = this;
1248                 try {
1249                         if (obj.node.getAttribute('no_sort')) {
1250                                 return;
1251                         }
1252                         var col_pos;
1253                         for (var i = 0; i < obj.columns.length; i++) { 
1254                                 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i); 
1255                         }
1256             obj.wrap_in_full_retrieve(
1257                 function() {
1258                                         try {
1259                                                 JSAN.use('util.money');
1260                                                 var rows = [];
1261                                                 var treeitems = obj.treechildren.childNodes;
1262                                                 for (var i = 0; i < treeitems.length; i++) {
1263                                                         var treeitem = treeitems[i];
1264                                                         var treerow = treeitem.firstChild;
1265                                                         var treecell = treerow.childNodes[ col_pos ];
1266                                                         value = ( { 'value' : treecell ? treecell.getAttribute('label') : '', 'node' : treeitem } );
1267                                                         rows.push( value );
1268                                                 }
1269                                                 rows = rows.sort( function(a,b) { 
1270                                                         a = a.value; b = b.value; 
1271                                                         if (col.getAttribute('sort_type')) {
1272                                                                 switch(col.getAttribute('sort_type')) {
1273                                                                         case 'number' :
1274                                                                                 a = Number(a); b = Number(b);
1275                                                                         break;
1276                                                                         case 'money' :
1277                                                                                 a = util.money.dollars_float_to_cents_integer(a);
1278                                                                                 b = util.money.dollars_float_to_cents_integer(b);
1279                                                                         break;
1280                                                                         case 'title' : /* special case for "a" and "the".  doesn't use marc 245 indicator */
1281                                                                                 a = String( a ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1282                                                                                 b = String( b ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1283                                                                         break;
1284                                                                         default:
1285                                                                                 a = String( a ).toUpperCase();
1286                                                                                 b = String( b ).toUpperCase();
1287                                                                         break;
1288                                                                 }
1289                                                         } else {
1290                                                                 if (typeof a == 'string' || typeof b == 'string') {
1291                                                                         a = String( a ).toUpperCase();
1292                                                                         b = String( b ).toUpperCase();
1293                                                                 }
1294                                                         }
1295                                                         if (a < b) return -1; 
1296                                                         if (a > b) return 1; 
1297                                                         return 0; 
1298                                                 } );
1299                                                 if (sortDir == 'asc') rows = rows.reverse();
1300                                                 while(obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
1301                                                 for (var i = 0; i < rows.length; i++) {
1302                                                         obj.treechildren.appendChild( rows[i].node );
1303                                                 }
1304                                         } catch(E) {
1305                                                 obj.error.standard_unexpected_error_alert('sorting',E); 
1306                                         }
1307                                 }
1308             );
1309                 } catch(E) {
1310                         obj.error.standard_unexpected_error_alert('pre sorting', E);
1311                 }
1312         },
1313
1314         'render_list_actions' : function(params) {
1315                 var obj = this;
1316                 switch(this.node.nodeName) {
1317                         case 'tree' : return this._render_list_actions_for_tree(params); break;
1318                         default: throw('NYI: Need ._render_list_actions() for ' + this.node.nodeName); break;
1319                 }
1320         },
1321
1322     '_render_list_actions_for_tree' : function(params) {
1323         var obj = this;
1324         try {
1325             var btn = document.createElement('button');
1326             btn.setAttribute('id',obj.node.id + '_list_actions');
1327             btn.setAttribute('type','menu');
1328             btn.setAttribute('allowevents','true');
1329             //btn.setAttribute('oncommand','this.firstChild.showPopup();');
1330             btn.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.menu.label'));
1331             btn.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.menu.accesskey'));
1332             var mp = document.createElement('menupopup');
1333             btn.appendChild(mp);
1334             var mi = document.createElement('menuitem');
1335             mi.setAttribute('id',obj.node.id + '_clipfield');
1336             mi.setAttribute('disabled','true');
1337             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.label'));
1338             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.accesskey'));
1339             mp.appendChild(mi);
1340             mi = document.createElement('menuitem');
1341             mi.setAttribute('id',obj.node.id + '_csv_to_clipboard');
1342             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.label'));
1343             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.accesskey'));
1344             mp.appendChild(mi);
1345             mi = document.createElement('menuitem');
1346             mi.setAttribute('id',obj.node.id + '_csv_to_printer');
1347             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.label'));
1348             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.accesskey'));
1349             mp.appendChild(mi);
1350             mi = document.createElement('menuitem');
1351             mi.setAttribute('id',obj.node.id + '_csv_to_file');
1352             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.label'));
1353             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.accesskey'));
1354             mp.appendChild(mi);
1355             mi = document.createElement('menuitem');
1356             mi.setAttribute('id',obj.node.id + '_save_columns');
1357             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.label'));
1358             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.accesskey'));
1359             mp.appendChild(mi);
1360             return btn;
1361         } catch(E) {
1362             obj.error.standard_unexpected_error_alert('rendering list actions',E);
1363         }
1364     },
1365
1366         'set_list_actions' : function(params) {
1367                 var obj = this;
1368                 switch(this.node.nodeName) {
1369                         case 'tree' : return this._set_list_actions_for_tree(params); break;
1370                         default: throw('NYI: Need ._set_list_actions() for ' + this.node.nodeName); break;
1371                 }
1372         },
1373
1374     '_set_list_actions_for_tree' : function(params) {
1375         // This should be called after the button element from render_list_actions has been appended to the DOM
1376         var obj = this;
1377         try {
1378             var x = document.getElementById(obj.node.id + '_clipfield');
1379             if (x) {
1380                 x.addEventListener(
1381                     'command',
1382                     function() {
1383                         obj.clipboard(params);
1384                         if (params && typeof params.on_complete == 'function') {
1385                             params.on_complete(params);
1386                         }
1387                     },
1388                     false
1389                 );
1390             }
1391             x = document.getElementById(obj.node.id + '_csv_to_clipboard');
1392             if (x) {
1393                 x.addEventListener(
1394                     'command',
1395                     function() {
1396                         obj.dump_csv_to_clipboard(params);
1397                         if (params && typeof params.on_complete == 'function') {
1398                             params.on_complete(params);
1399                         }
1400                     },
1401                     false
1402                 );
1403             }
1404             x = document.getElementById(obj.node.id + '_csv_to_printer');
1405             if (x) {
1406                 x.addEventListener(
1407                     'command',
1408                     function() {
1409                         obj.dump_csv_to_printer(params);
1410                         if (params && typeof params.on_complete == 'function') {
1411                             params.on_complete(params);
1412                         }
1413                     },
1414                     false
1415                 );
1416             }
1417             x = document.getElementById(obj.node.id + '_csv_to_file');
1418             if (x) {
1419                 x.addEventListener(
1420                     'command',
1421                     function() {
1422                         obj.dump_csv_to_file(params);
1423                         if (params && typeof params.on_complete == 'function') {
1424                             params.on_complete(params);
1425                         }
1426                     },
1427                     false
1428                 );
1429             }
1430             x = document.getElementById(obj.node.id + '_save_columns');
1431             if (x) {
1432                 x.addEventListener(
1433                     'command',
1434                     function() {
1435                         obj.save_columns(params);
1436                         if (params && typeof params.on_complete == 'function') {
1437                             params.on_complete(params);
1438                         }
1439                     },
1440                     false
1441                 );
1442             }
1443
1444         } catch(E) {
1445             obj.error.standard_unexpected_error_alert('setting list actions',E);
1446         }
1447     }
1448
1449 }
1450 dump('exiting util.list.js\n');