]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/common/js/fm_table.js
adding generic table drawer for fm objects
[Evergreen.git] / Open-ILS / web / opac / common / js / fm_table.js
1 /* 
2         Fieldmapper object table
3 */
4
5
6
7 function drawFMObjectTable( args ) {
8
9         var destination = args.dest;
10         var obj = args.obj;
11
12         if( typeof destination == 'string' ) 
13                 destination = $(destination);
14         var builder = new FMObjectBuilder(obj, args.display);
15         destination.appendChild(builder.build());
16         return builder;
17 }
18
19
20 /* Constructor for the builder object */
21 function FMObjectBuilder( obj, display, styleToggle ) {
22         this.obj                = obj;
23         this.table      = elem('table');
24         this.thead      = elem('thead');
25         this.tbody      = elem('tbody');
26         this.thead_tr = elem('tr');
27         this.subtables = [];
28         this.display = display;
29         this.styleToggle = styleToggle;
30         if(!this.display) this.display = {};
31
32         this.table.appendChild(this.thead);
33         this.table.appendChild(this.tbody);
34         this.thead.appendChild(this.thead_tr)
35
36         addCSSClass(this.table, 'fm_table');
37 }
38
39
40 /* Builds the table */
41 FMObjectBuilder.prototype.build = function() {
42         var o = this.obj;
43
44         if( instanceOf(this.obj, Array) ) 
45                 o = this.obj[0];
46         else this.obj = [this.obj];
47
48         if( o ) {
49
50                 this.setKeys(o);
51                 for( var i = 0; i < this.keys.length; i++ ) 
52                         this.thead_tr.appendChild(elem('td',null,this.keys[i]));
53         
54                 for( var i = 0; i < this.obj.length; i++ ) 
55                         this.buildObjectRow(this.obj[i]);
56         }
57
58         return this.table;
59 }
60
61
62 /* */
63 FMObjectBuilder.prototype.setKeys = function(o) {
64         if( this.display[o.classname] ) 
65                 this.keys = this.display[o.classname].fields;
66
67         if(!this.keys && FM_TABLE_DISPLAY[o.classname])
68                 this.keys = FM_TABLE_DISPLAY[o.classname].fields;
69
70         if(!this.keys)
71                 this.keys = fmclasses[o.classname];
72
73         this.keys = this.keys.sort();
74 }
75
76 /* Inserts one row into the table to represent a single object */
77 FMObjectBuilder.prototype.buildObjectRow = function(obj) {
78         var row = elem('tr');
79         for( var i = 0; i < this.keys.length; i++ ) {
80                 var td = elem('td');    
81                 var data = obj[this.keys[i]]();
82                 this.fleshData(td, data, this.keys[i]);
83                 row.appendChild(td);
84         }
85         this.tbody.appendChild(row);
86 }
87
88 FMObjectBuilder.prototype.dataName = function(data) {
89         var name;
90         if( this.display[data.classname] ) 
91                 name = this.display[data.classname].name;
92
93         if(!name && FM_TABLE_DISPLAY[data.classname])
94                 name = FM_TABLE_DISPLAY[data.classname].name;
95
96         if(!name) name = 'id';
97
98         return data[name]();
99         return name;
100 }
101
102
103 FMObjectBuilder.prototype.fleshData = function(td, data, key) {
104         if(data == null) data = '';
105
106         if( typeof data == 'object' ) {
107                 var atext;
108
109                 if( data._isfieldmapper ) 
110                         atext = this.dataName(data);
111
112                 else if (instanceOf(data, Array) )
113                         atext = data.length;
114
115                 if( atext ) {
116
117                         var master = this;
118                         var expand = function () { 
119                                 var buildme = true;
120                                 var row = td.parentNode.nextSibling;
121                                 if( row && row.getAttribute('subrow') == key) buildme = false;
122                                 master.hideSubTables();
123                                 if(buildme) master.buildSubTable(td, data, key);
124                         };
125
126                         var a = elem('a',{href:'javascript:void(0);'});
127                         a.onclick = expand;
128                         a.appendChild(text(atext));
129                         td.appendChild(a);
130
131                 } else {
132                         td.appendChild(text(''));
133                 }
134
135         } else {
136                 td.appendChild(text( data ));
137         }
138 }
139
140 FMObjectBuilder.prototype.hideSubTables = function() {
141
142         /* clear out any existing subrows */
143         for( var i = 0; i < this.tbody.childNodes.length; i++ ) {
144                 var r = this.tbody.childNodes[i];
145                 if( r.getAttribute('subrow') )
146                         this.tbody.removeChild(r);
147         }
148
149         /* un-style any selected tds */
150         var tds = this.tbody.getElementsByTagName('td');
151         for( i = 0; i < tds.length; i++ )
152                 removeCSSClass( tds[i], 'fm_selected' );
153 }
154
155 FMObjectBuilder.prototype.buildSubTable = function(td, obj, key) {
156
157         var left = td.offsetLeft;
158         var div = elem('div');
159         var row = elem('tr');
160         var subtd= elem('td');
161
162         if( td.parentNode.nextSibling ) 
163                 this.tbody.insertBefore(row, td.parentNode.nextSibling);
164         else
165                 this.tbody.appendChild(row);
166
167         row.appendChild(subtd);
168         row.setAttribute('subrow', key);
169         subtd.appendChild(div);
170
171         addCSSClass(td, 'fm_selected');
172         subtd.setAttribute('colspan',this.keys.length);
173
174         subtd.setAttribute('style', 'width: 100%; padding-left:'+left+';');
175         var builder = drawFMObjectTable({dest:div, obj:obj, display:this.display});
176         builder.table.setAttribute('style', 'width: auto;');
177         addCSSClass(builder.table, 'fm_selected');
178
179         var style = subtd.getAttribute('style');
180         var newleft = left - (builder.table.clientWidth / 2) + (td.clientWidth / 2);
181         style = style.replace(new RegExp(left), newleft);
182         subtd.setAttribute('style', style);
183 }
184
185
186
187