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