]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/list.js
put polling back in to catch unfleshed rows
[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                                 var splitter = document.createElement('splitter');
69                                 splitter.setAttribute('class','tree-splitter');
70                                 treecols.appendChild(splitter);
71                         }
72
73                         var treechildren = document.createElement('treechildren');
74                         this.node.appendChild(treechildren);
75                         this.treechildren = treechildren;
76                 }
77                 if (typeof params.on_select == 'function') {
78                         this.node.addEventListener(
79                                 'select',
80                                 params.on_select,
81                                 false
82                         );
83                 }
84                 if (typeof params.on_click == 'function') {
85                         this.node.addEventListener(
86                                 'click',
87                                 params.on_click,
88                                 false
89                         );
90                 }
91                 /*
92                 this.node.addEventListener(
93                         'mousemove',
94                         function(ev) { obj.detect_visible(); },
95                         false
96                 );
97                 */
98                 this.node.addEventListener(
99                         'keypress',
100                         function(ev) { obj.auto_retrieve(); },
101                         false
102                 );
103                 this.node.addEventListener(
104                         'click',
105                         function(ev) { obj.auto_retrieve(); },
106                         false
107                 );
108                 window.addEventListener(
109                         'resize',
110                         function(ev) { obj.auto_retrieve(); },
111                         false
112                 );
113                 /* FIXME -- find events on scrollbar to trigger this */
114                 obj.detect_visible_polling();   
115                 /*
116                 var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(this.node)[1] )[1];
117                 var slider = document.getAnonymousNodes( scrollbar )[2];
118                 alert('scrollbar = ' + scrollbar.nodeName + ' grippy = ' + slider.nodeName);
119                 scrollbar.addEventListener('click',function(){alert('sb click');},false);
120                 scrollbar.addEventListener('command',function(){alert('sb command');},false);
121                 scrollbar.addEventListener('scroll',function(){alert('sb scroll');},false);
122                 slider.addEventListener('click',function(){alert('slider click');},false);
123                 slider.addEventListener('command',function(){alert('slider command');},false);
124                 slider.addEventListener('scroll',function(){alert('slider scroll');},false);
125                 */
126                 this.node.addEventListener('scroll',function(){ obj.auto_retrieve(); },false);
127         },
128
129         '_init_listbox' : function (params) {
130                 if (this.prebuilt) {
131                 } else {
132                         var listhead = document.createElement('listhead');
133                         this.node.appendChild(listhead);
134
135                         var listcols = document.createElement('listcols');
136                         this.node.appendChild(listcols);
137
138                         for (var i = 0; i < this.columns.length; i++) {
139                                 var listheader = document.createElement('listheader');
140                                 listhead.appendChild(listheader);
141                                 var listcol = document.createElement('listcol');
142                                 listcols.appendChild(listcol);
143                                 for (var j in this.columns[i]) {
144                                         listheader.setAttribute(j,this.columns[i][j]);
145                                         listcol.setAttribute(j,this.columns[i][j]);
146                                 };
147                         }
148                 }
149         },
150
151         'clear' : function (params) {
152                 switch (this.node.nodeName) {
153                         case 'tree' : this._clear_tree(params); break;
154                         case 'listbox' : this._clear_listbox(params); break;
155                         default: throw('NYI: Need .clear() for ' + this.node.nodeName); break;
156                 }
157                 this.error.sdump('D_LIST','Clearing list ' + this.node.getAttribute('id') + '\n');
158                 this.row_count.total = 0;
159                 this.row_count.fleshed = 0;
160                 if (typeof obj.on_all_fleshed == 'function') {
161                         setTimeout( function() { obj.on_all_fleshed(); }, 0 );
162                 }
163         },
164
165         '_clear_tree' : function(params) {
166                 var obj = this;
167                 if (obj.error.sdump_levels.D_LIST_DUMP_ON_CLEAR) {
168                         obj.error.sdump('D_LIST_DUMP_ON_CLEAR',obj.dump());
169                 }
170                 if (obj.error.sdump_levels.D_LIST_DUMP_WITH_KEYS_ON_CLEAR) {
171                         obj.error.sdump('D_LIST_DUMP_WITH_KEYS_ON_CLEAR',obj.dump_with_keys());
172                 }
173                 while (obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
174         },
175
176         '_clear_listbox' : function(params) {
177                 var items = [];
178                 var nl = this.node.getElementsByTagName('listitem');
179                 for (var i = 0; i < nl.length; i++) {
180                         items.push( nl[i] );
181                 }
182                 for (var i = 0; i < items.length; i++) {
183                         this.node.removeChild(items[i]);
184                 }
185         },
186
187         'append' : function (params) {
188                 var rnode;
189                 var obj = this;
190                 switch (this.node.nodeName) {
191                         case 'tree' : rnode = this._append_to_tree(params); break;
192                         case 'listbox' : rnode = this._append_to_listbox(params); break;
193                         default: throw('NYI: Need .append() for ' + this.node.nodeName); break;
194                 }
195                 if (rnode && params.attributes) {
196                         for (var i in params.attributes) {
197                                 rnode.setAttribute(i,params.attributes[i]);
198                         }
199                 }
200                 this.row_count.total++;
201                 if (this.row_count.fleshed == this.row_count.total) {
202                         if (typeof this.on_all_fleshed == 'function') {
203                                 setTimeout( function() { obj.on_all_fleshed(); }, 0 );
204                         }
205                 }
206                 return rnode;
207         },
208
209         '_append_to_tree' : function (params) {
210
211                 var obj = this;
212
213                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
214
215                 var s = ('util.list.append: params = ' + (params) + '\n');
216
217                 var treechildren_node = this.treechildren;
218
219                 if (params.node && params.node.nodeName == 'treeitem') {
220                         params.node.setAttribute('container','true'); /* params.node.setAttribute('open','true'); */
221                         if (params.node.lastChild.nodeName == 'treechildren') {
222                                 treechildren_node = params.node.lastChild;
223                         } else {
224                                 treechildren_node = document.createElement('treechildren');
225                                 params.node.appendChild(treechildren_node);
226                         }
227                 }
228
229                 var treeitem = document.createElement('treeitem');
230                 treeitem.setAttribute('retrieve_id',params.retrieve_id);
231                 treechildren_node.appendChild( treeitem );
232                 var treerow = document.createElement('treerow');
233                 treeitem.appendChild( treerow );
234                 treerow.setAttribute('retrieve_id',params.retrieve_id);
235
236                 s += ('tree = ' + this.node + '  treechildren = ' + treechildren_node + '\n');
237                 s += ('treeitem = ' + treeitem + '  treerow = ' + treerow + '\n');
238
239                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
240
241                         obj.put_retrieving_label(treerow);
242                         treerow.addEventListener(
243                                 'flesh',
244                                 function() {
245
246                                         if (treerow.getAttribute('retrieved') == 'true') return; /* already running */
247
248                                         treerow.setAttribute('retrieved','true');
249
250                                         //dump('fleshing = ' + params.retrieve_id + '\n');
251
252                                         function inc_fleshed() {
253                                                 if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
254                                                 treerow.setAttribute('fleshed','true');
255                                                 obj.row_count.fleshed++;
256                                                 if (obj.row_count.fleshed == obj.row_count.total) {
257                                                         if (typeof obj.on_all_fleshed == 'function') {
258                                                                 setTimeout( function() { obj.on_all_fleshed(); }, 0 );
259                                                         }
260                                                 }
261                                         }
262
263                                         params.row_node = treeitem;
264                                         params.on_retrieve = function(p) {
265                                                 try {
266                                                         p.row = params.row;
267                                                         obj._map_row_to_treecell(p,treerow);
268                                                         inc_fleshed();
269                                                 } catch(E) {
270                                                         alert('fixme2: ' + E);
271                                                 }
272                                         }
273
274                                         if (typeof params.retrieve_row == 'function') {
275
276                                                 params.retrieve_row( params );
277
278                                         } else if (typeof obj.retrieve_row == 'function') {
279
280                                                         obj.retrieve_row( params );
281
282                                         } else {
283                                         
284                                                         inc_fleshed();
285                                         }
286                                 },
287                                 false
288                         );
289                         /*
290                         setTimeout(
291                                 function() {
292                                         util.widgets.dispatch('flesh',treerow);
293                                 }, 0
294                         );
295                         */
296                 } else {
297                         obj.put_retrieving_label(treerow);
298                         treerow.addEventListener(
299                                 'flesh',
300                                 function() {
301                                         //dump('fleshing anon\n');
302                                         if (treerow.getAttribute('fleshed') == 'true') return; /* already fleshed */
303                                         obj._map_row_to_treecell(params,treerow);
304                                         treerow.setAttribute('retrieved','true');
305                                         treerow.setAttribute('fleshed','true');
306                                         obj.row_count.fleshed++;
307                                         if (obj.row_count.fleshed == obj.row_count.total) {
308                                                 if (typeof obj.on_all_fleshed == 'function') {
309                                                         setTimeout( function() { obj.on_all_fleshed(); }, 0 );
310                                                 }
311                                         }
312                                 },
313                                 false
314                         );
315                         /*
316                         setTimeout(
317                                 function() {
318                                         util.widgets.dispatch('flesh',treerow);
319                                 }, 0
320                         );
321                         */
322                 }
323                 this.error.sdump('D_LIST',s);
324
325                 setTimeout( function() { obj.auto_retrieve(); }, 0 );
326
327                 return treeitem;
328         },
329
330         'put_retrieving_label' : function(treerow) {
331                 var obj = this;
332                 try {
333                         /*
334                         var cols_idx = 0;
335                         dump('put_retrieving_label.  columns = ' + js2JSON(obj.columns) + '\n');
336                         while( obj.columns[cols_idx] && obj.columns[cols_idx].hidden && obj.columns[cols_idx].hidden == 'true') {
337                                 dump('\t' + cols_idx);
338                                 var treecell = document.createElement('treecell');
339                                 treerow.appendChild(treecell);
340                                 cols_idx++;
341                         }
342                         */
343                         for (var i = 0; i < obj.columns.length; i++) {
344                         var treecell = document.createElement('treecell'); treecell.setAttribute('label','Retrieving...');
345                         treerow.appendChild(treecell);
346                         }
347                         /*
348                         dump('\t' + cols_idx + '\n');
349                         */
350                 } catch(E) {
351                         alert(E);
352                 }
353         },
354
355         'detect_visible' : function() {
356                 try {
357                         var obj = this;
358                         //dump('detect_visible  obj.node = ' + obj.node + '\n');
359                         /* FIXME - this is a hack.. if the implementation of tree changes, this could break */
360                         var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(obj.node)[1] )[1];
361                         var curpos = scrollbar.getAttribute('curpos');
362                         var maxpos = scrollbar.getAttribute('maxpos');
363                         //alert('curpos = ' + curpos + ' maxpos = ' + maxpos + ' obj.curpos = ' + obj.curpos + ' obj.maxpos = ' + obj.maxpos + '\n');
364                         if ((curpos != obj.curpos) || (maxpos != obj.maxpos)) {
365                                 if ( obj.auto_retrieve() > 0 ) {
366                                         obj.curpos = curpos; obj.maxpos = maxpos;
367                                 }
368                         }
369                 } catch(E) { alert(E); }
370         },
371
372         'detect_visible_polling' : function() {
373                 try {
374                         //alert('detect_visible_polling');
375                         var obj = this;
376                         obj.detect_visible();
377                         setTimeout(function() { try { obj.detect_visible_polling(); } catch(E) { alert(E); } },2000);
378                 } catch(E) {
379                         alert(E);
380                 }
381         },
382
383
384         'auto_retrieve' : function(params) {
385                 var obj = this;
386                 switch (this.node.nodeName) {
387                         case 'tree' : obj._auto_retrieve_tree(params); break;
388                         default: throw('NYI: Need .auto_retrieve() for ' + obj.node.nodeName); break;
389                 }
390         },
391
392         '_auto_retrieve_tree' : function (params) {
393                 var obj = this;
394                 if (!obj.auto_retrieve_in_progress) {
395                         obj.auto_retrieve_in_progress = true;
396                         setTimeout(
397                                 function() {
398                                         try {
399                                                         //alert('auto_retrieve\n');
400                                                         var count = 0;
401                                                         var startpos = obj.node.treeBoxObject.getFirstVisibleRow();
402                                                         var endpos = obj.node.treeBoxObject.getLastVisibleRow();
403                                                         if (startpos > endpos) endpos = obj.node.treeBoxObject.getPageLength();
404                                                         //dump('startpos = ' + startpos + ' endpos = ' + endpos + '\n');
405                                                         for (var i = startpos; i < endpos + 2; i++) {
406                                                                 try {
407                                                                         //dump('trying index ' + i + '\n');
408                                                                         var item = obj.node.contentView.getItemAtIndex(i).firstChild;
409                                                                         if (item && item.getAttribute('retrieved') != 'true' ) {
410                                                                                 //dump('\tgot an unfleshed item = ' + item + ' = ' + item.nodeName + '\n');
411                                                                                 util.widgets.dispatch('flesh',item); count++;
412                                                                         }
413                                                                 } catch(E) {
414                                                                         //dump(i + ' : ' + E + '\n');
415                                                                 }
416                                                         }
417                                                         obj.auto_retrieve_in_progress = false;
418                                                         return count;
419                                         } catch(E) { alert(E); }
420                                 }, 1
421                         );
422                 }
423         },
424
425         'full_retrieve' : function(params) {
426                 var obj = this;
427                 switch (this.node.nodeName) {
428                         case 'tree' : obj._full_retrieve_tree(params); break;
429                         default: throw('NYI: Need .full_retrieve() for ' + obj.node.nodeName); break;
430                 }
431         },
432
433         '_full_retrieve_tree' : function(params) {
434                 var obj = this;
435                 try {
436                         if (obj.row_count.total == obj.row_count.fleshed) {
437                                 //alert('Full retrieve... tree seems to be in sync\n' + js2JSON(obj.row_count));
438                                 if (typeof obj.on_all_fleshed == 'function') {
439                                         setTimeout( function() { obj.on_all_fleshed(); }, 0 );
440                                 } else {
441                                         alert('.full_retrieve called with no callback?');
442                                 }
443                         } else {
444                                 //alert('Full retrieve... syncing tree' + js2JSON(obj.row_count));
445                                 JSAN.use('util.widgets');
446                                 var nodes = obj.treechildren.childNodes;
447                                 for (var i = 0; i < nodes.length; i++) {
448                                         util.widgets.dispatch('flesh',nodes[i].firstChild);
449                                 }
450                         }
451                 } catch(E) {
452                         obj.error.standard_unexpected_error_alert('_full_retrieve_tree',E);
453                 }
454         },
455
456         '_append_to_listbox' : function (params) {
457
458                 var obj = this;
459
460                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
461
462                 var s = ('util.list.append: params = ' + (params) + '\n');
463
464                 var listitem = document.createElement('listitem');
465
466                 s += ('listbox = ' + this.node + '  listitem = ' + listitem + '\n');
467
468                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
469
470                         setTimeout(
471                                 function() {
472                                         listitem.setAttribute('retrieve_id',params.retrieve_id);
473                                         //FIXME//Make async and fire when row is visible in list
474                                         var row;
475
476                                         params.row_node = listitem;
477                                         params.on_retrieve = function(row) {
478                                                 params.row = row;
479                                                 obj._map_row_to_listcell(params,listitem);
480                                                 obj.node.appendChild( listitem );
481                                         }
482
483                                         if (typeof params.retrieve_row == 'function') {
484
485                                                 row = params.retrieve_row( params );
486
487                                         } else {
488
489                                                 if (typeof obj.retrieve_row == 'function') {
490
491                                                         row = obj.retrieve_row( params );
492
493                                                 }
494                                         }
495                                 }, 0
496                         );
497                 } else {
498                         this._map_row_to_listcell(params,listitem);
499                         this.node.appendChild( listitem );
500                 }
501
502                 this.error.sdump('D_LIST',s);
503                 return listitem;
504
505         },
506
507         '_map_row_to_treecell' : function(params,treerow) {
508                 var s = '';
509                 util.widgets.remove_children(treerow);
510                 for (var i = 0; i < this.columns.length; i++) {
511                         var treecell = document.createElement('treecell');
512                         var label = '';
513                         if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
514                                 treecell.setAttribute('label',label);
515                                 treerow.appendChild( treecell );
516                                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
517                                 continue;
518                         }
519                         if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
520                                 treecell.setAttribute('label',label);
521                                 treerow.appendChild( treecell );
522                                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
523                                 continue;
524                         }
525                         if (typeof params.map_row_to_column == 'function')  {
526
527                                 label = params.map_row_to_column(params.row,this.columns[i]);
528
529                         } else {
530
531                                 if (typeof this.map_row_to_column == 'function') {
532
533                                         label = this.map_row_to_column(params.row,this.columns[i]);
534
535                                 } else {
536
537                                         throw('No map_row_to_column function');
538
539                                 }
540                         }
541                         treecell.setAttribute('label',label);
542                         treerow.appendChild( treecell );
543                         s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
544                 }
545                 this.error.sdump('D_LIST',s);
546         },
547
548         '_map_row_to_listcell' : function(params,listitem) {
549                 var s = '';
550                 for (var i = 0; i < this.columns.length; i++) {
551                         var value = '';
552                         if (typeof params.map_row_to_column == 'function')  {
553
554                                 value = params.map_row_to_column(params.row,this.columns[i]);
555
556                         } else {
557
558                                 if (typeof this.map_row_to_column == 'function') {
559
560                                         value = this.map_row_to_column(params.row,this.columns[i]);
561                                 }
562                         }
563                         if (typeof value == 'string' || typeof value == 'number') {
564                                 var listcell = document.createElement('listcell');
565                                 listcell.setAttribute('label',value);
566                                 listitem.appendChild(listcell);
567                                 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
568                         } else {
569                                 listitem.appendChild(value);
570                                 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
571                         }
572                 }
573                 this.error.sdump('D_LIST',s);
574         },
575
576         'retrieve_selection' : function(params) {
577                 switch(this.node.nodeName) {
578                         case 'tree' : return this._retrieve_selection_from_tree(params); break;
579                         default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
580                 }
581         },
582
583         '_retrieve_selection_from_tree' : function(params) {
584                 var list = [];
585                 var start = new Object();
586                 var end = new Object();
587                 var numRanges = this.node.view.selection.getRangeCount();
588                 for (var t=0; t<numRanges; t++){
589                         this.node.view.selection.getRangeAt(t,start,end);
590                         for (var v=start.value; v<=end.value; v++){
591                                 var i = this.node.contentView.getItemAtIndex(v);
592                                 list.push( i );
593                         }
594                 }
595                 return list;
596         },
597
598         'dump' : function(params) {
599                 switch(this.node.nodeName) {
600                         case 'tree' : return this._dump_tree(params); break;
601                         default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
602                 }
603         },
604
605         '_dump_tree' : function(params) {
606                 var dump = [];
607                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
608                         var row = [];
609                         var treeitem = this.treechildren.childNodes[i];
610                         var treerow = treeitem.firstChild;
611                         for (var j = 0; j < treerow.childNodes.length; j++) {
612                                 row.push( treerow.childNodes[j].getAttribute('label') );
613                         }
614                         dump.push( row );
615                 }
616                 return dump;
617         },
618
619         'dump_with_keys' : function(params) {
620                 switch(this.node.nodeName) {
621                         case 'tree' : return this._dump_tree_with_keys(params); break;
622                         default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
623                 }
624
625         },
626
627         '_dump_tree_with_keys' : function(params) {
628                 var obj = this;
629                 var dump = [];
630                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
631                         var row = {};
632                         var treeitem = this.treechildren.childNodes[i];
633                         var treerow = treeitem.firstChild;
634                         for (var j = 0; j < treerow.childNodes.length; j++) {
635                                 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
636                         }
637                         dump.push( row );
638                 }
639                 return dump;
640         },
641
642         'dump_retrieve_ids' : function(params) {
643                 switch(this.node.nodeName) {
644                         case 'tree' : return this._dump_retrieve_ids_tree(params); break;
645                         default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
646                 }
647         },
648
649         '_dump_retrieve_ids_tree' : function(params) {
650                 var dump = [];
651                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
652                         var treeitem = this.treechildren.childNodes[i];
653                         dump.push( treeitem.getAttribute('retrieve_id') );
654                 }
655                 return dump;
656         },
657
658 }
659 dump('exiting util.list.js\n');