]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/list.js
sort_type (number, money) hints for list columns
[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                                                 alert('FIXME: 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                 treechildren_node.appendChild( treeitem );
328                 var treerow = document.createElement('treerow');
329                 treeitem.appendChild( treerow );
330                 treerow.setAttribute('retrieve_id',params.retrieve_id);
331
332                 s += ('tree = ' + this.node + '  treechildren = ' + treechildren_node + '\n');
333                 s += ('treeitem = ' + treeitem + '  treerow = ' + treerow + '\n');
334
335                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
336
337                         obj.put_retrieving_label(treerow);
338                         treerow.addEventListener(
339                                 'flesh',
340                                 function() {
341
342                                         if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
343
344                                         treerow.setAttribute('retrieved','true');
345
346                                         //dump('fleshing = ' + params.retrieve_id + '\n');
347
348                                         function inc_fleshed() {
349                                                 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
350                                                 treerow.setAttribute('fleshed','true');
351                                                 obj.row_count.fleshed++;
352                                                 if (obj.row_count.fleshed == obj.row_count.total) {
353                                                         if (typeof obj.on_all_fleshed == 'function') {
354                                                                 setTimeout( function() { obj.on_all_fleshed(); }, 0 );
355                                                         }
356                                                 }
357                                         }
358
359                                         params.row_node = treeitem;
360                                         params.on_retrieve = function(p) {
361                                                 try {
362                                                         p.row = params.row;
363                                                         obj._map_row_to_treecell(p,treerow);
364                                                         inc_fleshed();
365                                                 } catch(E) {
366                                                         alert('fixme2: ' + E);
367                                                 }
368                                         }
369
370                                         if (typeof params.retrieve_row == 'function') {
371
372                                                 params.retrieve_row( params );
373
374                                         } else if (typeof obj.retrieve_row == 'function') {
375
376                                                         obj.retrieve_row( params );
377
378                                         } else {
379                                         
380                                                         inc_fleshed();
381                                         }
382                                 },
383                                 false
384                         );
385                         /*
386                         setTimeout(
387                                 function() {
388                                         util.widgets.dispatch('flesh',treerow);
389                                 }, 0
390                         );
391                         */
392                 } else {
393                         obj.put_retrieving_label(treerow);
394                         treerow.addEventListener(
395                                 'flesh',
396                                 function() {
397                                         //dump('fleshing anon\n');
398                                         if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
399                                         obj._map_row_to_treecell(params,treerow);
400                                         treerow.setAttribute('retrieved','true');
401                                         treerow.setAttribute('fleshed','true');
402                                         obj.row_count.fleshed++;
403                                         if (obj.row_count.fleshed == obj.row_count.total) {
404                                                 if (typeof obj.on_all_fleshed == 'function') {
405                                                         setTimeout( function() { obj.on_all_fleshed(); }, 0 );
406                                                 }
407                                         }
408                                 },
409                                 false
410                         );
411                         /*
412                         setTimeout(
413                                 function() {
414                                         util.widgets.dispatch('flesh',treerow);
415                                 }, 0
416                         );
417                         */
418                 }
419                 this.error.sdump('D_LIST',s);
420
421                 setTimeout( function() { obj.auto_retrieve(); }, 0 );
422
423                 return treeitem;
424         },
425
426         'put_retrieving_label' : function(treerow) {
427                 var obj = this;
428                 try {
429                         /*
430                         var cols_idx = 0;
431                         dump('put_retrieving_label.  columns = ' + js2JSON(obj.columns) + '\n');
432                         while( obj.columns[cols_idx] && obj.columns[cols_idx].hidden && obj.columns[cols_idx].hidden == 'true') {
433                                 dump('\t' + cols_idx);
434                                 var treecell = document.createElement('treecell');
435                                 treerow.appendChild(treecell);
436                                 cols_idx++;
437                         }
438                         */
439                         for (var i = 0; i < obj.columns.length; i++) {
440                         var treecell = document.createElement('treecell'); treecell.setAttribute('label','Retrieving...');
441                         treerow.appendChild(treecell);
442                         }
443                         /*
444                         dump('\t' + cols_idx + '\n');
445                         */
446                 } catch(E) {
447                         alert(E);
448                 }
449         },
450
451         'detect_visible' : function() {
452                 var obj = this;
453                 try {
454                         //dump('detect_visible  obj.node = ' + obj.node + '\n');
455                         /* FIXME - this is a hack.. if the implementation of tree changes, this could break */
456                         var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(obj.node)[1] )[1];
457                         var curpos = scrollbar.getAttribute('curpos');
458                         var maxpos = scrollbar.getAttribute('maxpos');
459                         //alert('curpos = ' + curpos + ' maxpos = ' + maxpos + ' obj.curpos = ' + obj.curpos + ' obj.maxpos = ' + obj.maxpos + '\n');
460                         if ((curpos != obj.curpos) || (maxpos != obj.maxpos)) {
461                                 if ( obj.auto_retrieve() > 0 ) {
462                                         obj.curpos = curpos; obj.maxpos = maxpos;
463                                 }
464                         }
465                 } catch(E) { obj.error.sdump('D_ERROR',E); }
466         },
467
468         'detect_visible_polling' : function() {
469                 try {
470                         //alert('detect_visible_polling');
471                         var obj = this;
472                         obj.detect_visible();
473                         setTimeout(function() { try { obj.detect_visible_polling(); } catch(E) { alert(E); } },2000);
474                 } catch(E) {
475                         alert(E);
476                 }
477         },
478
479
480         'auto_retrieve' : function(params) {
481                 var obj = this;
482                 switch (this.node.nodeName) {
483                         case 'tree' : obj._auto_retrieve_tree(params); break;
484                         default: throw('NYI: Need .auto_retrieve() for ' + obj.node.nodeName); break;
485                 }
486         },
487
488         '_auto_retrieve_tree' : function (params) {
489                 var obj = this;
490                 if (!obj.auto_retrieve_in_progress) {
491                         obj.auto_retrieve_in_progress = true;
492                         setTimeout(
493                                 function() {
494                                         try {
495                                                         //alert('auto_retrieve\n');
496                                                         var count = 0;
497                                                         var startpos = obj.node.treeBoxObject.getFirstVisibleRow();
498                                                         var endpos = obj.node.treeBoxObject.getLastVisibleRow();
499                                                         if (startpos > endpos) endpos = obj.node.treeBoxObject.getPageLength();
500                                                         //dump('startpos = ' + startpos + ' endpos = ' + endpos + '\n');
501                                                         for (var i = startpos; i < endpos + 4; i++) {
502                                                                 try {
503                                                                         //dump('trying index ' + i + '\n');
504                                                                         var item = obj.node.contentView.getItemAtIndex(i).firstChild;
505                                                                         if (item && item.getAttribute('retrieved') != 'true' ) {
506                                                                                 //dump('\tgot an unfleshed item = ' + item + ' = ' + item.nodeName + '\n');
507                                                                                 util.widgets.dispatch('flesh',item); count++;
508                                                                         }
509                                                                 } catch(E) {
510                                                                         //dump(i + ' : ' + E + '\n');
511                                                                 }
512                                                         }
513                                                         obj.auto_retrieve_in_progress = false;
514                                                         return count;
515                                         } catch(E) { alert(E); }
516                                 }, 1
517                         );
518                 }
519         },
520
521         'full_retrieve' : function(params) {
522                 var obj = this;
523                 switch (this.node.nodeName) {
524                         case 'tree' : obj._full_retrieve_tree(params); break;
525                         default: throw('NYI: Need .full_retrieve() for ' + obj.node.nodeName); break;
526                 }
527         },
528
529         '_full_retrieve_tree' : function(params) {
530                 var obj = this;
531                 try {
532                         if (obj.row_count.total == obj.row_count.fleshed) {
533                                 //alert('Full retrieve... tree seems to be in sync\n' + js2JSON(obj.row_count));
534                                 if (typeof obj.on_all_fleshed == 'function') {
535                                         setTimeout( function() { obj.on_all_fleshed(); }, 0 );
536                                 } else {
537                                         alert('.full_retrieve called with no callback?');
538                                 }
539                         } else {
540                                 //alert('Full retrieve... syncing tree' + js2JSON(obj.row_count));
541                                 JSAN.use('util.widgets');
542                                 var nodes = obj.treechildren.childNodes;
543                                 for (var i = 0; i < nodes.length; i++) {
544                                         util.widgets.dispatch('flesh',nodes[i].firstChild);
545                                 }
546                         }
547                 } catch(E) {
548                         obj.error.standard_unexpected_error_alert('_full_retrieve_tree',E);
549                 }
550         },
551
552         '_append_to_listbox' : function (params) {
553
554                 var obj = this;
555
556                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
557
558                 var s = ('util.list.append: params = ' + (params) + '\n');
559
560                 var listitem = document.createElement('listitem');
561
562                 s += ('listbox = ' + this.node + '  listitem = ' + listitem + '\n');
563
564                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
565
566                         setTimeout(
567                                 function() {
568                                         listitem.setAttribute('retrieve_id',params.retrieve_id);
569                                         //FIXME//Make async and fire when row is visible in list
570                                         var row;
571
572                                         params.row_node = listitem;
573                                         params.on_retrieve = function(row) {
574                                                 params.row = row;
575                                                 obj._map_row_to_listcell(params,listitem);
576                                                 obj.node.appendChild( listitem );
577                                         }
578
579                                         if (typeof params.retrieve_row == 'function') {
580
581                                                 row = params.retrieve_row( params );
582
583                                         } else {
584
585                                                 if (typeof obj.retrieve_row == 'function') {
586
587                                                         row = obj.retrieve_row( params );
588
589                                                 }
590                                         }
591                                 }, 0
592                         );
593                 } else {
594                         this._map_row_to_listcell(params,listitem);
595                         this.node.appendChild( listitem );
596                 }
597
598                 this.error.sdump('D_LIST',s);
599                 return listitem;
600
601         },
602
603         '_map_row_to_treecell' : function(params,treerow) {
604                 var obj = this;
605                 var s = '';
606                 util.widgets.remove_children(treerow);
607                 for (var i = 0; i < this.columns.length; i++) {
608                         var treecell = document.createElement('treecell');
609                         var label = '';
610                         if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
611                                 treecell.setAttribute('label',label);
612                                 treerow.appendChild( treecell );
613                                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
614                                 continue;
615                         }
616                         if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
617                                 treecell.setAttribute('label',label);
618                                 treerow.appendChild( treecell );
619                                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
620                                 continue;
621                         }
622                         if (typeof params.map_row_to_column == 'function')  {
623
624                                 label = params.map_row_to_column(params.row,this.columns[i]);
625
626                         } else {
627
628                                 if (typeof this.map_row_to_column == 'function') {
629
630                                         label = this.map_row_to_column(params.row,this.columns[i]);
631
632                                 } else {
633
634                                         throw('No map_row_to_column function');
635
636                                 }
637                         }
638                         treecell.setAttribute('label',label);
639                         treerow.appendChild( treecell );
640                         s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
641                 }
642                 this.error.sdump('D_LIST',s);
643         },
644
645         '_map_row_to_listcell' : function(params,listitem) {
646                 var obj = this;
647                 var s = '';
648                 for (var i = 0; i < this.columns.length; i++) {
649                         var value = '';
650                         if (typeof params.map_row_to_column == 'function')  {
651
652                                 value = params.map_row_to_column(params.row,this.columns[i]);
653
654                         } else {
655
656                                 if (typeof this.map_row_to_column == 'function') {
657
658                                         value = this.map_row_to_column(params.row,this.columns[i]);
659                                 }
660                         }
661                         if (typeof value == 'string' || typeof value == 'number') {
662                                 var listcell = document.createElement('listcell');
663                                 listcell.setAttribute('label',value);
664                                 listitem.appendChild(listcell);
665                                 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
666                         } else {
667                                 listitem.appendChild(value);
668                                 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
669                         }
670                 }
671                 this.error.sdump('D_LIST',s);
672         },
673
674         'select_all' : function(params) {
675                 var obj = this;
676                 switch(this.node.nodeName) {
677                         case 'tree' : return this._select_all_from_tree(params); break;
678                         default: throw('NYI: Need ._select_all_from_() for ' + this.node.nodeName); break;
679                 }
680         },
681
682         '_select_all_from_tree' : function(params) {
683                 var obj = this;
684                 this.node.view.selection.selectAll();
685         },
686
687         'retrieve_selection' : function(params) {
688                 var obj = this;
689                 switch(this.node.nodeName) {
690                         case 'tree' : return this._retrieve_selection_from_tree(params); break;
691                         default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
692                 }
693         },
694
695         '_retrieve_selection_from_tree' : function(params) {
696                 var obj = this;
697                 var list = [];
698                 var start = new Object();
699                 var end = new Object();
700                 var numRanges = this.node.view.selection.getRangeCount();
701                 for (var t=0; t<numRanges; t++){
702                         this.node.view.selection.getRangeAt(t,start,end);
703                         for (var v=start.value; v<=end.value; v++){
704                                 var i = this.node.contentView.getItemAtIndex(v);
705                                 list.push( i );
706                         }
707                 }
708                 return list;
709         },
710
711         'dump' : function(params) {
712                 var obj = this;
713                 switch(this.node.nodeName) {
714                         case 'tree' : return this._dump_tree(params); break;
715                         default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
716                 }
717         },
718
719         '_dump_tree' : function(params) {
720                 var obj = this;
721                 var dump = [];
722                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
723                         var row = [];
724                         var treeitem = this.treechildren.childNodes[i];
725                         var treerow = treeitem.firstChild;
726                         for (var j = 0; j < treerow.childNodes.length; j++) {
727                                 row.push( treerow.childNodes[j].getAttribute('label') );
728                         }
729                         dump.push( row );
730                 }
731                 return dump;
732         },
733
734         'dump_with_keys' : function(params) {
735                 var obj = this;
736                 switch(this.node.nodeName) {
737                         case 'tree' : return this._dump_tree_with_keys(params); break;
738                         default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
739                 }
740
741         },
742
743         '_dump_tree_with_keys' : function(params) {
744                 var obj = this;
745                 var dump = [];
746                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
747                         var row = {};
748                         var treeitem = this.treechildren.childNodes[i];
749                         var treerow = treeitem.firstChild;
750                         for (var j = 0; j < treerow.childNodes.length; j++) {
751                                 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
752                         }
753                         dump.push( row );
754                 }
755                 return dump;
756         },
757
758         'dump_selected_with_keys' : function(params) {
759                 var obj = this;
760                 switch(this.node.nodeName) {
761                         case 'tree' : return this._dump_tree_selection_with_keys(params); break;
762                         default: throw('NYI: Need .dump_selection_with_keys() for ' + this.node.nodeName); break;
763                 }
764
765         },
766
767         '_dump_tree_selection_with_keys' : function(params) {
768                 var obj = this;
769                 var dump = [];
770                 var list = obj._retrieve_selection_from_tree();
771                 for (var i = 0; i < list.length; i++) {
772                         var row = {};
773                         var treeitem = list[i];
774                         var treerow = treeitem.firstChild;
775                         for (var j = 0; j < treerow.childNodes.length; j++) {
776                                 var value = treerow.childNodes[j].getAttribute('label');
777                                 //FIXME
778                                 //if (params.skip_hidden_columns) if (obj.node.firstChild.childNodes[j].getAttribute('hidden')) continue;
779                                 var id = obj.columns[j].id; if (params.labels_instead_of_ids) id = obj.columns[j].label;
780                                 row[ id ] = value;
781                         }
782                         dump.push( row );
783                 }
784                 return dump;
785         },
786
787         'clipboard' : function() {
788                 try {
789                         var obj = this;
790                         var dump = obj.dump_selected_with_keys({'skip_hidden_columns':true,'labels_instead_of_ids':true});
791                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
792                         data.list_clipboard = dump; data.stash('list_clipboard');
793                         JSAN.use('util.window'); var win = new util.window();
794                         win.open(urls.XUL_LIST_CLIPBOARD,'list_clipboard','chrome,resizable,modal');
795                 } catch(E) {
796                         this.error.standard_unexpected_error_alert('clipboard',E);
797                 }
798         },
799
800         'dump_retrieve_ids' : function(params) {
801                 var obj = this;
802                 switch(this.node.nodeName) {
803                         case 'tree' : return this._dump_retrieve_ids_tree(params); break;
804                         default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
805                 }
806         },
807
808         '_dump_retrieve_ids_tree' : function(params) {
809                 var obj = this;
810                 var dump = [];
811                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
812                         var treeitem = this.treechildren.childNodes[i];
813                         dump.push( treeitem.getAttribute('retrieve_id') );
814                 }
815                 return dump;
816         },
817
818         '_sort_tree' : function(col,sortDir) {
819                 var obj = this;
820                 try {
821                         if (obj.node.getAttribute('no_sort')) {
822                                 return;
823                         }
824                         if (obj.on_all_fleshed) {
825                                 alert('This list is busy rendering/retrieving data.');
826                                 return;
827                         }
828                         var col_pos;
829                         for (var i = 0; i < obj.columns.length; i++) { 
830                                 if (obj.columns[i].id == col.id) col_pos = function(a){return a;}(i); 
831                         }
832                         obj.on_all_fleshed =
833                                 function() {
834                                         try {
835                                                 JSAN.use('util.money');
836                                                 var rows = [];
837                                                 var treeitems = obj.treechildren.childNodes;
838                                                 for (var i = 0; i < treeitems.length; i++) {
839                                                         var treeitem = treeitems[i];
840                                                         var treerow = treeitem.firstChild;
841                                                         var treecell = treerow.childNodes[ col_pos ];
842                                                         //alert('treeitem = ' + treeitem.nodeName + ' treeitem.childNodes.length = ' + treeitem.childNodes.length + ' treerow = ' + treerow.nodeName + ' treerow.childNodes.length = ' + treerow.childNodes.length + ' col_pos = ' + col_pos + ' treecell = ' + treecell);
843                                                         value = ( { 'value' : treecell ? treecell.getAttribute('label') : '', 'node' : treeitem } );
844                                                         //alert('value = ' + value.value + ' node = ' + value.node);
845                                                         rows.push( value );
846                                                 }
847                                                 rows = rows.sort( function(a,b) { 
848                                                         a = a.value; b = b.value; 
849                                                         if (col.getAttribute('sort_type')) {
850                                                                 switch(col.getAttribute('sort_type')) {
851                                                                         case 'number' :
852                                                                                 a = Number(a); b = Number(b);
853                                                                         break;
854                                                                         case 'money' :
855                                                                                 a = util.money.dollars_float_to_cents_integer(a);
856                                                                                 b = util.money.dollars_float_to_cents_integer(b);
857                                                                         break;
858                                                                 }
859                                                         }
860                                                         if (a < b) return -1; 
861                                                         if (a > b) return 1; 
862                                                         return 0; 
863                                                 } );
864                                                 if (sortDir == 'asc') rows = rows.reverse();
865                                                 while(obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
866                                                 for (var i = 0; i < rows.length; i++) {
867                                                         obj.treechildren.appendChild( rows[i].node );
868                                                 }
869                                         } catch(E) {
870                                                 obj.error.standard_unexpected_error_alert('sorting',E); 
871                                         }
872                                         setTimeout(function(){ obj.on_all_fleshed = null; },0);
873                                 }
874                         obj.full_retrieve();
875                 } catch(E) {
876                         obj.error.standard_unexpected_error_alert('pre sorting', E);
877                 }
878         },
879
880 }
881 dump('exiting util.list.js\n');