]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/list.js
Support some extra callback functions for staff client lists (on_sort and on_checkbox...
[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
416                 s += ('tree = ' + this.node + '  treechildren = ' + treechildren_node + '\n');
417                 s += ('treeitem = ' + treeitem + '  treerow = ' + treerow + '\n');
418
419                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
420
421                         obj.put_retrieving_label(treerow);
422                         treerow.addEventListener(
423                                 'flesh',
424                                 function() {
425
426                                         if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
427
428                                         treerow.setAttribute('retrieved','true');
429
430                                         //dump('fleshing = ' + params.retrieve_id + '\n');
431
432                                         function inc_fleshed() {
433                                                 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
434                                                 treerow.setAttribute('fleshed','true');
435                                                 obj.row_count.fleshed++;
436                                                 if (obj.row_count.fleshed >= obj.row_count.total) {
437                                                         setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
438                                                 }
439                                         }
440
441                                         params.row_node = treeitem;
442                                         params.on_retrieve = function(p) {
443                                                 try {
444                                                         p.row = params.row;
445                                                         obj._map_row_to_treecell(p,treerow);
446                                                         inc_fleshed();
447                                                         var idx = obj.node.contentView.getIndexOfItem( params.row_node );
448                                                         dump('idx = ' + idx + '\n');
449                                                         // if current row is selected, send another select event to re-sync data that the client code fetches on selects
450                                                         if ( obj.node.view.selection.isSelected( idx ) ) {
451                                                                 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
452                                                                 util.widgets.dispatch('select',obj.node);
453                                                         }
454                                                 } catch(E) {
455                             // Let's not alert on this for now.  Getting contentView has no properties in record buckets under certain conditions
456                                                         dump('fixme2: ' + E + '\n');
457                                                 }
458                                         }
459
460                                         if (typeof params.retrieve_row == 'function') {
461
462                                                 params.retrieve_row( params );
463
464                                         } else if (typeof obj.retrieve_row == 'function') {
465
466                                                         obj.retrieve_row( params );
467
468                                         } else {
469                                         
470                                                         inc_fleshed();
471                                         }
472                                 },
473                                 false
474                         );
475                         /*
476                         setTimeout(
477                                 function() {
478                                         util.widgets.dispatch('flesh',treerow);
479                                 }, 0
480                         );
481                         */
482                 } else {
483                         obj.put_retrieving_label(treerow);
484                         treerow.addEventListener(
485                                 'flesh',
486                                 function() {
487                                         //dump('fleshing anon\n');
488                                         if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
489                                         obj._map_row_to_treecell(params,treerow);
490                                         treerow.setAttribute('retrieved','true');
491                                         treerow.setAttribute('fleshed','true');
492                                         obj.row_count.fleshed++;
493                                         if (obj.row_count.fleshed >= obj.row_count.total) {
494                                                 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
495                                         }
496                                 },
497                                 false
498                         );
499                         /*
500                         setTimeout(
501                                 function() {
502                                         util.widgets.dispatch('flesh',treerow);
503                                 }, 0
504                         );
505                         */
506                 }
507                 this.error.sdump('D_LIST',s);
508
509                         try {
510
511                                 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
512                                         // Remove oldest row
513                                         //if (typeof params.to_bottom != 'undefined') 
514                                         if (typeof params.to_top == 'undefined') {
515                                                 treechildren_node.removeChild( treechildren_node.firstChild );
516                                         } else {
517                                                 treechildren_node.removeChild( treechildren_node.lastChild );
518                                         }
519                                 }
520                         } catch(E) {
521                         }
522
523                 setTimeout( function() { obj.auto_retrieve(); }, 0 );
524
525                 params.my_node = treeitem;
526                 return params;
527         },
528
529         '_refresh_row_in_tree' : function (params) {
530
531                 var obj = this;
532
533                 if (typeof params.row == 'undefined') throw('util.list.refresh_row: Object must contain a row');
534                 if (typeof params.my_node == 'undefined') throw('util.list.refresh_row: Object must contain a my_node');
535                 if (params.my_node.nodeName != 'treeitem') throw('util.list.refresh_rwo: my_node must be a treeitem');
536
537                 var s = ('util.list.refresh_row: params = ' + (params) + '\n');
538
539                 var treeitem = params.my_node;
540                 treeitem.setAttribute('retrieve_id',params.retrieve_id);
541                 if (typeof params.to_bottom != 'undefined') {
542                         if (typeof params.no_auto_select == 'undefined') {
543                                 if (!obj.auto_select_pending) {
544                                         obj.auto_select_pending = true;
545                                         setTimeout(function() {
546                                                 dump('auto-selecting\n');
547                                                 var idx = Number(obj.node.view.rowCount)-1;
548                                                 try { obj.node.view.selection.select(idx); } catch(E) { obj.error.sdump('D_WARN','tree auto select: ' + E + '\n'); }
549                                                 try { if (typeof params.on_select == 'function') params.on_select(); } catch(E) { obj.error.sdump('D_WARN','tree auto select, on_select: ' + E + '\n'); }
550                                                 obj.auto_select_pending = false;
551                                                 try { util.widgets.dispatch('flesh',obj.node.contentView.getItemAtIndex(idx).firstChild); } catch(E) { obj.error.sdump('D_WARN','tree auto select, flesh: ' + E + '\n'); }
552                                         }, 1000);
553                                 }
554                         }
555                 }
556                 var delete_me = [];
557                 for (var i in treeitem.childNodes) if (treeitem.childNodes[i].nodeName == 'treerow') delete_me.push(treeitem.childNodes[i]);
558                 for (var i = 0; i < delete_me.length; i++) treeitem.removeChild(delete_me[i]);
559                 var treerow = document.createElement('treerow');
560                 treeitem.appendChild( treerow );
561                 treerow.setAttribute('retrieve_id',params.retrieve_id);
562
563                 s += ('tree = ' + this.node + '\n');
564                 s += ('treeitem = ' + treeitem + '  treerow = ' + treerow + '\n');
565
566                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
567
568                         obj.put_retrieving_label(treerow);
569                         treerow.addEventListener(
570                                 'flesh',
571                                 function() {
572
573                                         if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
574
575                                         treerow.setAttribute('retrieved','true');
576
577                                         //dump('fleshing = ' + params.retrieve_id + '\n');
578
579                                         function inc_fleshed() {
580                                                 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
581                                                 treerow.setAttribute('fleshed','true');
582                                                 obj.row_count.fleshed++;
583                                                 if (obj.row_count.fleshed >= obj.row_count.total) {
584                                                         setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
585                                                 }
586                                         }
587
588                                         params.row_node = treeitem;
589                                         params.on_retrieve = function(p) {
590                                                 try {
591                                                         p.row = params.row;
592                                                         obj._map_row_to_treecell(p,treerow);
593                                                         inc_fleshed();
594                                                         var idx = obj.node.contentView.getIndexOfItem( params.row_node );
595                                                         dump('idx = ' + idx + '\n');
596                                                         // if current row is selected, send another select event to re-sync data that the client code fetches on selects
597                                                         if ( obj.node.view.selection.isSelected( idx ) ) {
598                                                                 dump('dispatching select event for on_retrieve for idx = ' + idx + '\n');
599                                                                 util.widgets.dispatch('select',obj.node);
600                                                         }
601                                                 } catch(E) {
602                             // Let's not alert on this for now.  Getting contentView has no properties in record buckets under certain conditions
603                                                         dump('fixme2: ' + E + '\n');
604                                                 }
605                                         }
606
607                                         if (typeof params.retrieve_row == 'function') {
608
609                                                 params.retrieve_row( params );
610
611                                         } else if (typeof obj.retrieve_row == 'function') {
612
613                                                         obj.retrieve_row( params );
614
615                                         } else {
616                                         
617                                                         inc_fleshed();
618                                         }
619                                 },
620                                 false
621                         );
622                         /*
623                         setTimeout(
624                                 function() {
625                                         util.widgets.dispatch('flesh',treerow);
626                                 }, 0
627                         );
628                         */
629                 } else {
630                         obj.put_retrieving_label(treerow);
631                         treerow.addEventListener(
632                                 'flesh',
633                                 function() {
634                                         //dump('fleshing anon\n');
635                                         if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
636                                         obj._map_row_to_treecell(params,treerow);
637                                         treerow.setAttribute('retrieved','true');
638                                         treerow.setAttribute('fleshed','true');
639                                         obj.row_count.fleshed++;
640                                         if (obj.row_count.fleshed >= obj.row_count.total) {
641                                                 setTimeout( function() { obj.exec_on_all_fleshed(); }, 0 );
642                                         }
643                                 },
644                                 false
645                         );
646                         /*
647                         setTimeout(
648                                 function() {
649                                         util.widgets.dispatch('flesh',treerow);
650                                 }, 0
651                         );
652                         */
653                 }
654                 this.error.sdump('D_LIST',s);
655
656                         try {
657
658                                 if (obj.trim_list && obj.row_count.total >= obj.trim_list) {
659                                         // Remove oldest row
660                                         //if (typeof params.to_bottom != 'undefined') 
661                                         if (typeof params.to_top == 'undefined') {
662                                                 treechildren_node.removeChild( treechildren_node.firstChild );
663                                         } else {
664                                                 treechildren_node.removeChild( treechildren_node.lastChild );
665                                         }
666                                 }
667                         } catch(E) {
668                         }
669
670                 setTimeout( function() { obj.auto_retrieve(); }, 0 );
671
672         JSAN.use('util.widgets'); util.widgets.dispatch('select',obj.node);
673
674                 return params;
675         },
676
677         'put_retrieving_label' : function(treerow) {
678                 var obj = this;
679                 try {
680                         /*
681                         var cols_idx = 0;
682                         dump('put_retrieving_label.  columns = ' + js2JSON(obj.columns) + '\n');
683                         while( obj.columns[cols_idx] && obj.columns[cols_idx].hidden && obj.columns[cols_idx].hidden == 'true') {
684                                 dump('\t' + cols_idx);
685                                 var treecell = document.createElement('treecell');
686                                 treerow.appendChild(treecell);
687                                 cols_idx++;
688                         }
689                         */
690                         for (var i = 0; i < obj.columns.length; i++) {
691                         var treecell = document.createElement('treecell'); treecell.setAttribute('label',document.getElementById('offlineStrings').getString('list.row_retrieving'));
692                         treerow.appendChild(treecell);
693                         }
694                         /*
695                         dump('\t' + cols_idx + '\n');
696                         */
697                 } catch(E) {
698                         alert(E);
699                 }
700         },
701
702         'detect_visible' : function() {
703                 var obj = this;
704                 try {
705                         //dump('detect_visible  obj.node = ' + obj.node + '\n');
706                         /* FIXME - this is a hack.. if the implementation of tree changes, this could break */
707                         try {
708                                 var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(obj.node)[1] )[1];
709                                 var curpos = scrollbar.getAttribute('curpos');
710                                 var maxpos = scrollbar.getAttribute('maxpos');
711                                 //alert('curpos = ' + curpos + ' maxpos = ' + maxpos + ' obj.curpos = ' + obj.curpos + ' obj.maxpos = ' + obj.maxpos + '\n');
712                                 if ((curpos != obj.curpos) || (maxpos != obj.maxpos)) {
713                                         if ( obj.auto_retrieve() > 0 ) {
714                                                 obj.curpos = curpos; obj.maxpos = maxpos;
715                                         }
716                                 }
717                         } catch(E) {
718                                 obj.error.sdump('D_XULRUNNER', 'List implementation changed? ' + E);
719                         }
720                 } catch(E) { obj.error.sdump('D_ERROR',E); }
721         },
722
723         'detect_visible_polling' : function() {
724                 try {
725                         //alert('detect_visible_polling');
726                         var obj = this;
727                         obj.detect_visible();
728                         setTimeout(function() { try { obj.detect_visible_polling(); } catch(E) { alert(E); } },2000);
729                 } catch(E) {
730                         alert(E);
731                 }
732         },
733
734
735         'auto_retrieve' : function(params) {
736                 var obj = this;
737                 switch (this.node.nodeName) {
738                         case 'tree' : obj._auto_retrieve_tree(params); break;
739                         default: throw('NYI: Need .auto_retrieve() for ' + obj.node.nodeName); break;
740                 }
741         },
742
743         '_auto_retrieve_tree' : function (params) {
744                 var obj = this;
745                 if (!obj.auto_retrieve_in_progress) {
746                         obj.auto_retrieve_in_progress = true;
747                         setTimeout(
748                                 function() {
749                                         try {
750                                                         //alert('auto_retrieve\n');
751                                                         var count = 0;
752                                                         var startpos = obj.node.treeBoxObject.getFirstVisibleRow();
753                                                         var endpos = obj.node.treeBoxObject.getLastVisibleRow();
754                                                         if (startpos > endpos) endpos = obj.node.treeBoxObject.getPageLength();
755                                                         //dump('startpos = ' + startpos + ' endpos = ' + endpos + '\n');
756                                                         for (var i = startpos; i < endpos + 4; i++) {
757                                                                 try {
758                                                                         //dump('trying index ' + i + '\n');
759                                                                         var item = obj.node.contentView.getItemAtIndex(i).firstChild;
760                                                                         if (item && item.getAttribute('retrieved') != 'true' ) {
761                                                                                 //dump('\tgot an unfleshed item = ' + item + ' = ' + item.nodeName + '\n');
762                                                                                 util.widgets.dispatch('flesh',item); count++;
763                                                                         }
764                                                                 } catch(E) {
765                                                                         //dump(i + ' : ' + E + '\n');
766                                                                 }
767                                                         }
768                                                         obj.auto_retrieve_in_progress = false;
769                                                         return count;
770                                         } catch(E) { alert(E); }
771                                 }, 1
772                         );
773                 }
774         },
775
776     'exec_on_all_fleshed' : function() {
777         var obj = this;
778         try {
779             if (obj.on_all_fleshed) {
780                                 if (typeof obj.on_all_fleshed == 'function') {
781                     dump('exec_on_all_fleshed == function\n');
782                                         setTimeout( 
783                         function() { 
784                             try { obj.on_all_fleshed(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',obj.on_all_fleshed); }
785                         }, 0 
786                     );
787                                 } else if (typeof obj.on_all_fleshed.length != 'undefined') {
788                     dump('exec_on_all_fleshed == array\n');
789                     setTimeout(
790                         function() {
791                             try {
792                                 dump('exec_on_all_fleshed, processing on_all_fleshed array, length = ' + obj.on_all_fleshed.length + '\n');
793                                 var f = obj.on_all_fleshed.pop();
794                                 if (typeof f == 'function') { 
795                                     try { f(); } catch(E) { obj.error.standard_unexpected_error_alert('_full_retrieve_tree callback',f); } 
796                                 }
797                                 if (obj.on_all_fleshed.length > 0) arguments.callee(); 
798                             } catch(E) {
799                                 obj.error.standard_unexpected_error_alert('exec_on_all_fleshed callback error',E);
800                             }
801                         }, 0
802                     ); 
803                                 } else {
804                     obj.error.standard_unexpected_error_alert('unexpected on_all_fleshed object: ', obj.on_all_fleshed);
805                 }
806             }
807         } catch(E) {
808             obj.error.standard_unexpected_error_alert('exec_on_all-fleshed error',E);
809         }
810     },
811
812         'full_retrieve' : function(params) {
813                 var obj = this;
814                 switch (this.node.nodeName) {
815                         case 'tree' : obj._full_retrieve_tree(params); break;
816                         default: throw('NYI: Need .full_retrieve() for ' + obj.node.nodeName); break;
817                 }
818         },
819
820         '_full_retrieve_tree' : function(params) {
821                 var obj = this;
822                 try {
823                         if (obj.row_count.fleshed >= obj.row_count.total) {
824                                 dump('Full retrieve... tree seems to be in sync\n' + js2JSON(obj.row_count) + '\n');
825                 obj.exec_on_all_fleshed();
826                         } else {
827                                 dump('Full retrieve... syncing tree' + js2JSON(obj.row_count) + '\n');
828                                 JSAN.use('util.widgets');
829                                 var nodes = obj.treechildren.childNodes;
830                                 for (var i = 0; i < nodes.length; i++) {
831                                         util.widgets.dispatch('flesh',nodes[i].firstChild);
832                                 }
833                         }
834                 } catch(E) {
835                         obj.error.standard_unexpected_error_alert('_full_retrieve_tree',E);
836                 }
837         },
838
839         '_append_to_listbox' : function (params) {
840
841                 var obj = this;
842
843                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
844
845                 var s = ('util.list.append: params = ' + (params) + '\n');
846
847                 var listitem = document.createElement('listitem');
848
849                 s += ('listbox = ' + this.node + '  listitem = ' + listitem + '\n');
850
851                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
852
853                         setTimeout(
854                                 function() {
855                                         listitem.setAttribute('retrieve_id',params.retrieve_id);
856                                         //FIXME//Make async and fire when row is visible in list
857                                         var row;
858
859                                         params.row_node = listitem;
860                                         params.on_retrieve = function(row) {
861                                                 params.row = row;
862                                                 obj._map_row_to_listcell(params,listitem);
863                                                 obj.node.appendChild( listitem );
864                                                 util.widgets.dispatch('select',obj.node);
865                                         }
866
867                                         if (typeof params.retrieve_row == 'function') {
868
869                                                 row = params.retrieve_row( params );
870
871                                         } else {
872
873                                                 if (typeof obj.retrieve_row == 'function') {
874
875                                                         row = obj.retrieve_row( params );
876
877                                                 }
878                                         }
879                                 }, 0
880                         );
881                 } else {
882                         this._map_row_to_listcell(params,listitem);
883                         this.node.appendChild( listitem );
884                 }
885
886                 this.error.sdump('D_LIST',s);
887                 params.my_node = listitem;
888                 return params;
889
890         },
891
892         '_map_row_to_treecell' : function(params,treerow) {
893                 var obj = this;
894                 var s = '';
895                 util.widgets.remove_children(treerow);
896
897                 if (typeof params.map_row_to_column == 'function' || typeof this.map_row_to_column == 'function') {
898
899                         for (var i = 0; i < this.columns.length; i++) {
900                                 var treecell = document.createElement('treecell');
901                 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
902                                 var label = '';
903                                 if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
904                                         treecell.setAttribute('label',label);
905                                         treerow.appendChild( treecell );
906                                         s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
907                                         continue;
908                                 }
909                                 if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
910                                         treecell.setAttribute('label',label);
911                                         treerow.appendChild( treecell );
912                                         s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
913                                         continue;
914                                 }
915         
916                                 if (typeof params.map_row_to_column == 'function')  {
917         
918                                         label = params.map_row_to_column(params.row,this.columns[i]);
919         
920                                 } else if (typeof this.map_row_to_column == 'function') {
921         
922                                         label = this.map_row_to_column(params.row,this.columns[i]);
923         
924                                 }
925                                 if (this.columns[i].type == 'checkbox') { treecell.setAttribute('value',label); } else { treecell.setAttribute('label',label ? label : ''); }
926                                 treerow.appendChild( treecell );
927                                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
928                         }
929                 } else if (typeof params.map_row_to_columns == 'function' || typeof this.map_row_to_columns == 'function') {
930
931                         var labels = [];
932
933                         if (typeof params.map_row_to_columns == 'function') {
934
935                                 labels = params.map_row_to_columns(params.row,this.columns);
936
937                         } else if (typeof this.map_row_to_columns == 'function') {
938
939                                 labels = this.map_row_to_columns(params.row,this.columns);
940
941                         }
942                         for (var i = 0; i < labels.length; i++) {
943                                 var treecell = document.createElement('treecell');
944                 if ( this.columns[i].editable == false ) { treecell.setAttribute('editable','false'); }
945                 if ( this.columns[i].type == 'checkbox') {
946                     treecell.setAttribute('value', labels[i]);
947                 } else {
948                                     treecell.setAttribute('label',typeof labels[i] == 'string' || typeof labels[i] == 'number' ? labels[i] : '');
949                 }
950                                 treerow.appendChild( treecell );
951                                 s += ('treecell = ' + treecell + ' with label = ' + labels[i] + '\n');
952                         }
953
954                 } else {
955
956                         throw('No row to column mapping function.');
957                 }
958                 this.error.sdump('D_LIST',s);
959         },
960
961         '_map_row_to_listcell' : function(params,listitem) {
962                 var obj = this;
963                 var s = '';
964                 for (var i = 0; i < this.columns.length; i++) {
965                         var value = '';
966                         if (typeof params.map_row_to_column == 'function')  {
967
968                                 value = params.map_row_to_column(params.row,this.columns[i]);
969
970                         } else {
971
972                                 if (typeof this.map_row_to_column == 'function') {
973
974                                         value = this.map_row_to_column(params.row,this.columns[i]);
975                                 }
976                         }
977                         if (typeof value == 'string' || typeof value == 'number') {
978                                 var listcell = document.createElement('listcell');
979                                 listcell.setAttribute('label',value);
980                                 listitem.appendChild(listcell);
981                                 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
982                         } else {
983                                 listitem.appendChild(value);
984                                 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
985                         }
986                 }
987                 this.error.sdump('D_LIST',s);
988         },
989
990         'select_all' : function(params) {
991                 var obj = this;
992                 switch(this.node.nodeName) {
993                         case 'tree' : return this._select_all_from_tree(params); break;
994                         default: throw('NYI: Need ._select_all_from_() for ' + this.node.nodeName); break;
995                 }
996         },
997
998         '_select_all_from_tree' : function(params) {
999                 var obj = this;
1000                 this.node.view.selection.selectAll();
1001         },
1002
1003         'retrieve_selection' : function(params) {
1004                 var obj = this;
1005                 switch(this.node.nodeName) {
1006                         case 'tree' : return this._retrieve_selection_from_tree(params); break;
1007                         default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
1008                 }
1009         },
1010
1011         '_retrieve_selection_from_tree' : function(params) {
1012                 var obj = this;
1013                 var list = [];
1014                 var start = new Object();
1015                 var end = new Object();
1016                 var numRanges = this.node.view.selection.getRangeCount();
1017                 for (var t=0; t<numRanges; t++){
1018                         this.node.view.selection.getRangeAt(t,start,end);
1019                         for (var v=start.value; v<=end.value; v++){
1020                                 var i = this.node.contentView.getItemAtIndex(v);
1021                                 list.push( i );
1022                         }
1023                 }
1024                 return list;
1025         },
1026
1027         'dump' : function(params) {
1028                 var obj = this;
1029                 switch(this.node.nodeName) {
1030                         case 'tree' : return this._dump_tree(params); break;
1031                         default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
1032                 }
1033         },
1034
1035         '_dump_tree' : function(params) {
1036                 var obj = this;
1037                 var dump = [];
1038                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1039                         var row = [];
1040                         var treeitem = this.treechildren.childNodes[i];
1041                         var treerow = treeitem.firstChild;
1042                         for (var j = 0; j < treerow.childNodes.length; j++) {
1043                                 row.push( treerow.childNodes[j].getAttribute('label') );
1044                         }
1045                         dump.push( row );
1046                 }
1047                 return dump;
1048         },
1049
1050         'dump_with_keys' : function(params) {
1051                 var obj = this;
1052                 switch(this.node.nodeName) {
1053                         case 'tree' : return this._dump_tree_with_keys(params); break;
1054                         default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
1055                 }
1056
1057         },
1058
1059         '_dump_tree_with_keys' : function(params) {
1060                 var obj = this;
1061                 var dump = [];
1062                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1063                         var row = {};
1064                         var treeitem = this.treechildren.childNodes[i];
1065                         var treerow = treeitem.firstChild;
1066                         for (var j = 0; j < treerow.childNodes.length; j++) {
1067                                 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
1068                         }
1069                         dump.push( row );
1070                 }
1071                 return dump;
1072         },
1073
1074         'dump_csv' : function(params) {
1075                 var obj = this;
1076                 switch(this.node.nodeName) {
1077                         case 'tree' : return this._dump_tree_csv(params); break;
1078                         default: throw('NYI: Need .dump_csv() for ' + this.node.nodeName); break;
1079                 }
1080
1081         },
1082
1083         '_dump_tree_csv' : function(params) {
1084                 var obj = this;
1085                 var dump = '';
1086                 for (var j = 0; j < obj.columns.length; j++) {
1087                         if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1088                                 /* skip */
1089                         } else {
1090                                 if (dump) dump += ',';
1091                                 dump += '"' + obj.columns[j].label.replace(/"/g, '""') + '"';
1092                         }
1093                 }
1094                 dump += '\r\n';
1095                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1096                         var row = '';
1097                         var treeitem = this.treechildren.childNodes[i];
1098                         var treerow = treeitem.firstChild;
1099                         for (var j = 0; j < treerow.childNodes.length; j++) {
1100                                 if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') {
1101                                         /* skip */
1102                                 } else {
1103                                         if (row) row += ',';
1104                                         row += '"' + treerow.childNodes[j].getAttribute('label').replace(/"/g, '""') + '"';
1105                                 }
1106                         }
1107                         dump +=  row + '\r\n';
1108                 }
1109                 return dump;
1110         },
1111
1112     'dump_csv_to_clipboard' : function(params) {
1113         var obj = this;
1114         if (typeof params == 'undefined') params = {};
1115         if (params.no_full_retrieve) {
1116             copy_to_clipboard( obj.dump_csv( params ) );
1117         } else {
1118             obj.wrap_in_full_retrieve( function() { copy_to_clipboard( obj.dump_csv( params ) ); } );
1119         }
1120     },
1121
1122     'dump_csv_to_printer' : function(params) {
1123         var obj = this;
1124         JSAN.use('util.print'); var print = new util.print();
1125         if (typeof params == 'undefined') params = {};
1126         if (params.no_full_retrieve) {
1127             print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1128         } else {
1129             obj.wrap_in_full_retrieve( 
1130                 function() { 
1131                     print.simple( obj.dump_csv( params ), {'content_type':'text/plain'} );
1132                 }
1133             );
1134         }
1135     },
1136
1137     'dump_csv_to_file' : function(params) {
1138         var obj = this;
1139         JSAN.use('util.file'); var f = new util.file();
1140         if (typeof params == 'undefined') params = {};
1141         if (params.no_full_retrieve) {
1142             params.data = obj.dump_csv( params );
1143             params.not_json = true;
1144             if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1145             f.export_file( params );
1146         } else {
1147             obj.wrap_in_full_retrieve( 
1148                 function() { 
1149                     params.data = obj.dump_csv( params );
1150                     params.not_json = true;
1151                     if (!params.title) params.title = document.getElementById('offlineStrings').getString('list.save_csv_as');
1152                     f.export_file( params );
1153                 }
1154             );
1155         }
1156     },
1157
1158     'print' : function(params) {
1159         if (!params) params = {};
1160                 switch(this.node.nodeName) {
1161                         case 'tree' : return this._print_tree(params); break;
1162                         default: throw('NYI: Need ._print() for ' + this.node.nodeName); break;
1163                 }
1164     },
1165
1166     '_print_tree' : function(params) {
1167         var obj = this;
1168         try {
1169                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
1170             if (!params.staff && data.list.au && data.list.au[0]) {
1171                 params.staff = data.list.au[0];
1172             }
1173             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() ]) {
1174                 params.lib = data.hash.aou[ data.list.au[0].ws_ou() ];
1175                 params.lib.children(null);
1176             }
1177             if (params.template && data.print_list_templates[ params.template ]) {
1178                 var template = data.print_list_templates[ params.template ];
1179                 for (var i in template) params[i] = template[i];
1180             }
1181             obj.wrap_in_full_retrieve(
1182                 function() {
1183                     try {
1184                         if (!params.list) params.list = obj.dump_with_keys();
1185                         JSAN.use('util.print'); var print = new util.print();
1186                         print.tree_list( params );
1187                         if (typeof params.callback == 'function') params.callback();
1188                     } catch(E) {
1189                                     obj.error.standard_unexpected_error_alert('inner _print_tree',E);
1190                     }
1191                 }
1192             );
1193             
1194         } catch(E) {
1195                         obj.error.standard_unexpected_error_alert('_print_tree',E);
1196         }
1197     },
1198
1199         'dump_selected_with_keys' : function(params) {
1200                 var obj = this;
1201                 switch(this.node.nodeName) {
1202                         case 'tree' : return this._dump_tree_selection_with_keys(params); break;
1203                         default: throw('NYI: Need .dump_selection_with_keys() for ' + this.node.nodeName); break;
1204                 }
1205
1206         },
1207
1208         '_dump_tree_selection_with_keys' : function(params) {
1209                 var obj = this;
1210                 var dump = [];
1211                 var list = obj._retrieve_selection_from_tree();
1212                 for (var i = 0; i < list.length; i++) {
1213                         var row = {};
1214                         var treeitem = list[i];
1215                         var treerow = treeitem.firstChild;
1216                         for (var j = 0; j < treerow.childNodes.length; j++) {
1217                                 var value = treerow.childNodes[j].getAttribute('label');
1218                                 if (params.skip_hidden_columns) if (obj.node.treeBoxObject.columns.getColumnAt(j).element.getAttribute('hidden') == 'true') continue;
1219                                 var id = obj.columns[j].id; if (params.labels_instead_of_ids) id = obj.columns[j].label;
1220                                 row[ id ] = value;
1221                         }
1222                         dump.push( row );
1223                 }
1224                 return dump;
1225         },
1226
1227         'clipboard' : function(params) {
1228                 try {
1229                         var obj = this;
1230                         var dump = obj.dump_selected_with_keys({'skip_hidden_columns':true,'labels_instead_of_ids':true});
1231                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
1232                         data.list_clipboard = dump; data.stash('list_clipboard');
1233                         JSAN.use('util.window'); var win = new util.window();
1234                         win.open(urls.XUL_LIST_CLIPBOARD,'list_clipboard','chrome,resizable,modal');
1235             window.focus(); // sometimes the main window will lower after a clipboard action
1236                 } catch(E) {
1237                         this.error.standard_unexpected_error_alert('clipboard',E);
1238                 }
1239         },
1240
1241         'dump_retrieve_ids' : function(params) {
1242                 var obj = this;
1243                 switch(this.node.nodeName) {
1244                         case 'tree' : return this._dump_retrieve_ids_tree(params); break;
1245                         default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
1246                 }
1247         },
1248
1249         '_dump_retrieve_ids_tree' : function(params) {
1250                 var obj = this;
1251                 var dump = [];
1252                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
1253                         var treeitem = this.treechildren.childNodes[i];
1254                         dump.push( treeitem.getAttribute('retrieve_id') );
1255                 }
1256                 return dump;
1257         },
1258
1259     'wrap_in_full_retrieve' : function(f) {
1260         var obj = this;
1261                 if (typeof obj.on_all_fleshed == 'function') { // legacy
1262             obj.on_all_fleshed = [ obj.on_all_fleshed ];
1263                 }
1264         if (! obj.on_all_fleshed) obj.on_all_fleshed = [];
1265         obj.on_all_fleshed.push(f);
1266         obj.full_retrieve();
1267     },
1268
1269         '_sort_tree' : function(col,sortDir) {
1270                 var obj = this;
1271                 try {
1272                         if (obj.node.getAttribute('no_sort')) {
1273                                 return;
1274                         }
1275                         var col_pos;
1276                         for (var i = 0; i < obj.columns.length; i++) { 
1277                                 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i); 
1278                         }
1279             obj.wrap_in_full_retrieve(
1280                 function() {
1281                                         try {
1282                                                 JSAN.use('util.money');
1283                                                 var rows = [];
1284                                                 var treeitems = obj.treechildren.childNodes;
1285                                                 for (var i = 0; i < treeitems.length; i++) {
1286                                                         var treeitem = treeitems[i];
1287                                                         var treerow = treeitem.firstChild;
1288                                                         var treecell = treerow.childNodes[ col_pos ];
1289                                                         value = ( { 'value' : treecell ? treecell.getAttribute('label') : '', 'node' : treeitem } );
1290                                                         rows.push( value );
1291                                                 }
1292                                                 rows = rows.sort( function(a,b) { 
1293                                                         a = a.value; b = b.value; 
1294                                                         if (col.getAttribute('sort_type')) {
1295                                                                 switch(col.getAttribute('sort_type')) {
1296                                                                         case 'number' :
1297                                                                                 a = Number(a); b = Number(b);
1298                                                                         break;
1299                                                                         case 'money' :
1300                                                                                 a = util.money.dollars_float_to_cents_integer(a);
1301                                                                                 b = util.money.dollars_float_to_cents_integer(b);
1302                                                                         break;
1303                                                                         case 'title' : /* special case for "a" and "the".  doesn't use marc 245 indicator */
1304                                                                                 a = String( a ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1305                                                                                 b = String( b ).toUpperCase().replace( /^\s*(THE|A|AN)\s+/, '' );
1306                                                                         break;
1307                                                                         default:
1308                                                                                 a = String( a ).toUpperCase();
1309                                                                                 b = String( b ).toUpperCase();
1310                                                                         break;
1311                                                                 }
1312                                                         } else {
1313                                                                 if (typeof a == 'string' || typeof b == 'string') {
1314                                                                         a = String( a ).toUpperCase();
1315                                                                         b = String( b ).toUpperCase();
1316                                                                 }
1317                                                         }
1318                                                         if (a < b) return -1; 
1319                                                         if (a > b) return 1; 
1320                                                         return 0; 
1321                                                 } );
1322                                                 if (sortDir == 'asc') rows = rows.reverse();
1323                                                 while(obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
1324                                                 for (var i = 0; i < rows.length; i++) {
1325                                                         obj.treechildren.appendChild( rows[i].node );
1326                                                 }
1327                         if (typeof obj.on_sort == 'function') obj.on_sort();
1328                                         } catch(E) {
1329                                                 obj.error.standard_unexpected_error_alert('sorting',E); 
1330                                         }
1331                                 }
1332             );
1333                 } catch(E) {
1334                         obj.error.standard_unexpected_error_alert('pre sorting', E);
1335                 }
1336         },
1337
1338     '_toggle_checkbox_column' : function(col,toggle) {
1339         var obj = this;
1340         try {
1341             if (obj.node.getAttribute('no_toggle')) {
1342                 return;
1343             }
1344             var col_pos;
1345             for (var i = 0; i < obj.columns.length; i++) { 
1346                 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i); 
1347             }
1348             var treeitems = obj.treechildren.childNodes;
1349             for (var i = 0; i < treeitems.length; i++) {
1350                 var treeitem = treeitems[i];
1351                 var treerow = treeitem.firstChild;
1352                 var treecell = treerow.childNodes[ col_pos ];
1353                 treecell.setAttribute('value',(toggle == 'on'));
1354             }
1355             if (typeof obj.on_checkbox_toggle == 'function') obj.on_checkbox_toggle(toggle);
1356         } catch(E) {
1357             obj.error.standard_unexpected_error_alert('pre toggle', E);
1358         }
1359     },
1360
1361         'render_list_actions' : function(params) {
1362                 var obj = this;
1363                 switch(this.node.nodeName) {
1364                         case 'tree' : return this._render_list_actions_for_tree(params); break;
1365                         default: throw('NYI: Need ._render_list_actions() for ' + this.node.nodeName); break;
1366                 }
1367         },
1368
1369     '_render_list_actions_for_tree' : function(params) {
1370         var obj = this;
1371         try {
1372             var btn = document.createElement('button');
1373             btn.setAttribute('id',obj.node.id + '_list_actions');
1374             btn.setAttribute('type','menu');
1375             btn.setAttribute('allowevents','true');
1376             //btn.setAttribute('oncommand','this.firstChild.showPopup();');
1377             btn.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.menu.label'));
1378             btn.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.menu.accesskey'));
1379             var mp = document.createElement('menupopup');
1380             btn.appendChild(mp);
1381             var mi = document.createElement('menuitem');
1382             mi.setAttribute('id',obj.node.id + '_clipfield');
1383             mi.setAttribute('disabled','true');
1384             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.label'));
1385             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.field_to_clipboard.accesskey'));
1386             mp.appendChild(mi);
1387             mi = document.createElement('menuitem');
1388             mi.setAttribute('id',obj.node.id + '_csv_to_clipboard');
1389             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.label'));
1390             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_clipboard.accesskey'));
1391             mp.appendChild(mi);
1392             mi = document.createElement('menuitem');
1393             mi.setAttribute('id',obj.node.id + '_csv_to_printer');
1394             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.label'));
1395             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_printer.accesskey'));
1396             mp.appendChild(mi);
1397             mi = document.createElement('menuitem');
1398             mi.setAttribute('id',obj.node.id + '_csv_to_file');
1399             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.label'));
1400             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.csv_to_file.accesskey'));
1401             mp.appendChild(mi);
1402             mi = document.createElement('menuitem');
1403             mi.setAttribute('id',obj.node.id + '_save_columns');
1404             mi.setAttribute('label',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.label'));
1405             mi.setAttribute('accesskey',document.getElementById('offlineStrings').getString('list.actions.save_column_configuration.accesskey'));
1406             mp.appendChild(mi);
1407             return btn;
1408         } catch(E) {
1409             obj.error.standard_unexpected_error_alert('rendering list actions',E);
1410         }
1411     },
1412
1413         'set_list_actions' : function(params) {
1414                 var obj = this;
1415                 switch(this.node.nodeName) {
1416                         case 'tree' : return this._set_list_actions_for_tree(params); break;
1417                         default: throw('NYI: Need ._set_list_actions() for ' + this.node.nodeName); break;
1418                 }
1419         },
1420
1421     '_set_list_actions_for_tree' : function(params) {
1422         // This should be called after the button element from render_list_actions has been appended to the DOM
1423         var obj = this;
1424         try {
1425             var x = document.getElementById(obj.node.id + '_clipfield');
1426             if (x) {
1427                 x.addEventListener(
1428                     'command',
1429                     function() {
1430                         obj.clipboard(params);
1431                         if (params && typeof params.on_complete == 'function') {
1432                             params.on_complete(params);
1433                         }
1434                     },
1435                     false
1436                 );
1437             }
1438             x = document.getElementById(obj.node.id + '_csv_to_clipboard');
1439             if (x) {
1440                 x.addEventListener(
1441                     'command',
1442                     function() {
1443                         obj.dump_csv_to_clipboard(params);
1444                         if (params && typeof params.on_complete == 'function') {
1445                             params.on_complete(params);
1446                         }
1447                     },
1448                     false
1449                 );
1450             }
1451             x = document.getElementById(obj.node.id + '_csv_to_printer');
1452             if (x) {
1453                 x.addEventListener(
1454                     'command',
1455                     function() {
1456                         obj.dump_csv_to_printer(params);
1457                         if (params && typeof params.on_complete == 'function') {
1458                             params.on_complete(params);
1459                         }
1460                     },
1461                     false
1462                 );
1463             }
1464             x = document.getElementById(obj.node.id + '_csv_to_file');
1465             if (x) {
1466                 x.addEventListener(
1467                     'command',
1468                     function() {
1469                         obj.dump_csv_to_file(params);
1470                         if (params && typeof params.on_complete == 'function') {
1471                             params.on_complete(params);
1472                         }
1473                     },
1474                     false
1475                 );
1476             }
1477             x = document.getElementById(obj.node.id + '_save_columns');
1478             if (x) {
1479                 x.addEventListener(
1480                     'command',
1481                     function() {
1482                         obj.save_columns(params);
1483                         if (params && typeof params.on_complete == 'function') {
1484                             params.on_complete(params);
1485                         }
1486                     },
1487                     false
1488                 );
1489             }
1490
1491         } catch(E) {
1492             obj.error.standard_unexpected_error_alert('setting list actions',E);
1493         }
1494     }
1495
1496 }
1497 dump('exiting util.list.js\n');