2 Fieldmapper object table
9 function drawFMObjectTable( args ) {
11 var destination = args.dest;
14 if( typeof destination == 'string' )
15 destination = $(destination);
16 var builder = new FMObjectBuilder(obj, args.display);
17 destination.appendChild(builder.build());
22 /* Constructor for the builder object */
23 function FMObjectBuilder( obj, display, styleToggle ) {
25 this.table = elem('table');
26 this.thead = elem('thead');
27 this.tbody = elem('tbody');
28 this.thead_tr = elem('tr');
30 this.display = display;
31 this.styleToggle = styleToggle;
32 if(!this.display) this.display = {};
34 this.table.appendChild(this.thead);
35 this.table.appendChild(this.tbody);
36 this.thead.appendChild(this.thead_tr)
38 addCSSClass(this.table, 'fm_table');
39 addCSSClass(this.table, 'sortable');
40 this.table.id = 'fm_table_' + (ID_GEN++);
44 /* Builds the table */
45 FMObjectBuilder.prototype.build = function() {
48 if( instanceOf(this.obj, Array) )
50 else this.obj = [this.obj];
55 for( var i = 0; i < this.keys.length; i++ )
56 this.thead_tr.appendChild(elem('td',null,this.keys[i]));
58 for( var i = 0; i < this.obj.length; i++ )
59 this.buildObjectRow(this.obj[i]);
67 FMObjectBuilder.prototype.setKeys = function(o) {
69 if( this.display[o.classname] )
70 this.keys = this.display[o.classname].fields;
72 if(!this.keys && FM_TABLE_DISPLAY[o.classname])
73 this.keys = FM_TABLE_DISPLAY[o.classname].fields;
76 this.keys = fmclasses[o.classname];
80 if(sortme) this.keys = this.keys.sort();
83 /* Inserts one row into the table to represent a single object */
84 FMObjectBuilder.prototype.buildObjectRow = function(obj) {
86 for( var i = 0; i < this.keys.length; i++ ) {
88 var data = obj[this.keys[i]]();
89 this.fleshData(td, data, this.keys[i]);
92 this.tbody.appendChild(row);
95 FMObjectBuilder.prototype.dataName = function(data) {
97 if( this.display[data.classname] )
98 name = this.display[data.classname].name;
100 if(!name && FM_TABLE_DISPLAY[data.classname])
101 name = FM_TABLE_DISPLAY[data.classname].name;
103 if(!name) name = 'id';
110 FMObjectBuilder.prototype.fleshData = function(td, data, key) {
111 if(data == null) data = '';
113 if( typeof data == 'object' ) {
116 if( data._isfieldmapper )
117 atext = this.dataName(data);
119 else if (instanceOf(data, Array) )
125 var expand = function () {
127 var row = td.parentNode.nextSibling;
128 if( row && row.getAttribute('subrow') == key) buildme = false;
129 master.hideSubTables();
130 if(buildme) master.buildSubTable(td, data, key);
133 var a = elem('a',{href:'javascript:void(0);'});
135 a.appendChild(text(atext));
139 td.appendChild(text(''));
143 td.appendChild(text( data ));
147 FMObjectBuilder.prototype.hideSubTables = function() {
149 /* clear out any existing subrows */
150 for( var i = 0; i < this.tbody.childNodes.length; i++ ) {
151 var r = this.tbody.childNodes[i];
152 if( r.getAttribute('subrow') )
153 this.tbody.removeChild(r);
156 /* un-style any selected tds */
157 var tds = this.tbody.getElementsByTagName('td');
158 for( i = 0; i < tds.length; i++ )
159 removeCSSClass( tds[i], 'fm_selected' );
162 FMObjectBuilder.prototype.buildSubTable = function(td, obj, key) {
164 var left = td.offsetLeft;
165 var div = elem('div');
166 var row = elem('tr');
167 var subtd= elem('td');
169 if( td.parentNode.nextSibling )
170 this.tbody.insertBefore(row, td.parentNode.nextSibling);
172 this.tbody.appendChild(row);
174 row.appendChild(subtd);
175 row.setAttribute('subrow', key);
176 subtd.appendChild(div);
178 addCSSClass(td, 'fm_selected');
179 subtd.setAttribute('colspan',this.keys.length);
181 subtd.setAttribute('style', 'width: 100%; padding-left:'+left+';');
182 var builder = drawFMObjectTable({dest:div, obj:obj, display:this.display});
183 builder.table.setAttribute('style', 'width: auto;');
184 addCSSClass(builder.table, 'fm_selected');
186 var newleft = left - (builder.table.clientWidth / 2) + (td.clientWidth / 2);
188 if( newleft < left ) {
189 _debug("left = "+left+" : newleft = "+newleft);
190 var style = subtd.getAttribute('style');
191 style = style.replace(new RegExp(left), newleft);
192 subtd.setAttribute('style', style);