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