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