]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/reports/oils_rpt_builder.js
added link style option to slimtree
[Evergreen.git] / Open-ILS / web / reports / oils_rpt_builder.js
1 /** initializes reports, some basid display settings, 
2   * grabs and builds the IDL tree
3   */
4 function oilsInitReportBuilder() {
5         oilsInitReports();
6         oilsRpt = new oilsReport();
7         oilsRptDisplaySelector  = $('oils_rpt_display_selector');
8         oilsRptFilterSelector   = $('oils_rpt_filter_selector');
9         oilsDrawRptTree(
10                 function() { 
11                         hideMe($('oils_rpt_tree_loading')); 
12                         unHideMe($('oils_rpt_table')); 
13                 }
14         );
15 }
16
17 /* returns just the column name */
18 function oilsRptPathCol(path) {
19         var parts = path.split(/-/);
20         return parts.pop();
21 }
22
23 /* returns the IDL class of the selected column */
24 function oilsRptPathClass(path) {
25         var parts = path.split(/-/);
26         parts.pop();
27         return parts.pop();
28 }
29
30 /* returns everything prior to the column name */
31 function oilsRptPathRel(path) {
32         var parts = path.split(/-/);
33         parts.pop();
34         return parts.join('-');
35 }
36
37 /* creates a label "path" based on the column path */
38 function oilsRptMakeLabel(path) {
39         var parts = path.split(/-/);
40         var str = '';
41         for( var i = 0; i < parts.length; i++ ) {
42                 if(i%2 == 0) {
43                         if( i == 0 )
44                                 str += oilsIDL[parts[i]].label;
45                 } else {
46                         var column = parts[i];
47                         var data = oilsIDL[parts[i-1]];
48                         var f = grep(data.fields, 
49                                 function(j){return (j.name == column); })[0];
50                         str += ":"+f.label;
51                 }
52         }
53         return str;
54 }
55
56
57 /* adds an item to the display window */
58 function oilsAddRptDisplayItem(path, name) {
59         if( ! oilsAddSelectorItem(oilsRptDisplaySelector, path, name) ) 
60                 return;
61
62         /* add the selected columns to the report output */
63         name = (name) ? name : oilsRptPathCol(path);
64         var param = oilsRptNextParam();
65
66         /* add this item to the select blob */
67         oilsRpt.def.select.push( {
68                 relation:oilsRptPathRel(path), 
69                 column:oilsRptPathCol(path), 
70                 alias:param
71         });
72
73         mergeObjects( oilsRpt.def.from, oilsRptBuildFromClause(path));
74         oilsRpt.params[param] = name;
75         oilsRptDebug();
76 }
77
78 /* takes a column path and builds a from-clause object for the path */
79 function oilsRptBuildFromClause(path) {
80         var parts = path.split(/-/);
81         //var obj = {from : {}};
82         var obj = {};
83         var tobj = obj;
84         var newpath = "";
85         for( var i = 0; i < parts.length; i += 2 ) {
86                 var cls = parts[i];
87                 var col = parts[i+1];
88                 var node = oilsIDL[parts[i]];
89                 var field = oilsRptFindField(node, col);
90                 newpath = (newpath) ? newpath + '-'+ cls : cls;
91
92                 tobj.table = node.table;
93                 tobj.alias = newpath;
94                 _debug('field type is ' + field.type);
95                 if( i == (parts.length - 2) ) break;
96
97                 tobj.join = {};
98                 tobj = tobj.join;
99                 tobj[col] = {};
100                 tobj = tobj[col];
101                 if( field.type == 'link' )
102                         tobj.key = field.key;
103
104                 newpath = newpath + '-'+ col;
105         }
106
107         _debug("built 'from' clause: path="+path+"\n"+formatJSON(js2JSON(obj)));
108         return obj;
109 }
110
111
112 /* removes a specific item from the display window */
113 function oilsDelDisplayItem(val) {
114         oilsDelSelectorItem(oilsRptDisplaySelector, val);
115 }
116
117 /* removes selected items from the display window */
118 function oilsDelSelectedDisplayItems() {
119         var list = oilsDelSelectedItems(oilsRptDisplaySelector);
120
121         /* remove the de-selected columns from the report output */
122         oilsRpt.def.select = grep( oilsRpt.def.select, 
123                 function(i) {
124                         for( var j = 0; j < list.length; j++ ) {
125                                 var d = list[j];
126                                 if( oilsRptPathRel(d) == i.relation 
127                                                 && oilsRptPathCol(d) == i.column ) {
128                                         var param = (i.alias) ? i.alias.match(/::PARAM\d*/) : null;
129                                         if( param ) delete oilsRpt.params[param];
130                                         return false;
131                                 }
132                         }
133                         return true;
134                 }
135         );
136         if(!oilsRpt.def.select) oilsRpt.def.select = [];
137
138         for( var j = 0; j < list.length; j++ ) 
139                 oilsRptPruneFromClause(list[j]);
140
141         oilsRptDebug();
142 }
143
144 /* for each item in the path list, remove the associated data
145 from the "from" clause */
146 function oilsRptPruneFromClause(pathlist) {
147 }
148
149 /* adds an item to the display window */
150 function oilsAddRptFilterItem(val) {
151         oilsAddSelectorItem(oilsRptFilterSelector, val);
152 }
153
154 /* removes a specific item from the display window */
155 function oilsDelFilterItem(val) {
156         oilsDelSelectorItem(oilsRptFilterSelector, val);
157 }
158
159 /* removes selected items from the display window */
160 function oilsDelSelectedFilterItems() {
161         oilsDelSelectedItems(oilsRptFilterSelector);
162 }
163
164
165 /* adds an item to the display window */
166 function oilsAddSelectorItem(sel, val, name) {
167         name = (name) ? name : oilsRptMakeLabel(val);
168         _debug("adding selector item "+name+' = ' +val);
169         for( var i = 0; i < sel.options.length; i++ ) {
170                 var opt = sel.options[i];
171                 if( opt.value == val ) return false;
172         }
173         insertSelectorVal( sel, -1, name, val );
174         return true;
175 }
176
177
178 /* removes a specific item from the display window */
179 function oilsDelSelectorItem(sel, val) {
180         _debug("deleting selector item "+val);
181         var opts = sel.options;
182         for( var i = 0; i < opts.length; i++ ) {
183                 var opt = opts[i];
184                 if( opt.value == val )  {
185                         if( i == opts.length - 1 ) 
186                                 opts[i] = null;
187                         else opts[i] = opts[i+1];
188                         return;
189                 }
190         }
191 }
192
193 /* removes selected items from the display window */
194 function oilsDelSelectedItems(sel) {
195         var list = getSelectedList(sel);
196         for( var i = 0; i < list.length; i++ ) 
197                 oilsDelSelectorItem(sel, list[i]);
198         return list;
199 }
200
201
202 /* hides the different field editor tabs */
203 function oilsRptHideEditorDivs() {
204         hideMe($('oils_rpt_tform_div'));
205         hideMe($('oils_rpt_filter_div'));
206         hideMe($('oils_rpt_agg_filter_div'));
207 }
208
209
210 /**
211   This draws the 3-tabbed window containing the transform,
212   filter, and aggregate filter picker window
213   */
214 function oilsRptDrawDataWindow(path) {
215         var col = oilsRptPathCol(path);
216         var cls = oilsRptPathClass(path);
217         var field = grep(oilsIDL[cls].fields, function(f){return (f.name==col);})[0];
218         _debug("setting update data window for column "+col+' on class '+cls);
219
220         var div = $('oils_rpt_column_editor');
221         unHideMe(div);
222         /* don't let them see the floating div until the position is fully determined */
223         div.style.visibility='hidden'; 
224
225         oilsRptDrawTransformWindow(path, col, cls, field);
226
227         $('oils_rpt_column_editor_close_button').onclick = function(){hideMe(div);};
228         buildFloatingDiv(div, 600);
229         div.style.visibility='visible';
230
231         /* focus after all the shifting to make sure the div is at least visible */
232         $('oils_rpt_tform_label_input').focus();
233
234         /* give the tab links behavior */
235         $('oils_rpt_tform_tab').onclick = 
236                 function(){oilsRptHideEditorDivs();unHideMe($('oils_rpt_tform_div'))};
237         $('oils_rpt_filter_tab').onclick = 
238                 function(){oilsRptHideEditorDivs();unHideMe($('oils_rpt_filter_div'))};
239         $('oils_rpt_agg_filter_tab').onclick = 
240                 function(){oilsRptHideEditorDivs();unHideMe($('oils_rpt_agg_filter_div'))};
241 }
242
243
244 /* draws the transform window */
245 function oilsRptDrawTransformWindow(path, col, cls, field) {
246         appendClear($('oils_rpt_tform_label'), text(oilsRptMakeLabel(path)));
247         $('oils_rpt_tform_label_input').value = oilsRptMakeLabel(path);
248
249         $('oils_rpt_tform_submit').onclick = 
250                 function(){ oilsAddRptDisplayItem(path, $('oils_rpt_tform_label_input').value) };
251
252         $('oils_rpt_tform_label_input').focus();
253         $('oils_rpt_tform_label_input').select();
254
255         if( field.datatype == 'timestamp' )
256                 unHideMe($('oils_rpt_tform_date_div'));
257 }
258
259
260