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;
68 '_init_listbox' : function (params) {
71 var listhead = document.createElement('listhead');
72 this.node.appendChild(listhead);
74 var listcols = document.createElement('listcols');
75 this.node.appendChild(listcols);
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]);
90 'append' : function (params) {
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;
97 if (rnode && params.attributes) {
98 for (var i in params.attributes) {
99 rnode.setAttribute(i,params.attributes[i]);
104 '_append_to_tree' : function (params) {
106 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
108 var s = ('util.list.append: params = ' + js2JSON(params) + '\n');
110 var treeitem = document.createElement('treeitem');
111 this.treechildren.appendChild( treeitem );
112 var treerow = document.createElement('treerow');
113 treeitem.appendChild( treerow );
115 s += ('tree = ' + this.node + ' treechildren = ' + this.treechildren + '\n');
116 s += ('treeitem = ' + treeitem + ' treerow = ' + treerow + '\n');
118 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
120 treerow.setAttribute('retrieve_id',params.retrieve_id);
121 //FIXME//Make async and fire when row is visible in list
123 if (typeof params.retrieve_row == 'function') {
125 row = params.retrieve_row( params );
129 if (typeof this.retrieve_row == 'function') {
131 row = this.retrieve_row( params );
136 this._map_row_to_treecell(params,treerow);
139 this._map_row_to_treecell(params,treerow);
141 this.error.sdump('D_LIST',s);
146 '_append_to_listbox' : function (params) {
148 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
150 var s = ('util.list.append: params = ' + js2JSON(params) + '\n');
152 var listitem = document.createElement('listitem');
154 s += ('listbox = ' + this.node + ' listitem = ' + listitem + '\n');
156 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
158 listitem.setAttribute('retrieve_id',params.retrieve_id);
159 //FIXME//Make async and fire when row is visible in list
161 if (typeof params.retrieve_row == 'function') {
163 row = params.retrieve_row( params );
167 if (typeof this.retrieve_row == 'function') {
169 row = this.retrieve_row( params );
174 this._map_row_to_listcell(params,listitem);
177 this._map_row_to_listcell(params,listitem);
180 this.node.appendChild( listitem );
182 this.error.sdump('D_LIST',s);
188 '_map_row_to_treecell' : function(params,treerow) {
190 for (var i = 0; i < this.columns.length; i++) {
191 var treecell = document.createElement('treecell');
193 if (typeof params.map_row_to_column == 'function') {
195 label = params.map_row_to_column(params.row,this.columns[i]);
199 if (typeof this.map_row_to_column == 'function') {
201 label = this.map_row_to_column(params.row,this.columns[i]);
204 treecell.setAttribute('label',label);
205 treerow.appendChild( treecell );
206 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
208 this.error.sdump('D_LIST',s);
211 '_map_row_to_listcell' : function(params,listitem) {
213 for (var i = 0; i < this.columns.length; i++) {
215 if (typeof params.map_row_to_column == 'function') {
217 value = params.map_row_to_column(params.row,this.columns[i]);
221 if (typeof this.map_row_to_column == 'function') {
223 value = this.map_row_to_column(params.row,this.columns[i]);
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');
232 listitem.appendChild(value);
233 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
236 this.error.sdump('D_LIST',s);
240 dump('exiting util.list.js\n');