1 dump('entering util.list.js\n');
3 if (typeof main == 'undefined') main = {};
4 util.list = function (id) {
6 this.node = document.getElementById(id);
8 if (!this.node) throw('Could not find element ' + id);
9 switch(this.node.nodeName) {
13 throw(this.node.nodeName + ' not yet supported'); break;
14 default: throw(this.node.nodeName + ' not supported'); break;
17 JSAN.use('util.error'); this.error = new util.error();
22 util.list.prototype = {
24 'init' : function (params) {
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;
29 this.prebuilt = false;
30 if (typeof params.prebuilt != 'undefined') this.prebuilt = params.prebuilt;
32 if (typeof params.columns == 'undefined') throw('util.list.init: No columns');
33 this.columns = params.columns;
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;
42 '_init_tree' : function (params) {
45 this.treechildren = this.node.lastChild;
48 var treecols = document.createElement('treecols');
49 this.node.appendChild(treecols);
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]);
56 treecols.appendChild(treecol);
57 var splitter = document.createElement('splitter');
58 splitter.setAttribute('class','tree-splitter');
59 treecols.appendChild(splitter);
62 var treechildren = document.createElement('treechildren');
63 this.node.appendChild(treechildren);
64 this.treechildren = treechildren;
66 if (typeof params.on_select == 'function') {
67 this.node.addEventListener(
75 '_init_listbox' : function (params) {
78 var listhead = document.createElement('listhead');
79 this.node.appendChild(listhead);
81 var listcols = document.createElement('listcols');
82 this.node.appendChild(listcols);
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]);
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;
103 this.error.sdump('D_LIST','Clearing list ' + this.node.getAttribute('id') + '\n');
106 '_clear_tree' : function(params) {
107 while (this.treechildren.lastChild) this.treechildren.removeChild( this.treechildren.lastChild );
110 '_clear_listbox' : function(params) {
112 var nl = this.node.getElementsByTagName('listitem');
113 for (var i = 0; i < nl.length; i++) {
116 for (var i = 0; i < items.length; i++) {
117 this.node.removeChild(items[i]);
121 'append' : function (params) {
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;
128 if (rnode && params.attributes) {
129 for (var i in params.attributes) {
130 rnode.setAttribute(i,params.attributes[i]);
136 '_append_to_tree' : function (params) {
140 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
142 var s = ('util.list.append: params = ' + js2JSON(params) + '\n');
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 );
150 s += ('tree = ' + this.node + ' treechildren = ' + this.treechildren + '\n');
151 s += ('treeitem = ' + treeitem + ' treerow = ' + treerow + '\n');
153 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
157 treerow.setAttribute('retrieve_id',params.retrieve_id);
158 //FIXME//Make async and fire when row is visible in list
161 params.row_node = treeitem;
162 params.on_retrieve = function(row) {
164 obj._map_row_to_treecell(params,treerow);
167 if (typeof params.retrieve_row == 'function') {
169 row = params.retrieve_row( params );
173 if (typeof obj.retrieve_row == 'function') {
175 row = obj.retrieve_row( params );
182 this._map_row_to_treecell(params,treerow);
184 this.error.sdump('D_LIST',s);
189 '_append_to_listbox' : function (params) {
193 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
195 var s = ('util.list.append: params = ' + js2JSON(params) + '\n');
197 var listitem = document.createElement('listitem');
199 s += ('listbox = ' + this.node + ' listitem = ' + listitem + '\n');
201 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
205 listitem.setAttribute('retrieve_id',params.retrieve_id);
206 //FIXME//Make async and fire when row is visible in list
209 params.row_node = listitem;
210 params.on_retrieve = function(row) {
212 obj._map_row_to_listcell(params,listitem);
213 obj.node.appendChild( listitem );
216 if (typeof params.retrieve_row == 'function') {
218 row = params.retrieve_row( params );
222 if (typeof obj.retrieve_row == 'function') {
224 row = obj.retrieve_row( params );
231 this._map_row_to_listcell(params,listitem);
232 this.node.appendChild( listitem );
235 this.error.sdump('D_LIST',s);
240 '_map_row_to_treecell' : function(params,treerow) {
242 for (var i = 0; i < this.columns.length; i++) {
243 var treecell = document.createElement('treecell');
245 if (typeof params.map_row_to_column == 'function') {
247 label = params.map_row_to_column(params.row,this.columns[i]);
251 if (typeof this.map_row_to_column == 'function') {
253 label = this.map_row_to_column(params.row,this.columns[i]);
257 throw('No map_row_to_column function');
261 treecell.setAttribute('label',label);
262 treerow.appendChild( treecell );
263 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
265 this.error.sdump('D_LIST',s);
268 '_map_row_to_listcell' : function(params,listitem) {
270 for (var i = 0; i < this.columns.length; i++) {
272 if (typeof params.map_row_to_column == 'function') {
274 value = params.map_row_to_column(params.row,this.columns[i]);
278 if (typeof this.map_row_to_column == 'function') {
280 value = this.map_row_to_column(params.row,this.columns[i]);
283 if (typeof value == 'string' || typeof value == 'number') {
284 var listcell = document.createElement('listcell');
285 listcell.setAttribute('label',value);
286 listitem.appendChild(listcell);
287 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
289 listitem.appendChild(value);
290 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
293 this.error.sdump('D_LIST',s);
296 'retrieve_selection' : function(params) {
297 switch(this.node.nodeName) {
298 case 'tree' : return this._retrieve_selection_from_tree(params); break;
299 default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
303 '_retrieve_selection_from_tree' : function(params) {
305 var start = new Object();
306 var end = new Object();
307 var numRanges = this.node.view.selection.getRangeCount();
308 for (var t=0; t<numRanges; t++){
309 this.node.view.selection.getRangeAt(t,start,end);
310 for (var v=start.value; v<=end.value; v++){
311 var i = this.node.contentView.getItemAtIndex(v);
318 'dump' : function(params) {
319 switch(this.node.nodeName) {
320 case 'tree' : return this._dump_tree(params); break;
321 default: throw('NYI: Need .dump() for ' + this.node.nodeName); break;
325 '_dump_tree' : function(params) {
327 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
329 var treeitem = this.treechildren.childNodes[i];
330 var treerow = treeitem.firstChild;
331 for (var j = 0; j < treerow.childNodes.length; j++) {
332 row.push( treerow.childNodes[j].getAttribute('label') );
339 'dump_with_keys' : function(params) {
340 switch(this.node.nodeName) {
341 case 'tree' : return this._dump_tree_with_keys(params); break;
342 default: throw('NYI: Need .dump_with_keys() for ' + this.node.nodeName); break;
347 '_dump_tree_with_keys' : function(params) {
350 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
352 var treeitem = this.treechildren.childNodes[i];
353 var treerow = treeitem.firstChild;
354 for (var j = 0; j < treerow.childNodes.length; j++) {
355 row[ obj.columns[j].id ] = treerow.childNodes[j].getAttribute('label');
362 'dump_retrieve_ids' : function(params) {
363 switch(this.node.nodeName) {
364 case 'tree' : return this._dump_retrieve_ids_tree(params); break;
365 default: throw('NYI: Need .dump_retrieve_ids() for ' + this.node.nodeName); break;
369 '_dump_retrieve_ids_tree' : function(params) {
371 for (var i = 0; i < this.treechildren.childNodes.length; i++) {
372 var treeitem = this.treechildren.childNodes[i];
373 dump.push( treeitem.getAttribute('retrieve_id') );
379 dump('exiting util.list.js\n');