]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/evergreen/util/list.js
7f0caf3d1472c89594379eb23060e4a95fb6a431
[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                 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         'append' : function (params) {
98                 var rnode;
99                 switch (this.node.nodeName) {
100                         case 'tree' : rnode = this._append_to_tree(params); break;
101                         case 'listbox' : rnode = this._append_to_listbox(params); break;
102                         default: throw('NYI: Need .append() for ' + this.node.nodeName); break;
103                 }
104                 if (rnode && params.attributes) {
105                         for (var i in params.attributes) {
106                                 rnode.setAttribute(i,params.attributes[i]);
107                         }
108                 }
109                 return rnode;
110         },
111
112         '_append_to_tree' : function (params) {
113
114                 var obj = this;
115
116                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
117
118                 var s = ('util.list.append: params = ' + js2JSON(params) + '\n');
119
120                 var treeitem = document.createElement('treeitem');
121                 treeitem.setAttribute('retrieve_id',params.retrieve_id);
122                 this.treechildren.appendChild( treeitem );
123                 var treerow = document.createElement('treerow');
124                 treeitem.appendChild( treerow );
125
126                 s += ('tree = ' + this.node + '  treechildren = ' + this.treechildren + '\n');
127                 s += ('treeitem = ' + treeitem + '  treerow = ' + treerow + '\n');
128
129                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
130
131                         setTimeout(
132                                 function() {
133                                         treerow.setAttribute('retrieve_id',params.retrieve_id);
134                                         //FIXME//Make async and fire when row is visible in list
135                                         var row;
136                                         if (typeof params.retrieve_row == 'function') {
137
138                                                 row = params.retrieve_row( params );
139
140                                         } else {
141
142                                                 if (typeof obj.retrieve_row == 'function') {
143
144                                                         row = obj.retrieve_row( params );
145
146                                                 }
147                                         }
148                                         params.row = row;
149                                         obj._map_row_to_treecell(params,treerow);
150                                 }, 0
151                         );
152                 } else {
153                         this._map_row_to_treecell(params,treerow);
154                 }
155                 this.error.sdump('D_LIST',s);
156
157                 return treeitem;
158         },
159
160         '_append_to_listbox' : function (params) {
161
162                 var obj = this;
163
164                 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
165
166                 var s = ('util.list.append: params = ' + js2JSON(params) + '\n');
167
168                 var listitem = document.createElement('listitem');
169
170                 s += ('listbox = ' + this.node + '  listitem = ' + listitem + '\n');
171
172                 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
173
174                         setTimeout(
175                                 function() {
176                                         listitem.setAttribute('retrieve_id',params.retrieve_id);
177                                         //FIXME//Make async and fire when row is visible in list
178                                         var row;
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                                         params.row = row;
192                                         obj._map_row_to_listcell(params,listitem);
193                                         obj.node.appendChild( listitem );
194                                 }, 0
195                         );
196                 } else {
197                         this._map_row_to_listcell(params,listitem);
198                         this.node.appendChild( listitem );
199                 }
200
201                 this.error.sdump('D_LIST',s);
202                 return listitem;
203
204         },
205
206         '_map_row_to_treecell' : function(params,treerow) {
207                 var s = '';
208                 for (var i = 0; i < this.columns.length; i++) {
209                         var treecell = document.createElement('treecell');
210                         var value = '';
211                         if (typeof params.map_row_to_column == 'function')  {
212
213                                 label = params.map_row_to_column(params.row,this.columns[i]);
214
215                         } else {
216
217                                 if (typeof this.map_row_to_column == 'function') {
218
219                                         label = this.map_row_to_column(params.row,this.columns[i]);
220                                 }
221                         }
222                         treecell.setAttribute('label',label);
223                         treerow.appendChild( treecell );
224                         s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
225                 }
226                 this.error.sdump('D_LIST',s);
227         },
228
229         '_map_row_to_listcell' : function(params,listitem) {
230                 var s = '';
231                 for (var i = 0; i < this.columns.length; i++) {
232                         var value = '';
233                         if (typeof params.map_row_to_column == 'function')  {
234
235                                 value = params.map_row_to_column(params.row,this.columns[i]);
236
237                         } else {
238
239                                 if (typeof this.map_row_to_column == 'function') {
240
241                                         value = this.map_row_to_column(params.row,this.columns[i]);
242                                 }
243                         }
244                         if (typeof value == 'string') {
245                                 var listcell = document.createElement('listcell');
246                                 listcell.setAttribute('label',value);
247                                 listitem.appendChild(listcell);
248                                 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
249                         } else {
250                                 listitem.appendChild(value);
251                                 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
252                         }
253                 }
254                 this.error.sdump('D_LIST',s);
255         },
256
257         'retrieve_selection' : function(params) {
258                 switch(this.node.nodeName) {
259                         case 'tree' : return this._retrieve_selection_from_tree(params); break;
260                         default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
261                 }
262         },
263
264         '_retrieve_selection_from_tree' : function(params) {
265                 var list = [];
266                 var start = new Object();
267                 var end = new Object();
268                 var numRanges = this.node.view.selection.getRangeCount();
269                 for (var t=0; t<numRanges; t++){
270                         this.node.view.selection.getRangeAt(t,start,end);
271                         for (var v=start.value; v<=end.value; v++){
272                                 var i = this.node.contentView.getItemAtIndex(v);
273                                 list.push( i );
274                         }
275                 }
276                 return list;
277         },
278 }
279 dump('exiting util.list.js\n');