]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/evergreen/util/list.js
bug
[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                 this.node.appendChild( listitem );
148
149                 s += ('listbox = ' + this.node + '  listitem = ' + listitem + '\n');
150
151                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
152
153                         listitem.setAttribute('retrieve_id',params.retrieve_id);
154                         //FIXME//Make async and fire when row is visible in list
155                         var row;
156                         if (typeof params.retrieve_row == 'function') {
157
158                                 row = params.retrieve_row( params );
159
160                         } else {
161
162                                 if (typeof this.retrieve_row == 'function') {
163
164                                         row = this.retrieve_row( params );
165
166                                 }
167                         }
168                         params.row = row;
169                         this._map_row_to_listcell(params,listitem);
170
171                 } else {
172                         this._map_row_to_listcell(params,listitem);
173                 }
174                 this.error.sdump('D_LIST',s);
175
176                 return listitem;
177
178         },
179
180         '_map_row_to_treecell' : function(params,treerow) {
181                 var s = '';
182                 for (var i = 0; i < this.columns.length; i++) {
183                         var treecell = document.createElement('treecell');
184                         var value = '';
185                         if (typeof params.map_row_to_column == 'function')  {
186
187                                 label = params.map_row_to_column(params.row,this.columns[i]);
188
189                         } else {
190
191                                 if (typeof this.map_row_to_column == 'function') {
192
193                                         label = this.map_row_to_column(params.row,this.columns[i]);
194                                 }
195                         }
196                         treecell.setAttribute('label',label);
197                         treerow.appendChild( treecell );
198                         s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
199                 }
200                 this.error.sdump('D_LIST',s);
201         },
202
203         '_map_row_to_listcell' : function(params,listitem) {
204                 var s = '';
205                 for (var i = 0; i < this.columns.length; i++) {
206                         var value = '';
207                         if (typeof params.map_row_to_column == 'function')  {
208
209                                 value = params.map_row_to_column(params.row,this.columns[i]);
210
211                         } else {
212
213                                 if (typeof this.map_row_to_column == 'function') {
214
215                                         value = this.map_row_to_column(params.row,this.columns[i]);
216                                 }
217                         }
218                         if (typeof value == 'string') {
219                                 var listcell = document.createElement('listcell');
220                                 listcell.setAttribute('label',value);
221                                 listitem.appendChild(listcell);
222                                 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
223                         } else {
224                                 listitem.appendChild(value);
225                                 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
226                         }
227                 }
228                 this.error.sdump('D_LIST',s);
229         },
230
231 }
232 dump('exiting util.list.js\n');