return the retrieve_ids (given with each appended row) for the 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 = ' + js2JSON(params) + '\n');
143
144                 var treeitem = document.createElement('treeitem');
145                 treeitem.setAttribute('retrieve_id',params.retrieve_id);
146                 this.treechildren.appendChild( treeitem );
147                 var treerow = document.createElement('treerow');
148                 treeitem.appendChild( treerow );
149
150                 s += ('tree = ' + this.node + '  treechildren = ' + this.treechildren + '\n');
151                 s += ('treeitem = ' + treeitem + '  treerow = ' + treerow + '\n');
152
153                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
154
155                         setTimeout(
156                                 function() {
157                                         treerow.setAttribute('retrieve_id',params.retrieve_id);
158                                         //FIXME//Make async and fire when row is visible in list
159                                         var row;
160
161                                         params.row_node = treeitem;
162                                         params.on_retrieve = function(row) {
163                                                 params.row = row;
164                                                 obj._map_row_to_treecell(params,treerow);
165                                         }
166
167                                         if (typeof params.retrieve_row == 'function') {
168
169                                                 row = params.retrieve_row( params );
170
171                                         } else {
172
173                                                 if (typeof obj.retrieve_row == 'function') {
174
175                                                         row = obj.retrieve_row( params );
176
177                                                 }
178                                         }
179                                 }, 0
180                         );
181                 } else {
182                         this._map_row_to_treecell(params,treerow);
183                 }
184                 this.error.sdump('D_LIST',s);
185
186                 return treeitem;
187         },
188
189         '_append_to_listbox' : function (params) {
190
191                 var obj = this;
192
193                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
194
195                 var s = ('util.list.append: params = ' + js2JSON(params) + '\n');
196
197                 var listitem = document.createElement('listitem');
198
199                 s += ('listbox = ' + this.node + '  listitem = ' + listitem + '\n');
200
201                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
202
203                         setTimeout(
204                                 function() {
205                                         listitem.setAttribute('retrieve_id',params.retrieve_id);
206                                         //FIXME//Make async and fire when row is visible in list
207                                         var row;
208
209                                         params.row_node = listitem;
210                                         params.on_retrieve = function(row) {
211                                                 params.row = row;
212                                                 obj._map_row_to_listcell(params,listitem);
213                                                 obj.node.appendChild( listitem );
214                                         }
215
216                                         if (typeof params.retrieve_row == 'function') {
217
218                                                 row = params.retrieve_row( params );
219
220                                         } else {
221
222                                                 if (typeof obj.retrieve_row == 'function') {
223
224                                                         row = obj.retrieve_row( params );
225
226                                                 }
227                                         }
228                                 }, 0
229                         );
230                 } else {
231                         this._map_row_to_listcell(params,listitem);
232                         this.node.appendChild( listitem );
233                 }
234
235                 this.error.sdump('D_LIST',s);
236                 return listitem;
237
238         },
239
240         '_map_row_to_treecell' : function(params,treerow) {
241                 var s = '';
242                 for (var i = 0; i < this.columns.length; i++) {
243                         var treecell = document.createElement('treecell');
244                         var value = '';
245                         if (typeof params.map_row_to_column == 'function')  {
246
247                                 label = params.map_row_to_column(params.row,this.columns[i]);
248
249                         } else {
250
251                                 if (typeof this.map_row_to_column == 'function') {
252
253                                         label = this.map_row_to_column(params.row,this.columns[i]);
254                                 }
255                         }
256                         treecell.setAttribute('label',label);
257                         treerow.appendChild( treecell );
258                         s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
259                 }
260                 this.error.sdump('D_LIST',s);
261         },
262
263         '_map_row_to_listcell' : function(params,listitem) {
264                 var s = '';
265                 for (var i = 0; i < this.columns.length; i++) {
266                         var value = '';
267                         if (typeof params.map_row_to_column == 'function')  {
268
269                                 value = params.map_row_to_column(params.row,this.columns[i]);
270
271                         } else {
272
273                                 if (typeof this.map_row_to_column == 'function') {
274
275                                         value = this.map_row_to_column(params.row,this.columns[i]);
276                                 }
277                         }
278                         if (typeof value == 'string' || typeof value == 'number') {
279                                 var listcell = document.createElement('listcell');
280                                 listcell.setAttribute('label',value);
281                                 listitem.appendChild(listcell);
282                                 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
283                         } else {
284                                 listitem.appendChild(value);
285                                 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
286                         }
287                 }
288                 this.error.sdump('D_LIST',s);
289         },
290
291         'retrieve_selection' : function(params) {
292                 switch(this.node.nodeName) {
293                         case 'tree' : return this._retrieve_selection_from_tree(params); break;
294                         default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
295                 }
296         },
297
298         '_retrieve_selection_from_tree' : function(params) {
299                 var list = [];
300                 var start = new Object();
301                 var end = new Object();
302                 var numRanges = this.node.view.selection.getRangeCount();
303                 for (var t=0; t<numRanges; t++){
304                         this.node.view.selection.getRangeAt(t,start,end);
305                         for (var v=start.value; v<=end.value; v++){
306                                 var i = this.node.contentView.getItemAtIndex(v);
307                                 list.push( i );
308                         }
309                 }
310                 return list;
311         },
312
313         'dump' : function(params) {
314                 switch(this.node.nodeName) {
315                         case 'tree' : return this._dump_tree(params); break;
316                         default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
317                 }
318         },
319
320         '_dump_tree' : function(params) {
321                 var dump = [];
322                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
323                         var row = [];
324                         var treeitem = this.treechildren.childNodes[i];
325                         var treerow = treeitem.firstChild;
326                         for (var j = 0; j < treerow.childNodes.length; j++) {
327                                 row.push( treerow.childNodes[j].getAttribute('label') );
328                         }
329                         dump.push( row );
330                 }
331                 return dump;
332         },
333
334         'dump_retrieve_ids' : function(params) {
335                 switch(this.node.nodeName) {
336                         case 'tree' : return this._dump_retrieve_ids_tree(params); break;
337                         default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
338                 }
339         },
340
341         '_dump_retrieve_ids_tree' : function(params) {
342                 var dump = [];
343                 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
344                         var treeitem = this.treechildren.childNodes[i];
345                         dump.push( treeitem.getAttribute('retrieve_id') );
346                 }
347                 return dump;
348         },
349
350 }
351 dump('exiting util.list.js\n');