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