]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/list.js
trying to speed things up a bit
[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                 if (typeof params.map_row_to_column == 'function') this.map_row_to_column = params.map_row_to_column;
27                 if (typeof params.retrieve_row == 'function') this.retrieve_row = params.retrieve_row;
28
29                 this.prebuilt = false;
30                 if (typeof params.prebuilt != 'undefined') this.prebuilt = params.prebuilt;
31
32                 if (typeof params.columns == 'undefined') throw('util.list.init: No columns');
33                 this.columns = params.columns;
34
35                 switch(this.node.nodeName) {
36                         case 'tree' : this._init_tree(params); break;
37                         case 'listbox' : this._init_listbox(params); break;
38                         default: throw('NYI: Need ._init() for ' + this.node.nodeName); break;
39                 }
40         },
41
42         '_init_tree' : function (params) {
43                 if (this.prebuilt) {
44                 
45                         this.treechildren = this.node.lastChild;        
46                 
47                 } else {
48                         var treecols = document.createElement('treecols');
49                         this.node.appendChild(treecols);
50
51                         for (var i = 0; i < this.columns.length; i++) {
52                                 var treecol = document.createElement('treecol');
53                                 for (var j in this.columns[i]) {
54                                         treecol.setAttribute(j,this.columns[i][j]);
55                                 }
56                                 treecols.appendChild(treecol);
57                                 var splitter = document.createElement('splitter');
58                                 splitter.setAttribute('class','tree-splitter');
59                                 treecols.appendChild(splitter);
60                         }
61
62                         var treechildren = document.createElement('treechildren');
63                         this.node.appendChild(treechildren);
64                         this.treechildren = treechildren;
65                 }
66                 if (typeof params.on_select == 'function') {
67                         this.node.addEventListener(
68                                 'select',
69                                 params.on_select,
70                                 false
71                         );
72                 }
73                 if (typeof params.on_click == 'function') {
74                         this.node.addEventListener(
75                                 'click',
76                                 params.on_click,
77                                 false
78                         );
79                 }
80         },
81
82         '_init_listbox' : function (params) {
83                 if (this.prebuilt) {
84                 } else {
85                         var listhead = document.createElement('listhead');
86                         this.node.appendChild(listhead);
87
88                         var listcols = document.createElement('listcols');
89                         this.node.appendChild(listcols);
90
91                         for (var i = 0; i < this.columns.length; i++) {
92                                 var listheader = document.createElement('listheader');
93                                 listhead.appendChild(listheader);
94                                 var listcol = document.createElement('listcol');
95                                 listcols.appendChild(listcol);
96                                 for (var j in this.columns[i]) {
97                                         listheader.setAttribute(j,this.columns[i][j]);
98                                         listcol.setAttribute(j,this.columns[i][j]);
99                                 };
100                         }
101                 }
102         },
103
104         'clear' : function (params) {
105                 switch (this.node.nodeName) {
106                         case 'tree' : this._clear_tree(params); break;
107                         case 'listbox' : this._clear_listbox(params); break;
108                         default: throw('NYI: Need .clear() for ' + this.node.nodeName); break;
109                 }
110                 this.error.sdump('D_LIST','Clearing list ' + this.node.getAttribute('id') + '\n');
111         },
112
113         '_clear_tree' : function(params) {
114                 while (this.treechildren.lastChild) this.treechildren.removeChild( this.treechildren.lastChild );
115         },
116
117         '_clear_listbox' : function(params) {
118                 var items = [];
119                 var nl = this.node.getElementsByTagName('listitem');
120                 for (var i = 0; i < nl.length; i++) {
121                         items.push( nl[i] );
122                 }
123                 for (var i = 0; i < items.length; i++) {
124                         this.node.removeChild(items[i]);
125                 }
126         },
127
128         'append' : function (params) {
129                 var rnode;
130                 switch (this.node.nodeName) {
131                         case 'tree' : rnode = this._append_to_tree(params); break;
132                         case 'listbox' : rnode = this._append_to_listbox(params); break;
133                         default: throw('NYI: Need .append() for ' + this.node.nodeName); break;
134                 }
135                 if (rnode && params.attributes) {
136                         for (var i in params.attributes) {
137                                 rnode.setAttribute(i,params.attributes[i]);
138                         }
139                 }
140                 return rnode;
141         },
142
143         '_append_to_tree' : function (params) {
144
145                 var obj = this;
146
147                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
148
149                 var s = ('util.list.append: params = ' + (params) + '\n');
150
151                 var treechildren_node = this.treechildren;
152
153                 if (params.node && params.node.nodeName == 'treeitem') {
154                         params.node.setAttribute('container','true'); /* params.node.setAttribute('open','true'); */
155                         if (params.node.lastChild.nodeName == 'treechildren') {
156                                 treechildren_node = params.node.lastChild;
157                         } else {
158                                 treechildren_node = document.createElement('treechildren');
159                                 params.node.appendChild(treechildren_node);
160                         }
161                 }
162
163                 var treeitem = document.createElement('treeitem');
164                 treeitem.setAttribute('retrieve_id',params.retrieve_id);
165                 treechildren_node.appendChild( treeitem );
166                 var treerow = document.createElement('treerow');
167                 treeitem.appendChild( treerow );
168
169                 s += ('tree = ' + this.node + '  treechildren = ' + treechildren_node + '\n');
170                 s += ('treeitem = ' + treeitem + '  treerow = ' + treerow + '\n');
171
172                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
173
174                         setTimeout(
175                                 function() {
176                                         treerow.setAttribute('retrieve_id',params.retrieve_id);
177                                         //FIXME//Make async and fire when row is visible in list
178                                         var row;
179
180                                         params.row_node = treeitem;
181                                         params.on_retrieve = function(row) {
182                                                 params.row = row;
183                                                 obj._map_row_to_treecell(params,treerow);
184                                         }
185
186                                         if (typeof params.retrieve_row == 'function') {
187
188                                                 row = params.retrieve_row( params );
189
190                                         } else {
191
192                                                 if (typeof obj.retrieve_row == 'function') {
193
194                                                         row = obj.retrieve_row( params );
195
196                                                 }
197                                         }
198                                 }, 0
199                         );
200                 } else {
201                         this._map_row_to_treecell(params,treerow);
202                 }
203                 this.error.sdump('D_LIST',s);
204
205                 return treeitem;
206         },
207
208         '_append_to_listbox' : function (params) {
209
210                 var obj = this;
211
212                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
213
214                 var s = ('util.list.append: params = ' + (params) + '\n');
215
216                 var listitem = document.createElement('listitem');
217
218                 s += ('listbox = ' + this.node + '  listitem = ' + listitem + '\n');
219
220                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
221
222                         setTimeout(
223                                 function() {
224                                         listitem.setAttribute('retrieve_id',params.retrieve_id);
225                                         //FIXME//Make async and fire when row is visible in list
226                                         var row;
227
228                                         params.row_node = listitem;
229                                         params.on_retrieve = function(row) {
230                                                 params.row = row;
231                                                 obj._map_row_to_listcell(params,listitem);
232                                                 obj.node.appendChild( listitem );
233                                         }
234
235                                         if (typeof params.retrieve_row == 'function') {
236
237                                                 row = params.retrieve_row( params );
238
239                                         } else {
240
241                                                 if (typeof obj.retrieve_row == 'function') {
242
243                                                         row = obj.retrieve_row( params );
244
245                                                 }
246                                         }
247                                 }, 0
248                         );
249                 } else {
250                         this._map_row_to_listcell(params,listitem);
251                         this.node.appendChild( listitem );
252                 }
253
254                 this.error.sdump('D_LIST',s);
255                 return listitem;
256
257         },
258
259         '_map_row_to_treecell' : function(params,treerow) {
260                 var s = '';
261                 for (var i = 0; i < this.columns.length; i++) {
262                         var treecell = document.createElement('treecell');
263                         var label = '';
264                         if (params.skip_columns && (params.skip_columns.indexOf(i) != -1)) {
265                                 treecell.setAttribute('label',label);
266                                 treerow.appendChild( treecell );
267                                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
268                                 continue;
269                         }
270                         if (params.skip_all_columns_except && (params.skip_all_columns_except.indexOf(i) == -1)) {
271                                 treecell.setAttribute('label',label);
272                                 treerow.appendChild( treecell );
273                                 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
274                                 continue;
275                         }
276                         if (typeof params.map_row_to_column == 'function')  {
277
278                                 label = params.map_row_to_column(params.row,this.columns[i]);
279
280                         } else {
281
282                                 if (typeof this.map_row_to_column == 'function') {
283
284                                         label = this.map_row_to_column(params.row,this.columns[i]);
285
286                                 } else {
287
288                                         throw('No map_row_to_column function');
289
290                                 }
291                         }
292                         treecell.setAttribute('label',label);
293                         treerow.appendChild( treecell );
294                         s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
295                 }
296                 this.error.sdump('D_LIST',s);
297         },
298
299         '_map_row_to_listcell' : function(params,listitem) {
300                 var s = '';
301                 for (var i = 0; i < this.columns.length; i++) {
302                         var value = '';
303                         if (typeof params.map_row_to_column == 'function')  {
304
305                                 value = params.map_row_to_column(params.row,this.columns[i]);
306
307                         } else {
308
309                                 if (typeof this.map_row_to_column == 'function') {
310
311                                         value = this.map_row_to_column(params.row,this.columns[i]);
312                                 }
313                         }
314                         if (typeof value == 'string' || typeof value == 'number') {
315                                 var listcell = document.createElement('listcell');
316                                 listcell.setAttribute('label',value);
317                                 listitem.appendChild(listcell);
318                                 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
319                         } else {
320                                 listitem.appendChild(value);
321                                 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
322                         }
323                 }
324                 this.error.sdump('D_LIST',s);
325         },
326
327         'retrieve_selection' : function(params) {
328                 switch(this.node.nodeName) {
329                         case 'tree' : return this._retrieve_selection_from_tree(params); break;
330                         default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
331                 }
332         },
333
334         '_retrieve_selection_from_tree' : function(params) {
335                 var list = [];
336                 var start = new Object();
337                 var end = new Object();
338                 var numRanges = this.node.view.selection.getRangeCount();
339                 for (var t=0; t<numRanges; t++){
340                         this.node.view.selection.getRangeAt(t,start,end);
341                         for (var v=start.value; v<=end.value; v++){
342                                 var i = this.node.contentView.getItemAtIndex(v);
343                                 list.push( i );
344                         }
345                 }
346                 return list;
347         },
348
349         'dump' : function(params) {
350                 switch(this.node.nodeName) {
351                         case 'tree' : return this._dump_tree(params); break;
352                         default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
353                 }
354         },
355
356         '_dump_tree' : function(params) {
357                 var dump = [];
358                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
359                         var row = [];
360                         var treeitem = this.treechildren.childNodes[i];
361                         var treerow = treeitem.firstChild;
362                         for (var j = 0; j < treerow.childNodes.length; j++) {
363                                 row.push( treerow.childNodes[j].getAttribute('label') );
364                         }
365                         dump.push( row );
366                 }
367                 return dump;
368         },
369
370         'dump_with_keys' : function(params) {
371                 switch(this.node.nodeName) {
372                         case 'tree' : return this._dump_tree_with_keys(params); break;
373                         default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
374                 }
375
376         },
377
378         '_dump_tree_with_keys' : function(params) {
379                 var obj = this;
380                 var dump = [];
381                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
382                         var row = {};
383                         var treeitem = this.treechildren.childNodes[i];
384                         var treerow = treeitem.firstChild;
385                         for (var j = 0; j < treerow.childNodes.length; j++) {
386                                 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
387                         }
388                         dump.push( row );
389                 }
390                 return dump;
391         },
392
393         'dump_retrieve_ids' : function(params) {
394                 switch(this.node.nodeName) {
395                         case 'tree' : return this._dump_retrieve_ids_tree(params); break;
396                         default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
397                 }
398         },
399
400         '_dump_retrieve_ids_tree' : function(params) {
401                 var dump = [];
402                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
403                         var treeitem = this.treechildren.childNodes[i];
404                         dump.push( treeitem.getAttribute('retrieve_id') );
405                 }
406                 return dump;
407         },
408
409 }
410 dump('exiting util.list.js\n');