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 'append' : function (params) {
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;
104 if (rnode && params.attributes) {
105 for (var i in params.attributes) {
106 rnode.setAttribute(i,params.attributes[i]);
112 '_append_to_tree' : function (params) {
114 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
116 var s = ('util.list.append: params = ' + js2JSON(params) + '\n');
118 var treeitem = document.createElement('treeitem');
119 treeitem.setAttribute('retrieve_id',params.retrieve_id);
120 this.treechildren.appendChild( treeitem );
121 var treerow = document.createElement('treerow');
122 treeitem.appendChild( treerow );
124 s += ('tree = ' + this.node + ' treechildren = ' + this.treechildren + '\n');
125 s += ('treeitem = ' + treeitem + ' treerow = ' + treerow + '\n');
127 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
129 treerow.setAttribute('retrieve_id',params.retrieve_id);
130 //FIXME//Make async and fire when row is visible in list
132 if (typeof params.retrieve_row == 'function') {
134 row = params.retrieve_row( params );
138 if (typeof this.retrieve_row == 'function') {
140 row = this.retrieve_row( params );
145 this._map_row_to_treecell(params,treerow);
148 this._map_row_to_treecell(params,treerow);
150 this.error.sdump('D_LIST',s);
155 '_append_to_listbox' : function (params) {
157 if (typeof params.row == 'undefined') throw('util.list.append: Object must contain a row');
159 var s = ('util.list.append: params = ' + js2JSON(params) + '\n');
161 var listitem = document.createElement('listitem');
163 s += ('listbox = ' + this.node + ' listitem = ' + listitem + '\n');
165 if (typeof params.retrieve_row == 'function' || typeof this.retrieve_row == 'function') {
167 listitem.setAttribute('retrieve_id',params.retrieve_id);
168 //FIXME//Make async and fire when row is visible in list
170 if (typeof params.retrieve_row == 'function') {
172 row = params.retrieve_row( params );
176 if (typeof this.retrieve_row == 'function') {
178 row = this.retrieve_row( params );
183 this._map_row_to_listcell(params,listitem);
186 this._map_row_to_listcell(params,listitem);
189 this.node.appendChild( listitem );
191 this.error.sdump('D_LIST',s);
197 '_map_row_to_treecell' : function(params,treerow) {
199 for (var i = 0; i < this.columns.length; i++) {
200 var treecell = document.createElement('treecell');
202 if (typeof params.map_row_to_column == 'function') {
204 label = params.map_row_to_column(params.row,this.columns[i]);
208 if (typeof this.map_row_to_column == 'function') {
210 label = this.map_row_to_column(params.row,this.columns[i]);
213 treecell.setAttribute('label',label);
214 treerow.appendChild( treecell );
215 s += ('treecell = ' + treecell + ' with label = ' + label + '\n');
217 this.error.sdump('D_LIST',s);
220 '_map_row_to_listcell' : function(params,listitem) {
222 for (var i = 0; i < this.columns.length; i++) {
224 if (typeof params.map_row_to_column == 'function') {
226 value = params.map_row_to_column(params.row,this.columns[i]);
230 if (typeof this.map_row_to_column == 'function') {
232 value = this.map_row_to_column(params.row,this.columns[i]);
235 if (typeof value == 'string') {
236 var listcell = document.createElement('listcell');
237 listcell.setAttribute('label',value);
238 listitem.appendChild(listcell);
239 s += ('listcell = ' + listcell + ' with label = ' + value + '\n');
241 listitem.appendChild(value);
242 s += ('listcell = ' + value + ' is really a ' + value.nodeName + '\n');
245 this.error.sdump('D_LIST',s);
248 'retrieve_selection' : function(params) {
249 switch(this.node.nodeName) {
250 case 'tree' : return this._retrieve_selection_from_tree(params); break;
251 default: throw('NYI: Need ._retrieve_selection_from_() for ' + this.node.nodeName); break;
255 '_retrieve_selection_from_tree' : function(params) {
257 var start = new Object();
258 var end = new Object();
259 var numRanges = this.node.view.selection.getRangeCount();
260 for (var t=0; t<numRanges; t++){
261 this.node.view.selection.getRangeAt(t,start,end);
262 for (var v=start.value; v<=end.value; v++){
263 var i = this.node.contentView.getItemAtIndex(v);
270 dump('exiting util.list.js\n');