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