]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/list.js
chain exec chunking, and "Retrieve" label across all columns in lists
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / util / list.js
1 dump('entering util.list.js\n');
2
3 if (typeof main == 'undefined') main = {};
4 util.list = function (id) {
5
6         this.node = document.getElementById(id);
7
8         if (!this.node) throw('Could not find element ' + id);
9         switch(this.node.nodeName) {
10                 case 'listbox' : 
11                 case 'tree' : break;
12                 case 'richlistbox' :
13                         throw(this.node.nodeName + ' not yet supported'); break;
14                 default: throw(this.node.nodeName + ' not supported'); break;
15         }
16
17         JSAN.use('util.error'); this.error = new util.error();
18
19         return this;
20 };
21
22 util.list.prototype = {
23
24         '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                 //dump('curpos = ' + curpos + ' maxpos = ' + maxpos + ' obj.curpos = ' + obj.curpos + ' obj.maxpos = ' + obj.maxpos + '\n');
298                 if ((curpos != obj.curpos) || (maxpos != obj.maxpos)) {
299                         obj.auto_retrieve();
300                         obj.curpos = curpos; obj.maxpos = maxpos;
301                 }
302                 } catch(E) { alert(E); }
303         },
304
305         'detect_visible_polling' : function() {
306                 try {
307                         var obj = this;
308                         obj.detect_visible();
309                         setTimeout(function() { obj.detect_visible_polling(); },1);
310                 } catch(E) {
311                         alert(E);
312                 }
313         },
314
315         'auto_retrieve' : function () {
316                 try {
317                 //dump('auto_retrieve\n');
318                 var obj = this;
319                 var startpos = obj.node.treeBoxObject.getFirstVisibleRow();
320                 var endpos = obj.node.treeBoxObject.getLastVisibleRow();
321                 if (startpos > endpos) endpos = obj.node.treeBoxObject.getPageLength();
322                 //dump('startpos = ' + startpos + ' endpos = ' + endpos + '\n');
323                 for (var i = startpos; i < endpos + 2; i++) {
324                         try {
325                                 //dump('trying index ' + i + '\n');
326                                 var item = obj.node.contentView.getItemAtIndex(i).firstChild;
327                                 if (item && item.getAttribute('retrieved') != 'true' ) {
328                                         //dump('\tgot an unfleshed item = ' + item + ' = ' + item.nodeName + '\n');
329                                         util.widgets.dispatch('flesh',item);
330                                 }
331                         } catch(E) {
332                                 //dump(i + ' : ' + E + '\n');
333                         }
334                 }
335                 } catch(E) { alert(E); }
336         },
337
338         '_append_to_listbox' : function (params) {
339
340                 var obj = this;
341
342                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
343
344                 var s = ('util.list.append: params = ' + (params) + '\n');
345
346                 var listitem = document.createElement('listitem');
347
348                 s += ('listbox = ' + this.node + '  listitem = ' + listitem + '\n');
349
350                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
351
352                         setTimeout(
353                                 function() {
354                                         listitem.setAttribute('retrieve_id',params.retrieve_id);
355                                         //FIXME//Make async and fire when row is visible in list
356                                         var row;
357
358                                         params.row_node = listitem;
359                                         params.on_retrieve = function(row) {
360                                                 params.row = row;
361                                                 obj._map_row_to_listcell(params,listitem);
362                                                 obj.node.appendChild( listitem );
363                                         }
364
365                                         if (typeof params.retrieve_row == 'function') {
366
367                                                 row = params.retrieve_row( params );
368
369                                         } else {
370
371                                                 if (typeof obj.retrieve_row == 'function') {
372
373                                                         row = obj.retrieve_row( params );
374
375                                                 }
376                                         }
377                                 }, 0
378                         );
379                 } else {
380                         this._map_row_to_listcell(params,listitem);
381                         this.node.appendChild( listitem );
382                 }
383
384                 this.error.sdump('D_LIST',s);
385                 return listitem;
386
387         },
388
389         '_map_row_to_treecell' : function(params,treerow) {
390                 var s = '';
391                 util.widgets.remove_children(treerow);
392                 for (var i = 0; i < this.columns.length; i++) {
393                         var treecell = document.createElement('treecell');
394                         var label = '';
395                         if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
396                                 treecell.setAttribute('label',label);
397                                 treerow.appendChild( treecell );
398                                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
399                                 continue;
400                         }
401                         if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
402                                 treecell.setAttribute('label',label);
403                                 treerow.appendChild( treecell );
404                                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
405                                 continue;
406                         }
407                         if (typeof params.map_row_to_column == 'function')  {
408
409                                 label = params.map_row_to_column(params.row,this.columns[i]);
410
411                         } else {
412
413                                 if (typeof this.map_row_to_column == 'function') {
414
415                                         label = this.map_row_to_column(params.row,this.columns[i]);
416
417                                 } else {
418
419                                         throw('No map_row_to_column function');
420
421                                 }
422                         }
423                         treecell.setAttribute('label',label);
424                         treerow.appendChild( treecell );
425                         s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
426                 }
427                 this.error.sdump('D_LIST',s);
428         },
429
430         '_map_row_to_listcell' : function(params,listitem) {
431                 var s = '';
432                 for (var i = 0; i < this.columns.length; i++) {
433                         var value = '';
434                         if (typeof params.map_row_to_column == 'function')  {
435
436                                 value = params.map_row_to_column(params.row,this.columns[i]);
437
438                         } else {
439
440                                 if (typeof this.map_row_to_column == 'function') {
441
442                                         value = this.map_row_to_column(params.row,this.columns[i]);
443                                 }
444                         }
445                         if (typeof value == 'string' || typeof value == 'number') {
446                                 var listcell = document.createElement('listcell');
447                                 listcell.setAttribute('label',value);
448                                 listitem.appendChild(listcell);
449                                 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
450                         } else {
451                                 listitem.appendChild(value);
452                                 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
453                         }
454                 }
455                 this.error.sdump('D_LIST',s);
456         },
457
458         'retrieve_selection' : function(params) {
459                 switch(this.node.nodeName) {
460                         case 'tree' : return this._retrieve_selection_from_tree(params); break;
461                         default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
462                 }
463         },
464
465         '_retrieve_selection_from_tree' : function(params) {
466                 var list = [];
467                 var start = new Object();
468                 var end = new Object();
469                 var numRanges = this.node.view.selection.getRangeCount();
470                 for (var t=0; t<numRanges; t++){
471                         this.node.view.selection.getRangeAt(t,start,end);
472                         for (var v=start.value; v<=end.value; v++){
473                                 var i = this.node.contentView.getItemAtIndex(v);
474                                 list.push( i );
475                         }
476                 }
477                 return list;
478         },
479
480         'dump' : function(params) {
481                 switch(this.node.nodeName) {
482                         case 'tree' : return this._dump_tree(params); break;
483                         default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
484                 }
485         },
486
487         '_dump_tree' : function(params) {
488                 var dump = [];
489                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
490                         var row = [];
491                         var treeitem = this.treechildren.childNodes[i];
492                         var treerow = treeitem.firstChild;
493                         for (var j = 0; j < treerow.childNodes.length; j++) {
494                                 row.push( treerow.childNodes[j].getAttribute('label') );
495                         }
496                         dump.push( row );
497                 }
498                 return dump;
499         },
500
501         'dump_with_keys' : function(params) {
502                 switch(this.node.nodeName) {
503                         case 'tree' : return this._dump_tree_with_keys(params); break;
504                         default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
505                 }
506
507         },
508
509         '_dump_tree_with_keys' : function(params) {
510                 var obj = this;
511                 var dump = [];
512                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
513                         var row = {};
514                         var treeitem = this.treechildren.childNodes[i];
515                         var treerow = treeitem.firstChild;
516                         for (var j = 0; j < treerow.childNodes.length; j++) {
517                                 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
518                         }
519                         dump.push( row );
520                 }
521                 return dump;
522         },
523
524         'dump_retrieve_ids' : function(params) {
525                 switch(this.node.nodeName) {
526                         case 'tree' : return this._dump_retrieve_ids_tree(params); break;
527                         default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
528                 }
529         },
530
531         '_dump_retrieve_ids_tree' : function(params) {
532                 var dump = [];
533                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
534                         var treeitem = this.treechildren.childNodes[i];
535                         dump.push( treeitem.getAttribute('retrieve_id') );
536                 }
537                 return dump;
538         },
539
540 }
541 dump('exiting util.list.js\n');