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