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