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