]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/list.js
prepend certain lists
[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         if (!this.node) throw('Could not find element ' + id);
9         switch(this.node.nodeName) {
10                 case 'listbox' : 
11                 case 'tree' : break;
12                 case 'richlistbox' :
13                         throw(this.node.nodeName + ' not yet supported'); break;
14                 default: throw(this.node.nodeName + ' not supported'); break;
15         }
16
17         JSAN.use('util.error'); this.error = new util.error();
18
19         return this;
20 };
21
22 util.list.prototype = {
23
24         'row_count' : { 'total' : 0, 'fleshed' : 0 },
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.retrieve_row == 'function') obj.retrieve_row = params.retrieve_row;
34
35                 obj.prebuilt = false;
36                 if (typeof params.prebuilt != 'undefined') obj.prebuilt = params.prebuilt;
37
38                 if (typeof params.columns == 'undefined') throw('util.list.init: No columns');
39                 obj.columns = params.columns;
40
41                 switch(obj.node.nodeName) {
42                         case 'tree' : obj._init_tree(params); break;
43                         case 'listbox' : obj._init_listbox(params); break;
44                         default: throw('NYI: Need ._init() for ' + obj.node.nodeName); break;
45                 }
46         },
47
48         'register_all_fleshed_callback' : function(f) {
49                 this.on_all_fleshed = f;
50         },
51
52         '_init_tree' : function (params) {
53                 var obj = this;
54                 if (this.prebuilt) {
55                 
56                         this.treechildren = this.node.lastChild;        
57                 
58                 } else {
59                         var treecols = document.createElement('treecols');
60                         this.node.appendChild(treecols);
61
62                         for (var i = 0; i < this.columns.length; i++) {
63                                 var treecol = document.createElement('treecol');
64                                 for (var j in this.columns[i]) {
65                                         treecol.setAttribute(j,this.columns[i][j]);
66                                 }
67                                 treecols.appendChild(treecol);
68                                 treecol.addEventListener(
69                                         'click', 
70                                         function(ev) {
71                                                 var sortDir = ev.target.getAttribute('sortDir') || 'desc';
72                                                 if (sortDir == 'desc') sortDir = 'asc'; else sortDir = 'desc';
73                                                 //alert('sort ' + ev.target.id + ' ' + sortDir);
74                                                 ev.target.setAttribute('sortDir',sortDir);
75                                                 obj._sort_tree(ev.target,sortDir);
76                                         },
77                                         false
78                                 );
79                                 var splitter = document.createElement('splitter');
80                                 splitter.setAttribute('class','tree-splitter');
81                                 treecols.appendChild(splitter);
82                         }
83
84                         var treechildren = document.createElement('treechildren');
85                         this.node.appendChild(treechildren);
86                         this.treechildren = treechildren;
87                 }
88                 if (typeof params.on_select == 'function') {
89                         this.node.addEventListener(
90                                 'select',
91                                 params.on_select,
92                                 false
93                         );
94                 }
95                 if (typeof params.on_click == 'function') {
96                         this.node.addEventListener(
97                                 'click',
98                                 params.on_click,
99                                 false
100                         );
101                 }
102                 /*
103                 this.node.addEventListener(
104                         'mousemove',
105                         function(ev) { obj.detect_visible(); },
106                         false
107                 );
108                 */
109                 this.node.addEventListener(
110                         'keypress',
111                         function(ev) { obj.auto_retrieve(); },
112                         false
113                 );
114                 this.node.addEventListener(
115                         'click',
116                         function(ev) { obj.auto_retrieve(); },
117                         false
118                 );
119                 window.addEventListener(
120                         'resize',
121                         function(ev) { obj.auto_retrieve(); },
122                         false
123                 );
124                 /* FIXME -- find events on scrollbar to trigger this */
125                 obj.detect_visible_polling();   
126                 /*
127                 var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(this.node)[1] )[1];
128                 var slider = document.getAnonymousNodes( scrollbar )[2];
129                 alert('scrollbar = ' + scrollbar.nodeName + ' grippy = ' + slider.nodeName);
130                 scrollbar.addEventListener('click',function(){alert('sb click');},false);
131                 scrollbar.addEventListener('command',function(){alert('sb command');},false);
132                 scrollbar.addEventListener('scroll',function(){alert('sb scroll');},false);
133                 slider.addEventListener('click',function(){alert('slider click');},false);
134                 slider.addEventListener('command',function(){alert('slider command');},false);
135                 slider.addEventListener('scroll',function(){alert('slider scroll');},false);
136                 */
137                 this.node.addEventListener('scroll',function(){ obj.auto_retrieve(); },false);
138
139                 this.restores_columns(params);
140         },
141
142         '_init_listbox' : function (params) {
143                 if (this.prebuilt) {
144                 } else {
145                         var listhead = document.createElement('listhead');
146                         this.node.appendChild(listhead);
147
148                         var listcols = document.createElement('listcols');
149                         this.node.appendChild(listcols);
150
151                         for (var i = 0; i < this.columns.length; i++) {
152                                 var listheader = document.createElement('listheader');
153                                 listhead.appendChild(listheader);
154                                 var listcol = document.createElement('listcol');
155                                 listcols.appendChild(listcol);
156                                 for (var j in this.columns[i]) {
157                                         listheader.setAttribute(j,this.columns[i][j]);
158                                         listcol.setAttribute(j,this.columns[i][j]);
159                                 };
160                         }
161                 }
162         },
163
164         'save_columns' : function (params) {
165                 var obj = this;
166                 switch (this.node.nodeName) {
167                         case 'tree' : this._save_columns_tree(params); break;
168                         default: throw('NYI: Need .save_columns() for ' + this.node.nodeName); break;
169                 }
170         },
171
172         '_save_columns_tree' : function (params) {
173                 var obj = this;
174                 try {
175                         var id = obj.node.getAttribute('id'); if (!id) {
176                                 alert("FIXME: The columns for this list cannot be saved because the list has no id.");
177                                 return;
178                         }
179                         var my_cols = {};
180                         var nl = obj.node.getElementsByTagName('treecol');
181                         for (var i = 0; i < nl.length; i++) {
182                                 var col = nl[i];
183                                 var col_id = col.getAttribute('id');
184                                 if (!col_id) {
185                                         alert('FIXME: A column in this list does not have an id and cannot be saved');
186                                         continue;
187                                 }
188                                 var col_hidden = col.getAttribute('hidden'); 
189                                 var col_width = col.getAttribute('width'); 
190                                 var col_ordinal = col.getAttribute('ordinal'); 
191                                 my_cols[ col_id ] = { 'hidden' : col_hidden, 'width' : col_width, 'ordinal' : col_ordinal };
192                         }
193                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
194                         JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
195                         file.set_object(my_cols);
196                         file.close();
197                         alert('Columns saved.');
198                 } catch(E) {
199                         obj.error.standard_unexpected_error_alert('_save_columns_tree',E);
200                 }
201         },
202
203         'restores_columns' : function (params) {
204                 var obj = this;
205                 switch (this.node.nodeName) {
206                         case 'tree' : this._restores_columns_tree(params); break;
207                         default: throw('NYI: Need .restores_columns() for ' + this.node.nodeName); break;
208                 }
209         },
210
211         '_restores_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 restored because the list has no id.");
216                                 return;
217                         }
218
219                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
220                         JSAN.use('util.file'); var file = new util.file('tree_columns_for_'+window.escape(id));
221                         if (file._file.exists()) {
222                                 var my_cols = file.get_object(); file.close();
223                                 var nl = obj.node.getElementsByTagName('treecol');
224                                 for (var i = 0; i < nl.length; i++) {
225                                         var col = nl[i];
226                                         var col_id = col.getAttribute('id');
227                                         if (!col_id) {
228                                                 alert('FIXME: A column in this list does not have an id and cannot be saved');
229                                                 continue;
230                                         }
231                                         if (typeof my_cols[col_id] != 'undefined') {
232                                                 col.setAttribute('hidden',my_cols[col_id].hidden); 
233                                                 col.setAttribute('width',my_cols[col_id].width); 
234                                                 col.setAttribute('ordinal',my_cols[col_id].ordinal); 
235                                         } else {
236                                                 obj.error.sdump('D_ERROR','WARNING: Column ' + col_id + ' did not have a saved state.');
237                                         }
238                                 }
239                         }
240                 } catch(E) {
241                         obj.error.standard_unexpected_error_alert('_restore_columns_tree',E);
242                 }
243         },
244
245         'clear' : function (params) {
246                 var obj = this;
247                 switch (this.node.nodeName) {
248                         case 'tree' : this._clear_tree(params); break;
249                         case 'listbox' : this._clear_listbox(params); break;
250                         default: throw('NYI: Need .clear() for ' + this.node.nodeName); break;
251                 }
252                 this.error.sdump('D_LIST','Clearing list ' + this.node.getAttribute('id') + '\n');
253                 this.row_count.total = 0;
254                 this.row_count.fleshed = 0;
255                 if (typeof obj.on_all_fleshed == 'function') {
256                         setTimeout( function() { obj.on_all_fleshed(); }, 0 );
257                 }
258         },
259
260         '_clear_tree' : function(params) {
261                 var obj = this;
262                 if (obj.error.sdump_levels.D_LIST_DUMP_ON_CLEAR) {
263                         obj.error.sdump('D_LIST_DUMP_ON_CLEAR',obj.dump());
264                 }
265                 if (obj.error.sdump_levels.D_LIST_DUMP_WITH_KEYS_ON_CLEAR) {
266                         obj.error.sdump('D_LIST_DUMP_WITH_KEYS_ON_CLEAR',obj.dump_with_keys());
267                 }
268                 while (obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
269         },
270
271         '_clear_listbox' : function(params) {
272                 var obj = this;
273                 var items = [];
274                 var nl = this.node.getElementsByTagName('listitem');
275                 for (var i = 0; i < nl.length; i++) {
276                         items.push( nl[i] );
277                 }
278                 for (var i = 0; i < items.length; i++) {
279                         this.node.removeChild(items[i]);
280                 }
281         },
282
283         'append' : function (params) {
284                 var rnode;
285                 var obj = this;
286                 switch (this.node.nodeName) {
287                         case 'tree' : rnode = this._append_to_tree(params); break;
288                         case 'listbox' : rnode = this._append_to_listbox(params); break;
289                         default: throw('NYI: Need .append() for ' + this.node.nodeName); break;
290                 }
291                 if (rnode && params.attributes) {
292                         for (var i in params.attributes) {
293                                 rnode.setAttribute(i,params.attributes[i]);
294                         }
295                 }
296                 this.row_count.total++;
297                 if (this.row_count.fleshed == this.row_count.total) {
298                         if (typeof this.on_all_fleshed == 'function') {
299                                 setTimeout( function() { obj.on_all_fleshed(); }, 0 );
300                         }
301                 }
302                 return rnode;
303         },
304
305         '_append_to_tree' : function (params) {
306
307                 var obj = this;
308
309                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
310
311                 var s = ('util.list.append: params = ' + (params) + '\n');
312
313                 var treechildren_node = this.treechildren;
314
315                 if (params.node && params.node.nodeName == 'treeitem') {
316                         params.node.setAttribute('container','true'); /* params.node.setAttribute('open','true'); */
317                         if (params.node.lastChild.nodeName == 'treechildren') {
318                                 treechildren_node = params.node.lastChild;
319                         } else {
320                                 treechildren_node = document.createElement('treechildren');
321                                 params.node.appendChild(treechildren_node);
322                         }
323                 }
324
325                 var treeitem = document.createElement('treeitem');
326                 treeitem.setAttribute('retrieve_id',params.retrieve_id);
327                 if (typeof params.to_top == 'undefined') {
328                         treechildren_node.appendChild( treeitem );
329                 } else {
330                         if (treechildren_node.firstChild) {
331                                 treechildren_node.insertBefore( treeitem, treechildren_node.firstChild );
332                         } else {
333                                 treechildren_node.appendChild( treeitem );
334                         }
335                 }
336                 var treerow = document.createElement('treerow');
337                 treeitem.appendChild( treerow );
338                 treerow.setAttribute('retrieve_id',params.retrieve_id);
339
340                 s += ('tree = ' + this.node + '  treechildren = ' + treechildren_node + '\n');
341                 s += ('treeitem = ' + treeitem + '  treerow = ' + treerow + '\n');
342
343                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
344
345                         obj.put_retrieving_label(treerow);
346                         treerow.addEventListener(
347                                 'flesh',
348                                 function() {
349
350                                         if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
351
352                                         treerow.setAttribute('retrieved','true');
353
354                                         //dump('fleshing = ' + params.retrieve_id + '\n');
355
356                                         function inc_fleshed() {
357                                                 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
358                                                 treerow.setAttribute('fleshed','true');
359                                                 obj.row_count.fleshed++;
360                                                 if (obj.row_count.fleshed == obj.row_count.total) {
361                                                         if (typeof obj.on_all_fleshed == 'function') {
362                                                                 setTimeout( function() { obj.on_all_fleshed(); }, 0 );
363                                                         }
364                                                 }
365                                         }
366
367                                         params.row_node = treeitem;
368                                         params.on_retrieve = function(p) {
369                                                 try {
370                                                         p.row = params.row;
371                                                         obj._map_row_to_treecell(p,treerow);
372                                                         inc_fleshed();
373                                                 } catch(E) {
374                                                         alert('fixme2: ' + E);
375                                                 }
376                                         }
377
378                                         if (typeof params.retrieve_row == 'function') {
379
380                                                 params.retrieve_row( params );
381
382                                         } else if (typeof obj.retrieve_row == 'function') {
383
384                                                         obj.retrieve_row( params );
385
386                                         } else {
387                                         
388                                                         inc_fleshed();
389                                         }
390                                 },
391                                 false
392                         );
393                         /*
394                         setTimeout(
395                                 function() {
396                                         util.widgets.dispatch('flesh',treerow);
397                                 }, 0
398                         );
399                         */
400                 } else {
401                         obj.put_retrieving_label(treerow);
402                         treerow.addEventListener(
403                                 'flesh',
404                                 function() {
405                                         //dump('fleshing anon\n');
406                                         if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
407                                         obj._map_row_to_treecell(params,treerow);
408                                         treerow.setAttribute('retrieved','true');
409                                         treerow.setAttribute('fleshed','true');
410                                         obj.row_count.fleshed++;
411                                         if (obj.row_count.fleshed == obj.row_count.total) {
412                                                 if (typeof obj.on_all_fleshed == 'function') {
413                                                         setTimeout( function() { obj.on_all_fleshed(); }, 0 );
414                                                 }
415                                         }
416                                 },
417                                 false
418                         );
419                         /*
420                         setTimeout(
421                                 function() {
422                                         util.widgets.dispatch('flesh',treerow);
423                                 }, 0
424                         );
425                         */
426                 }
427                 this.error.sdump('D_LIST',s);
428
429                 setTimeout( function() { obj.auto_retrieve(); }, 0 );
430
431                 return treeitem;
432         },
433
434         'put_retrieving_label' : function(treerow) {
435                 var obj = this;
436                 try {
437                         /*
438                         var cols_idx = 0;
439                         dump('put_retrieving_label.  columns = ' + js2JSON(obj.columns) + '\n');
440                         while( obj.columns[cols_idx] && obj.columns[cols_idx].hidden && obj.columns[cols_idx].hidden == 'true') {
441                                 dump('\t' + cols_idx);
442                                 var treecell = document.createElement('treecell');
443                                 treerow.appendChild(treecell);
444                                 cols_idx++;
445                         }
446                         */
447                         for (var i = 0; i < obj.columns.length; i++) {
448                         var treecell = document.createElement('treecell'); treecell.setAttribute('label','Retrieving...');
449                         treerow.appendChild(treecell);
450                         }
451                         /*
452                         dump('\t' + cols_idx + '\n');
453                         */
454                 } catch(E) {
455                         alert(E);
456                 }
457         },
458
459         'detect_visible' : function() {
460                 var obj = this;
461                 try {
462                         //dump('detect_visible  obj.node = ' + obj.node + '\n');
463                         /* FIXME - this is a hack.. if the implementation of tree changes, this could break */
464                         try {
465                                 var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(obj.node)[1] )[1];
466                                 var curpos = scrollbar.getAttribute('curpos');
467                                 var maxpos = scrollbar.getAttribute('maxpos');
468                                 //alert('curpos = ' + curpos + ' maxpos = ' + maxpos + ' obj.curpos = ' + obj.curpos + ' obj.maxpos = ' + obj.maxpos + '\n');
469                                 if ((curpos != obj.curpos) || (maxpos != obj.maxpos)) {
470                                         if ( obj.auto_retrieve() > 0 ) {
471                                                 obj.curpos = curpos; obj.maxpos = maxpos;
472                                         }
473                                 }
474                         } catch(E) {
475                                 obj.error.sdump('D_XULRUNNER', 'List implementation changed? ' + E);
476                         }
477                 } catch(E) { obj.error.sdump('D_ERROR',E); }
478         },
479
480         'detect_visible_polling' : function() {
481                 try {
482                         //alert('detect_visible_polling');
483                         var obj = this;
484                         obj.detect_visible();
485                         setTimeout(function() { try { obj.detect_visible_polling(); } catch(E) { alert(E); } },2000);
486                 } catch(E) {
487                         alert(E);
488                 }
489         },
490
491
492         'auto_retrieve' : function(params) {
493                 var obj = this;
494                 switch (this.node.nodeName) {
495                         case 'tree' : obj._auto_retrieve_tree(params); break;
496                         default: throw('NYI: Need .auto_retrieve() for ' + obj.node.nodeName); break;
497                 }
498         },
499
500         '_auto_retrieve_tree' : function (params) {
501                 var obj = this;
502                 if (!obj.auto_retrieve_in_progress) {
503                         obj.auto_retrieve_in_progress = true;
504                         setTimeout(
505                                 function() {
506                                         try {
507                                                         //alert('auto_retrieve\n');
508                                                         var count = 0;
509                                                         var startpos = obj.node.treeBoxObject.getFirstVisibleRow();
510                                                         var endpos = obj.node.treeBoxObject.getLastVisibleRow();
511                                                         if (startpos > endpos) endpos = obj.node.treeBoxObject.getPageLength();
512                                                         //dump('startpos = ' + startpos + ' endpos = ' + endpos + '\n');
513                                                         for (var i = startpos; i < endpos + 4; i++) {
514                                                                 try {
515                                                                         //dump('trying index ' + i + '\n');
516                                                                         var item = obj.node.contentView.getItemAtIndex(i).firstChild;
517                                                                         if (item && item.getAttribute('retrieved') != 'true' ) {
518                                                                                 //dump('\tgot an unfleshed item = ' + item + ' = ' + item.nodeName + '\n');
519                                                                                 util.widgets.dispatch('flesh',item); count++;
520                                                                         }
521                                                                 } catch(E) {
522                                                                         //dump(i + ' : ' + E + '\n');
523                                                                 }
524                                                         }
525                                                         obj.auto_retrieve_in_progress = false;
526                                                         return count;
527                                         } catch(E) { alert(E); }
528                                 }, 1
529                         );
530                 }
531         },
532
533         'full_retrieve' : function(params) {
534                 var obj = this;
535                 switch (this.node.nodeName) {
536                         case 'tree' : obj._full_retrieve_tree(params); break;
537                         default: throw('NYI: Need .full_retrieve() for ' + obj.node.nodeName); break;
538                 }
539         },
540
541         '_full_retrieve_tree' : function(params) {
542                 var obj = this;
543                 try {
544                         if (obj.row_count.total == obj.row_count.fleshed) {
545                                 //alert('Full retrieve... tree seems to be in sync\n' + js2JSON(obj.row_count));
546                                 if (typeof obj.on_all_fleshed == 'function') {
547                                         setTimeout( function() { obj.on_all_fleshed(); }, 0 );
548                                 } else {
549                                         alert('.full_retrieve called with no callback?');
550                                 }
551                         } else {
552                                 //alert('Full retrieve... syncing tree' + js2JSON(obj.row_count));
553                                 JSAN.use('util.widgets');
554                                 var nodes = obj.treechildren.childNodes;
555                                 for (var i = 0; i < nodes.length; i++) {
556                                         util.widgets.dispatch('flesh',nodes[i].firstChild);
557                                 }
558                         }
559                 } catch(E) {
560                         obj.error.standard_unexpected_error_alert('_full_retrieve_tree',E);
561                 }
562         },
563
564         '_append_to_listbox' : function (params) {
565
566                 var obj = this;
567
568                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
569
570                 var s = ('util.list.append: params = ' + (params) + '\n');
571
572                 var listitem = document.createElement('listitem');
573
574                 s += ('listbox = ' + this.node + '  listitem = ' + listitem + '\n');
575
576                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
577
578                         setTimeout(
579                                 function() {
580                                         listitem.setAttribute('retrieve_id',params.retrieve_id);
581                                         //FIXME//Make async and fire when row is visible in list
582                                         var row;
583
584                                         params.row_node = listitem;
585                                         params.on_retrieve = function(row) {
586                                                 params.row = row;
587                                                 obj._map_row_to_listcell(params,listitem);
588                                                 obj.node.appendChild( listitem );
589                                         }
590
591                                         if (typeof params.retrieve_row == 'function') {
592
593                                                 row = params.retrieve_row( params );
594
595                                         } else {
596
597                                                 if (typeof obj.retrieve_row == 'function') {
598
599                                                         row = obj.retrieve_row( params );
600
601                                                 }
602                                         }
603                                 }, 0
604                         );
605                 } else {
606                         this._map_row_to_listcell(params,listitem);
607                         this.node.appendChild( listitem );
608                 }
609
610                 this.error.sdump('D_LIST',s);
611                 return listitem;
612
613         },
614
615         '_map_row_to_treecell' : function(params,treerow) {
616                 var obj = this;
617                 var s = '';
618                 util.widgets.remove_children(treerow);
619                 for (var i = 0; i < this.columns.length; i++) {
620                         var treecell = document.createElement('treecell');
621                         var label = '';
622                         if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
623                                 treecell.setAttribute('label',label);
624                                 treerow.appendChild( treecell );
625                                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
626                                 continue;
627                         }
628                         if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
629                                 treecell.setAttribute('label',label);
630                                 treerow.appendChild( treecell );
631                                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
632                                 continue;
633                         }
634                         if (typeof params.map_row_to_column == 'function')  {
635
636                                 label = params.map_row_to_column(params.row,this.columns[i]);
637
638                         } else {
639
640                                 if (typeof this.map_row_to_column == 'function') {
641
642                                         label = this.map_row_to_column(params.row,this.columns[i]);
643
644                                 } else {
645
646                                         throw('No map_row_to_column function');
647
648                                 }
649                         }
650                         treecell.setAttribute('label',label);
651                         treerow.appendChild( treecell );
652                         s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
653                 }
654                 this.error.sdump('D_LIST',s);
655         },
656
657         '_map_row_to_listcell' : function(params,listitem) {
658                 var obj = this;
659                 var s = '';
660                 for (var i = 0; i < this.columns.length; i++) {
661                         var value = '';
662                         if (typeof params.map_row_to_column == 'function')  {
663
664                                 value = params.map_row_to_column(params.row,this.columns[i]);
665
666                         } else {
667
668                                 if (typeof this.map_row_to_column == 'function') {
669
670                                         value = this.map_row_to_column(params.row,this.columns[i]);
671                                 }
672                         }
673                         if (typeof value == 'string' || typeof value == 'number') {
674                                 var listcell = document.createElement('listcell');
675                                 listcell.setAttribute('label',value);
676                                 listitem.appendChild(listcell);
677                                 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
678                         } else {
679                                 listitem.appendChild(value);
680                                 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
681                         }
682                 }
683                 this.error.sdump('D_LIST',s);
684         },
685
686         'select_all' : function(params) {
687                 var obj = this;
688                 switch(this.node.nodeName) {
689                         case 'tree' : return this._select_all_from_tree(params); break;
690                         default: throw('NYI: Need ._select_all_from_() for ' + this.node.nodeName); break;
691                 }
692         },
693
694         '_select_all_from_tree' : function(params) {
695                 var obj = this;
696                 this.node.view.selection.selectAll();
697         },
698
699         'retrieve_selection' : function(params) {
700                 var obj = this;
701                 switch(this.node.nodeName) {
702                         case 'tree' : return this._retrieve_selection_from_tree(params); break;
703                         default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
704                 }
705         },
706
707         '_retrieve_selection_from_tree' : function(params) {
708                 var obj = this;
709                 var list = [];
710                 var start = new Object();
711                 var end = new Object();
712                 var numRanges = this.node.view.selection.getRangeCount();
713                 for (var t=0; t<numRanges; t++){
714                         this.node.view.selection.getRangeAt(t,start,end);
715                         for (var v=start.value; v<=end.value; v++){
716                                 var i = this.node.contentView.getItemAtIndex(v);
717                                 list.push( i );
718                         }
719                 }
720                 return list;
721         },
722
723         'dump' : function(params) {
724                 var obj = this;
725                 switch(this.node.nodeName) {
726                         case 'tree' : return this._dump_tree(params); break;
727                         default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
728                 }
729         },
730
731         '_dump_tree' : function(params) {
732                 var obj = this;
733                 var dump = [];
734                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
735                         var row = [];
736                         var treeitem = this.treechildren.childNodes[i];
737                         var treerow = treeitem.firstChild;
738                         for (var j = 0; j < treerow.childNodes.length; j++) {
739                                 row.push( treerow.childNodes[j].getAttribute('label') );
740                         }
741                         dump.push( row );
742                 }
743                 return dump;
744         },
745
746         'dump_with_keys' : function(params) {
747                 var obj = this;
748                 switch(this.node.nodeName) {
749                         case 'tree' : return this._dump_tree_with_keys(params); break;
750                         default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
751                 }
752
753         },
754
755         '_dump_tree_with_keys' : function(params) {
756                 var obj = this;
757                 var dump = [];
758                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
759                         var row = {};
760                         var treeitem = this.treechildren.childNodes[i];
761                         var treerow = treeitem.firstChild;
762                         for (var j = 0; j < treerow.childNodes.length; j++) {
763                                 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
764                         }
765                         dump.push( row );
766                 }
767                 return dump;
768         },
769
770         'dump_selected_with_keys' : function(params) {
771                 var obj = this;
772                 switch(this.node.nodeName) {
773                         case 'tree' : return this._dump_tree_selection_with_keys(params); break;
774                         default: throw('NYI: Need .dump_selection_with_keys() for ' + this.node.nodeName); break;
775                 }
776
777         },
778
779         '_dump_tree_selection_with_keys' : function(params) {
780                 var obj = this;
781                 var dump = [];
782                 var list = obj._retrieve_selection_from_tree();
783                 for (var i = 0; i < list.length; i++) {
784                         var row = {};
785                         var treeitem = list[i];
786                         var treerow = treeitem.firstChild;
787                         for (var j = 0; j < treerow.childNodes.length; j++) {
788                                 var value = treerow.childNodes[j].getAttribute('label');
789                                 //FIXME
790                                 //if (params.skip_hidden_columns) if (obj.node.firstChild.childNodes[j].getAttribute('hidden')) continue;
791                                 var id = obj.columns[j].id; if (params.labels_instead_of_ids) id = obj.columns[j].label;
792                                 row[ id ] = value;
793                         }
794                         dump.push( row );
795                 }
796                 return dump;
797         },
798
799         'clipboard' : function() {
800                 try {
801                         var obj = this;
802                         var dump = obj.dump_selected_with_keys({'skip_hidden_columns':true,'labels_instead_of_ids':true});
803                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
804                         data.list_clipboard = dump; data.stash('list_clipboard');
805                         JSAN.use('util.window'); var win = new util.window();
806                         win.open(urls.XUL_LIST_CLIPBOARD,'list_clipboard','chrome,resizable,modal');
807                 } catch(E) {
808                         this.error.standard_unexpected_error_alert('clipboard',E);
809                 }
810         },
811
812         'dump_retrieve_ids' : function(params) {
813                 var obj = this;
814                 switch(this.node.nodeName) {
815                         case 'tree' : return this._dump_retrieve_ids_tree(params); break;
816                         default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
817                 }
818         },
819
820         '_dump_retrieve_ids_tree' : function(params) {
821                 var obj = this;
822                 var dump = [];
823                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
824                         var treeitem = this.treechildren.childNodes[i];
825                         dump.push( treeitem.getAttribute('retrieve_id') );
826                 }
827                 return dump;
828         },
829
830         '_sort_tree' : function(col,sortDir) {
831                 var obj = this;
832                 try {
833                         if (obj.node.getAttribute('no_sort')) {
834                                 return;
835                         }
836                         if (obj.on_all_fleshed) {
837                                 alert('This list is busy rendering/retrieving data.');
838                                 return;
839                         }
840                         var col_pos;
841                         for (var i = 0; i < obj.columns.length; i++) { 
842                                 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i); 
843                         }
844                         obj.on_all_fleshed =
845                                 function() {
846                                         try {
847                                                 JSAN.use('util.money');
848                                                 var rows = [];
849                                                 var treeitems = obj.treechildren.childNodes;
850                                                 for (var i = 0; i < treeitems.length; i++) {
851                                                         var treeitem = treeitems[i];
852                                                         var treerow = treeitem.firstChild;
853                                                         var treecell = treerow.childNodes[ col_pos ];
854                                                         value = ( { 'value' : treecell ? treecell.getAttribute('label') : '', 'node' : treeitem } );
855                                                         rows.push( value );
856                                                 }
857                                                 rows = rows.sort( function(a,b) { 
858                                                         a = a.value; b = b.value; 
859                                                         if (col.getAttribute('sort_type')) {
860                                                                 switch(col.getAttribute('sort_type')) {
861                                                                         case 'number' :
862                                                                                 a = Number(a); b = Number(b);
863                                                                         break;
864                                                                         case 'money' :
865                                                                                 a = util.money.dollars_float_to_cents_integer(a);
866                                                                                 b = util.money.dollars_float_to_cents_integer(b);
867                                                                         break;
868                                                                         case 'title' : /* special case for "a" and "the".  doesn't use marc 245 indicator */
869                                                                                 a = String( a ).toUpperCase().replace( /^(THE|A)\s+/, '' );
870                                                                                 b = String( b ).toUpperCase().replace( /^(THE|A)\s+/, '' );
871                                                                         break;
872                                                                         default:
873                                                                                 a = String( a ).toUpperCase();
874                                                                                 b = String( a ).toUpperCase();
875                                                                         break;
876                                                                 }
877                                                         }
878                                                         if (a < b) return -1; 
879                                                         if (a > b) return 1; 
880                                                         return 0; 
881                                                 } );
882                                                 if (sortDir == 'asc') rows = rows.reverse();
883                                                 while(obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
884                                                 for (var i = 0; i < rows.length; i++) {
885                                                         obj.treechildren.appendChild( rows[i].node );
886                                                 }
887                                         } catch(E) {
888                                                 obj.error.standard_unexpected_error_alert('sorting',E); 
889                                         }
890                                         setTimeout(function(){ obj.on_all_fleshed = null; },0);
891                                 }
892                         obj.full_retrieve();
893                 } catch(E) {
894                         obj.error.standard_unexpected_error_alert('pre sorting', E);
895                 }
896         },
897
898 }
899 dump('exiting util.list.js\n');