]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/list.js
reduce CPU usage.. this timeout code is a kludge, so need to replace it one day
[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         'init' : function (params) {
25
26                 JSAN.use('util.widgets');
27
28                 if (typeof params.map_row_to_column == 'function') this.map_row_to_column = params.map_row_to_column;
29                 if (typeof params.retrieve_row == 'function') this.retrieve_row = params.retrieve_row;
30
31                 this.prebuilt = false;
32                 if (typeof params.prebuilt != 'undefined') this.prebuilt = params.prebuilt;
33
34                 if (typeof params.columns == 'undefined') throw('util.list.init: No columns');
35                 this.columns = params.columns;
36
37                 switch(this.node.nodeName) {
38                         case 'tree' : this._init_tree(params); break;
39                         case 'listbox' : this._init_listbox(params); break;
40                         default: throw('NYI: Need ._init() for ' + this.node.nodeName); break;
41                 }
42         },
43
44         '_init_tree' : function (params) {
45                 var obj = this;
46                 if (this.prebuilt) {
47                 
48                         this.treechildren = this.node.lastChild;        
49                 
50                 } else {
51                         var treecols = document.createElement('treecols');
52                         this.node.appendChild(treecols);
53
54                         for (var i = 0; i < this.columns.length; i++) {
55                                 var treecol = document.createElement('treecol');
56                                 for (var j in this.columns[i]) {
57                                         treecol.setAttribute(j,this.columns[i][j]);
58                                 }
59                                 treecols.appendChild(treecol);
60                                 var splitter = document.createElement('splitter');
61                                 splitter.setAttribute('class','tree-splitter');
62                                 treecols.appendChild(splitter);
63                         }
64
65                         var treechildren = document.createElement('treechildren');
66                         this.node.appendChild(treechildren);
67                         this.treechildren = treechildren;
68                 }
69                 if (typeof params.on_select == 'function') {
70                         this.node.addEventListener(
71                                 'select',
72                                 params.on_select,
73                                 false
74                         );
75                 }
76                 if (typeof params.on_click == 'function') {
77                         this.node.addEventListener(
78                                 'click',
79                                 params.on_click,
80                                 false
81                         );
82                 }
83                 /*
84                 this.node.addEventListener(
85                         'mousemove',
86                         function(ev) { obj.detect_visible(); },
87                         false
88                 );
89                 */
90                 this.node.addEventListener(
91                         'keypress',
92                         function(ev) { obj.detect_visible(); },
93                         false
94                 );
95                 this.node.addEventListener(
96                         'click',
97                         function(ev) { obj.detect_visible(); },
98                         false
99                 );
100                 window.addEventListener(
101                         'resize',
102                         function(ev) { obj.auto_retrieve(); },
103                         false
104                 );
105                 /* FIXME -- find events on scrollbar to trigger this */
106                 obj.detect_visible_polling();   
107         },
108
109         '_init_listbox' : function (params) {
110                 if (this.prebuilt) {
111                 } else {
112                         var listhead = document.createElement('listhead');
113                         this.node.appendChild(listhead);
114
115                         var listcols = document.createElement('listcols');
116                         this.node.appendChild(listcols);
117
118                         for (var i = 0; i < this.columns.length; i++) {
119                                 var listheader = document.createElement('listheader');
120                                 listhead.appendChild(listheader);
121                                 var listcol = document.createElement('listcol');
122                                 listcols.appendChild(listcol);
123                                 for (var j in this.columns[i]) {
124                                         listheader.setAttribute(j,this.columns[i][j]);
125                                         listcol.setAttribute(j,this.columns[i][j]);
126                                 };
127                         }
128                 }
129         },
130
131         'clear' : function (params) {
132                 switch (this.node.nodeName) {
133                         case 'tree' : this._clear_tree(params); break;
134                         case 'listbox' : this._clear_listbox(params); break;
135                         default: throw('NYI: Need .clear() for ' + this.node.nodeName); break;
136                 }
137                 this.error.sdump('D_LIST','Clearing list ' + this.node.getAttribute('id') + '\n');
138         },
139
140         '_clear_tree' : function(params) {
141                 var obj = this;
142                 if (obj.error.sdump_levels.D_LIST_DUMP_ON_CLEAR) {
143                         obj.error.sdump('D_LIST_DUMP_ON_CLEAR',obj.dump());
144                 }
145                 if (obj.error.sdump_levels.D_LIST_DUMP_WITH_KEYS_ON_CLEAR) {
146                         obj.error.sdump('D_LIST_DUMP_WITH_KEYS_ON_CLEAR',obj.dump_with_keys());
147                 }
148                 while (obj.treechildren.lastChild) obj.treechildren.removeChild( obj.treechildren.lastChild );
149         },
150
151         '_clear_listbox' : function(params) {
152                 var items = [];
153                 var nl = this.node.getElementsByTagName('listitem');
154                 for (var i = 0; i < nl.length; i++) {
155                         items.push( nl[i] );
156                 }
157                 for (var i = 0; i < items.length; i++) {
158                         this.node.removeChild(items[i]);
159                 }
160         },
161
162         'append' : function (params) {
163                 var rnode;
164                 switch (this.node.nodeName) {
165                         case 'tree' : rnode = this._append_to_tree(params); break;
166                         case 'listbox' : rnode = this._append_to_listbox(params); break;
167                         default: throw('NYI: Need .append() for ' + this.node.nodeName); break;
168                 }
169                 if (rnode && params.attributes) {
170                         for (var i in params.attributes) {
171                                 rnode.setAttribute(i,params.attributes[i]);
172                         }
173                 }
174                 return rnode;
175         },
176
177         '_append_to_tree' : function (params) {
178
179                 var obj = this;
180
181                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
182
183                 var s = ('util.list.append: params = ' + (params) + '\n');
184
185                 var treechildren_node = this.treechildren;
186
187                 if (params.node && params.node.nodeName == 'treeitem') {
188                         params.node.setAttribute('container','true'); /* params.node.setAttribute('open','true'); */
189                         if (params.node.lastChild.nodeName == 'treechildren') {
190                                 treechildren_node = params.node.lastChild;
191                         } else {
192                                 treechildren_node = document.createElement('treechildren');
193                                 params.node.appendChild(treechildren_node);
194                         }
195                 }
196
197                 var treeitem = document.createElement('treeitem');
198                 treeitem.setAttribute('retrieve_id',params.retrieve_id);
199                 treechildren_node.appendChild( treeitem );
200                 var treerow = document.createElement('treerow');
201                 treeitem.appendChild( treerow );
202
203                 s += ('tree = ' + this.node + '  treechildren = ' + treechildren_node + '\n');
204                 s += ('treeitem = ' + treeitem + '  treerow = ' + treerow + '\n');
205
206                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
207
208                         treerow.setAttribute('retrieve_id',params.retrieve_id);
209                         obj.put_retrieving_label(treerow);
210                         treerow.addEventListener(
211                                 'flesh',
212                                 function() {
213                                         //dump('fleshing = ' + params.retrieve_id + '\n');
214                                         var row;
215
216                                         params.row_node = treeitem;
217                                         params.on_retrieve = function(row) {
218                                                 params.row = row;
219                                                 obj._map_row_to_treecell(params,treerow);
220                                         }
221
222                                         if (typeof params.retrieve_row == 'function') {
223
224                                                 row = params.retrieve_row( params );
225
226                                         } else {
227
228                                                 if (typeof obj.retrieve_row == 'function') {
229
230                                                         row = obj.retrieve_row( params );
231
232                                                 }
233                                         }
234
235                                         treerow.setAttribute('retrieved','true');
236                                 },
237                                 false
238                         );
239                         /*
240                         setTimeout(
241                                 function() {
242                                         util.widgets.dispatch('flesh',treerow);
243                                 }, 0
244                         );
245                         */
246                 } else {
247                         obj.put_retrieving_label(treerow);
248                         treerow.addEventListener(
249                                 'flesh',
250                                 function() {
251                                         //dump('fleshing anon\n');
252                                         obj._map_row_to_treecell(params,treerow);
253                                         treerow.setAttribute('retrieved','true');
254                                 },
255                                 false
256                         );
257                         /*
258                         setTimeout(
259                                 function() {
260                                         util.widgets.dispatch('flesh',treerow);
261                                 }, 0
262                         );
263                         */
264                 }
265                 this.error.sdump('D_LIST',s);
266
267                 setTimeout( function() { obj.auto_retrieve(); }, 0 );
268
269                 return treeitem;
270         },
271
272         'put_retrieving_label' : function(treerow) {
273                 var obj = this;
274                 try {
275                         /*
276                         var cols_idx = 0;
277                         dump('put_retrieving_label.  columns = ' + js2JSON(obj.columns) + '\n');
278                         while( obj.columns[cols_idx] && obj.columns[cols_idx].hidden && obj.columns[cols_idx].hidden == 'true') {
279                                 dump('\t' + cols_idx);
280                                 var treecell = document.createElement('treecell');
281                                 treerow.appendChild(treecell);
282                                 cols_idx++;
283                         }
284                         */
285                         for (var i = 0; i < obj.columns.length; i++) {
286                         var treecell = document.createElement('treecell'); treecell.setAttribute('label','Retrieving...');
287                         treerow.appendChild(treecell);
288                         }
289                         /*
290                         dump('\t' + cols_idx + '\n');
291                         */
292                 } catch(E) {
293                         alert(E);
294                 }
295         },
296
297         'detect_visible' : function() {
298                 try {
299                         var obj = this;
300                         //dump('detect_visible  obj.node = ' + obj.node + '\n');
301                         /* FIXME - this is a hack.. if the implementation of tree changes, this could break */
302                         var scrollbar = document.getAnonymousNodes( document.getAnonymousNodes(obj.node)[1] )[1];
303                         var curpos = scrollbar.getAttribute('curpos');
304                         var maxpos = scrollbar.getAttribute('maxpos');
305                         //alert('curpos = ' + curpos + ' maxpos = ' + maxpos + ' obj.curpos = ' + obj.curpos + ' obj.maxpos = ' + obj.maxpos + '\n');
306                         if ((curpos != obj.curpos) || (maxpos != obj.maxpos)) {
307                                 if ( obj.auto_retrieve() > 0 ) {
308                                         obj.curpos = curpos; obj.maxpos = maxpos;
309                                 }
310                         }
311                 } catch(E) { alert(E); }
312         },
313
314         'detect_visible_polling' : function() {
315                 try {
316                         //alert('detect_visible_polling');
317                         var obj = this;
318                         obj.detect_visible();
319                         setTimeout(function() { try { obj.detect_visible_polling(); } catch(E) { alert(E); } },1000);
320                 } catch(E) {
321                         alert(E);
322                 }
323         },
324
325         'auto_retrieve' : function () {
326                 try {
327                                 //alert('auto_retrieve\n');
328                                 var obj = this; var count = 0;
329                                 var startpos = obj.node.treeBoxObject.getFirstVisibleRow();
330                                 var endpos = obj.node.treeBoxObject.getLastVisibleRow();
331                                 if (startpos > endpos) endpos = obj.node.treeBoxObject.getPageLength();
332                                 //dump('startpos = ' + startpos + ' endpos = ' + endpos + '\n');
333                                 for (var i = startpos; i < endpos + 2; i++) {
334                                         try {
335                                                 //dump('trying index ' + i + '\n');
336                                                 var item = obj.node.contentView.getItemAtIndex(i).firstChild;
337                                                 if (item && item.getAttribute('retrieved') != 'true' ) {
338                                                         //dump('\tgot an unfleshed item = ' + item + ' = ' + item.nodeName + '\n');
339                                                         util.widgets.dispatch('flesh',item); count++;
340                                                 }
341                                         } catch(E) {
342                                                 //dump(i + ' : ' + E + '\n');
343                                         }
344                                 }
345                                 return count;
346                 } catch(E) { alert(E); }
347         },
348
349         '_append_to_listbox' : function (params) {
350
351                 var obj = this;
352
353                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
354
355                 var s = ('util.list.append: params = ' + (params) + '\n');
356
357                 var listitem = document.createElement('listitem');
358
359                 s += ('listbox = ' + this.node + '  listitem = ' + listitem + '\n');
360
361                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
362
363                         setTimeout(
364                                 function() {
365                                         listitem.setAttribute('retrieve_id',params.retrieve_id);
366                                         //FIXME//Make async and fire when row is visible in list
367                                         var row;
368
369                                         params.row_node = listitem;
370                                         params.on_retrieve = function(row) {
371                                                 params.row = row;
372                                                 obj._map_row_to_listcell(params,listitem);
373                                                 obj.node.appendChild( listitem );
374                                         }
375
376                                         if (typeof params.retrieve_row == 'function') {
377
378                                                 row = params.retrieve_row( params );
379
380                                         } else {
381
382                                                 if (typeof obj.retrieve_row == 'function') {
383
384                                                         row = obj.retrieve_row( params );
385
386                                                 }
387                                         }
388                                 }, 0
389                         );
390                 } else {
391                         this._map_row_to_listcell(params,listitem);
392                         this.node.appendChild( listitem );
393                 }
394
395                 this.error.sdump('D_LIST',s);
396                 return listitem;
397
398         },
399
400         '_map_row_to_treecell' : function(params,treerow) {
401                 var s = '';
402                 util.widgets.remove_children(treerow);
403                 for (var i = 0; i < this.columns.length; i++) {
404                         var treecell = document.createElement('treecell');
405                         var label = '';
406                         if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
407                                 treecell.setAttribute('label',label);
408                                 treerow.appendChild( treecell );
409                                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
410                                 continue;
411                         }
412                         if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
413                                 treecell.setAttribute('label',label);
414                                 treerow.appendChild( treecell );
415                                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
416                                 continue;
417                         }
418                         if (typeof params.map_row_to_column == 'function')  {
419
420                                 label = params.map_row_to_column(params.row,this.columns[i]);
421
422                         } else {
423
424                                 if (typeof this.map_row_to_column == 'function') {
425
426                                         label = this.map_row_to_column(params.row,this.columns[i]);
427
428                                 } else {
429
430                                         throw('No map_row_to_column function');
431
432                                 }
433                         }
434                         treecell.setAttribute('label',label);
435                         treerow.appendChild( treecell );
436                         s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
437                 }
438                 this.error.sdump('D_LIST',s);
439         },
440
441         '_map_row_to_listcell' : function(params,listitem) {
442                 var s = '';
443                 for (var i = 0; i < this.columns.length; i++) {
444                         var value = '';
445                         if (typeof params.map_row_to_column == 'function')  {
446
447                                 value = params.map_row_to_column(params.row,this.columns[i]);
448
449                         } else {
450
451                                 if (typeof this.map_row_to_column == 'function') {
452
453                                         value = this.map_row_to_column(params.row,this.columns[i]);
454                                 }
455                         }
456                         if (typeof value == 'string' || typeof value == 'number') {
457                                 var listcell = document.createElement('listcell');
458                                 listcell.setAttribute('label',value);
459                                 listitem.appendChild(listcell);
460                                 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
461                         } else {
462                                 listitem.appendChild(value);
463                                 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
464                         }
465                 }
466                 this.error.sdump('D_LIST',s);
467         },
468
469         'retrieve_selection' : function(params) {
470                 switch(this.node.nodeName) {
471                         case 'tree' : return this._retrieve_selection_from_tree(params); break;
472                         default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
473                 }
474         },
475
476         '_retrieve_selection_from_tree' : function(params) {
477                 var list = [];
478                 var start = new Object();
479                 var end = new Object();
480                 var numRanges = this.node.view.selection.getRangeCount();
481                 for (var t=0; t<numRanges; t++){
482                         this.node.view.selection.getRangeAt(t,start,end);
483                         for (var v=start.value; v<=end.value; v++){
484                                 var i = this.node.contentView.getItemAtIndex(v);
485                                 list.push( i );
486                         }
487                 }
488                 return list;
489         },
490
491         'dump' : function(params) {
492                 switch(this.node.nodeName) {
493                         case 'tree' : return this._dump_tree(params); break;
494                         default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
495                 }
496         },
497
498         '_dump_tree' : function(params) {
499                 var dump = [];
500                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
501                         var row = [];
502                         var treeitem = this.treechildren.childNodes[i];
503                         var treerow = treeitem.firstChild;
504                         for (var j = 0; j < treerow.childNodes.length; j++) {
505                                 row.push( treerow.childNodes[j].getAttribute('label') );
506                         }
507                         dump.push( row );
508                 }
509                 return dump;
510         },
511
512         'dump_with_keys' : function(params) {
513                 switch(this.node.nodeName) {
514                         case 'tree' : return this._dump_tree_with_keys(params); break;
515                         default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
516                 }
517
518         },
519
520         '_dump_tree_with_keys' : function(params) {
521                 var obj = this;
522                 var dump = [];
523                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
524                         var row = {};
525                         var treeitem = this.treechildren.childNodes[i];
526                         var treerow = treeitem.firstChild;
527                         for (var j = 0; j < treerow.childNodes.length; j++) {
528                                 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
529                         }
530                         dump.push( row );
531                 }
532                 return dump;
533         },
534
535         'dump_retrieve_ids' : function(params) {
536                 switch(this.node.nodeName) {
537                         case 'tree' : return this._dump_retrieve_ids_tree(params); break;
538                         default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
539                 }
540         },
541
542         '_dump_retrieve_ids_tree' : function(params) {
543                 var dump = [];
544                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
545                         var treeitem = this.treechildren.childNodes[i];
546                         dump.push( treeitem.getAttribute('retrieve_id') );
547                 }
548                 return dump;
549         },
550
551 }
552 dump('exiting util.list.js\n');