Yay! Inserting an empty listitem into a listbox gives us the magic column we hate...
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / evergreen / 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         },
67
68         '_init_listbox' : function (params) {
69                 if (this.prebuilt) {
70                 } else {
71                         var listhead = document.createElement('listhead');
72                         this.node.appendChild(listhead);
73
74                         var listcols = document.createElement('listcols');
75                         this.node.appendChild(listcols);
76
77                         for (var i = 0; i < this.columns.length; i++) {
78                                 var listheader = document.createElement('listheader');
79                                 listhead.appendChild(listheader);
80                                 var listcol = document.createElement('listcol');
81                                 listcols.appendChild(listcol);
82                                 for (var j in this.columns[i]) {
83                                         listheader.setAttribute(j,this.columns[i][j]);
84                                         listcol.setAttribute(j,this.columns[i][j]);
85                                 };
86                         }
87                 }
88         },
89
90         'append' : function (params) {
91                 switch (this.node.nodeName) {
92                         case 'tree' : this._append_to_tree(params); break;
93                         case 'listbox' : this._append_to_listbox(params); break;
94                         default: throw('NYI: Need .append() for ' + this.node.nodeName); break;
95                 }
96         },
97
98         '_append_to_tree' : function (params) {
99
100                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
101
102                 var s = ('util.list.append: params = ' + js2JSON(params) + '\n');
103
104                 var treeitem = document.createElement('treeitem');
105                 this.treechildren.appendChild( treeitem );
106                 var treerow = document.createElement('treerow');
107                 treeitem.appendChild( treerow );
108
109                 s += ('tree = ' + this.node + '  treechildren = ' + this.treechildren + '\n');
110                 s += ('treeitem = ' + treeitem + '  treerow = ' + treerow + '\n');
111
112                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
113
114                         treerow.setAttribute('retrieve_id',params.retrieve_id);
115                         //FIXME//Make async and fire when row is visible in list
116                         var row;
117                         if (typeof params.retrieve_row == 'function') {
118
119                                 row = params.retrieve_row( params );
120
121                         } else {
122
123                                 if (typeof this.retrieve_row == 'function') {
124
125                                         row = this.retrieve_row( params );
126
127                                 }
128                         }
129                         params.row = row;
130                         this._map_row_to_treecell(params,treerow);
131
132                 } else {
133                         this._map_row_to_treecell(params,treerow);
134                 }
135                 this.error.sdump('D_LIST',s);
136
137                 return treeitem;
138         },
139
140         '_append_to_listbox' : function (params) {
141
142                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
143
144                 var s = ('util.list.append: params = ' + js2JSON(params) + '\n');
145
146                 var listitem = document.createElement('listitem');
147
148                 s += ('listbox = ' + this.node + '  listitem = ' + listitem + '\n');
149
150                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
151
152                         listitem.setAttribute('retrieve_id',params.retrieve_id);
153                         //FIXME//Make async and fire when row is visible in list
154                         var row;
155                         if (typeof params.retrieve_row == 'function') {
156
157                                 row = params.retrieve_row( params );
158
159                         } else {
160
161                                 if (typeof this.retrieve_row == 'function') {
162
163                                         row = this.retrieve_row( params );
164
165                                 }
166                         }
167                         params.row = row;
168                         this._map_row_to_listcell(params,listitem);
169
170                 } else {
171                         this._map_row_to_listcell(params,listitem);
172                 }
173
174                 this.node.appendChild( listitem );
175
176                 this.error.sdump('D_LIST',s);
177
178                 return listitem;
179
180         },
181
182         '_map_row_to_treecell' : function(params,treerow) {
183                 var s = '';
184                 for (var i = 0; i < this.columns.length; i++) {
185                         var treecell = document.createElement('treecell');
186                         var value = '';
187                         if (typeof params.map_row_to_column == 'function')  {
188
189                                 label = params.map_row_to_column(params.row,this.columns[i]);
190
191                         } else {
192
193                                 if (typeof this.map_row_to_column == 'function') {
194
195                                         label = this.map_row_to_column(params.row,this.columns[i]);
196                                 }
197                         }
198                         treecell.setAttribute('label',label);
199                         treerow.appendChild( treecell );
200                         s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
201                 }
202                 this.error.sdump('D_LIST',s);
203         },
204
205         '_map_row_to_listcell' : function(params,listitem) {
206                 var s = '';
207                 for (var i = 0; i < this.columns.length; i++) {
208                         var value = '';
209                         if (typeof params.map_row_to_column == 'function')  {
210
211                                 value = params.map_row_to_column(params.row,this.columns[i]);
212
213                         } else {
214
215                                 if (typeof this.map_row_to_column == 'function') {
216
217                                         value = this.map_row_to_column(params.row,this.columns[i]);
218                                 }
219                         }
220                         if (typeof value == 'string') {
221                                 var listcell = document.createElement('listcell');
222                                 listcell.setAttribute('label',value);
223                                 listitem.appendChild(listcell);
224                                 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
225                         } else {
226                                 listitem.appendChild(value);
227                                 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
228                         }
229                 }
230                 this.error.sdump('D_LIST',s);
231         },
232
233 }
234 dump('exiting util.list.js\n');